Next / Previous / Contents / Shipman's homepage

45. Scan.flatInt(): Parse a fixed-size integer field

logscan.py
# - - -   S c a n . f l a t I n t

    def flatInt(self, n):
        '''Parse a fixed-field integer of size n
        '''

This method uses the same regular expression as Section 43, “Scan.integer(): Parse an integer”; see Section 9.5, “INT_PATTERN. First we make sure that there are at least n characters left on the line. If so, they are matched against INT_PATTERN; if there is a match, it must match all n characters.

logscan.py
        #-- 1 --
        # [ if the current line has at least n characters left ->
        #     rawInt  :=  the next n characters from the line
        #   else ->
        #     return None ]
        if len(self.line) - self.pos < n:
            return None
        else:
            rawInt = self.line[self.pos:self.pos+n]

        #-- 2 --
        # [ if rawInt matches INT_PATTERN ->
        #     matchLen  :=  the length of the matching part
        #   else -> return None ]
        m = INT_PATTERN.match(rawInt)
        if m is None:
            return None
        else:
            matchLen = m.end() - m.start()

        #-- 3 --
        if matchLen < n:
            return None
        else:
            return int(rawInt)