Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
badw-it
DHParser
Commits
6a26260c
Commit
6a26260c
authored
Aug 31, 2017
by
Eckhart Arnold
Browse files
- toolkit.py: added TextView class for non-copy string-slicing
parent
b930b98d
Changes
2
Hide whitespace changes
Inline
Side-by-side
DHParser/parser.py
View file @
6a26260c
...
...
@@ -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
...
...
DHParser/toolkit.py
View file @
6a26260c
...
...
@@ -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:
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment