1
/* Functions only needed on narrow builds of Python
2
for converting back and forth between the NumPy Unicode data-type
4
and the Python Unicode scalar (2-bytes on a narrow build).
7
/* the ucs2 buffer must be large enough to hold 2*ucs4length characters
8
due to the use of surrogate pairs.
10
The return value is the number of ucs2 bytes used-up which
11
is ucs4length + number of surrogate pairs found.
13
values above 0xffff are converted to surrogate pairs.
16
PyUCS2Buffer_FromUCS4(Py_UNICODE *ucs2, PyArray_UCS4 *ucs4, int ucs4length)
21
for (i=0; i<ucs4length; i++) {
26
*ucs2++ = 0xD800 + (Py_UNICODE) (chr >> 10);
27
*ucs2++ = 0xDC00 + (Py_UNICODE) (chr & 0x03FF);
30
*ucs2++ = (Py_UNICODE) chr;
38
/* This converts a UCS2 buffer of the given length to UCS4 buffer.
39
It converts up to ucs4len characters of UCS2
41
It returns the number of characters converted which can
42
be less than ucslen if there are surrogate pairs in ucs2.
44
The return value is the actual size of the used part of the ucs4 buffer.
48
PyUCS2Buffer_AsUCS4(Py_UNICODE *ucs2, PyArray_UCS4 *ucs4, int ucs2len, int ucs4len)
51
register PyArray_UCS4 chr;
52
register Py_UNICODE ch;
53
register int numchars=0;
55
for (i=0; (i < ucs2len) && (numchars < ucs4len); i++) {
57
if (ch >= 0xd800 && ch <= 0xdfff) {
59
chr = ((PyArray_UCS4)(ch-0xd800)) << 10;
60
chr += *ucs2++ + 0x2400; /* -0xdc00 + 0x10000 */
64
chr = (PyArray_UCS4) ch;
74
MyPyUnicode_New(int length)
76
PyUnicodeObject *unicode;
77
unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type);
78
if (unicode == NULL) return NULL;
79
unicode->str = PyMem_NEW(Py_UNICODE, length+1);
81
_Py_ForgetReference((PyObject *)unicode);
82
PyObject_Del(unicode);
83
return PyErr_NoMemory();
86
unicode->str[length] = 0;
87
unicode->length = length;
89
unicode->defenc = NULL;
90
return (PyObject *)unicode;
94
MyPyUnicode_Resize(PyUnicodeObject *uni, int length)
99
PyMem_RESIZE(uni->str, Py_UNICODE, length+1);
105
uni->str[length] = 0;
106
uni->length = length;