(c)
| 18406 | |
| 18407 | |
| 18408 | def 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 | |
| 18437 | def JM_BufferFromBytes(stream): |
no outgoing calls
no test coverage detected
searching dependent graphs…