1992-09-03 20:21:07 +00:00
|
|
|
/***********************************************************
|
1995-01-04 19:10:35 +00:00
|
|
|
Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
|
|
|
|
The Netherlands.
|
1992-09-03 20:21:07 +00:00
|
|
|
|
|
|
|
All Rights Reserved
|
|
|
|
|
1996-10-25 14:44:06 +00:00
|
|
|
Permission to use, copy, modify, and distribute this software and its
|
|
|
|
documentation for any purpose and without fee is hereby granted,
|
1992-09-03 20:21:07 +00:00
|
|
|
provided that the above copyright notice appear in all copies and that
|
1996-10-25 14:44:06 +00:00
|
|
|
both that copyright notice and this permission notice appear in
|
1992-09-03 20:21:07 +00:00
|
|
|
supporting documentation, and that the names of Stichting Mathematisch
|
1996-10-25 14:44:06 +00:00
|
|
|
Centrum or CWI or Corporation for National Research Initiatives or
|
|
|
|
CNRI not be used in advertising or publicity pertaining to
|
|
|
|
distribution of the software without specific, written prior
|
|
|
|
permission.
|
|
|
|
|
|
|
|
While CWI is the initial source for this software, a modified version
|
|
|
|
is made available by the Corporation for National Research Initiatives
|
|
|
|
(CNRI) at the Internet address ftp://ftp.python.org.
|
|
|
|
|
|
|
|
STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
|
|
|
|
REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
|
|
|
|
CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
|
|
|
|
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
|
|
|
|
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
|
|
|
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
|
|
PERFORMANCE OF THIS SOFTWARE.
|
1992-09-03 20:21:07 +00:00
|
|
|
|
|
|
|
******************************************************************/
|
|
|
|
|
|
|
|
/* strop module */
|
|
|
|
|
1996-12-09 18:35:56 +00:00
|
|
|
#include "Python.h"
|
1992-09-03 20:21:07 +00:00
|
|
|
|
1993-07-08 11:12:36 +00:00
|
|
|
#include <ctype.h>
|
1993-07-09 10:51:31 +00:00
|
|
|
/* XXX This file assumes that the <ctype.h> is*() functions
|
|
|
|
XXX are defined for all 8-bit characters! */
|
1993-07-08 11:12:36 +00:00
|
|
|
|
1996-08-08 19:16:15 +00:00
|
|
|
/* The lstrip(), rstrip() and strip() functions are implemented
|
|
|
|
in do_strip(), which uses an additional parameter to indicate what
|
|
|
|
type of strip should occur. */
|
|
|
|
|
|
|
|
#define LEFTSTRIP 0
|
|
|
|
#define RIGHTSTRIP 1
|
|
|
|
#define BOTHSTRIP 2
|
|
|
|
|
1992-09-03 20:21:07 +00:00
|
|
|
|
1996-12-09 18:35:56 +00:00
|
|
|
static PyObject *
|
1996-08-08 19:16:15 +00:00
|
|
|
split_whitespace(s, len, maxsplit)
|
1992-09-03 20:21:07 +00:00
|
|
|
char *s;
|
1995-05-03 17:40:23 +00:00
|
|
|
int len;
|
1996-08-08 19:16:15 +00:00
|
|
|
int maxsplit;
|
1995-05-03 17:40:23 +00:00
|
|
|
{
|
1997-01-03 22:45:34 +00:00
|
|
|
int i = 0, j, err;
|
|
|
|
int countsplit = 0;
|
|
|
|
PyObject* item;
|
|
|
|
PyObject *list = PyList_New(0);
|
1992-09-03 20:21:07 +00:00
|
|
|
|
|
|
|
if (list == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
while (i < len) {
|
1995-02-10 17:01:56 +00:00
|
|
|
while (i < len && isspace(Py_CHARMASK(s[i]))) {
|
1992-09-03 20:21:07 +00:00
|
|
|
i = i+1;
|
|
|
|
}
|
|
|
|
j = i;
|
1995-02-14 00:58:59 +00:00
|
|
|
while (i < len && !isspace(Py_CHARMASK(s[i]))) {
|
1992-09-03 20:21:07 +00:00
|
|
|
i = i+1;
|
|
|
|
}
|
|
|
|
if (j < i) {
|
1996-12-09 18:35:56 +00:00
|
|
|
item = PyString_FromStringAndSize(s+j, (int)(i-j));
|
1997-01-03 22:45:34 +00:00
|
|
|
if (item == NULL)
|
|
|
|
goto finally;
|
|
|
|
|
1996-12-09 18:35:56 +00:00
|
|
|
err = PyList_Append(list, item);
|
|
|
|
Py_DECREF(item);
|
1997-01-03 22:45:34 +00:00
|
|
|
if (err < 0)
|
|
|
|
goto finally;
|
1996-08-08 19:16:15 +00:00
|
|
|
|
|
|
|
countsplit++;
|
|
|
|
if (maxsplit && (countsplit >= maxsplit)) {
|
1996-12-09 18:35:56 +00:00
|
|
|
item = PyString_FromStringAndSize(
|
|
|
|
s+i, (int)(len - i));
|
1997-01-03 22:45:34 +00:00
|
|
|
if (item == NULL)
|
|
|
|
goto finally;
|
|
|
|
|
1996-12-09 18:35:56 +00:00
|
|
|
err = PyList_Append(list, item);
|
|
|
|
Py_DECREF(item);
|
1997-01-03 22:45:34 +00:00
|
|
|
if (err < 0)
|
|
|
|
goto finally;
|
|
|
|
|
1996-08-08 19:16:15 +00:00
|
|
|
i = len;
|
|
|
|
}
|
|
|
|
|
1992-09-03 20:21:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return list;
|
1997-01-03 22:45:34 +00:00
|
|
|
finally:
|
|
|
|
Py_DECREF(list);
|
|
|
|
return NULL;
|
1992-09-03 20:21:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1996-12-09 18:35:56 +00:00
|
|
|
static PyObject *
|
1992-09-03 20:21:07 +00:00
|
|
|
strop_splitfields(self, args)
|
1996-12-09 18:35:56 +00:00
|
|
|
PyObject *self; /* Not used */
|
|
|
|
PyObject *args;
|
1992-09-03 20:21:07 +00:00
|
|
|
{
|
1993-11-05 10:14:49 +00:00
|
|
|
int len, n, i, j, err;
|
1996-08-08 19:16:15 +00:00
|
|
|
int splitcount, maxsplit;
|
1992-09-03 20:21:07 +00:00
|
|
|
char *s, *sub;
|
1996-12-09 18:35:56 +00:00
|
|
|
PyObject *list, *item;
|
1992-09-03 20:21:07 +00:00
|
|
|
|
1995-05-03 17:40:23 +00:00
|
|
|
sub = NULL;
|
|
|
|
n = 0;
|
1996-08-08 19:16:15 +00:00
|
|
|
splitcount = 0;
|
|
|
|
maxsplit = 0;
|
1996-12-09 18:35:56 +00:00
|
|
|
if (!PyArg_ParseTuple(args, "s#|z#i", &s, &len, &sub, &n, &maxsplit))
|
1992-09-03 20:21:07 +00:00
|
|
|
return NULL;
|
1995-05-03 17:40:23 +00:00
|
|
|
if (sub == NULL)
|
1996-08-08 19:16:15 +00:00
|
|
|
return split_whitespace(s, len, maxsplit);
|
1992-09-03 20:21:07 +00:00
|
|
|
if (n == 0) {
|
1996-12-09 18:35:56 +00:00
|
|
|
PyErr_SetString(PyExc_ValueError, "empty separator");
|
1992-09-03 20:21:07 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1996-12-09 18:35:56 +00:00
|
|
|
list = PyList_New(0);
|
1992-09-03 20:21:07 +00:00
|
|
|
if (list == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
i = j = 0;
|
|
|
|
while (i+n <= len) {
|
1996-10-04 13:39:37 +00:00
|
|
|
if (s[i] == sub[0] && (n == 1 || memcmp(s+i, sub, n) == 0)) {
|
1996-12-09 18:35:56 +00:00
|
|
|
item = PyString_FromStringAndSize(s+j, (int)(i-j));
|
1993-11-05 10:14:49 +00:00
|
|
|
if (item == NULL)
|
|
|
|
goto fail;
|
1996-12-09 18:35:56 +00:00
|
|
|
err = PyList_Append(list, item);
|
|
|
|
Py_DECREF(item);
|
1993-11-05 10:14:49 +00:00
|
|
|
if (err < 0)
|
|
|
|
goto fail;
|
1992-09-03 20:21:07 +00:00
|
|
|
i = j = i + n;
|
1996-08-08 19:16:15 +00:00
|
|
|
splitcount++;
|
|
|
|
if (maxsplit && (splitcount >= maxsplit))
|
|
|
|
break;
|
1992-09-03 20:21:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
i++;
|
|
|
|
}
|
1996-12-09 18:35:56 +00:00
|
|
|
item = PyString_FromStringAndSize(s+j, (int)(len-j));
|
1993-11-05 10:14:49 +00:00
|
|
|
if (item == NULL)
|
|
|
|
goto fail;
|
1996-12-09 18:35:56 +00:00
|
|
|
err = PyList_Append(list, item);
|
|
|
|
Py_DECREF(item);
|
1993-11-05 10:14:49 +00:00
|
|
|
if (err < 0)
|
|
|
|
goto fail;
|
1992-09-03 20:21:07 +00:00
|
|
|
|
|
|
|
return list;
|
1993-11-05 10:14:49 +00:00
|
|
|
|
|
|
|
fail:
|
1996-12-09 18:35:56 +00:00
|
|
|
Py_DECREF(list);
|
1993-11-05 10:14:49 +00:00
|
|
|
return NULL;
|
1992-09-03 20:21:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1996-12-09 18:35:56 +00:00
|
|
|
static PyObject *
|
1992-11-26 08:54:07 +00:00
|
|
|
strop_joinfields(self, args)
|
1996-12-09 18:35:56 +00:00
|
|
|
PyObject *self; /* Not used */
|
|
|
|
PyObject *args;
|
1992-11-26 08:54:07 +00:00
|
|
|
{
|
1996-12-09 18:35:56 +00:00
|
|
|
PyObject *seq, *item, *res;
|
1997-01-03 23:46:51 +00:00
|
|
|
char *p, *sep = NULL;
|
|
|
|
int seqlen, reslen, itemlen, i, seplen = 0;
|
|
|
|
int ownrefs = 0;
|
|
|
|
PyObject* (*getitemfunc) Py_PROTO((PyObject*, int));
|
1992-11-26 08:54:07 +00:00
|
|
|
|
1996-12-09 18:35:56 +00:00
|
|
|
if (!PyArg_ParseTuple(args, "O|s#", &seq, &sep, &seplen))
|
1992-11-26 08:54:07 +00:00
|
|
|
return NULL;
|
1995-05-03 17:40:23 +00:00
|
|
|
if (sep == NULL) {
|
|
|
|
sep = " ";
|
|
|
|
seplen = 1;
|
|
|
|
}
|
1997-01-03 23:46:51 +00:00
|
|
|
/* do it this way to optimize for the common cases, but also
|
|
|
|
* support the more general sequence protocol
|
|
|
|
*/
|
|
|
|
if (PyList_Check(seq)) {
|
|
|
|
getitemfunc = PyList_GetItem;
|
|
|
|
seqlen = PyList_Size(seq);
|
|
|
|
}
|
|
|
|
else if (PyTuple_Check(seq)) {
|
|
|
|
getitemfunc = PyTuple_GetItem;
|
|
|
|
seqlen = PyTuple_Size(seq);
|
|
|
|
}
|
|
|
|
else if (PySequence_Check(seq)) {
|
|
|
|
getitemfunc = PySequence_GetItem;
|
|
|
|
seqlen = PySequence_Length(seq);
|
|
|
|
ownrefs = 1;
|
|
|
|
}
|
|
|
|
else {
|
1996-12-09 18:35:56 +00:00
|
|
|
PyErr_SetString(PyExc_TypeError,
|
1997-01-03 22:45:34 +00:00
|
|
|
"first argument must be a sequence");
|
1992-11-26 08:54:07 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-01-03 22:45:34 +00:00
|
|
|
if (seqlen < 0 && PyErr_Occurred())
|
|
|
|
return NULL;
|
|
|
|
|
1992-11-26 08:54:07 +00:00
|
|
|
reslen = 0;
|
|
|
|
for (i = 0; i < seqlen; i++) {
|
1997-01-03 23:46:51 +00:00
|
|
|
if (!(item = getitemfunc(seq, i)))
|
1997-01-03 22:45:34 +00:00
|
|
|
return NULL;
|
|
|
|
|
1996-12-09 18:35:56 +00:00
|
|
|
if (!PyString_Check(item)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
1997-01-03 22:45:34 +00:00
|
|
|
"first argument must be sequence of strings");
|
1997-01-03 23:46:51 +00:00
|
|
|
if (ownrefs)
|
|
|
|
Py_DECREF(item);
|
1992-11-26 08:54:07 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (i > 0)
|
1997-01-03 22:45:34 +00:00
|
|
|
reslen += seplen;
|
|
|
|
reslen += PyString_Size(item);
|
1997-01-03 23:46:51 +00:00
|
|
|
if (ownrefs)
|
|
|
|
Py_DECREF(item);
|
1992-11-26 08:54:07 +00:00
|
|
|
}
|
1997-01-03 23:46:51 +00:00
|
|
|
if (seqlen == 1) {
|
1992-11-26 08:54:07 +00:00
|
|
|
/* Optimization if there's only one item */
|
1997-01-03 23:46:51 +00:00
|
|
|
item = getitemfunc(seq, 0);
|
|
|
|
if (!ownrefs)
|
|
|
|
Py_XINCREF(item);
|
|
|
|
return item;
|
|
|
|
}
|
1997-01-03 22:45:34 +00:00
|
|
|
|
1996-12-09 18:35:56 +00:00
|
|
|
res = PyString_FromStringAndSize((char *)NULL, reslen);
|
1992-11-26 08:54:07 +00:00
|
|
|
if (res == NULL)
|
|
|
|
return NULL;
|
1996-12-09 18:35:56 +00:00
|
|
|
p = PyString_AsString(res);
|
1992-11-26 08:54:07 +00:00
|
|
|
for (i = 0; i < seqlen; i++) {
|
1997-01-03 23:46:51 +00:00
|
|
|
if (!(item = getitemfunc(seq, i))) {
|
1997-01-03 22:45:34 +00:00
|
|
|
Py_DECREF(res);
|
|
|
|
return NULL;
|
|
|
|
}
|
1992-11-26 08:54:07 +00:00
|
|
|
if (i > 0) {
|
|
|
|
memcpy(p, sep, seplen);
|
|
|
|
p += seplen;
|
|
|
|
}
|
1996-12-09 18:35:56 +00:00
|
|
|
itemlen = PyString_Size(item);
|
|
|
|
memcpy(p, PyString_AsString(item), itemlen);
|
1992-11-26 08:54:07 +00:00
|
|
|
p += itemlen;
|
1997-01-03 23:46:51 +00:00
|
|
|
if (ownrefs)
|
|
|
|
Py_DECREF(item);
|
1992-11-26 08:54:07 +00:00
|
|
|
}
|
1996-12-09 18:35:56 +00:00
|
|
|
if (p != PyString_AsString(res) + reslen) {
|
|
|
|
PyErr_SetString(PyExc_SystemError,
|
|
|
|
"strop.joinfields: assertion failed");
|
1992-11-26 08:54:07 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1996-12-09 18:35:56 +00:00
|
|
|
static PyObject *
|
1994-08-17 13:15:46 +00:00
|
|
|
strop_find(self, args)
|
1996-12-09 18:35:56 +00:00
|
|
|
PyObject *self; /* Not used */
|
|
|
|
PyObject *args;
|
1992-09-03 20:21:07 +00:00
|
|
|
{
|
|
|
|
char *s, *sub;
|
1997-01-03 22:45:34 +00:00
|
|
|
int len, n, i = 0;
|
1992-09-03 20:21:07 +00:00
|
|
|
|
1997-01-03 22:45:34 +00:00
|
|
|
if (!PyArg_ParseTuple(args, "s#s#|i", &s, &len, &sub, &n, &i))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (i < 0)
|
|
|
|
i += len;
|
|
|
|
if (i < 0)
|
1992-09-03 20:21:07 +00:00
|
|
|
i = 0;
|
|
|
|
|
|
|
|
if (n == 0)
|
1996-12-09 18:35:56 +00:00
|
|
|
return PyInt_FromLong((long)i);
|
1992-09-03 20:21:07 +00:00
|
|
|
|
|
|
|
len -= n;
|
1993-10-26 15:23:55 +00:00
|
|
|
for (; i <= len; ++i)
|
|
|
|
if (s[i] == sub[0] &&
|
1996-10-04 13:39:37 +00:00
|
|
|
(n == 1 || memcmp(&s[i+1], &sub[1], n-1) == 0))
|
1996-12-09 18:35:56 +00:00
|
|
|
return PyInt_FromLong((long)i);
|
1993-10-26 15:23:55 +00:00
|
|
|
|
1996-12-09 18:35:56 +00:00
|
|
|
return PyInt_FromLong(-1L);
|
1993-10-26 15:23:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1996-12-09 18:35:56 +00:00
|
|
|
static PyObject *
|
1994-08-17 13:15:46 +00:00
|
|
|
strop_rfind(self, args)
|
1996-12-09 18:35:56 +00:00
|
|
|
PyObject *self; /* Not used */
|
|
|
|
PyObject *args;
|
1993-10-26 15:23:55 +00:00
|
|
|
{
|
|
|
|
char *s, *sub;
|
1997-01-03 22:45:34 +00:00
|
|
|
int len, n, j;
|
|
|
|
int i = 0;
|
1993-10-26 15:23:55 +00:00
|
|
|
|
1997-01-03 22:45:34 +00:00
|
|
|
if (!PyArg_ParseTuple(args, "s#s#|i", &s, &len, &sub, &n, &i))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (i < 0)
|
|
|
|
i += len;
|
|
|
|
if (i < 0)
|
1994-08-01 11:34:53 +00:00
|
|
|
i = 0;
|
1993-10-26 15:23:55 +00:00
|
|
|
|
|
|
|
if (n == 0)
|
1996-12-09 18:35:56 +00:00
|
|
|
return PyInt_FromLong((long)len);
|
1993-10-26 15:23:55 +00:00
|
|
|
|
1994-08-01 11:34:53 +00:00
|
|
|
for (j = len-n; j >= i; --j)
|
|
|
|
if (s[j] == sub[0] &&
|
1996-10-04 13:39:37 +00:00
|
|
|
(n == 1 || memcmp(&s[j+1], &sub[1], n-1) == 0))
|
1996-12-09 18:35:56 +00:00
|
|
|
return PyInt_FromLong((long)j);
|
1992-09-03 20:21:07 +00:00
|
|
|
|
1996-12-09 18:35:56 +00:00
|
|
|
return PyInt_FromLong(-1L);
|
1992-09-03 20:21:07 +00:00
|
|
|
}
|
|
|
|
|
1996-12-09 18:35:56 +00:00
|
|
|
static PyObject *
|
1996-08-08 19:16:15 +00:00
|
|
|
do_strip(args, striptype)
|
1996-12-09 18:35:56 +00:00
|
|
|
PyObject *args;
|
1996-08-08 19:16:15 +00:00
|
|
|
int striptype;
|
1992-09-03 20:21:07 +00:00
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
int len, i, j;
|
|
|
|
|
1996-08-08 19:16:15 +00:00
|
|
|
|
1996-12-09 18:35:56 +00:00
|
|
|
if (!PyArg_Parse(args, "s#", &s, &len))
|
1992-09-03 20:21:07 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
i = 0;
|
1996-08-08 19:16:15 +00:00
|
|
|
if (striptype != RIGHTSTRIP) {
|
|
|
|
while (i < len && isspace(Py_CHARMASK(s[i]))) {
|
|
|
|
i++;
|
|
|
|
}
|
1992-09-03 20:21:07 +00:00
|
|
|
}
|
1996-08-08 19:16:15 +00:00
|
|
|
|
1992-09-03 20:21:07 +00:00
|
|
|
|
|
|
|
j = len;
|
1996-08-08 19:16:15 +00:00
|
|
|
if (striptype != LEFTSTRIP) {
|
|
|
|
do {
|
|
|
|
j--;
|
|
|
|
} while (j >= i && isspace(Py_CHARMASK(s[j])));
|
|
|
|
j++;
|
|
|
|
}
|
1992-09-03 20:21:07 +00:00
|
|
|
|
|
|
|
if (i == 0 && j == len) {
|
1996-12-09 18:35:56 +00:00
|
|
|
Py_INCREF(args);
|
1992-09-03 20:21:07 +00:00
|
|
|
return args;
|
|
|
|
}
|
|
|
|
else
|
1996-12-09 18:35:56 +00:00
|
|
|
return PyString_FromStringAndSize(s+i, j-i);
|
1992-09-03 20:21:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1996-12-09 18:35:56 +00:00
|
|
|
static PyObject *
|
1996-08-08 19:16:15 +00:00
|
|
|
strop_strip(self, args)
|
1996-12-09 18:35:56 +00:00
|
|
|
PyObject *self; /* Not used */
|
|
|
|
PyObject *args;
|
1996-08-08 19:16:15 +00:00
|
|
|
{
|
|
|
|
return do_strip(args, BOTHSTRIP);
|
|
|
|
}
|
|
|
|
|
1996-12-09 18:35:56 +00:00
|
|
|
static PyObject *
|
1996-08-08 19:16:15 +00:00
|
|
|
strop_lstrip(self, args)
|
1996-12-09 18:35:56 +00:00
|
|
|
PyObject *self; /* Not used */
|
|
|
|
PyObject *args;
|
1996-08-08 19:16:15 +00:00
|
|
|
{
|
|
|
|
return do_strip(args, LEFTSTRIP);
|
|
|
|
}
|
|
|
|
|
1996-12-09 18:35:56 +00:00
|
|
|
static PyObject *
|
1996-08-08 19:16:15 +00:00
|
|
|
strop_rstrip(self, args)
|
1996-12-09 18:35:56 +00:00
|
|
|
PyObject *self; /* Not used */
|
|
|
|
PyObject *args;
|
1996-08-08 19:16:15 +00:00
|
|
|
{
|
|
|
|
return do_strip(args, RIGHTSTRIP);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1996-12-09 18:35:56 +00:00
|
|
|
static PyObject *
|
1997-01-03 23:46:51 +00:00
|
|
|
strop_lower(self, args)
|
1996-12-09 18:35:56 +00:00
|
|
|
PyObject *self; /* Not used */
|
|
|
|
PyObject *args;
|
1992-09-11 23:55:51 +00:00
|
|
|
{
|
1993-10-22 12:04:32 +00:00
|
|
|
char *s, *s_new;
|
1992-09-11 23:55:51 +00:00
|
|
|
int i, n;
|
1996-12-09 18:35:56 +00:00
|
|
|
PyObject *new;
|
1992-09-11 23:55:51 +00:00
|
|
|
int changed;
|
|
|
|
|
1996-12-09 18:35:56 +00:00
|
|
|
if (!PyArg_Parse(args, "s#", &s, &n))
|
1992-09-11 23:55:51 +00:00
|
|
|
return NULL;
|
1996-12-09 18:35:56 +00:00
|
|
|
new = PyString_FromStringAndSize(NULL, n);
|
1992-09-11 23:55:51 +00:00
|
|
|
if (new == NULL)
|
|
|
|
return NULL;
|
1996-12-09 18:35:56 +00:00
|
|
|
s_new = PyString_AsString(new);
|
1992-09-11 23:55:51 +00:00
|
|
|
changed = 0;
|
|
|
|
for (i = 0; i < n; i++) {
|
1995-02-10 17:01:56 +00:00
|
|
|
int c = Py_CHARMASK(*s++);
|
1997-01-03 23:46:51 +00:00
|
|
|
if (isupper(c)) {
|
1992-09-11 23:55:51 +00:00
|
|
|
changed = 1;
|
1997-01-03 23:46:51 +00:00
|
|
|
*s_new = tolower(c);
|
1993-10-22 12:04:32 +00:00
|
|
|
} else
|
|
|
|
*s_new = c;
|
|
|
|
s_new++;
|
1992-09-11 23:55:51 +00:00
|
|
|
}
|
|
|
|
if (!changed) {
|
1996-12-09 18:35:56 +00:00
|
|
|
Py_DECREF(new);
|
|
|
|
Py_INCREF(args);
|
1992-09-11 23:55:51 +00:00
|
|
|
return args;
|
|
|
|
}
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1996-12-09 18:35:56 +00:00
|
|
|
static PyObject *
|
1992-09-11 23:55:51 +00:00
|
|
|
strop_upper(self, args)
|
1996-12-09 18:35:56 +00:00
|
|
|
PyObject *self; /* Not used */
|
|
|
|
PyObject *args;
|
1992-09-11 23:55:51 +00:00
|
|
|
{
|
1997-01-03 23:46:51 +00:00
|
|
|
char *s, *s_new;
|
|
|
|
int i, n;
|
|
|
|
PyObject *new;
|
|
|
|
int changed;
|
|
|
|
|
|
|
|
if (!PyArg_Parse(args, "s#", &s, &n))
|
|
|
|
return NULL;
|
|
|
|
new = PyString_FromStringAndSize(NULL, n);
|
|
|
|
if (new == NULL)
|
|
|
|
return NULL;
|
|
|
|
s_new = PyString_AsString(new);
|
|
|
|
changed = 0;
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
int c = Py_CHARMASK(*s++);
|
|
|
|
if (islower(c)) {
|
|
|
|
changed = 1;
|
|
|
|
*s_new = toupper(c);
|
|
|
|
} else
|
|
|
|
*s_new = c;
|
|
|
|
s_new++;
|
|
|
|
}
|
|
|
|
if (!changed) {
|
|
|
|
Py_DECREF(new);
|
|
|
|
Py_INCREF(args);
|
|
|
|
return args;
|
|
|
|
}
|
|
|
|
return new;
|
1992-09-11 23:55:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1996-12-09 18:35:56 +00:00
|
|
|
static PyObject *
|
1996-06-12 04:24:52 +00:00
|
|
|
strop_capitalize(self, args)
|
1996-12-09 18:35:56 +00:00
|
|
|
PyObject *self; /* Not used */
|
|
|
|
PyObject *args;
|
1996-06-12 04:24:52 +00:00
|
|
|
{
|
|
|
|
char *s, *s_new;
|
|
|
|
int i, n;
|
1996-12-09 18:35:56 +00:00
|
|
|
PyObject *new;
|
1996-06-12 04:24:52 +00:00
|
|
|
int changed;
|
|
|
|
|
1996-12-09 18:35:56 +00:00
|
|
|
if (!PyArg_Parse(args, "s#", &s, &n))
|
1996-06-12 04:24:52 +00:00
|
|
|
return NULL;
|
1996-12-09 18:35:56 +00:00
|
|
|
new = PyString_FromStringAndSize(NULL, n);
|
1996-06-12 04:24:52 +00:00
|
|
|
if (new == NULL)
|
|
|
|
return NULL;
|
1996-12-09 18:35:56 +00:00
|
|
|
s_new = PyString_AsString(new);
|
1996-06-12 04:24:52 +00:00
|
|
|
changed = 0;
|
1996-06-17 16:59:33 +00:00
|
|
|
if (0 < n) {
|
1996-06-12 04:24:52 +00:00
|
|
|
int c = Py_CHARMASK(*s++);
|
|
|
|
if (islower(c)) {
|
|
|
|
changed = 1;
|
|
|
|
*s_new = toupper(c);
|
|
|
|
} else
|
|
|
|
*s_new = c;
|
|
|
|
s_new++;
|
|
|
|
}
|
|
|
|
for (i = 1; i < n; i++) {
|
|
|
|
int c = Py_CHARMASK(*s++);
|
|
|
|
if (isupper(c)) {
|
|
|
|
changed = 1;
|
|
|
|
*s_new = tolower(c);
|
|
|
|
} else
|
|
|
|
*s_new = c;
|
|
|
|
s_new++;
|
|
|
|
}
|
|
|
|
if (!changed) {
|
1996-12-09 18:35:56 +00:00
|
|
|
Py_DECREF(new);
|
|
|
|
Py_INCREF(args);
|
1996-06-12 04:24:52 +00:00
|
|
|
return args;
|
|
|
|
}
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1996-12-09 18:35:56 +00:00
|
|
|
static PyObject *
|
1992-09-11 23:55:51 +00:00
|
|
|
strop_swapcase(self, args)
|
1996-12-09 18:35:56 +00:00
|
|
|
PyObject *self; /* Not used */
|
|
|
|
PyObject *args;
|
1992-09-11 23:55:51 +00:00
|
|
|
{
|
1993-10-22 12:04:32 +00:00
|
|
|
char *s, *s_new;
|
1992-09-11 23:55:51 +00:00
|
|
|
int i, n;
|
1996-12-09 18:35:56 +00:00
|
|
|
PyObject *new;
|
1992-09-11 23:55:51 +00:00
|
|
|
int changed;
|
|
|
|
|
1996-12-09 18:35:56 +00:00
|
|
|
if (!PyArg_Parse(args, "s#", &s, &n))
|
1992-09-11 23:55:51 +00:00
|
|
|
return NULL;
|
1996-12-09 18:35:56 +00:00
|
|
|
new = PyString_FromStringAndSize(NULL, n);
|
1992-09-11 23:55:51 +00:00
|
|
|
if (new == NULL)
|
|
|
|
return NULL;
|
1996-12-09 18:35:56 +00:00
|
|
|
s_new = PyString_AsString(new);
|
1992-09-11 23:55:51 +00:00
|
|
|
changed = 0;
|
|
|
|
for (i = 0; i < n; i++) {
|
1995-02-10 17:01:56 +00:00
|
|
|
int c = Py_CHARMASK(*s++);
|
1992-09-11 23:55:51 +00:00
|
|
|
if (islower(c)) {
|
|
|
|
changed = 1;
|
1993-10-22 12:04:32 +00:00
|
|
|
*s_new = toupper(c);
|
1992-09-11 23:55:51 +00:00
|
|
|
}
|
|
|
|
else if (isupper(c)) {
|
|
|
|
changed = 1;
|
1993-10-22 12:04:32 +00:00
|
|
|
*s_new = tolower(c);
|
1992-09-11 23:55:51 +00:00
|
|
|
}
|
1993-10-22 12:04:32 +00:00
|
|
|
else
|
|
|
|
*s_new = c;
|
|
|
|
s_new++;
|
1992-09-11 23:55:51 +00:00
|
|
|
}
|
|
|
|
if (!changed) {
|
1996-12-09 18:35:56 +00:00
|
|
|
Py_DECREF(new);
|
|
|
|
Py_INCREF(args);
|
1992-09-11 23:55:51 +00:00
|
|
|
return args;
|
|
|
|
}
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1996-12-09 18:35:56 +00:00
|
|
|
static PyObject *
|
1994-08-01 11:34:53 +00:00
|
|
|
strop_atoi(self, args)
|
1996-12-09 18:35:56 +00:00
|
|
|
PyObject *self; /* Not used */
|
|
|
|
PyObject *args;
|
1994-08-01 11:34:53 +00:00
|
|
|
{
|
1996-12-09 18:35:56 +00:00
|
|
|
extern long PyOS_strtol Py_PROTO((const char *, char **, int));
|
|
|
|
extern unsigned long
|
|
|
|
PyOS_strtoul Py_PROTO((const char *, char **, int));
|
1994-08-01 11:34:53 +00:00
|
|
|
char *s, *end;
|
|
|
|
int base = 10;
|
|
|
|
long x;
|
1996-09-11 23:30:42 +00:00
|
|
|
char buffer[256]; /* For errors */
|
1994-08-01 11:34:53 +00:00
|
|
|
|
1997-01-03 22:45:34 +00:00
|
|
|
if (!PyArg_ParseTuple(args, "s|i", &s, &base))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if ((base != 0 && base < 2) || base > 36) {
|
|
|
|
PyErr_SetString(PyExc_ValueError, "invalid base for atoi()");
|
1994-08-01 11:34:53 +00:00
|
|
|
return NULL;
|
1997-01-03 22:45:34 +00:00
|
|
|
}
|
|
|
|
|
1996-09-11 23:30:42 +00:00
|
|
|
while (*s && isspace(Py_CHARMASK(*s)))
|
|
|
|
s++;
|
1996-08-21 20:02:25 +00:00
|
|
|
if (s[0] == '\0') {
|
1996-12-09 18:35:56 +00:00
|
|
|
PyErr_SetString(PyExc_ValueError, "empty string for atoi()");
|
1996-08-21 20:02:25 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
1994-08-01 11:34:53 +00:00
|
|
|
errno = 0;
|
|
|
|
if (base == 0 && s[0] == '0')
|
1996-12-09 18:35:56 +00:00
|
|
|
x = (long) PyOS_strtoul(s, &end, base);
|
1994-08-01 11:34:53 +00:00
|
|
|
else
|
1996-12-09 18:35:56 +00:00
|
|
|
x = PyOS_strtol(s, &end, base);
|
1996-09-11 23:30:42 +00:00
|
|
|
while (*end && isspace(Py_CHARMASK(*end)))
|
|
|
|
end++;
|
1994-08-01 11:34:53 +00:00
|
|
|
if (*end != '\0') {
|
1996-09-11 23:30:42 +00:00
|
|
|
sprintf(buffer, "invalid literal for atoi(): %.200s", s);
|
1996-12-09 18:35:56 +00:00
|
|
|
PyErr_SetString(PyExc_ValueError, buffer);
|
1994-08-01 11:34:53 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else if (errno != 0) {
|
1996-09-11 23:30:42 +00:00
|
|
|
sprintf(buffer, "atoi() literal too large: %.200s", s);
|
1996-12-09 18:35:56 +00:00
|
|
|
PyErr_SetString(PyExc_ValueError, buffer);
|
1994-08-01 11:34:53 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
1996-12-09 18:35:56 +00:00
|
|
|
return PyInt_FromLong(x);
|
1994-08-01 11:34:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1996-12-09 18:35:56 +00:00
|
|
|
static PyObject *
|
1994-08-01 11:34:53 +00:00
|
|
|
strop_atol(self, args)
|
1996-12-09 18:35:56 +00:00
|
|
|
PyObject *self; /* Not used */
|
|
|
|
PyObject *args;
|
1994-08-01 11:34:53 +00:00
|
|
|
{
|
|
|
|
char *s, *end;
|
|
|
|
int base = 10;
|
1996-12-09 18:35:56 +00:00
|
|
|
PyObject *x;
|
1996-09-11 23:30:42 +00:00
|
|
|
char buffer[256]; /* For errors */
|
1994-08-01 11:34:53 +00:00
|
|
|
|
1997-01-03 22:45:34 +00:00
|
|
|
if (!PyArg_ParseTuple(args, "s|i", &s, &base))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if ((base != 0 && base < 2) || base > 36) {
|
|
|
|
PyErr_SetString(PyExc_ValueError, "invalid base for atol()");
|
1994-08-01 11:34:53 +00:00
|
|
|
return NULL;
|
1997-01-03 22:45:34 +00:00
|
|
|
}
|
|
|
|
|
1996-09-11 23:30:42 +00:00
|
|
|
while (*s && isspace(Py_CHARMASK(*s)))
|
|
|
|
s++;
|
1996-08-21 20:02:25 +00:00
|
|
|
if (s[0] == '\0') {
|
1996-12-09 18:35:56 +00:00
|
|
|
PyErr_SetString(PyExc_ValueError, "empty string for atol()");
|
1996-08-21 20:02:25 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
1996-12-09 18:35:56 +00:00
|
|
|
x = PyLong_FromString(s, &end, base);
|
1994-08-01 11:34:53 +00:00
|
|
|
if (x == NULL)
|
|
|
|
return NULL;
|
|
|
|
if (base == 0 && (*end == 'l' || *end == 'L'))
|
|
|
|
end++;
|
1996-09-11 23:30:42 +00:00
|
|
|
while (*end && isspace(Py_CHARMASK(*end)))
|
|
|
|
end++;
|
1994-08-01 11:34:53 +00:00
|
|
|
if (*end != '\0') {
|
1996-09-11 23:30:42 +00:00
|
|
|
sprintf(buffer, "invalid literal for atol(): %.200s", s);
|
1996-12-09 18:35:56 +00:00
|
|
|
PyErr_SetString(PyExc_ValueError, buffer);
|
|
|
|
Py_DECREF(x);
|
1994-08-01 11:34:53 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1996-12-09 18:35:56 +00:00
|
|
|
static PyObject *
|
1994-08-01 11:34:53 +00:00
|
|
|
strop_atof(self, args)
|
1996-12-09 18:35:56 +00:00
|
|
|
PyObject *self; /* Not used */
|
|
|
|
PyObject *args;
|
1994-08-01 11:34:53 +00:00
|
|
|
{
|
1996-12-09 18:35:56 +00:00
|
|
|
extern double strtod Py_PROTO((const char *, char **));
|
1994-08-01 11:34:53 +00:00
|
|
|
char *s, *end;
|
|
|
|
double x;
|
1996-09-11 23:30:42 +00:00
|
|
|
char buffer[256]; /* For errors */
|
1994-08-01 11:34:53 +00:00
|
|
|
|
1996-12-09 18:35:56 +00:00
|
|
|
if (!PyArg_Parse(args, "s", &s))
|
1994-08-01 11:34:53 +00:00
|
|
|
return NULL;
|
1996-09-11 23:30:42 +00:00
|
|
|
while (*s && isspace(Py_CHARMASK(*s)))
|
|
|
|
s++;
|
1996-08-21 20:02:25 +00:00
|
|
|
if (s[0] == '\0') {
|
1996-12-09 18:35:56 +00:00
|
|
|
PyErr_SetString(PyExc_ValueError, "empty string for atof()");
|
1996-08-21 20:02:25 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
1994-08-01 11:34:53 +00:00
|
|
|
errno = 0;
|
|
|
|
x = strtod(s, &end);
|
1996-09-11 23:30:42 +00:00
|
|
|
while (*end && isspace(Py_CHARMASK(*end)))
|
|
|
|
end++;
|
1994-08-01 11:34:53 +00:00
|
|
|
if (*end != '\0') {
|
1996-09-11 23:30:42 +00:00
|
|
|
sprintf(buffer, "invalid literal for atof(): %.200s", s);
|
1996-12-09 18:35:56 +00:00
|
|
|
PyErr_SetString(PyExc_ValueError, buffer);
|
1994-08-01 11:34:53 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else if (errno != 0) {
|
1996-09-11 23:30:42 +00:00
|
|
|
sprintf(buffer, "atof() literal too large: %.200s", s);
|
1996-12-09 18:35:56 +00:00
|
|
|
PyErr_SetString(PyExc_ValueError, buffer);
|
1994-08-01 11:34:53 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
1996-12-09 18:35:56 +00:00
|
|
|
return PyFloat_FromDouble(x);
|
1994-08-01 11:34:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1996-07-23 18:12:39 +00:00
|
|
|
static PyObject *
|
|
|
|
strop_maketrans(self, args)
|
|
|
|
PyObject *self; /* Not used */
|
|
|
|
PyObject *args;
|
|
|
|
{
|
|
|
|
unsigned char c[256], *from=NULL, *to=NULL;
|
|
|
|
int i, fromlen=0, tolen=0;
|
|
|
|
|
1997-01-03 22:45:34 +00:00
|
|
|
if (!PyArg_ParseTuple(args, "s#s#", &from, &fromlen, &to, &tolen))
|
|
|
|
return NULL;
|
1996-07-23 18:12:39 +00:00
|
|
|
|
1997-01-03 22:45:34 +00:00
|
|
|
if (fromlen != tolen) {
|
1996-12-09 18:35:56 +00:00
|
|
|
PyErr_SetString(PyExc_ValueError,
|
1996-07-23 18:12:39 +00:00
|
|
|
"maketrans arguments must have same length");
|
|
|
|
return NULL;
|
|
|
|
}
|
1997-01-03 22:45:34 +00:00
|
|
|
for (i = 0; i < 256; i++)
|
1996-07-23 18:12:39 +00:00
|
|
|
c[i]=(unsigned char)i;
|
1997-01-03 22:45:34 +00:00
|
|
|
for (i = 0; i < fromlen; i++)
|
1996-07-23 18:12:39 +00:00
|
|
|
c[from[i]]=to[i];
|
1997-01-03 22:45:34 +00:00
|
|
|
|
1996-07-23 18:12:39 +00:00
|
|
|
return PyString_FromStringAndSize((char *)c, 256);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1996-12-09 18:35:56 +00:00
|
|
|
static PyObject *
|
1995-09-13 17:39:06 +00:00
|
|
|
strop_translate(self, args)
|
1996-12-09 18:35:56 +00:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1995-09-13 17:39:06 +00:00
|
|
|
{
|
1996-07-23 18:12:39 +00:00
|
|
|
char *input, *table, *output, *output_start, *delete=NULL;
|
1997-01-03 22:45:34 +00:00
|
|
|
int inlen, tablen, dellen = 0;
|
1996-07-23 18:12:39 +00:00
|
|
|
PyObject *result;
|
|
|
|
int i, trans_table[256];
|
1995-09-13 17:39:06 +00:00
|
|
|
|
1996-07-23 18:12:39 +00:00
|
|
|
if (!PyArg_ParseTuple(args, "s#s#|s#", &input, &inlen,
|
|
|
|
&table, &tablen, &delete, &dellen))
|
1995-09-13 17:39:06 +00:00
|
|
|
return NULL;
|
|
|
|
if (tablen != 256) {
|
1996-12-09 18:35:56 +00:00
|
|
|
PyErr_SetString(PyExc_ValueError,
|
1997-01-03 22:45:34 +00:00
|
|
|
"translation table must be 256 characters long");
|
1995-09-13 17:39:06 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-01-03 22:45:34 +00:00
|
|
|
for (i = 0; i < 256; i++)
|
|
|
|
trans_table[i] = Py_CHARMASK(table[i]);
|
|
|
|
|
|
|
|
if (delete != NULL) {
|
|
|
|
for (i = 0; i < dellen; i++)
|
|
|
|
trans_table[(int)delete[i]] = -1;
|
1996-07-23 18:12:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
result = PyString_FromStringAndSize((char *)NULL, inlen);
|
1995-09-13 17:39:06 +00:00
|
|
|
if (result == NULL)
|
|
|
|
return NULL;
|
1996-07-23 18:12:39 +00:00
|
|
|
output_start = output = PyString_AsString(result);
|
1997-01-03 22:45:34 +00:00
|
|
|
|
|
|
|
if (delete != NULL && dellen != 0) {
|
1996-07-23 18:12:39 +00:00
|
|
|
for (i = 0; i < inlen; i++) {
|
|
|
|
int c = Py_CHARMASK(*input++);
|
1997-01-03 22:45:34 +00:00
|
|
|
if (trans_table[c] != -1)
|
1996-07-23 18:12:39 +00:00
|
|
|
*output++ = (char)trans_table[c];
|
|
|
|
}
|
|
|
|
/* Fix the size of the resulting string */
|
|
|
|
if (inlen > 0 &&_PyString_Resize(&result, output-output_start))
|
|
|
|
return NULL;
|
|
|
|
} else {
|
1996-08-08 19:16:15 +00:00
|
|
|
/* If no deletions are required, use a faster loop */
|
1996-07-23 18:12:39 +00:00
|
|
|
for (i = 0; i < inlen; i++) {
|
|
|
|
int c = Py_CHARMASK(*input++);
|
1996-08-08 19:16:15 +00:00
|
|
|
*output++ = (char)trans_table[c];
|
|
|
|
}
|
1995-09-13 17:39:06 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1992-09-03 20:21:07 +00:00
|
|
|
/* List of functions defined in the module */
|
|
|
|
|
1997-01-03 22:45:34 +00:00
|
|
|
static PyMethodDef
|
|
|
|
strop_methods[] = {
|
1994-08-01 11:34:53 +00:00
|
|
|
{"atof", strop_atof},
|
1997-01-03 22:45:34 +00:00
|
|
|
{"atoi", strop_atoi, 1},
|
|
|
|
{"atol", strop_atol, 1},
|
1996-06-12 04:24:52 +00:00
|
|
|
{"capitalize", strop_capitalize},
|
1997-01-03 22:45:34 +00:00
|
|
|
{"find", strop_find, 1},
|
1995-05-03 17:40:23 +00:00
|
|
|
{"join", strop_joinfields, 1},
|
|
|
|
{"joinfields", strop_joinfields, 1},
|
1996-08-08 19:16:15 +00:00
|
|
|
{"lstrip", strop_lstrip},
|
1992-09-11 23:55:51 +00:00
|
|
|
{"lower", strop_lower},
|
1997-01-03 22:45:34 +00:00
|
|
|
{"rfind", strop_rfind, 1},
|
1996-08-08 19:16:15 +00:00
|
|
|
{"rstrip", strop_rstrip},
|
1995-05-03 17:40:23 +00:00
|
|
|
{"split", strop_splitfields, 1},
|
|
|
|
{"splitfields", strop_splitfields, 1},
|
1992-09-03 20:21:07 +00:00
|
|
|
{"strip", strop_strip},
|
1992-09-11 23:55:51 +00:00
|
|
|
{"swapcase", strop_swapcase},
|
1996-07-23 18:12:39 +00:00
|
|
|
{"maketrans", strop_maketrans, 1},
|
1995-09-13 17:39:06 +00:00
|
|
|
{"translate", strop_translate, 1},
|
1992-09-11 23:55:51 +00:00
|
|
|
{"upper", strop_upper},
|
1992-09-03 20:21:07 +00:00
|
|
|
{NULL, NULL} /* sentinel */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
initstrop()
|
|
|
|
{
|
1996-12-09 18:35:56 +00:00
|
|
|
PyObject *m, *d, *s;
|
1993-07-08 11:12:36 +00:00
|
|
|
char buf[256];
|
|
|
|
int c, n;
|
1996-12-09 18:35:56 +00:00
|
|
|
m = Py_InitModule("strop", strop_methods);
|
|
|
|
d = PyModule_GetDict(m);
|
1993-07-09 10:51:31 +00:00
|
|
|
|
|
|
|
/* Create 'whitespace' object */
|
1993-07-08 11:12:36 +00:00
|
|
|
n = 0;
|
1995-02-10 17:01:56 +00:00
|
|
|
for (c = 0; c < 256; c++) {
|
1993-07-08 11:12:36 +00:00
|
|
|
if (isspace(c))
|
|
|
|
buf[n++] = c;
|
|
|
|
}
|
1996-12-09 18:35:56 +00:00
|
|
|
s = PyString_FromStringAndSize(buf, n);
|
1993-07-09 10:51:31 +00:00
|
|
|
if (s) {
|
1996-12-09 18:35:56 +00:00
|
|
|
PyDict_SetItemString(d, "whitespace", s);
|
|
|
|
Py_DECREF(s);
|
1993-07-09 10:51:31 +00:00
|
|
|
}
|
|
|
|
/* Create 'lowercase' object */
|
|
|
|
n = 0;
|
1995-02-10 17:01:56 +00:00
|
|
|
for (c = 0; c < 256; c++) {
|
1993-07-09 10:51:31 +00:00
|
|
|
if (islower(c))
|
|
|
|
buf[n++] = c;
|
|
|
|
}
|
1996-12-09 18:35:56 +00:00
|
|
|
s = PyString_FromStringAndSize(buf, n);
|
1993-07-09 10:51:31 +00:00
|
|
|
if (s) {
|
1996-12-09 18:35:56 +00:00
|
|
|
PyDict_SetItemString(d, "lowercase", s);
|
|
|
|
Py_DECREF(s);
|
1993-07-09 10:51:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Create 'uppercase' object */
|
|
|
|
n = 0;
|
1995-02-10 17:01:56 +00:00
|
|
|
for (c = 0; c < 256; c++) {
|
1993-07-09 10:51:31 +00:00
|
|
|
if (isupper(c))
|
|
|
|
buf[n++] = c;
|
|
|
|
}
|
1996-12-09 18:35:56 +00:00
|
|
|
s = PyString_FromStringAndSize(buf, n);
|
1993-07-09 10:51:31 +00:00
|
|
|
if (s) {
|
1996-12-09 18:35:56 +00:00
|
|
|
PyDict_SetItemString(d, "uppercase", s);
|
|
|
|
Py_DECREF(s);
|
1993-07-09 10:51:31 +00:00
|
|
|
}
|
|
|
|
|
1996-12-09 18:35:56 +00:00
|
|
|
if (PyErr_Occurred())
|
|
|
|
Py_FatalError("can't initialize module strop");
|
1992-09-03 20:21:07 +00:00
|
|
|
}
|