[Zope-Checkins] CVS: Zope/lib/python/Products/PluginIndexes/TextIndexNG/src - normalizer.c:1.1.2.2
Andreas Jung
andreas@digicool.com
Thu, 7 Feb 2002 09:56:21 -0500
Update of /cvs-repository/Zope/lib/python/Products/PluginIndexes/TextIndexNG/src
In directory cvs.zope.org:/tmp/cvs-serv3490/src
Modified Files:
Tag: ajung-textindexng-branch
normalizer.c
Log Message:
- complete rewrite
- leak free !
=== Zope/lib/python/Products/PluginIndexes/TextIndexNG/src/normalizer.c 1.1.2.1 => 1.1.2.2 ===
normalizer_dealloc(normalizer *self)
{
- Py_XDECREF(self->table);
+ Py_DECREF(self->table);
PyMem_DEL(self);
}
+static PyUnicodeObject *NormalizeWord(normalizer *self,PyUnicodeObject *word) {
+
+ PyUnicodeObject *temp;
+ int i;
+
+ temp = (PyUnicodeObject *) PyUnicode_FromUnicode(word->str,word->length);
+
+ for (i=0; i<PyList_GET_SIZE(self->table); i++) {
+ PyUnicodeObject *s;
+ PyObject *item, *key, *value;
+
+ item = PyList_GetItem(self->table, i);
+
+ key = PyTuple_GetItem(item,0);
+ value = PyTuple_GetItem(item,1);
+
+ s = (PyUnicodeObject *) PyUnicode_Replace((PyObject *) temp, key, value, -1);
+ Py_DECREF(temp);
+ temp = s;
+ }
+
+ return temp;
+}
+
static PyObject *normalize(normalizer *self, PyObject*args)
{
- int i,j;
- PyObject * data=NULL, *key=NULL, *value=NULL, *item=NULL;
-
- if (! (PyArg_ParseTuple(args,"O", &data)))
- return NULL;
+ int j;
+ PyObject * data=NULL ;
+ if (! (PyArg_ParseTuple(args,"O", &data))) return NULL;
if (PyList_Check(data)) {
- PyObject *word,*list;
+ PyObject *list;
list = PyList_New(0);
for (j=0; j<PyList_Size(data); j++) {
- word = PyList_GetItem(data,j);
+ PyUnicodeObject *word=NULL,*item=NULL;
+ item = (PyUnicodeObject *) PyList_GetItem(data,j);
- if (! (PyUnicode_Check(word) ||
- PyString_Check(word))) {
+ if (PyUnicode_Check(item)) {
+ word = NormalizeWord(self,item);
+ } else if (PyString_Check(item)) {
+ PyUnicodeObject *unicodeword;
- PyErr_SetString(PyExc_TypeError,"argument must be string or unicode");
- return NULL;
- }
+ unicodeword = (PyUnicodeObject *) PyUnicode_FromObject((PyObject *) item);
- for (i=0; i<PyList_GET_SIZE(self->table); i++) {
- PyObject *s;
+ word = NormalizeWord(self,unicodeword);
- item = PyList_GetItem(self->table, i);
+ Py_DECREF(unicodeword);
+ }
- key = PyTuple_GetItem(item,0);
- value = PyTuple_GetItem(item,1);
+ PyList_Append(list, (PyObject *) word);
+ Py_DECREF(word);
+ }
- s = PyUnicode_Replace(word, key, value, -1);
+ Py_DECREF(data);
- word = s;
- Py_INCREF(data);
+ return list;
- }
+ } else if (PyUnicode_Check(data)) {
- PyList_Append(list,word);
+ PyUnicodeObject *word;
- }
+ word = NormalizeWord(self, (PyUnicodeObject *) data);
- return list;
- } else if (PyUnicode_Check(data) || PyString_Check(data) ) {
+ return (PyObject *) word;
+ } else if (PyString_Check(data)) {
- for (i=0; i<PyList_GET_SIZE(self->table); i++) {
- PyObject *s;
+ PyUnicodeObject *word,*unicodeword;
- item = PyList_GetItem(self->table, i);
+ unicodeword = (PyUnicodeObject *) PyUnicode_FromObject(data);
- key = PyTuple_GetItem(item,0);
- value = PyTuple_GetItem(item,1);
+ word = NormalizeWord(self,unicodeword);
- s = PyUnicode_Replace(data, key, value, -1);
+ Py_DECREF(unicodeword);
- data = s;
- Py_INCREF(data);
+ return (PyObject *) word;
- }
} else {
PyErr_SetString(PyExc_TypeError,"argument must be unicode or string");
return NULL;
}
-
-
return data;
}
@@ -104,50 +121,43 @@
if (! PyTuple_Check(item)) {
PyErr_SetString(PyExc_TypeError,"nested arguments must be tuples");
- Py_DECREF(item);
- return 0;
+ goto err;
}
if (PyTuple_Size(item) != 2) {
PyErr_SetString(PyExc_TypeError,"nested arguments must be 2-tuples of strings/unicode strings");
- Py_DECREF(item);
- return 0;
+ goto err;
}
-
key = PyTuple_GetItem(item,0);
value = PyTuple_GetItem(item,1);
if (! (PyString_Check(key) || PyUnicode_Check(key))) {
PyErr_SetString(PyExc_TypeError, "arg 1 or 2-tuple must be string or unicode");
- Py_DECREF(key);
- Py_DECREF(value);
- Py_DECREF(item);
- return 0;
+ goto err;
}
if (! (PyString_Check(value) || PyUnicode_Check(value))) {
PyErr_SetString(PyExc_TypeError, "arg 2 or 2-tuple must be string or unicode");
- Py_DECREF(key);
- Py_DECREF(value);
- Py_DECREF(item);
- return 0;
+ goto err;
}
- Py_DECREF(key);
- Py_DECREF(value);
Py_DECREF(item);
}
return 1;
+
+err:
+ Py_DECREF(item);
+ return 0;
}
static struct PyMethodDef normalizer_methods[] =
{
{ "normalize", (PyCFunction)normalize,
METH_VARARGS, "normalize([string],[or list]) "
- "-- normalize a string or a list of strings"
+ "-- normalize a string or a list of strings/unicode strings"
},
{ NULL, NULL } /* sentinel */
};
@@ -161,29 +171,29 @@
static char normalizerType__doc__[] = "normalizer object";
static PyTypeObject normalizerType = {
- PyObject_HEAD_INIT(NULL)
- 0, /*ob_size*/
- "normalizer", /*tp_name*/
- sizeof(normalizer), /*tp_basicsize*/
- 0, /*tp_itemsize*/
- /* methods */
- (destructor)normalizer_dealloc, /*tp_dealloc*/
- (printfunc)0, /*tp_print*/
- (getattrfunc)normalizer_getattr, /*tp_getattr*/
- (setattrfunc)0, /*tp_setattr*/
- (cmpfunc)0, /*tp_compare*/
- (reprfunc)0, /*tp_repr*/
- 0, /*tp_as_number*/
- 0, /*tp_as_sequence*/
- 0, /*tp_as_mapping*/
- (hashfunc)0, /*tp_hash*/
- (ternaryfunc)0, /*tp_call*/
- (reprfunc)0, /*tp_str*/
-
- /* Space for future expansion */
- 0L,0L,0L,0L,
- normalizerType__doc__ /* Documentation string */
- };
+ PyObject_HEAD_INIT(NULL)
+ 0, /*ob_size*/
+ "normalizer", /*tp_name*/
+ sizeof(normalizer), /*tp_basicsize*/
+ 0, /*tp_itemsize*/
+ /* methods */
+ (destructor)normalizer_dealloc, /*tp_dealloc*/
+ (printfunc)0, /*tp_print*/
+ (getattrfunc)normalizer_getattr, /*tp_getattr*/
+ (setattrfunc)0, /*tp_setattr*/
+ (cmpfunc)0, /*tp_compare*/
+ (reprfunc)0, /*tp_repr*/
+ 0, /*tp_as_number*/
+ 0, /*tp_as_sequence*/
+ 0, /*tp_as_mapping*/
+ (hashfunc)0, /*tp_hash*/
+ (ternaryfunc)0, /*tp_call*/
+ (reprfunc)0, /*tp_str*/
+
+ /* Space for future expansion */
+ 0L,0L,0L,0L,
+ normalizerType__doc__ /* Documentation string */
+};
static PyObject *
@@ -210,7 +220,7 @@
static struct PyMethodDef normalizer_module_methods[] =
{
{ "Normalizer", (PyCFunction)newnormalizer, METH_VARARGS,
- "Normalizer(list) " "-- Normalizer module"
+ "Normalizer(list) " "-- Normalizer module - takes a list of 2-tuples of strings/unicode strings"
},
{ NULL, NULL }
};