Encode a string, so that it gives a C string literal. This doesn't handle limits.
(value)
| 24 | |
| 25 | |
| 26 | def _encodePythonStringToC(value): |
| 27 | """Encode a string, so that it gives a C string literal. |
| 28 | |
| 29 | This doesn't handle limits. |
| 30 | """ |
| 31 | assert type(value) is bytes, type(value) |
| 32 | |
| 33 | result = "" |
| 34 | octal = False |
| 35 | |
| 36 | for c in value: |
| 37 | if str is bytes: |
| 38 | cv = ord(c) |
| 39 | else: |
| 40 | cv = c |
| 41 | |
| 42 | if c in b'\\\t\r\n"?': |
| 43 | result += r"\%03o" % cv |
| 44 | |
| 45 | octal = True |
| 46 | elif 32 <= cv <= 127: |
| 47 | if octal and c in b"0123456789": |
| 48 | result += '" "' |
| 49 | |
| 50 | result += chr(cv) |
| 51 | |
| 52 | octal = False |
| 53 | else: |
| 54 | result += r"\%o" % cv |
| 55 | |
| 56 | octal = True |
| 57 | |
| 58 | result = result.replace('" "\\', "\\") |
| 59 | |
| 60 | return '"%s"' % result |
| 61 | |
| 62 | |
| 63 | def encodePythonUnicodeToC(value): |
no test coverage detected
searching dependent graphs…