remove `#!/usr/bin/env sh`
[liba.git] / script / switch.py
blob6009611c2a5767c5aafa4bad00f97b013d71ac11
1 #!/usr/bin/env python
2 from glob import glob
3 from re import findall
6 def switch(source):
7 with open(source, "r", encoding="UTF-8") as f:
8 text = text_ = f.read()
9 for item in findall(r"0x([0-9A-Fa-f]+): // (\w+)", text):
10 value = 0
11 for i in item[1]:
12 value = value * 131 + ord(i)
13 value &= 0xFFFFFFFF
14 new = "%08X" % value
15 if item[0] != new:
16 print("{} -> {} // {}".format(item[0], new, item[1]))
17 cur = "0x{}: // {}".format(item[0], item[1])
18 new = "0x{}: // {}".format(new, item[1])
19 text = text.replace(cur, new)
20 if text != text_:
21 with open(source, "wb") as f:
22 f.write(text.encode("UTF-8"))
25 def main(path):
26 for source in glob(path, recursive=True):
27 switch(source)
30 main("lua/src/**.c")
31 main("test/**.h")