MCPcopy Create free account
hub / github.com/numpy/numpy / PyUnicode_FromUCS4

Function PyUnicode_FromUCS4

numpy/core/src/common/ucsnarrow.c:42–71  ·  view source on GitHub ↗

* Returns a PyUnicodeObject initialized from a buffer containing * UCS4 unicode. * * Parameters * ---------- * src: char * * Pointer to buffer containing UCS4 unicode. * size: Py_ssize_t * Size of buffer in bytes. * swap: int * If true, the data will be swapped. * align: int * If true, the data will be aligned. * * Returns * ------- * new_reference: PyUnicod

Source from the content-addressed store, hash-verified

40 * new_reference: PyUnicodeObject
41 */
42NPY_NO_EXPORT PyUnicodeObject *
43PyUnicode_FromUCS4(char const *src_char, Py_ssize_t size, int swap, int align)
44{
45 Py_ssize_t ucs4len = size / sizeof(npy_ucs4);
46 npy_ucs4 const *src = (npy_ucs4 const *)src_char;
47 npy_ucs4 *buf = NULL;
48
49 /* swap and align if needed */
50 if (swap || align) {
51 buf = (npy_ucs4 *)malloc(size);
52 if (buf == NULL) {
53 PyErr_NoMemory();
54 return NULL;
55 }
56 memcpy(buf, src, size);
57 if (swap) {
58 byte_swap_vector(buf, ucs4len, sizeof(npy_ucs4));
59 }
60 src = buf;
61 }
62
63 /* trim trailing zeros */
64 while (ucs4len > 0 && src[ucs4len - 1] == 0) {
65 ucs4len--;
66 }
67 PyUnicodeObject *ret = (PyUnicodeObject *)PyUnicode_FromKindAndData(
68 PyUnicode_4BYTE_KIND, src, ucs4len);
69 free(buf);
70 return ret;
71}

Callers

nothing calls this directly

Calls 1

byte_swap_vectorFunction · 0.85

Tested by

no test coverage detected