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
try:
location = len(text) # mind that location is always the distance to the end
......
......@@ -43,9 +43,9 @@ except ImportError:
import sys
try:
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',
'is_logging',
......@@ -150,6 +150,24 @@ def clear_logs(logfile_types={'.cst', '.ast', '.log'}):
os.rmdir(log_dirname)
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