-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert-cpp.py
More file actions
64 lines (50 loc) · 2.19 KB
/
convert-cpp.py
File metadata and controls
64 lines (50 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python3
"""Batch convert ui-slim C++ files to use trailing return types."""
import re
import subprocess
from pathlib import Path
def convert_file(filepath):
"""Convert a .cpp file to use trailing return type syntax."""
with open(filepath, 'r', encoding='utf-8') as f:
content = f.read()
original = content
# Pattern to match traditional return type at function definition
# Matches: ReturnType ClassName::methodName(...) -> auto ClassName::methodName(...) -> ReturnType
# More specific: match at start of line (indented or not)
pattern = r'(^\s*)([a-zA-Z_:][a-zA-Z0-9_:* &]*?)\s+([A-Z][a-zA-Z0-9_]*)::([a-zA-Z_][a-zA-Z0-9_]*)\s*(\(.*?\))\s*(const)?\s*(\{|\n)'
def replace_func(match):
indent = match.group(1)
return_type = match.group(2).strip()
class_name = match.group(3)
method_name = match.group(4)
params = match.group(5)
is_const = match.group(6)
brace_or_newline = match.group(7)
# Skip if already has trailing return type
if '-> ' in return_type or return_type.startswith('auto'):
return match.group(0)
# Build the new signature
if is_const:
new_sig = f"{indent}auto {class_name}::{method_name}{params} {is_const} -> {return_type} {brace_or_newline}"
else:
new_sig = f"{indent}auto {class_name}::{method_name}{params} -> {return_type} {brace_or_newline}"
return new_sig.replace(' ->', ' ->').replace(' ->', ' ->') # Clean spacing
content = re.sub(pattern, replace_func, content, flags=re.MULTILINE)
# If content changed, write it back
if content != original:
with open(filepath, 'w', encoding='utf-8') as f:
f.write(content)
return True
return False
def main():
cpp_files = sorted(Path('ui-slim/src').glob('*.cpp'))
converted = 0
for filepath in cpp_files:
if convert_file(filepath):
print(f"✓ {filepath.name}")
converted += 1
else:
print(f" {filepath.name} (no changes)")
print(f"\nConverted {converted} files")
if __name__ == '__main__':
main()