Skip to content

Commit 9e39a63

Browse files
authored
perf: only iter on found escape sequences (#304)
Finding used sequences is much faster than iterating over each one of them for each character. The iteration is still used, but at least it will only be used once per any sequence that is actually used in the string. Most strings won't include any sequences and will just be fast. @moduon MT-1075
1 parent 8edb46c commit 9e39a63

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

tomlkit/_utils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,11 @@ def flush(inc=1):
133133

134134
return i + inc
135135

136+
found_sequences = {seq for seq in escape_sequences if seq in s}
137+
136138
i = 0
137139
while i < len(s):
138-
for seq in escape_sequences:
140+
for seq in found_sequences:
139141
seq_len = len(seq)
140142
if s[i:].startswith(seq):
141143
start = flush(seq_len)

0 commit comments

Comments
 (0)