Keep USet of contractions in the collator object

This commit is contained in:
Kovid Goyal 2012-07-03 22:41:03 +05:30
parent 3e13f2fab1
commit 16c1e6a102

View File

@ -13,6 +13,7 @@ typedef struct {
PyObject_HEAD PyObject_HEAD
// Type-specific fields go here. // Type-specific fields go here.
UCollator *collator; UCollator *collator;
USet *contractions;
} icu_Collator; } icu_Collator;
@ -20,6 +21,7 @@ static void
icu_Collator_dealloc(icu_Collator* self) icu_Collator_dealloc(icu_Collator* self)
{ {
if (self->collator != NULL) ucol_close(self->collator); if (self->collator != NULL) ucol_close(self->collator);
if (self->contractions != NULL) uset_close(self->contractions);
self->collator = NULL; self->collator = NULL;
self->ob_type->tp_free((PyObject*)self); self->ob_type->tp_free((PyObject*)self);
} }
@ -42,6 +44,7 @@ icu_Collator_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Py_DECREF(self); Py_DECREF(self);
return NULL; return NULL;
} }
self->contractions = NULL;
} }
return (PyObject *)self; return (PyObject *)self;
@ -211,7 +214,6 @@ icu_Collator_find(icu_Collator *self, PyObject *args, PyObject *kwargs) {
// Collator.contractions {{{ // Collator.contractions {{{
static PyObject * static PyObject *
icu_Collator_contractions(icu_Collator *self, PyObject *args, PyObject *kwargs) { icu_Collator_contractions(icu_Collator *self, PyObject *args, PyObject *kwargs) {
USet *contractions;
UErrorCode status = U_ZERO_ERROR; UErrorCode status = U_ZERO_ERROR;
UChar *str; UChar *str;
UChar32 start=0, end=0; UChar32 start=0, end=0;
@ -219,33 +221,35 @@ icu_Collator_contractions(icu_Collator *self, PyObject *args, PyObject *kwargs)
PyObject *ans = Py_None, *pbuf; PyObject *ans = Py_None, *pbuf;
wchar_t *buf; wchar_t *buf;
if (self->contractions == NULL) {
self->contractions = uset_open(1, 0);
if (self->contractions == NULL) return PyErr_NoMemory();
ucol_getContractionsAndExpansions(self->collator, self->contractions, NULL, 0, &status);
}
status = U_ZERO_ERROR;
str = (UChar*)calloc(100, sizeof(UChar)); str = (UChar*)calloc(100, sizeof(UChar));
buf = (wchar_t*)calloc(4*100+2, sizeof(wchar_t)); buf = (wchar_t*)calloc(4*100+2, sizeof(wchar_t));
if (str == NULL || buf == NULL) return PyErr_NoMemory(); if (str == NULL || buf == NULL) return PyErr_NoMemory();
contractions = uset_open(1, 0); count = uset_getItemCount(self->contractions);
ucol_getContractionsAndExpansions(self->collator, contractions, NULL, 0, &status); ans = PyTuple_New(count);
if (U_SUCCESS(status)) { if (ans != NULL) {
count = uset_getItemCount(contractions); for (i = 0; i < count; i++) {
ans = PyTuple_New(count); len = uset_getItem(self->contractions, i, &start, &end, str, 1000, &status);
if (ans != NULL) { if (len >= 2) {
for (i = 0; i < count; i++) { // We have a string
len = uset_getItem(contractions, i, &start, &end, str, 1000, &status); status = U_ZERO_ERROR;
if (len >= 2) { u_strToWCS(buf, 4*100 + 1, &dlen, str, len, &status);
// We have a string pbuf = PyUnicode_FromWideChar(buf, dlen);
status = U_ZERO_ERROR; if (pbuf == NULL) return PyErr_NoMemory();
u_strToWCS(buf, 4*100 + 1, &dlen, str, len, &status); PyTuple_SetItem(ans, i, pbuf);
pbuf = PyUnicode_FromWideChar(buf, dlen); } else {
if (pbuf == NULL) return PyErr_NoMemory(); // Ranges dont make sense for contractions, ignore them
PyTuple_SetItem(ans, i, pbuf); PyTuple_SetItem(ans, i, Py_None);
} else {
// Ranges dont make sense for contractions, ignore them
PyTuple_SetItem(ans, i, Py_None);
}
} }
} }
} }
uset_close(contractions);
free(str); free(buf); free(str); free(buf);
return Py_BuildValue("O", ans); return Py_BuildValue("O", ans);