1 /* Copyright 2000 by Jeffrey Chang. All rights reserved.
2 * This code is part of the Biopython distribution and governed by its
3 * license. Please see the LICENSE file that should have been included
4 * as part of this package.
11 #include <string.h> /* memset */
14 /* Functions in this module. */
16 static char cstringfns_splitany__doc__[] =
17 "splitany(str [,sep [,maxsplit [,negate]]]) -> list of strings\n\
19 Split a string. Similar to string.split, except that this considers\n\
20 any one of the characters in sep to be a delimiter. If negate is\n\
21 true, then everything but sep will be a separator.\n\
25 static PyObject *cstringfns_splitany(
26 PyObject *self, PyObject *args, PyObject *keywds)
29 int nsplit, maxsplit=0;
31 PyObject *py_negate=NULL;
32 PyObject *strlist, *newstr;
34 *sep=" \011\012\013\014\015"; /* whitespace */
36 static char *kwlist[] = {"str", "sep", "maxsplit", "negate", NULL};
38 if(!PyArg_ParseTupleAndKeywords(args, keywds, "s|siO", kwlist,
39 &str, &sep, &maxsplit, &py_negate))
43 /* negate = (py_negate && PyObject_IsTrue(py_negate));*/
44 /* XXX NO MORE NEGATE */
46 /* Set the tosplit array to 1 for characters to split on. */
47 memset(tosplit, 0, 256);
49 tosplit[(unsigned char)*sep++] = 1;
51 if(py_negate && PyObject_IsTrue(py_negate)) {
53 tosplit[i] = !tosplit[i];
56 /* Create a new list to store the variables. */
57 if(!(strlist = PyList_New(0))) {
58 PyErr_SetString(PyExc_SystemError, "I could not create a new list");
64 for(i=0; str[i] && (maxsplit == 0 || nsplit < maxsplit); i++) {
65 /*if(!(tosplit[(int)str[i]] == !negate))
67 if(!tosplit[(int)str[i]])
70 /* Split the string here. */
71 if(!(newstr = PyString_FromStringAndSize(&str[prev], i-prev))) {
72 PyErr_SetString(PyExc_SystemError,
73 "I could not create a new string");
76 if(PyList_Append(strlist, newstr) == -1) {
84 if(!PyErr_Occurred()) {
86 /* Add the last one. */
87 if(!(newstr = PyString_FromStringAndSize(&str[prev], i-prev))) {
88 PyErr_SetString(PyExc_SystemError,
89 "I could not create a new string");
91 PyList_Append(strlist, newstr);
105 /* Module definition stuff */
107 static PyMethodDef cstringfnsMethods[] = {
108 {"splitany", (PyCFunction)cstringfns_splitany, METH_VARARGS|METH_KEYWORDS,
109 cstringfns_splitany__doc__},
113 static char cstringfns__doc__[] =
114 "This provides helper functions for the stringfns module.\n\
115 You should never import this module on its own.\n\
119 void initcstringfns(void)
121 (void) Py_InitModule3("cstringfns", cstringfnsMethods, cstringfns__doc__);