MCPcopy Create free account
hub / github.com/pymupdf/PyMuPDF / JM_EscapeStrFromStr

Function JM_EscapeStrFromStr

src/__init__.py:18408–18434  ·  view source on GitHub ↗
(c)

Source from the content-addressed store, hash-verified

18406
18407
18408def JM_EscapeStrFromStr(c):
18409 # `c` is typically from SWIG which will have converted a `const char*` from
18410 # C into a Python `str` using `PyUnicode_DecodeUTF8(carray, static_cast<
18411 # Py_ssize_t >(size), "surrogateescape")`. This gives us a Python `str`
18412 # with some characters encoded as a \0xdcXY sequence, where `XY` are hex
18413 # digits for an invalid byte in the original `const char*`.
18414 #
18415 # This is actually a reasonable way of representing arbitrary
18416 # strings from C, but we want to mimic what PyMuPDF does. It uses
18417 # `PyUnicode_DecodeRawUnicodeEscape(c, (Py_ssize_t) strlen(c), "replace")`
18418 # which gives a string containing actual unicode characters for any invalid
18419 # bytes.
18420 #
18421 # We mimic this by converting the `str` to a `bytes` with 'surrogateescape'
18422 # to recognise \0xdcXY sequences, then convert the individual bytes into a
18423 # `str` using `chr()`.
18424 #
18425 # Would be good to have a more efficient way to do this.
18426 #
18427 if c is None:
18428 return ''
18429 assert isinstance(c, str), f'{type(c)=}'
18430 b = c.encode('utf8', 'surrogateescape')
18431 ret = ''
18432 for bb in b:
18433 ret += chr(bb)
18434 return ret
18435
18436
18437def JM_BufferFromBytes(stream):

Callers 9

file_infoMethod · 0.85
_embfile_infoMethod · 0.85
_embfile_namesMethod · 0.85
extract_fontMethod · 0.85
JM_gather_fontsFunction · 0.85
JM_gather_imagesFunction · 0.85
JM_insert_fontFunction · 0.85
JM_make_spanlistFunction · 0.85
jm_trace_text_spanFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…