MCPcopy Index your code
hub / github.com/RustPython/RustPython / PyUnicode_EncodeUTF32

Function PyUnicode_EncodeUTF32

Lib/_pycodecs.py:482–514  ·  view source on GitHub ↗

Encode a Unicode string to UTF-32.

(s, size, errors, byteorder="little")

Source from the content-addressed store, hash-verified

480
481
482def PyUnicode_EncodeUTF32(s, size, errors, byteorder="little"):
483 """Encode a Unicode string to UTF-32."""
484 p = []
485 bom = sys.byteorder
486
487 if byteorder == "native":
488 bom = sys.byteorder
489 # Add BOM for native encoding
490 p += STORECHAR32(0xFEFF, bom)
491
492 if byteorder == "little":
493 bom = "little"
494 elif byteorder == "big":
495 bom = "big"
496
497 pos = 0
498 while pos < len(s):
499 ch = ord(s[pos])
500 if 0xD800 <= ch <= 0xDFFF:
501 if errors == "surrogatepass":
502 p += STORECHAR32(ch, bom)
503 pos += 1
504 else:
505 res, pos = unicode_call_errorhandler(
506 errors, "utf-32", "surrogates not allowed", s, pos, pos + 1, False
507 )
508 for c in res:
509 p += STORECHAR32(ord(c), bom)
510 else:
511 p += STORECHAR32(ch, bom)
512 pos += 1
513
514 return p
515
516
517def utf_32_encode(obj, errors="strict"):

Callers 3

utf_32_encodeFunction · 0.85
utf_32_le_encodeFunction · 0.85
utf_32_be_encodeFunction · 0.85

Calls 4

STORECHAR32Function · 0.85
lenFunction · 0.85
ordFunction · 0.85

Tested by

no test coverage detected