UTF-32 decoding with BOM detection.
(data, errors="strict", final=0)
| 599 | |
| 600 | |
| 601 | def utf_32_decode(data, errors="strict", final=0): |
| 602 | """UTF-32 decoding with BOM detection.""" |
| 603 | if len(data) >= 4: |
| 604 | # Check for BOM |
| 605 | if data[0:4] == b"\xff\xfe\x00\x00": |
| 606 | # UTF-32 LE BOM |
| 607 | res, consumed, _ = PyUnicode_DecodeUTF32Stateful( |
| 608 | data[4:], len(data) - 4, errors, "little", final |
| 609 | ) |
| 610 | res = "".join(res) |
| 611 | return res, consumed + 4 |
| 612 | elif data[0:4] == b"\x00\x00\xfe\xff": |
| 613 | # UTF-32 BE BOM |
| 614 | res, consumed, _ = PyUnicode_DecodeUTF32Stateful( |
| 615 | data[4:], len(data) - 4, errors, "big", final |
| 616 | ) |
| 617 | res = "".join(res) |
| 618 | return res, consumed + 4 |
| 619 | |
| 620 | # Default to little-endian if no BOM |
| 621 | byteorder = "little" if sys.byteorder == "little" else "big" |
| 622 | res, consumed, _ = PyUnicode_DecodeUTF32Stateful( |
| 623 | data, len(data), errors, byteorder, final |
| 624 | ) |
| 625 | res = "".join(res) |
| 626 | return res, consumed |
| 627 | |
| 628 | |
| 629 | def utf_32_le_decode(data, errors="strict", final=0): |
nothing calls this directly
no test coverage detected