|
| 1 | +"""Encode valid C string literals from Python strings. |
| 2 | +
|
| 3 | +If a character is not allowed in C string literals, it is either emitted |
| 4 | +as a simple escape sequence (e.g. '\\n'), or an octal escape sequence |
| 5 | +with exactly three digits ('\\oXXX'). Question marks are escaped to |
| 6 | +prevent trigraphs in the string literal from being interpreted. Note |
| 7 | +that '\\?' is an invalid escape sequence in Python. |
| 8 | +
|
| 9 | +Consider the string literal "AB\\xCDEF". As one would expect, Python |
| 10 | +parses it as ['A', 'B', 0xCD, 'E', 'F']. However, the C standard |
| 11 | +specifies that all hexadecimal digits immediately following '\\x' will |
| 12 | +be interpreted as part of the escape sequence. Therefore, it is |
| 13 | +unexpectedly parsed as ['A', 'B', 0xCDEF]. |
| 14 | +
|
| 15 | +Emitting ("AB\\xCD" "EF") would avoid this behaviour. However, we opt |
| 16 | +for simplicity and use octal escape sequences instead. They do not |
| 17 | +suffer from the same issue as they are defined to parse at most three |
| 18 | +octal digits. |
| 19 | +""" |
| 20 | + |
| 21 | +import string |
| 22 | +from typing import Tuple |
| 23 | + |
| 24 | +CHAR_MAP = ['\\{:03o}'.format(i) for i in range(256)] |
| 25 | + |
| 26 | +# It is safe to use string.printable as it always uses the C locale. |
| 27 | +for c in string.printable: |
| 28 | + CHAR_MAP[ord(c)] = c |
| 29 | + |
| 30 | +# These assignments must come last because we prioritize simple escape |
| 31 | +# sequences over any other representation. |
| 32 | +for c in ('\'', '"', '\\', 'a', 'b', 'f', 'n', 'r', 't', 'v'): |
| 33 | + escaped = '\\{}'.format(c) |
| 34 | + decoded = escaped.encode('ascii').decode('unicode_escape') |
| 35 | + CHAR_MAP[ord(decoded)] = escaped |
| 36 | + |
| 37 | +# This escape sequence is invalid in Python. |
| 38 | +CHAR_MAP[ord('?')] = r'\?' |
| 39 | + |
| 40 | + |
| 41 | +def encode_as_c_string(s: str) -> Tuple[str, int]: |
| 42 | + """Produce a quoted C string literal and its size, for a UTF-8 string.""" |
| 43 | + return encode_bytes_as_c_string(s.encode('utf-8')) |
| 44 | + |
| 45 | + |
| 46 | +def encode_bytes_as_c_string(b: bytes) -> Tuple[str, int]: |
| 47 | + """Produce a quoted C string literal and its size, for a byte string.""" |
| 48 | + escaped = ''.join([CHAR_MAP[i] for i in b]) |
| 49 | + return '"{}"'.format(escaped), len(b) |
0 commit comments