~ubuntu-branches/ubuntu/feisty/python-numpy/feisty

« back to all changes in this revision

Viewing changes to numpy/core/src/ucsnarrow.c

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2006-07-12 10:00:24 UTC
  • Revision ID: james.westby@ubuntu.com-20060712100024-5lw9q2yczlisqcrt
Tags: upstream-0.9.8
ImportĀ upstreamĀ versionĀ 0.9.8

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Functions only needed on narrow builds of Python 
 
2
   for converting back and forth between the NumPy Unicode data-type 
 
3
   (always 4-byte)
 
4
   and the Python Unicode scalar (2-bytes on a narrow build). 
 
5
*/
 
6
 
 
7
/* the ucs2 buffer must be large enough to hold 2*ucs4length characters
 
8
   due to the use of surrogate pairs. 
 
9
 
 
10
   The return value is the number of ucs2 bytes used-up which
 
11
   is ucs4length + number of surrogate pairs found. 
 
12
 
 
13
   values above 0xffff are converted to surrogate pairs. 
 
14
 */
 
15
static int
 
16
PyUCS2Buffer_FromUCS4(Py_UNICODE *ucs2, PyArray_UCS4 *ucs4, int ucs4length)
 
17
{
 
18
        register int i;
 
19
        int numucs2 = 0;
 
20
        PyArray_UCS4 chr;
 
21
        for (i=0; i<ucs4length; i++) {
 
22
                chr = *ucs4++;
 
23
                if (chr > 0xffff) {
 
24
                        numucs2++;
 
25
                        chr -= 0x10000L;
 
26
                        *ucs2++ = 0xD800 + (Py_UNICODE) (chr >> 10);
 
27
                        *ucs2++ = 0xDC00 + (Py_UNICODE) (chr & 0x03FF);
 
28
                }
 
29
                else {
 
30
                        *ucs2++ = (Py_UNICODE) chr;
 
31
                }
 
32
                numucs2++;
 
33
        }
 
34
        return numucs2;
 
35
}
 
36
 
 
37
 
 
38
/* This converts a UCS2 buffer of the given length to UCS4 buffer.
 
39
   It converts up to ucs4len characters of UCS2
 
40
 
 
41
   It returns the number of characters converted which can
 
42
   be less than ucslen if there are surrogate pairs in ucs2.
 
43
 
 
44
   The return value is the actual size of the used part of the ucs4 buffer. 
 
45
*/
 
46
 
 
47
static int
 
48
PyUCS2Buffer_AsUCS4(Py_UNICODE *ucs2, PyArray_UCS4 *ucs4, int ucs2len, int ucs4len)
 
49
{
 
50
        register int i;
 
51
        register PyArray_UCS4 chr;
 
52
        register Py_UNICODE ch;
 
53
        register int numchars=0;
 
54
 
 
55
        for (i=0; (i < ucs2len) && (numchars < ucs4len); i++) {
 
56
                ch = *ucs2++;
 
57
                if (ch >= 0xd800 && ch <= 0xdfff) {
 
58
                        /* surrogate pair */
 
59
                        chr = ((PyArray_UCS4)(ch-0xd800)) << 10;
 
60
                        chr += *ucs2++ + 0x2400;  /* -0xdc00 + 0x10000 */
 
61
                        i++;
 
62
                }
 
63
                else {
 
64
                        chr = (PyArray_UCS4) ch;
 
65
                }
 
66
                *ucs4++ = chr;
 
67
                numchars++;
 
68
        }
 
69
        return numchars;
 
70
}
 
71
 
 
72
 
 
73
static PyObject *
 
74
MyPyUnicode_New(int length)
 
75
{
 
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);
 
80
        if (!unicode->str) {
 
81
                _Py_ForgetReference((PyObject *)unicode);
 
82
                PyObject_Del(unicode);
 
83
                return PyErr_NoMemory();
 
84
        }
 
85
        unicode->str[0] = 0;
 
86
        unicode->str[length] = 0;
 
87
        unicode->length = length;
 
88
        unicode->hash = -1;
 
89
        unicode->defenc = NULL;
 
90
        return (PyObject *)unicode;
 
91
}
 
92
 
 
93
static int
 
94
MyPyUnicode_Resize(PyUnicodeObject *uni, int length)
 
95
{       
 
96
        void *oldstr;
 
97
        
 
98
        oldstr = uni->str;
 
99
        PyMem_RESIZE(uni->str, Py_UNICODE, length+1);
 
100
        if (!uni->str) {
 
101
                uni->str = oldstr;
 
102
                PyErr_NoMemory();
 
103
                return -1;
 
104
        }
 
105
        uni->str[length] = 0;
 
106
        uni->length = length;
 
107
        return 0;
 
108
}