In January 2021 we will introduce a 10 GB quota for project repositories. Higher limits for individual projects will be available on request. Please see https://doku.lrz.de/display/PUBLIC/GitLab for more information.

Commit 9e1686d9 authored by Eckhart Arnold's avatar Eckhart Arnold

- DHParser/syntaxtree.py Node.content optimized so as to minimize string copy operations

parent af07a4db
......@@ -354,6 +354,18 @@ class Node: # (collections.abc.Sized): Base class omitted for cython-compatibil
self.children = NoChildren
self._result = result # cast(StrictResultType, result)
def _content(self) -> List[str]:
"""
Returns string content as list of string fragments
that are gathered from all child nodes in order.
"""
if self.children:
fragments = []
for child in self.children:
fragments.extend(child._content())
return fragments
self._result = str(self._result)
return [self._result]
@property
def content(self) -> str:
......@@ -362,7 +374,11 @@ class Node: # (collections.abc.Sized): Base class omitted for cython-compatibil
string content of the child-nodes is recursively read and then
concatenated.
"""
return "".join(child.content for child in self.children) if self.children else str(self._result)
return ''.join(self._content())
## unoptimized
# return "".join(child.content for child in self.children) if self.children else str(
# self._result)
## obsolete
# if self._content is None:
# if self.children:
# self._content = "".join(child.content for child in self.children)
......
......@@ -81,6 +81,17 @@ class TestNode:
self.recurring_nodes_sexpr = '(a (b x) (c (d e) (b y)))'
self.recurr_tree = parse_sxpr(self.recurring_nodes_sexpr)
def test_content_property(self):
tree = RootNode(parse_sxpr('(a (b c) (d e))'))
content = tree.content
b = tree.pick('b')
d = tree.pick('d')
b.result = "recently "
d.result = "changed"
assert content != tree.content
assert content == 'ce'
assert tree.content == 'recently changed'
def test_deepcopy(self):
tree = RootNode(parse_sxpr('(a (b c) (d (e f) (h i)))'))
tree.with_pos(0)
......
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