File: text.py

package info (click to toggle)
python-sybil 9.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,148 kB
  • sloc: python: 4,510; makefile: 90
file content (18 lines) | stat: -rw-r--r-- 474 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import re

NEWLINE = re.compile("\n")


class LineNumberOffsets:

    def __init__(self, text: str) -> None:
        self.offsets = {
            line: match.start()+1 for (line, match) in enumerate(NEWLINE.finditer(text), start=1)
        }
        self.offsets[0] = 0

    def get(self, line: int, column: int) -> int:
        """
        Return the character offset of the  zero based line number and column offset.
        """
        return self.offsets[line] + column