| 679 | |
| 680 | template <typename T> |
| 681 | Status AppendUTF32(const char* data, int64_t itemsize, int byteorder, T* builder) { |
| 682 | // The binary \x00\x00\x00\x00 indicates a nul terminator in NumPy unicode, |
| 683 | // so we need to detect that here to truncate if necessary. Yep. |
| 684 | Py_ssize_t actual_length = 0; |
| 685 | for (; actual_length < itemsize / kNumPyUnicodeSize; ++actual_length) { |
| 686 | const char* code_point = data + actual_length * kNumPyUnicodeSize; |
| 687 | if ((*code_point == '\0') && (*(code_point + 1) == '\0') && |
| 688 | (*(code_point + 2) == '\0') && (*(code_point + 3) == '\0')) { |
| 689 | break; |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | OwnedRef unicode_obj(PyUnicode_DecodeUTF32(data, actual_length * kNumPyUnicodeSize, |
| 694 | nullptr, &byteorder)); |
| 695 | RETURN_IF_PYERROR(); |
| 696 | OwnedRef utf8_obj(PyUnicode_AsUTF8String(unicode_obj.obj())); |
| 697 | if (utf8_obj.obj() == NULL) { |
| 698 | PyErr_Clear(); |
| 699 | return Status::Invalid("failed converting UTF32 to UTF8"); |
| 700 | } |
| 701 | |
| 702 | const int32_t length = static_cast<int32_t>(PyBytes_GET_SIZE(utf8_obj.obj())); |
| 703 | return builder->Append( |
| 704 | reinterpret_cast<const uint8_t*>(PyBytes_AS_STRING(utf8_obj.obj())), length); |
| 705 | } |
| 706 | |
| 707 | } // namespace |
| 708 |
no test coverage detected