MCPcopy Index your code
hub / github.com/python/mypy / encode_c_string_literal

Function encode_c_string_literal

mypyc/codegen/emitfunc.py:1003–1024  ·  view source on GitHub ↗

Convert bytestring to the C string literal syntax (with necessary escaping). For example, b'foo\n' gets converted to 'foo\\n' (note that double quotes are not added).

(b: bytes)

Source from the content-addressed store, hash-verified

1001
1002
1003def encode_c_string_literal(b: bytes) -> str:
1004 """Convert bytestring to the C string literal syntax (with necessary escaping).
1005
1006 For example, b'foo\n' gets converted to 'foo\\n' (note that double quotes are not added).
1007 """
1008 if not _translation_table:
1009 # Initialize the translation table on the first call.
1010 d = {
1011 ord("\n"): "\\n",
1012 ord("\r"): "\\r",
1013 ord("\t"): "\\t",
1014 ord('"'): '\\"',
1015 ord("\\"): "\\\\",
1016 }
1017 for i in range(256):
1018 if i not in d:
1019 if i < 32 or i >= 127:
1020 d[i] = "\\x%.2x" % i
1021 else:
1022 d[i] = chr(i)
1023 _translation_table.update(str.maketrans(d))
1024 return b.decode("latin1").translate(_translation_table)

Callers 1

regMethod · 0.85

Calls 6

ordFunction · 0.85
rangeClass · 0.85
chrFunction · 0.85
translateMethod · 0.80
updateMethod · 0.45
decodeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…