Skip to content

Commit 6b80bf6

Browse files
committed
Change the Genshi treeadapter to avoid O(n^2) string concat.
1 parent 580e06e commit 6b80bf6

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

html5lib/treeadapters/genshi.py

+7-10
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,14 @@
55

66

77
def to_genshi(walker):
8-
text = None
8+
text = []
99
for token in walker:
1010
type = token["type"]
1111
if type in ("Characters", "SpaceCharacters"):
12-
if text is None:
13-
text = token["data"]
14-
else:
15-
text += token["data"]
16-
elif text is not None:
17-
yield TEXT, text, (None, -1, -1)
18-
text = None
12+
text.append(token["data"])
13+
elif text:
14+
yield TEXT, "".join(text), (None, -1, -1)
15+
text = []
1916

2017
if type in ("StartTag", "EmptyTag"):
2118
if token["namespace"]:
@@ -46,5 +43,5 @@ def to_genshi(walker):
4643
else:
4744
pass # FIXME: What to do?
4845

49-
if text is not None:
50-
yield TEXT, text, (None, -1, -1)
46+
if text:
47+
yield TEXT, "".join(text), (None, -1, -1)

0 commit comments

Comments
 (0)