Created
August 4, 2017 01:06
-
-
Save cknave/234c1f568e5d6c12c40238a7085e9294 to your computer and use it in GitHub Desktop.
Fix unescaped 8-bit characters in C code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
"""Fix unescaped 8-bit characters in C code.""" | |
import os | |
import re | |
import sys | |
import tempfile | |
PRINTABLE = b'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$'\ | |
b'%&\'()*+,-./:;<=>?@[\\]^_`{|}~ ' | |
def escape_match(m): | |
value = m.group(1) | |
if value in PRINTABLE: | |
return m.group(0) | |
else: | |
return b"'\\x%02x'" % ord(value) | |
def process(path): | |
with open(path, 'rb') as f: | |
data = f.read() | |
fixed = re.sub(b"'(.)'", escape_match, data) | |
with tempfile.NamedTemporaryFile(delete=False) as f: | |
f.write(fixed) | |
fixed_path = f.name | |
os.replace(fixed_path, path) | |
def main(): | |
if len(sys.argv) == 1: | |
print('Fix unescaped 8-bit characters in C code') | |
print('USAGE: fix_chars.py <file1.c> <file2.c> ...') | |
for arg in sys.argv[1:]: | |
process(arg) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment