Next / Previous / Contents / Shipman's homepage

15. Sox._checkCharRange(): Is this code point in a given set of intervals?

sox.py
# - - -   _ c h e c k C h a r R a n g e

    def _checkCharRange(self, c, rangeTable):
        '''Is this character's code point in a given set of ranges?

          [ (c is a single Unicode character) and
            (rangeTable is a list of tuples of two integers (lo, hi)) ->
              if there is an entry in rangeTable such that
              lo <= ord(c) <= hi ->
                return True
              else ->
                return False ]
        '''

As you read this, are you thinking to yourself, gee, isn't this a terribly inefficient way to do it? Shouldn't we build a dictionary that maps all possible Unicode code points to bool values so we can find out whether a character is in a class in nearly constant time? Well, if so, just keep in mind the words of Dr. Donald Knuth, who once said, “…premature optimization is the root of all evil.”

sox.py
        #-- 1
        cp = ord(c)

        #-- 2
        for lo, hi in rangeTable:
            if lo <= cp <= hi:
                return True

        #-- 3
        return False