Next / Previous / Contents / Shipman's homepage

29. Scan.move(): Advance the scan position

logscan.py
# - - -   S c a n . m o v e

    def move(self, n):
        '''Move the scan position by n characters.
        '''

The value of n may be negative. First, check that the new position is somewhere within the current line. The new position may be at the end of the line, so the case (self.pos+n == lqen(self.line) is valid.

logscan.py
        #-- 1 --
        # [ if self.pos + n is within the current line ->
        #     newPos  :=  self.pos + n
        #   else -> raise IndexError ]
        newPos = self.pos + n
        if not (0 <= newPos <= len(self.line)):
            raise IndexError("Scan.move: new position out of range.")

Whether the move was backwards or forwards, we return the string between the old and new positions.

logscan.py
        #-- 2 --
        loPos = min(self.pos, newPos)
        hiPos = max(self.pos, newPos)
        self.pos = newPos

        #-- 3 --
        return self.line[loPos:hiPos]