MCPcopy
hub / github.com/Nuitka/Nuitka / _encodePythonStringToC

Function _encodePythonStringToC

nuitka/utils/CStrings.py:26–60  ·  view source on GitHub ↗

Encode a string, so that it gives a C string literal. This doesn't handle limits.

(value)

Source from the content-addressed store, hash-verified

24
25
26def _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
63def encodePythonUnicodeToC(value):

Callers 1

encodePythonStringToCFunction · 0.85

Calls 1

replaceMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…