perf(structgen): finalize 特殊字符 struct 名加引号合并为单次 re.sub
逐 name 全文本 re.sub 改为单正则 alternation 一次扫描,消除 N 次 O(text) 扫描(lookahead 边界保证备选互斥,语义等价)
This commit is contained in:
1 parent
fbd132284b
commit
267e9881b4
1 file changed
+10
-7
@@ -202,13 +202,16 @@ class StructGenMixin:
|
||||
insert_pos = header_end
|
||||
struct_block: str = '\n'.join(struct_defs) + '\n'
|
||||
ir_text = ir_text[:insert_pos + 1] + struct_block + ir_text[insert_pos + 1:]
|
||||
for name in struct_names:
|
||||
if name[0].isdigit() or '.' in name:
|
||||
ir_text = re.sub(
|
||||
r'%' + re.escape(name) + r'(?=[^a-zA-Z0-9_$\.]|$)',
|
||||
r'%"' + name + r'"',
|
||||
ir_text
|
||||
)
|
||||
# 仅含特殊字符(数字开头或含点)的 struct 名需要加引号:
|
||||
# 合并为单个正则一次扫描全文本,替代逐 name 各自 re.sub(每个 name
|
||||
# 一次 O(text) 扫描)。lookahead 边界排除保证各备选互斥,语义等价。
|
||||
special_names: list[str] = [n for n in struct_names if n[0].isdigit() or '.' in n]
|
||||
if special_names:
|
||||
ir_text = re.sub(
|
||||
r'%(' + '|'.join(re.escape(n) for n in special_names) + r')(?=[^a-zA-Z0-9_$\.]|$)',
|
||||
lambda m: r'%"' + m.group(1) + r'"',
|
||||
ir_text
|
||||
)
|
||||
|
||||
return ir_text
|
||||
|
||||
|
||||
Reference in new issue
Block a user