Skip to content

Commit

Permalink
Pre-compute the length!
Browse files Browse the repository at this point in the history
  • Loading branch information
tabatkins committed Nov 13, 2023
1 parent 02ffbca commit b214f66
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion bikeshed/h/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,11 +397,13 @@ def showErr(self) -> tuple[t.Any, int, Failure | None]:

class Stream:
_chars: str
_len: int
_lineBreaks: list[int]
startLine: int

def __init__(self, chars: str, startLine: int = 1) -> None:
self._chars = chars
self._len = len(chars)
self._lineBreaks = []
self.startLine = startLine
for i, char in enumerate(chars):
Expand All @@ -415,7 +417,7 @@ def __getitem__(self, key: int | slice) -> str:
return ""

def eof(self, index: int) -> bool:
return index >= len(self._chars)
return index >= self._len

def line(self, index: int) -> int:
# Zero-based line index
Expand Down

0 comments on commit b214f66

Please sign in to comment.