Skip to content

Instantly share code, notes, and snippets.

@cknave
Created August 4, 2017 01:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cknave/234c1f568e5d6c12c40238a7085e9294 to your computer and use it in GitHub Desktop.
Save cknave/234c1f568e5d6c12c40238a7085e9294 to your computer and use it in GitHub Desktop.
Fix unescaped 8-bit characters in C code
#!/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