Encode a Unicode string to UTF-32.
(s, size, errors, byteorder="little")
| 480 | |
| 481 | |
| 482 | def 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 | |
| 517 | def utf_32_encode(obj, errors="strict"): |
no test coverage detected