1 ##############################################################################
3 # Copyright (c) 2004 Zope Corporation and Contributors.
6 # This software is subject to the provisions of the Zope Public License,
7 # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
8 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
9 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
10 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
11 # FOR A PARTICULAR PURPOSE.
13 ##############################################################################
14 """Simple wrapper for ICU ucol API
20 cdef extern from "unicode/utypes.h":
23 U_USING_DEFAULT_WARNING = -127
24 U_USING_FALLBACK_WARNING = -128
26 U_ILLEGAL_ARGUMENT_ERROR = 1
29 int U_FAILURE(UErrorCode status)
31 cdef extern from "unicode/utf.h":
36 cdef extern from "unicode/ustring.h":
38 UChar *u_strFromUTF32(UChar *dest, int32_t destCapacity,
40 UChar32 *src, int32_t srcLength,
43 cdef extern from "unicode/ucol.h":
45 ctypedef struct UCollator:
47 UCollator *ucol_open(char *locale, UErrorCode *status)
48 void ucol_close(UCollator *collator)
49 int32_t ucol_getSortKey(UCollator *coll,
50 UChar *source, int32_t sourceLength,
54 int ucol_strcoll(UCollator *coll,
55 UChar *source, int32_t sourceLength,
56 UChar *target, int32_t targetLength)
58 cdef extern from "Python.h":
60 int PyUnicode_Check(ob)
61 int PyString_Check(ob)
63 ctypedef int Py_UNICODE
64 Py_UNICODE *PyUnicode_AS_UNICODE(ob)
65 int PyUnicode_GET_SIZE(ob)
66 char *PyString_AS_STRING(ob)
68 void *PyMem_Malloc(int size)
69 void PyMem_Free(void *p)
70 object PyString_FromStringAndSize(char *v, int l)
73 cdef class UCharString:
74 """Wrapper for ICU UChar arrays
78 cdef readonly int32_t length
79 cdef readonly object base
80 cdef readonly int need_to_free
82 def __cinit__(self, text):
84 cdef UErrorCode status
88 if not PyUnicode_Check(text):
89 if PyString_Check(text):
91 assert PyUnicode_Check(text)
93 raise TypeError("Expected unicode string")
95 length = PyUnicode_GET_SIZE(text)
96 str = PyUnicode_AS_UNICODE(text)
99 if sizeof(Py_UNICODE) == 2:
103 self.need_to_free = 0
105 buffsize = 2*length + 1
106 self.data = <UChar*>PyMem_Malloc(buffsize*sizeof(UChar))
107 if self.data == NULL:
109 status = U_ZERO_ERROR
110 u_strFromUTF32(self.data, buffsize, &(self.length),
111 <UChar32*>str, length, &status)
112 assert self.length <= buffsize
113 self.need_to_free = 1
114 if U_FAILURE(status):
116 "Couldn't convert Python unicode data to ICU unicode data."
119 def __dealloc__(self):
120 if self.need_to_free and self.data != NULL:
121 PyMem_Free(self.data)
126 """Compute a collation key for a unicode string.
129 cdef UCollator *collator
130 cdef readonly object locale
131 cdef readonly int used_default_information
133 def __cinit__(self, locale):
134 cdef UCollator *collator
135 cdef UErrorCode status
137 if not PyString_Check(locale):
138 raise TypeError("String locale expected")
140 status = U_ZERO_ERROR
141 collator = ucol_open(PyString_AS_STRING(locale), &status)
142 if U_FAILURE(status):
143 raise ValueError("Couldn't create a collator")
144 self.collator = collator
146 if (status == U_USING_DEFAULT_WARNING
147 or status == U_USING_FALLBACK_WARNING):
148 status = U_ILLEGAL_ARGUMENT_ERROR
149 self.used_default_information = status
151 def __dealloc__(self):
152 if self.collator != NULL:
153 ucol_close(self.collator)
156 """Compute a collation key for the given unicode text.
158 Of course, the key is only valid for the given locale.
164 icutext = UCharString(text)
165 bufsize = (<UCharString>icutext).length*2+10
167 # the +1 below is needed to avoid an apprent buffer overflow bug in ICU
168 buffer = <char*>PyMem_Malloc(bufsize +1)
171 size = ucol_getSortKey(self.collator,
172 (<UCharString>icutext).data,
173 (<UCharString>icutext).length,
174 <uint8_t*>buffer, bufsize)
175 while size > bufsize:
178 buffer = <char*>PyMem_Malloc(bufsize +1) # See above +1
181 size = ucol_getSortKey(self.collator,
182 (<UCharString>icutext).data,
183 (<UCharString>icutext).length,
184 <uint8_t*>buffer, bufsize)
186 result = PyString_FromStringAndSize(buffer, size)
190 def cmp(self, o1, o2):
195 (<UCharString>u1).data,
196 (<UCharString>u1).length,
197 (<UCharString>u2).data,
198 (<UCharString>u2).length,