diff --git a/lib/core/LLVMCG/StructGen.py b/lib/core/LLVMCG/StructGen.py index 865045f..97e994a 100644 --- a/lib/core/LLVMCG/StructGen.py +++ b/lib/core/LLVMCG/StructGen.py @@ -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