1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
/* Copyright 2000 by Jeffrey Chang. All rights reserved.
* This code is part of the Biopython distribution and governed by its
* license. Please see the LICENSE file that should have been included
* as part of this package.
*
* cstringfnsmodule.c
* Created 7 Jun 2000
*/
#include "Python.h"
#include <string.h> /* memset */
/* Functions in this module. */
static char cstringfns_splitany__doc__[] =
"splitany(str [,sep [,maxsplit [,negate]]]) -> list of strings\n\
\n\
Split a string. Similar to string.split, except that this considers\n\
any one of the characters in sep to be a delimiter. If negate is\n\
true, then everything but sep will be a separator.\n\
\n\
";
static PyObject *cstringfns_splitany(
PyObject *self, PyObject *args, PyObject *keywds)
{
int i, prev;
int nsplit, maxsplit=0;
/*int negate=0;*/
PyObject *py_negate=NULL;
PyObject *strlist, *newstr;
unsigned char *str,
*sep=" \011\012\013\014\015"; /* whitespace */
char tosplit[256];
static char *kwlist[] = {"str", "sep", "maxsplit", "negate", NULL};
if(!PyArg_ParseTupleAndKeywords(args, keywds, "s|siO", kwlist,
&str, &sep, &maxsplit, &py_negate))
return NULL;
if(maxsplit < 0)
maxsplit = 1;
/* negate = (py_negate && PyObject_IsTrue(py_negate));*/
/* XXX NO MORE NEGATE */
/* Set the tosplit array to 1 for characters to split on. */
memset(tosplit, 0, 256);
while(*sep) {
tosplit[(unsigned char)*sep++] = 1;
}
if(py_negate && PyObject_IsTrue(py_negate)) {
for(i=0; i<256; i++)
tosplit[i] = !tosplit[i];
}
/* Create a new list to store the variables. */
if(!(strlist = PyList_New(0))) {
PyErr_SetString(PyExc_SystemError, "I could not create a new list");
return NULL;
}
prev = 0;
nsplit = 0;
for(i=0; str[i] && (maxsplit == 0 || nsplit < maxsplit); i++) {
/*if(!(tosplit[(int)str[i]] == !negate))
continue; */
if(!tosplit[(int)str[i]])
continue;
/* Split the string here. */
if(!(newstr = PyString_FromStringAndSize(&str[prev], i-prev))) {
PyErr_SetString(PyExc_SystemError,
"I could not create a new string");
break;
}
if(PyList_Append(strlist, newstr) == -1) {
Py_DECREF(newstr);
break;
}
Py_DECREF(newstr);
prev = i+1;
nsplit++;
}
if(!PyErr_Occurred()) {
i = strlen(str);
/* Add the last one. */
if(!(newstr = PyString_FromStringAndSize(&str[prev], i-prev))) {
PyErr_SetString(PyExc_SystemError,
"I could not create a new string");
} else {
PyList_Append(strlist, newstr);
Py_DECREF(newstr);
}
} else {
Py_DECREF(strlist);
return NULL;
}
return strlist;
}
/* Module definition stuff */
static PyMethodDef cstringfnsMethods[] = {
{"splitany", (PyCFunction)cstringfns_splitany, METH_VARARGS|METH_KEYWORDS,
cstringfns_splitany__doc__},
{NULL, NULL}
};
static char cstringfns__doc__[] =
"This provides helper functions for the stringfns module.\n\
You should never import this module on its own.\n\
\n\
";
void initcstringfns(void)
{
(void) Py_InitModule3("cstringfns", cstringfnsMethods, cstringfns__doc__);
}
|