2.12.2021, 9:00 - 11:00: Due to updates GitLab may be unavailable for some minutes between 09:00 and 11:00.

Commit 6a26260c authored by Eckhart Arnold's avatar Eckhart Arnold
Browse files

- toolkit.py: added TextView class for non-copy string-slicing

parent b930b98d
......@@ -238,8 +238,9 @@ def add_parser_guard(parser_func):
# NOTE: An older and simpler implementation of memoization
# relied on `parser.visited[location] == node, rest`. Although,
# rest is really just a substring of one and the same document,
# this resulted in an explosion of memory usage. Seems as if
# `rext = text[i:]` really copies the sub-string!?
# this resulted in an explosion of memory usage. Seems that
# `rext = text[i:]` really copies the sub-string. See:
# https://mail.python.org/pipermail/python-dev/2008-May/079699.html
location = len(text) # mind that location is always the distance to the end
......@@ -43,9 +43,9 @@ except ImportError:
import sys
from typing import Any, List, Tuple
from typing import Any, List, Tuple, Optional
except ImportError:
from .typing34 import Any, List, Tuple
from .typing34 import Any, List, Tuple, Optional
__all__ = ('logging',
......@@ -150,6 +150,24 @@ def clear_logs(logfile_types={'.cst', '.ast', '.log'}):
class TextView:
__slots__ = ['text', 'begin', 'end']
def __init__(self, text: str, begin: Optional[int] = 0, end: Optional[int] = None) -> None:
self.text = text # type: str
self.begin = begin or 0 # type: int
self.end = end or len(text) # type: int
def __str__(self):
return self.text[self.begin:self.end]
def __getitem__(self, index):
assert isinstance(index, slice), "Minimal implementation of TextView just allows slicing."
start = index.start or 0
stop = index.stop or (self.end - self.begin)
return TextView(self.text, self.begin + start, self.begin + stop)
# def repr_call(f, parameter_list) -> str:
# """Turns a list of items into a string resembling the parameter
# list of a function call by omitting default values at the end:
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment