1992-09-03 17:21:07 -03:00
|
|
|
/* strop module */
|
|
|
|
|
1997-12-29 15:52:29 -04:00
|
|
|
static char strop_module__doc__[] =
|
2001-05-09 19:15:03 -03:00
|
|
|
"Common string manipulations, optimized for speed.\n"
|
|
|
|
"\n"
|
|
|
|
"Always use \"import string\" rather than referencing\n"
|
|
|
|
"this module directly.";
|
1997-12-29 15:52:29 -04:00
|
|
|
|
1996-12-09 14:35:56 -04:00
|
|
|
#include "Python.h"
|
1992-09-03 17:21:07 -03:00
|
|
|
|
1993-07-08 08:12:36 -03:00
|
|
|
#include <ctype.h>
|
1993-07-09 07:51:31 -03:00
|
|
|
/* XXX This file assumes that the <ctype.h> is*() functions
|
|
|
|
XXX are defined for all 8-bit characters! */
|
1993-07-08 08:12:36 -03:00
|
|
|
|
2001-05-14 23:14:44 -03:00
|
|
|
#define WARN if (PyErr_Warn(PyExc_DeprecationWarning, \
|
|
|
|
"strop functions are obsolete; use string methods")) \
|
|
|
|
return NULL
|
|
|
|
|
1996-08-08 16:16:15 -03: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 17:21:07 -03:00
|
|
|
|
1996-12-09 14:35:56 -04:00
|
|
|
static PyObject *
|
2000-07-10 06:43:24 -03:00
|
|
|
split_whitespace(char *s, int len, int maxsplit)
|
1995-05-03 14:40:23 -03:00
|
|
|
{
|
1997-01-03 18:45:34 -04:00
|
|
|
int i = 0, j, err;
|
|
|
|
int countsplit = 0;
|
|
|
|
PyObject* item;
|
|
|
|
PyObject *list = PyList_New(0);
|
1992-09-03 17:21:07 -03:00
|
|
|
|
|
|
|
if (list == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
while (i < len) {
|
1995-02-10 13:01:56 -04:00
|
|
|
while (i < len && isspace(Py_CHARMASK(s[i]))) {
|
1992-09-03 17:21:07 -03:00
|
|
|
i = i+1;
|
|
|
|
}
|
|
|
|
j = i;
|
1995-02-13 20:58:59 -04:00
|
|
|
while (i < len && !isspace(Py_CHARMASK(s[i]))) {
|
1992-09-03 17:21:07 -03:00
|
|
|
i = i+1;
|
|
|
|
}
|
|
|
|
if (j < i) {
|
1996-12-09 14:35:56 -04:00
|
|
|
item = PyString_FromStringAndSize(s+j, (int)(i-j));
|
1997-01-03 18:45:34 -04:00
|
|
|
if (item == NULL)
|
|
|
|
goto finally;
|
|
|
|
|
1996-12-09 14:35:56 -04:00
|
|
|
err = PyList_Append(list, item);
|
|
|
|
Py_DECREF(item);
|
1997-01-03 18:45:34 -04:00
|
|
|
if (err < 0)
|
|
|
|
goto finally;
|
1996-08-08 16:16:15 -03:00
|
|
|
|
|
|
|
countsplit++;
|
1997-12-01 20:29:30 -04:00
|
|
|
while (i < len && isspace(Py_CHARMASK(s[i]))) {
|
|
|
|
i = i+1;
|
|
|
|
}
|
|
|
|
if (maxsplit && (countsplit >= maxsplit) && i < len) {
|
1996-12-09 14:35:56 -04:00
|
|
|
item = PyString_FromStringAndSize(
|
|
|
|
s+i, (int)(len - i));
|
1997-01-03 18:45:34 -04:00
|
|
|
if (item == NULL)
|
|
|
|
goto finally;
|
|
|
|
|
1996-12-09 14:35:56 -04:00
|
|
|
err = PyList_Append(list, item);
|
|
|
|
Py_DECREF(item);
|
1997-01-03 18:45:34 -04:00
|
|
|
if (err < 0)
|
|
|
|
goto finally;
|
|
|
|
|
1996-08-08 16:16:15 -03:00
|
|
|
i = len;
|
|
|
|
}
|
1992-09-03 17:21:07 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return list;
|
1997-01-03 18:45:34 -04:00
|
|
|
finally:
|
|
|
|
Py_DECREF(list);
|
|
|
|
return NULL;
|
1992-09-03 17:21:07 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-12-29 15:52:29 -04:00
|
|
|
static char splitfields__doc__[] =
|
2001-05-09 19:15:03 -03:00
|
|
|
"split(s [,sep [,maxsplit]]) -> list of strings\n"
|
|
|
|
"splitfields(s [,sep [,maxsplit]]) -> list of strings\n"
|
|
|
|
"\n"
|
|
|
|
"Return a list of the words in the string s, using sep as the\n"
|
|
|
|
"delimiter string. If maxsplit is nonzero, splits into at most\n"
|
|
|
|
"maxsplit words. If sep is not specified, any whitespace string\n"
|
|
|
|
"is a separator. Maxsplit defaults to 0.\n"
|
|
|
|
"\n"
|
|
|
|
"(split and splitfields are synonymous)";
|
1997-12-29 15:52:29 -04:00
|
|
|
|
1996-12-09 14:35:56 -04:00
|
|
|
static PyObject *
|
2000-07-10 06:43:24 -03:00
|
|
|
strop_splitfields(PyObject *self, PyObject *args)
|
1992-09-03 17:21:07 -03:00
|
|
|
{
|
1993-11-05 06:14:49 -04:00
|
|
|
int len, n, i, j, err;
|
1996-08-08 16:16:15 -03:00
|
|
|
int splitcount, maxsplit;
|
1992-09-03 17:21:07 -03:00
|
|
|
char *s, *sub;
|
1996-12-09 14:35:56 -04:00
|
|
|
PyObject *list, *item;
|
1992-09-03 17:21:07 -03:00
|
|
|
|
2001-05-14 23:14:44 -03:00
|
|
|
WARN;
|
1995-05-03 14:40:23 -03:00
|
|
|
sub = NULL;
|
|
|
|
n = 0;
|
1996-08-08 16:16:15 -03:00
|
|
|
splitcount = 0;
|
|
|
|
maxsplit = 0;
|
2000-02-29 09:59:29 -04:00
|
|
|
if (!PyArg_ParseTuple(args, "t#|z#i:split", &s, &len, &sub, &n, &maxsplit))
|
1992-09-03 17:21:07 -03:00
|
|
|
return NULL;
|
1995-05-03 14:40:23 -03:00
|
|
|
if (sub == NULL)
|
1996-08-08 16:16:15 -03:00
|
|
|
return split_whitespace(s, len, maxsplit);
|
1992-09-03 17:21:07 -03:00
|
|
|
if (n == 0) {
|
1996-12-09 14:35:56 -04:00
|
|
|
PyErr_SetString(PyExc_ValueError, "empty separator");
|
1992-09-03 17:21:07 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1996-12-09 14:35:56 -04:00
|
|
|
list = PyList_New(0);
|
1992-09-03 17:21:07 -03:00
|
|
|
if (list == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
i = j = 0;
|
|
|
|
while (i+n <= len) {
|
1996-10-04 10:39:37 -03:00
|
|
|
if (s[i] == sub[0] && (n == 1 || memcmp(s+i, sub, n) == 0)) {
|
1996-12-09 14:35:56 -04:00
|
|
|
item = PyString_FromStringAndSize(s+j, (int)(i-j));
|
1993-11-05 06:14:49 -04:00
|
|
|
if (item == NULL)
|
|
|
|
goto fail;
|
1996-12-09 14:35:56 -04:00
|
|
|
err = PyList_Append(list, item);
|
|
|
|
Py_DECREF(item);
|
1993-11-05 06:14:49 -04:00
|
|
|
if (err < 0)
|
|
|
|
goto fail;
|
1992-09-03 17:21:07 -03:00
|
|
|
i = j = i + n;
|
1996-08-08 16:16:15 -03:00
|
|
|
splitcount++;
|
|
|
|
if (maxsplit && (splitcount >= maxsplit))
|
|
|
|
break;
|
1992-09-03 17:21:07 -03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
i++;
|
|
|
|
}
|
1996-12-09 14:35:56 -04:00
|
|
|
item = PyString_FromStringAndSize(s+j, (int)(len-j));
|
1993-11-05 06:14:49 -04:00
|
|
|
if (item == NULL)
|
|
|
|
goto fail;
|
1996-12-09 14:35:56 -04:00
|
|
|
err = PyList_Append(list, item);
|
|
|
|
Py_DECREF(item);
|
1993-11-05 06:14:49 -04:00
|
|
|
if (err < 0)
|
|
|
|
goto fail;
|
1992-09-03 17:21:07 -03:00
|
|
|
|
|
|
|
return list;
|
1993-11-05 06:14:49 -04:00
|
|
|
|
|
|
|
fail:
|
1996-12-09 14:35:56 -04:00
|
|
|
Py_DECREF(list);
|
1993-11-05 06:14:49 -04:00
|
|
|
return NULL;
|
1992-09-03 17:21:07 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-12-29 15:52:29 -04:00
|
|
|
static char joinfields__doc__[] =
|
2001-05-09 19:15:03 -03:00
|
|
|
"join(list [,sep]) -> string\n"
|
|
|
|
"joinfields(list [,sep]) -> string\n"
|
|
|
|
"\n"
|
|
|
|
"Return a string composed of the words in list, with\n"
|
|
|
|
"intervening occurrences of sep. Sep defaults to a single\n"
|
|
|
|
"space.\n"
|
|
|
|
"\n"
|
|
|
|
"(join and joinfields are synonymous)";
|
1997-12-29 15:52:29 -04:00
|
|
|
|
1996-12-09 14:35:56 -04:00
|
|
|
static PyObject *
|
2000-07-10 06:43:24 -03:00
|
|
|
strop_joinfields(PyObject *self, PyObject *args)
|
1992-11-26 04:54:07 -04:00
|
|
|
{
|
1997-01-06 18:48:32 -04:00
|
|
|
PyObject *seq;
|
|
|
|
char *sep = NULL;
|
|
|
|
int seqlen, seplen = 0;
|
|
|
|
int i, reslen = 0, slen = 0, sz = 100;
|
|
|
|
PyObject *res = NULL;
|
|
|
|
char* p = NULL;
|
|
|
|
intargfunc getitemfunc;
|
1992-11-26 04:54:07 -04:00
|
|
|
|
2001-05-14 23:14:44 -03:00
|
|
|
WARN;
|
2000-02-29 09:59:29 -04:00
|
|
|
if (!PyArg_ParseTuple(args, "O|t#:join", &seq, &sep, &seplen))
|
1992-11-26 04:54:07 -04:00
|
|
|
return NULL;
|
1995-05-03 14:40:23 -03:00
|
|
|
if (sep == NULL) {
|
|
|
|
sep = " ";
|
|
|
|
seplen = 1;
|
|
|
|
}
|
1997-01-06 18:48:32 -04:00
|
|
|
|
2000-07-12 10:05:33 -03:00
|
|
|
seqlen = PySequence_Size(seq);
|
1997-01-03 18:45:34 -04:00
|
|
|
if (seqlen < 0 && PyErr_Occurred())
|
|
|
|
return NULL;
|
|
|
|
|
1997-01-03 19:46:51 -04:00
|
|
|
if (seqlen == 1) {
|
1992-11-26 04:54:07 -04:00
|
|
|
/* Optimization if there's only one item */
|
1997-01-06 18:48:32 -04:00
|
|
|
PyObject *item = PySequence_GetItem(seq, 0);
|
1998-02-06 18:37:12 -04:00
|
|
|
if (item && !PyString_Check(item)) {
|
1997-01-06 18:48:32 -04:00
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"first argument must be sequence of strings");
|
1998-10-19 10:38:36 -03:00
|
|
|
Py_DECREF(item);
|
1998-02-06 18:37:12 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-01-03 19:46:51 -04:00
|
|
|
return item;
|
|
|
|
}
|
1997-01-03 18:45:34 -04:00
|
|
|
|
1997-01-06 18:48:32 -04:00
|
|
|
if (!(res = PyString_FromStringAndSize((char*)NULL, sz)))
|
1992-11-26 04:54:07 -04:00
|
|
|
return NULL;
|
1996-12-09 14:35:56 -04:00
|
|
|
p = PyString_AsString(res);
|
1997-01-06 18:48:32 -04:00
|
|
|
|
|
|
|
/* optimize for lists, since it's the most common case. all others
|
|
|
|
* (tuples and arbitrary sequences) just use the sequence abstract
|
|
|
|
* interface.
|
|
|
|
*/
|
|
|
|
if (PyList_Check(seq)) {
|
|
|
|
for (i = 0; i < seqlen; i++) {
|
|
|
|
PyObject *item = PyList_GET_ITEM(seq, i);
|
|
|
|
if (!PyString_Check(item)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"first argument must be sequence of strings");
|
|
|
|
Py_DECREF(res);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
slen = PyString_GET_SIZE(item);
|
|
|
|
while (reslen + slen + seplen >= sz) {
|
2002-04-27 15:44:32 -03:00
|
|
|
if (_PyString_Resize(&res, sz * 2) < 0)
|
1997-01-06 18:48:32 -04:00
|
|
|
return NULL;
|
|
|
|
sz *= 2;
|
|
|
|
p = PyString_AsString(res) + reslen;
|
|
|
|
}
|
|
|
|
if (i > 0) {
|
|
|
|
memcpy(p, sep, seplen);
|
|
|
|
p += seplen;
|
|
|
|
reslen += seplen;
|
|
|
|
}
|
|
|
|
memcpy(p, PyString_AS_STRING(item), slen);
|
|
|
|
p += slen;
|
|
|
|
reslen += slen;
|
|
|
|
}
|
2002-04-27 15:44:32 -03:00
|
|
|
_PyString_Resize(&res, reslen);
|
1997-01-06 18:48:32 -04:00
|
|
|
return res;
|
|
|
|
}
|
1998-05-21 21:53:47 -03:00
|
|
|
|
|
|
|
if (seq->ob_type->tp_as_sequence == NULL ||
|
|
|
|
(getitemfunc = seq->ob_type->tp_as_sequence->sq_item) == NULL)
|
|
|
|
{
|
1997-01-06 18:48:32 -04:00
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"first argument must be a sequence");
|
|
|
|
return NULL;
|
|
|
|
}
|
1998-05-21 21:53:47 -03:00
|
|
|
/* This is now type safe */
|
1992-11-26 04:54:07 -04:00
|
|
|
for (i = 0; i < seqlen; i++) {
|
1997-01-06 18:48:32 -04:00
|
|
|
PyObject *item = getitemfunc(seq, i);
|
|
|
|
if (!item || !PyString_Check(item)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"first argument must be sequence of strings");
|
1997-01-03 18:45:34 -04:00
|
|
|
Py_DECREF(res);
|
1997-01-06 18:48:32 -04:00
|
|
|
Py_XDECREF(item);
|
1997-01-03 18:45:34 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-01-06 18:48:32 -04:00
|
|
|
slen = PyString_GET_SIZE(item);
|
|
|
|
while (reslen + slen + seplen >= sz) {
|
2002-04-27 15:44:32 -03:00
|
|
|
if (_PyString_Resize(&res, sz * 2) < 0) {
|
1997-01-06 18:48:32 -04:00
|
|
|
Py_DECREF(item);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
sz *= 2;
|
|
|
|
p = PyString_AsString(res) + reslen;
|
|
|
|
}
|
1992-11-26 04:54:07 -04:00
|
|
|
if (i > 0) {
|
|
|
|
memcpy(p, sep, seplen);
|
|
|
|
p += seplen;
|
1997-01-06 18:48:32 -04:00
|
|
|
reslen += seplen;
|
1992-11-26 04:54:07 -04:00
|
|
|
}
|
1997-01-06 18:48:32 -04:00
|
|
|
memcpy(p, PyString_AS_STRING(item), slen);
|
|
|
|
p += slen;
|
|
|
|
reslen += slen;
|
|
|
|
Py_DECREF(item);
|
1992-11-26 04:54:07 -04:00
|
|
|
}
|
2002-04-27 15:44:32 -03:00
|
|
|
_PyString_Resize(&res, reslen);
|
1992-11-26 04:54:07 -04:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
1997-12-29 15:52:29 -04:00
|
|
|
|
|
|
|
static char find__doc__[] =
|
2001-05-09 19:15:03 -03:00
|
|
|
"find(s, sub [,start [,end]]) -> in\n"
|
|
|
|
"\n"
|
|
|
|
"Return the lowest index in s where substring sub is found,\n"
|
|
|
|
"such that sub is contained within s[start,end]. Optional\n"
|
|
|
|
"arguments start and end are interpreted as in slice notation.\n"
|
|
|
|
"\n"
|
|
|
|
"Return -1 on failure.";
|
1997-12-29 15:52:29 -04:00
|
|
|
|
1996-12-09 14:35:56 -04:00
|
|
|
static PyObject *
|
2000-07-10 06:43:24 -03:00
|
|
|
strop_find(PyObject *self, PyObject *args)
|
1992-09-03 17:21:07 -03:00
|
|
|
{
|
|
|
|
char *s, *sub;
|
1997-03-14 00:13:56 -04:00
|
|
|
int len, n, i = 0, last = INT_MAX;
|
1992-09-03 17:21:07 -03:00
|
|
|
|
2001-05-14 23:14:44 -03:00
|
|
|
WARN;
|
2000-02-29 09:59:29 -04:00
|
|
|
if (!PyArg_ParseTuple(args, "t#t#|ii:find", &s, &len, &sub, &n, &i, &last))
|
1997-01-03 18:45:34 -04:00
|
|
|
return NULL;
|
|
|
|
|
1997-03-14 00:13:56 -04:00
|
|
|
if (last > len)
|
|
|
|
last = len;
|
|
|
|
if (last < 0)
|
|
|
|
last += len;
|
|
|
|
if (last < 0)
|
|
|
|
last = 0;
|
1997-01-03 18:45:34 -04:00
|
|
|
if (i < 0)
|
|
|
|
i += len;
|
|
|
|
if (i < 0)
|
1992-09-03 17:21:07 -03:00
|
|
|
i = 0;
|
|
|
|
|
1998-03-24 00:19:22 -04:00
|
|
|
if (n == 0 && i <= last)
|
1996-12-09 14:35:56 -04:00
|
|
|
return PyInt_FromLong((long)i);
|
1992-09-03 17:21:07 -03:00
|
|
|
|
1997-03-14 00:13:56 -04:00
|
|
|
last -= n;
|
|
|
|
for (; i <= last; ++i)
|
1993-10-26 12:23:55 -03:00
|
|
|
if (s[i] == sub[0] &&
|
1996-10-04 10:39:37 -03:00
|
|
|
(n == 1 || memcmp(&s[i+1], &sub[1], n-1) == 0))
|
1996-12-09 14:35:56 -04:00
|
|
|
return PyInt_FromLong((long)i);
|
1993-10-26 12:23:55 -03:00
|
|
|
|
1996-12-09 14:35:56 -04:00
|
|
|
return PyInt_FromLong(-1L);
|
1993-10-26 12:23:55 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-12-29 15:52:29 -04:00
|
|
|
static char rfind__doc__[] =
|
2001-05-09 19:15:03 -03:00
|
|
|
"rfind(s, sub [,start [,end]]) -> int\n"
|
|
|
|
"\n"
|
|
|
|
"Return the highest index in s where substring sub is found,\n"
|
|
|
|
"such that sub is contained within s[start,end]. Optional\n"
|
|
|
|
"arguments start and end are interpreted as in slice notation.\n"
|
|
|
|
"\n"
|
|
|
|
"Return -1 on failure.";
|
1997-12-29 15:52:29 -04:00
|
|
|
|
1996-12-09 14:35:56 -04:00
|
|
|
static PyObject *
|
2000-07-10 06:43:24 -03:00
|
|
|
strop_rfind(PyObject *self, PyObject *args)
|
1993-10-26 12:23:55 -03:00
|
|
|
{
|
|
|
|
char *s, *sub;
|
1997-01-03 18:45:34 -04:00
|
|
|
int len, n, j;
|
1997-03-14 00:13:56 -04:00
|
|
|
int i = 0, last = INT_MAX;
|
1993-10-26 12:23:55 -03:00
|
|
|
|
2001-05-14 23:14:44 -03:00
|
|
|
WARN;
|
2000-02-29 09:59:29 -04:00
|
|
|
if (!PyArg_ParseTuple(args, "t#t#|ii:rfind", &s, &len, &sub, &n, &i, &last))
|
1997-01-03 18:45:34 -04:00
|
|
|
return NULL;
|
|
|
|
|
1997-03-14 00:13:56 -04:00
|
|
|
if (last > len)
|
|
|
|
last = len;
|
|
|
|
if (last < 0)
|
|
|
|
last += len;
|
|
|
|
if (last < 0)
|
|
|
|
last = 0;
|
1997-01-03 18:45:34 -04:00
|
|
|
if (i < 0)
|
|
|
|
i += len;
|
|
|
|
if (i < 0)
|
1994-08-01 08:34:53 -03:00
|
|
|
i = 0;
|
1993-10-26 12:23:55 -03:00
|
|
|
|
1998-03-24 00:19:22 -04:00
|
|
|
if (n == 0 && i <= last)
|
1997-03-14 00:13:56 -04:00
|
|
|
return PyInt_FromLong((long)last);
|
1993-10-26 12:23:55 -03:00
|
|
|
|
1997-03-14 00:13:56 -04:00
|
|
|
for (j = last-n; j >= i; --j)
|
1994-08-01 08:34:53 -03:00
|
|
|
if (s[j] == sub[0] &&
|
1996-10-04 10:39:37 -03:00
|
|
|
(n == 1 || memcmp(&s[j+1], &sub[1], n-1) == 0))
|
1996-12-09 14:35:56 -04:00
|
|
|
return PyInt_FromLong((long)j);
|
1992-09-03 17:21:07 -03:00
|
|
|
|
1996-12-09 14:35:56 -04:00
|
|
|
return PyInt_FromLong(-1L);
|
1992-09-03 17:21:07 -03:00
|
|
|
}
|
|
|
|
|
1997-12-29 15:52:29 -04:00
|
|
|
|
1996-12-09 14:35:56 -04:00
|
|
|
static PyObject *
|
2000-07-10 06:43:24 -03:00
|
|
|
do_strip(PyObject *args, int striptype)
|
1992-09-03 17:21:07 -03:00
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
int len, i, j;
|
|
|
|
|
1996-08-08 16:16:15 -03:00
|
|
|
|
2002-04-02 14:17:57 -04:00
|
|
|
if (PyString_AsStringAndSize(args, &s, &len))
|
1992-09-03 17:21:07 -03:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
i = 0;
|
1996-08-08 16:16:15 -03:00
|
|
|
if (striptype != RIGHTSTRIP) {
|
|
|
|
while (i < len && isspace(Py_CHARMASK(s[i]))) {
|
|
|
|
i++;
|
|
|
|
}
|
1992-09-03 17:21:07 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
j = len;
|
1996-08-08 16:16:15 -03:00
|
|
|
if (striptype != LEFTSTRIP) {
|
|
|
|
do {
|
|
|
|
j--;
|
|
|
|
} while (j >= i && isspace(Py_CHARMASK(s[j])));
|
|
|
|
j++;
|
|
|
|
}
|
1992-09-03 17:21:07 -03:00
|
|
|
|
|
|
|
if (i == 0 && j == len) {
|
1996-12-09 14:35:56 -04:00
|
|
|
Py_INCREF(args);
|
1992-09-03 17:21:07 -03:00
|
|
|
return args;
|
|
|
|
}
|
|
|
|
else
|
1996-12-09 14:35:56 -04:00
|
|
|
return PyString_FromStringAndSize(s+i, j-i);
|
1992-09-03 17:21:07 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-12-29 15:52:29 -04:00
|
|
|
static char strip__doc__[] =
|
2001-05-09 19:15:03 -03:00
|
|
|
"strip(s) -> string\n"
|
|
|
|
"\n"
|
|
|
|
"Return a copy of the string s with leading and trailing\n"
|
|
|
|
"whitespace removed.";
|
1997-12-29 15:52:29 -04:00
|
|
|
|
1996-12-09 14:35:56 -04:00
|
|
|
static PyObject *
|
2000-07-10 06:43:24 -03:00
|
|
|
strop_strip(PyObject *self, PyObject *args)
|
1996-08-08 16:16:15 -03:00
|
|
|
{
|
2001-05-14 23:14:44 -03:00
|
|
|
WARN;
|
1996-08-08 16:16:15 -03:00
|
|
|
return do_strip(args, BOTHSTRIP);
|
|
|
|
}
|
|
|
|
|
1997-12-29 15:52:29 -04:00
|
|
|
|
|
|
|
static char lstrip__doc__[] =
|
2001-05-09 19:15:03 -03:00
|
|
|
"lstrip(s) -> string\n"
|
|
|
|
"\n"
|
|
|
|
"Return a copy of the string s with leading whitespace removed.";
|
1997-12-29 15:52:29 -04:00
|
|
|
|
1996-12-09 14:35:56 -04:00
|
|
|
static PyObject *
|
2000-07-10 06:43:24 -03:00
|
|
|
strop_lstrip(PyObject *self, PyObject *args)
|
1996-08-08 16:16:15 -03:00
|
|
|
{
|
2001-05-14 23:14:44 -03:00
|
|
|
WARN;
|
1996-08-08 16:16:15 -03:00
|
|
|
return do_strip(args, LEFTSTRIP);
|
|
|
|
}
|
|
|
|
|
1997-12-29 15:52:29 -04:00
|
|
|
|
|
|
|
static char rstrip__doc__[] =
|
2001-05-09 19:15:03 -03:00
|
|
|
"rstrip(s) -> string\n"
|
|
|
|
"\n"
|
|
|
|
"Return a copy of the string s with trailing whitespace removed.";
|
1997-12-29 15:52:29 -04:00
|
|
|
|
1996-12-09 14:35:56 -04:00
|
|
|
static PyObject *
|
2000-07-10 06:43:24 -03:00
|
|
|
strop_rstrip(PyObject *self, PyObject *args)
|
1996-08-08 16:16:15 -03:00
|
|
|
{
|
2001-05-14 23:14:44 -03:00
|
|
|
WARN;
|
1996-08-08 16:16:15 -03:00
|
|
|
return do_strip(args, RIGHTSTRIP);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-12-29 15:52:29 -04:00
|
|
|
static char lower__doc__[] =
|
2001-05-09 19:15:03 -03:00
|
|
|
"lower(s) -> string\n"
|
|
|
|
"\n"
|
|
|
|
"Return a copy of the string s converted to lowercase.";
|
1997-12-29 15:52:29 -04:00
|
|
|
|
1996-12-09 14:35:56 -04:00
|
|
|
static PyObject *
|
2000-07-10 06:43:24 -03:00
|
|
|
strop_lower(PyObject *self, PyObject *args)
|
1992-09-11 20:55:51 -03:00
|
|
|
{
|
1993-10-22 09:04:32 -03:00
|
|
|
char *s, *s_new;
|
1992-09-11 20:55:51 -03:00
|
|
|
int i, n;
|
1996-12-09 14:35:56 -04:00
|
|
|
PyObject *new;
|
1992-09-11 20:55:51 -03:00
|
|
|
int changed;
|
|
|
|
|
2001-05-14 23:14:44 -03:00
|
|
|
WARN;
|
2002-04-02 14:17:57 -04:00
|
|
|
if (PyString_AsStringAndSize(args, &s, &n))
|
1992-09-11 20:55:51 -03:00
|
|
|
return NULL;
|
1996-12-09 14:35:56 -04:00
|
|
|
new = PyString_FromStringAndSize(NULL, n);
|
1992-09-11 20:55:51 -03:00
|
|
|
if (new == NULL)
|
|
|
|
return NULL;
|
1996-12-09 14:35:56 -04:00
|
|
|
s_new = PyString_AsString(new);
|
1992-09-11 20:55:51 -03:00
|
|
|
changed = 0;
|
|
|
|
for (i = 0; i < n; i++) {
|
1995-02-10 13:01:56 -04:00
|
|
|
int c = Py_CHARMASK(*s++);
|
1997-01-03 19:46:51 -04:00
|
|
|
if (isupper(c)) {
|
1992-09-11 20:55:51 -03:00
|
|
|
changed = 1;
|
1997-01-03 19:46:51 -04:00
|
|
|
*s_new = tolower(c);
|
1993-10-22 09:04:32 -03:00
|
|
|
} else
|
|
|
|
*s_new = c;
|
|
|
|
s_new++;
|
1992-09-11 20:55:51 -03:00
|
|
|
}
|
|
|
|
if (!changed) {
|
1996-12-09 14:35:56 -04:00
|
|
|
Py_DECREF(new);
|
|
|
|
Py_INCREF(args);
|
1992-09-11 20:55:51 -03:00
|
|
|
return args;
|
|
|
|
}
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-12-29 15:52:29 -04:00
|
|
|
static char upper__doc__[] =
|
2001-05-09 19:15:03 -03:00
|
|
|
"upper(s) -> string\n"
|
|
|
|
"\n"
|
|
|
|
"Return a copy of the string s converted to uppercase.";
|
1997-12-29 15:52:29 -04:00
|
|
|
|
1996-12-09 14:35:56 -04:00
|
|
|
static PyObject *
|
2000-07-10 06:43:24 -03:00
|
|
|
strop_upper(PyObject *self, PyObject *args)
|
1992-09-11 20:55:51 -03:00
|
|
|
{
|
1997-01-03 19:46:51 -04:00
|
|
|
char *s, *s_new;
|
|
|
|
int i, n;
|
|
|
|
PyObject *new;
|
|
|
|
int changed;
|
|
|
|
|
2001-05-14 23:14:44 -03:00
|
|
|
WARN;
|
2002-04-02 14:17:57 -04:00
|
|
|
if (PyString_AsStringAndSize(args, &s, &n))
|
1997-01-03 19:46:51 -04:00
|
|
|
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 20:55:51 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-12-29 15:52:29 -04:00
|
|
|
static char capitalize__doc__[] =
|
2001-05-09 19:15:03 -03:00
|
|
|
"capitalize(s) -> string\n"
|
|
|
|
"\n"
|
|
|
|
"Return a copy of the string s with only its first character\n"
|
|
|
|
"capitalized.";
|
1997-12-29 15:52:29 -04:00
|
|
|
|
1996-12-09 14:35:56 -04:00
|
|
|
static PyObject *
|
2000-07-10 06:43:24 -03:00
|
|
|
strop_capitalize(PyObject *self, PyObject *args)
|
1996-06-12 01:24:52 -03:00
|
|
|
{
|
|
|
|
char *s, *s_new;
|
|
|
|
int i, n;
|
1996-12-09 14:35:56 -04:00
|
|
|
PyObject *new;
|
1996-06-12 01:24:52 -03:00
|
|
|
int changed;
|
|
|
|
|
2001-05-14 23:14:44 -03:00
|
|
|
WARN;
|
2002-04-02 14:17:57 -04:00
|
|
|
if (PyString_AsStringAndSize(args, &s, &n))
|
1996-06-12 01:24:52 -03:00
|
|
|
return NULL;
|
1996-12-09 14:35:56 -04:00
|
|
|
new = PyString_FromStringAndSize(NULL, n);
|
1996-06-12 01:24:52 -03:00
|
|
|
if (new == NULL)
|
|
|
|
return NULL;
|
1996-12-09 14:35:56 -04:00
|
|
|
s_new = PyString_AsString(new);
|
1996-06-12 01:24:52 -03:00
|
|
|
changed = 0;
|
1996-06-17 13:59:33 -03:00
|
|
|
if (0 < n) {
|
1996-06-12 01:24:52 -03: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 14:35:56 -04:00
|
|
|
Py_DECREF(new);
|
|
|
|
Py_INCREF(args);
|
1996-06-12 01:24:52 -03:00
|
|
|
return args;
|
|
|
|
}
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-01-25 18:36:24 -04:00
|
|
|
static char expandtabs__doc__[] =
|
2001-05-09 19:15:03 -03:00
|
|
|
"expandtabs(string, [tabsize]) -> string\n"
|
|
|
|
"\n"
|
|
|
|
"Expand tabs in a string, i.e. replace them by one or more spaces,\n"
|
|
|
|
"depending on the current column and the given tab size (default 8).\n"
|
|
|
|
"The column number is reset to zero after each newline occurring in the\n"
|
|
|
|
"string. This doesn't understand other non-printing characters.";
|
1999-01-25 18:36:24 -04:00
|
|
|
|
|
|
|
static PyObject *
|
2000-07-10 06:43:24 -03:00
|
|
|
strop_expandtabs(PyObject *self, PyObject *args)
|
1999-01-25 18:36:24 -04:00
|
|
|
{
|
|
|
|
/* Original by Fredrik Lundh */
|
|
|
|
char* e;
|
|
|
|
char* p;
|
|
|
|
char* q;
|
|
|
|
int i, j;
|
|
|
|
PyObject* out;
|
|
|
|
char* string;
|
|
|
|
int stringlen;
|
|
|
|
int tabsize = 8;
|
|
|
|
|
2001-05-14 23:14:44 -03:00
|
|
|
WARN;
|
1999-01-25 18:36:24 -04:00
|
|
|
/* Get arguments */
|
2000-02-29 09:59:29 -04:00
|
|
|
if (!PyArg_ParseTuple(args, "s#|i:expandtabs", &string, &stringlen, &tabsize))
|
1999-01-25 18:36:24 -04:00
|
|
|
return NULL;
|
|
|
|
if (tabsize < 1) {
|
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
"tabsize must be at least 1");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* First pass: determine size of output string */
|
|
|
|
i = j = 0; /* j: current column; i: total of previous lines */
|
|
|
|
e = string + stringlen;
|
|
|
|
for (p = string; p < e; p++) {
|
|
|
|
if (*p == '\t')
|
|
|
|
j += tabsize - (j%tabsize);
|
|
|
|
else {
|
|
|
|
j++;
|
|
|
|
if (*p == '\n') {
|
|
|
|
i += j;
|
|
|
|
j = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Second pass: create output string and fill it */
|
|
|
|
out = PyString_FromStringAndSize(NULL, i+j);
|
|
|
|
if (out == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
q = PyString_AS_STRING(out);
|
|
|
|
|
|
|
|
for (p = string; p < e; p++) {
|
|
|
|
if (*p == '\t') {
|
|
|
|
j = tabsize - (i%tabsize);
|
|
|
|
i += j;
|
|
|
|
while (j-- > 0)
|
|
|
|
*q++ = ' ';
|
|
|
|
} else {
|
|
|
|
*q++ = *p;
|
|
|
|
i++;
|
|
|
|
if (*p == '\n')
|
|
|
|
i = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-10-06 16:43:14 -03:00
|
|
|
static char count__doc__[] =
|
2001-05-09 19:15:03 -03:00
|
|
|
"count(s, sub[, start[, end]]) -> int\n"
|
|
|
|
"\n"
|
|
|
|
"Return the number of occurrences of substring sub in string\n"
|
|
|
|
"s[start:end]. Optional arguments start and end are\n"
|
|
|
|
"interpreted as in slice notation.";
|
1998-10-06 16:43:14 -03:00
|
|
|
|
|
|
|
static PyObject *
|
2000-07-10 06:43:24 -03:00
|
|
|
strop_count(PyObject *self, PyObject *args)
|
1998-10-06 16:43:14 -03:00
|
|
|
{
|
|
|
|
char *s, *sub;
|
1998-10-07 13:36:14 -03:00
|
|
|
int len, n;
|
1998-10-06 16:43:14 -03:00
|
|
|
int i = 0, last = INT_MAX;
|
|
|
|
int m, r;
|
|
|
|
|
2001-05-14 23:14:44 -03:00
|
|
|
WARN;
|
2000-02-29 09:59:29 -04:00
|
|
|
if (!PyArg_ParseTuple(args, "t#t#|ii:count", &s, &len, &sub, &n, &i, &last))
|
1998-10-06 16:43:14 -03:00
|
|
|
return NULL;
|
|
|
|
if (last > len)
|
|
|
|
last = len;
|
|
|
|
if (last < 0)
|
|
|
|
last += len;
|
|
|
|
if (last < 0)
|
|
|
|
last = 0;
|
|
|
|
if (i < 0)
|
|
|
|
i += len;
|
|
|
|
if (i < 0)
|
|
|
|
i = 0;
|
|
|
|
m = last + 1 - n;
|
|
|
|
if (n == 0)
|
|
|
|
return PyInt_FromLong((long) (m-i));
|
|
|
|
|
|
|
|
r = 0;
|
|
|
|
while (i < m) {
|
|
|
|
if (!memcmp(s+i, sub, n)) {
|
|
|
|
r++;
|
|
|
|
i += n;
|
|
|
|
} else {
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return PyInt_FromLong((long) r);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-12-29 15:52:29 -04:00
|
|
|
static char swapcase__doc__[] =
|
2001-05-09 19:15:03 -03:00
|
|
|
"swapcase(s) -> string\n"
|
|
|
|
"\n"
|
|
|
|
"Return a copy of the string s with upper case characters\n"
|
|
|
|
"converted to lowercase and vice versa.";
|
1997-12-29 15:52:29 -04:00
|
|
|
|
1996-12-09 14:35:56 -04:00
|
|
|
static PyObject *
|
2000-07-10 06:43:24 -03:00
|
|
|
strop_swapcase(PyObject *self, PyObject *args)
|
1992-09-11 20:55:51 -03:00
|
|
|
{
|
1993-10-22 09:04:32 -03:00
|
|
|
char *s, *s_new;
|
1992-09-11 20:55:51 -03:00
|
|
|
int i, n;
|
1996-12-09 14:35:56 -04:00
|
|
|
PyObject *new;
|
1992-09-11 20:55:51 -03:00
|
|
|
int changed;
|
|
|
|
|
2001-05-14 23:14:44 -03:00
|
|
|
WARN;
|
2002-04-02 14:17:57 -04:00
|
|
|
if (PyString_AsStringAndSize(args, &s, &n))
|
1992-09-11 20:55:51 -03:00
|
|
|
return NULL;
|
1996-12-09 14:35:56 -04:00
|
|
|
new = PyString_FromStringAndSize(NULL, n);
|
1992-09-11 20:55:51 -03:00
|
|
|
if (new == NULL)
|
|
|
|
return NULL;
|
1996-12-09 14:35:56 -04:00
|
|
|
s_new = PyString_AsString(new);
|
1992-09-11 20:55:51 -03:00
|
|
|
changed = 0;
|
|
|
|
for (i = 0; i < n; i++) {
|
1995-02-10 13:01:56 -04:00
|
|
|
int c = Py_CHARMASK(*s++);
|
1992-09-11 20:55:51 -03:00
|
|
|
if (islower(c)) {
|
|
|
|
changed = 1;
|
1993-10-22 09:04:32 -03:00
|
|
|
*s_new = toupper(c);
|
1992-09-11 20:55:51 -03:00
|
|
|
}
|
|
|
|
else if (isupper(c)) {
|
|
|
|
changed = 1;
|
1993-10-22 09:04:32 -03:00
|
|
|
*s_new = tolower(c);
|
1992-09-11 20:55:51 -03:00
|
|
|
}
|
1993-10-22 09:04:32 -03:00
|
|
|
else
|
|
|
|
*s_new = c;
|
|
|
|
s_new++;
|
1992-09-11 20:55:51 -03:00
|
|
|
}
|
|
|
|
if (!changed) {
|
1996-12-09 14:35:56 -04:00
|
|
|
Py_DECREF(new);
|
|
|
|
Py_INCREF(args);
|
1992-09-11 20:55:51 -03:00
|
|
|
return args;
|
|
|
|
}
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-12-29 15:52:29 -04:00
|
|
|
static char atoi__doc__[] =
|
2001-05-09 19:15:03 -03:00
|
|
|
"atoi(s [,base]) -> int\n"
|
|
|
|
"\n"
|
|
|
|
"Return the integer represented by the string s in the given\n"
|
|
|
|
"base, which defaults to 10. The string s must consist of one\n"
|
|
|
|
"or more digits, possibly preceded by a sign. If base is 0, it\n"
|
|
|
|
"is chosen from the leading characters of s, 0 for octal, 0x or\n"
|
|
|
|
"0X for hexadecimal. If base is 16, a preceding 0x or 0X is\n"
|
|
|
|
"accepted.";
|
1997-12-29 15:52:29 -04:00
|
|
|
|
1996-12-09 14:35:56 -04:00
|
|
|
static PyObject *
|
2000-07-10 06:43:24 -03:00
|
|
|
strop_atoi(PyObject *self, PyObject *args)
|
1994-08-01 08:34:53 -03:00
|
|
|
{
|
|
|
|
char *s, *end;
|
|
|
|
int base = 10;
|
|
|
|
long x;
|
1996-09-11 20:30:42 -03:00
|
|
|
char buffer[256]; /* For errors */
|
1994-08-01 08:34:53 -03:00
|
|
|
|
2001-05-14 23:14:44 -03:00
|
|
|
WARN;
|
2000-02-29 09:59:29 -04:00
|
|
|
if (!PyArg_ParseTuple(args, "s|i:atoi", &s, &base))
|
1997-01-03 18:45:34 -04:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if ((base != 0 && base < 2) || base > 36) {
|
|
|
|
PyErr_SetString(PyExc_ValueError, "invalid base for atoi()");
|
1994-08-01 08:34:53 -03:00
|
|
|
return NULL;
|
1997-01-03 18:45:34 -04:00
|
|
|
}
|
|
|
|
|
1996-09-11 20:30:42 -03:00
|
|
|
while (*s && isspace(Py_CHARMASK(*s)))
|
|
|
|
s++;
|
1994-08-01 08:34:53 -03:00
|
|
|
errno = 0;
|
|
|
|
if (base == 0 && s[0] == '0')
|
1996-12-09 14:35:56 -04:00
|
|
|
x = (long) PyOS_strtoul(s, &end, base);
|
1994-08-01 08:34:53 -03:00
|
|
|
else
|
1996-12-09 14:35:56 -04:00
|
|
|
x = PyOS_strtol(s, &end, base);
|
2002-03-20 17:32:07 -04:00
|
|
|
if (end == s || !isalnum((int)end[-1]))
|
1998-08-04 12:04:52 -03:00
|
|
|
goto bad;
|
1996-09-11 20:30:42 -03:00
|
|
|
while (*end && isspace(Py_CHARMASK(*end)))
|
|
|
|
end++;
|
1994-08-01 08:34:53 -03:00
|
|
|
if (*end != '\0') {
|
1998-08-04 12:04:52 -03:00
|
|
|
bad:
|
2001-11-28 16:27:42 -04:00
|
|
|
PyOS_snprintf(buffer, sizeof(buffer),
|
|
|
|
"invalid literal for atoi(): %.200s", s);
|
1996-12-09 14:35:56 -04:00
|
|
|
PyErr_SetString(PyExc_ValueError, buffer);
|
1994-08-01 08:34:53 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else if (errno != 0) {
|
2001-11-28 18:07:30 -04:00
|
|
|
PyOS_snprintf(buffer, sizeof(buffer),
|
|
|
|
"atoi() literal too large: %.200s", s);
|
1996-12-09 14:35:56 -04:00
|
|
|
PyErr_SetString(PyExc_ValueError, buffer);
|
1994-08-01 08:34:53 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
1996-12-09 14:35:56 -04:00
|
|
|
return PyInt_FromLong(x);
|
1994-08-01 08:34:53 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-12-29 15:52:29 -04:00
|
|
|
static char atol__doc__[] =
|
2001-05-09 19:15:03 -03:00
|
|
|
"atol(s [,base]) -> long\n"
|
|
|
|
"\n"
|
|
|
|
"Return the long integer represented by the string s in the\n"
|
|
|
|
"given base, which defaults to 10. The string s must consist\n"
|
|
|
|
"of one or more digits, possibly preceded by a sign. If base\n"
|
|
|
|
"is 0, it is chosen from the leading characters of s, 0 for\n"
|
|
|
|
"octal, 0x or 0X for hexadecimal. If base is 16, a preceding\n"
|
|
|
|
"0x or 0X is accepted. A trailing L or l is not accepted,\n"
|
|
|
|
"unless base is 0.";
|
1997-12-29 15:52:29 -04:00
|
|
|
|
1996-12-09 14:35:56 -04:00
|
|
|
static PyObject *
|
2000-07-10 06:43:24 -03:00
|
|
|
strop_atol(PyObject *self, PyObject *args)
|
1994-08-01 08:34:53 -03:00
|
|
|
{
|
|
|
|
char *s, *end;
|
|
|
|
int base = 10;
|
1996-12-09 14:35:56 -04:00
|
|
|
PyObject *x;
|
1996-09-11 20:30:42 -03:00
|
|
|
char buffer[256]; /* For errors */
|
1994-08-01 08:34:53 -03:00
|
|
|
|
2001-05-14 23:14:44 -03:00
|
|
|
WARN;
|
2000-02-29 09:59:29 -04:00
|
|
|
if (!PyArg_ParseTuple(args, "s|i:atol", &s, &base))
|
1997-01-03 18:45:34 -04:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if ((base != 0 && base < 2) || base > 36) {
|
|
|
|
PyErr_SetString(PyExc_ValueError, "invalid base for atol()");
|
1994-08-01 08:34:53 -03:00
|
|
|
return NULL;
|
1997-01-03 18:45:34 -04:00
|
|
|
}
|
|
|
|
|
1996-09-11 20:30:42 -03:00
|
|
|
while (*s && isspace(Py_CHARMASK(*s)))
|
|
|
|
s++;
|
1996-08-21 17:02:25 -03:00
|
|
|
if (s[0] == '\0') {
|
1996-12-09 14:35:56 -04:00
|
|
|
PyErr_SetString(PyExc_ValueError, "empty string for atol()");
|
1996-08-21 17:02:25 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
1996-12-09 14:35:56 -04:00
|
|
|
x = PyLong_FromString(s, &end, base);
|
1994-08-01 08:34:53 -03:00
|
|
|
if (x == NULL)
|
|
|
|
return NULL;
|
|
|
|
if (base == 0 && (*end == 'l' || *end == 'L'))
|
|
|
|
end++;
|
1996-09-11 20:30:42 -03:00
|
|
|
while (*end && isspace(Py_CHARMASK(*end)))
|
|
|
|
end++;
|
1994-08-01 08:34:53 -03:00
|
|
|
if (*end != '\0') {
|
2001-11-28 18:07:30 -04:00
|
|
|
PyOS_snprintf(buffer, sizeof(buffer),
|
|
|
|
"invalid literal for atol(): %.200s", s);
|
1996-12-09 14:35:56 -04:00
|
|
|
PyErr_SetString(PyExc_ValueError, buffer);
|
|
|
|
Py_DECREF(x);
|
1994-08-01 08:34:53 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-12-29 15:52:29 -04:00
|
|
|
static char atof__doc__[] =
|
2001-05-09 19:15:03 -03:00
|
|
|
"atof(s) -> float\n"
|
|
|
|
"\n"
|
|
|
|
"Return the floating point number represented by the string s.";
|
1997-12-29 15:52:29 -04:00
|
|
|
|
1996-12-09 14:35:56 -04:00
|
|
|
static PyObject *
|
2000-07-10 06:43:24 -03:00
|
|
|
strop_atof(PyObject *self, PyObject *args)
|
1994-08-01 08:34:53 -03:00
|
|
|
{
|
2000-07-09 00:09:57 -03:00
|
|
|
extern double strtod(const char *, char **);
|
1994-08-01 08:34:53 -03:00
|
|
|
char *s, *end;
|
|
|
|
double x;
|
1996-09-11 20:30:42 -03:00
|
|
|
char buffer[256]; /* For errors */
|
1994-08-01 08:34:53 -03:00
|
|
|
|
2001-05-14 23:14:44 -03:00
|
|
|
WARN;
|
2000-02-29 09:59:29 -04:00
|
|
|
if (!PyArg_ParseTuple(args, "s:atof", &s))
|
1994-08-01 08:34:53 -03:00
|
|
|
return NULL;
|
1996-09-11 20:30:42 -03:00
|
|
|
while (*s && isspace(Py_CHARMASK(*s)))
|
|
|
|
s++;
|
1996-08-21 17:02:25 -03:00
|
|
|
if (s[0] == '\0') {
|
1996-12-09 14:35:56 -04:00
|
|
|
PyErr_SetString(PyExc_ValueError, "empty string for atof()");
|
1996-08-21 17:02:25 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
1994-08-01 08:34:53 -03:00
|
|
|
errno = 0;
|
1997-02-14 18:59:58 -04:00
|
|
|
PyFPE_START_PROTECT("strop_atof", return 0)
|
1994-08-01 08:34:53 -03:00
|
|
|
x = strtod(s, &end);
|
1997-03-14 00:13:56 -04:00
|
|
|
PyFPE_END_PROTECT(x)
|
1996-09-11 20:30:42 -03:00
|
|
|
while (*end && isspace(Py_CHARMASK(*end)))
|
|
|
|
end++;
|
1994-08-01 08:34:53 -03:00
|
|
|
if (*end != '\0') {
|
2001-11-28 16:27:42 -04:00
|
|
|
PyOS_snprintf(buffer, sizeof(buffer),
|
|
|
|
"invalid literal for atof(): %.200s", s);
|
1996-12-09 14:35:56 -04:00
|
|
|
PyErr_SetString(PyExc_ValueError, buffer);
|
1994-08-01 08:34:53 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else if (errno != 0) {
|
2001-11-28 16:27:42 -04:00
|
|
|
PyOS_snprintf(buffer, sizeof(buffer),
|
|
|
|
"atof() literal too large: %.200s", s);
|
1996-12-09 14:35:56 -04:00
|
|
|
PyErr_SetString(PyExc_ValueError, buffer);
|
1994-08-01 08:34:53 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
1996-12-09 14:35:56 -04:00
|
|
|
return PyFloat_FromDouble(x);
|
1994-08-01 08:34:53 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-12-29 15:52:29 -04:00
|
|
|
static char maketrans__doc__[] =
|
2001-05-09 19:15:03 -03:00
|
|
|
"maketrans(frm, to) -> string\n"
|
|
|
|
"\n"
|
|
|
|
"Return a translation table (a string of 256 bytes long)\n"
|
|
|
|
"suitable for use in string.translate. The strings frm and to\n"
|
|
|
|
"must be of the same length.";
|
1997-12-29 15:52:29 -04:00
|
|
|
|
1996-07-23 15:12:39 -03:00
|
|
|
static PyObject *
|
2000-07-10 06:43:24 -03:00
|
|
|
strop_maketrans(PyObject *self, PyObject *args)
|
1996-07-23 15:12:39 -03:00
|
|
|
{
|
1997-01-06 12:50:09 -04:00
|
|
|
unsigned char *c, *from=NULL, *to=NULL;
|
1996-07-23 15:12:39 -03:00
|
|
|
int i, fromlen=0, tolen=0;
|
1997-01-06 12:50:09 -04:00
|
|
|
PyObject *result;
|
1996-07-23 15:12:39 -03:00
|
|
|
|
2000-02-29 09:59:29 -04:00
|
|
|
if (!PyArg_ParseTuple(args, "t#t#:maketrans", &from, &fromlen, &to, &tolen))
|
1997-01-03 18:45:34 -04:00
|
|
|
return NULL;
|
1996-07-23 15:12:39 -03:00
|
|
|
|
1997-01-03 18:45:34 -04:00
|
|
|
if (fromlen != tolen) {
|
1996-12-09 14:35:56 -04:00
|
|
|
PyErr_SetString(PyExc_ValueError,
|
1996-07-23 15:12:39 -03:00
|
|
|
"maketrans arguments must have same length");
|
|
|
|
return NULL;
|
|
|
|
}
|
1997-01-06 12:50:09 -04:00
|
|
|
|
|
|
|
result = PyString_FromStringAndSize((char *)NULL, 256);
|
|
|
|
if (result == NULL)
|
|
|
|
return NULL;
|
|
|
|
c = (unsigned char *) PyString_AS_STRING((PyStringObject *)result);
|
1997-01-03 18:45:34 -04:00
|
|
|
for (i = 0; i < 256; i++)
|
1996-07-23 15:12:39 -03:00
|
|
|
c[i]=(unsigned char)i;
|
1997-01-03 18:45:34 -04:00
|
|
|
for (i = 0; i < fromlen; i++)
|
1996-07-23 15:12:39 -03:00
|
|
|
c[from[i]]=to[i];
|
1997-01-03 18:45:34 -04:00
|
|
|
|
1997-01-06 12:50:09 -04:00
|
|
|
return result;
|
1996-07-23 15:12:39 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-12-29 15:52:29 -04:00
|
|
|
static char translate__doc__[] =
|
2001-05-09 19:15:03 -03:00
|
|
|
"translate(s,table [,deletechars]) -> string\n"
|
|
|
|
"\n"
|
|
|
|
"Return a copy of the string s, where all characters occurring\n"
|
|
|
|
"in the optional argument deletechars are removed, and the\n"
|
|
|
|
"remaining characters have been mapped through the given\n"
|
|
|
|
"translation table, which must be a string of length 256.";
|
1997-12-29 15:52:29 -04:00
|
|
|
|
1996-12-09 14:35:56 -04:00
|
|
|
static PyObject *
|
2000-07-10 06:43:24 -03:00
|
|
|
strop_translate(PyObject *self, PyObject *args)
|
1995-09-13 14:39:06 -03:00
|
|
|
{
|
1997-01-06 12:50:09 -04:00
|
|
|
register char *input, *table, *output;
|
|
|
|
register int i, c, changed = 0;
|
|
|
|
PyObject *input_obj;
|
|
|
|
char *table1, *output_start, *del_table=NULL;
|
1997-01-03 18:45:34 -04:00
|
|
|
int inlen, tablen, dellen = 0;
|
1996-07-23 15:12:39 -03:00
|
|
|
PyObject *result;
|
1997-01-06 12:50:09 -04:00
|
|
|
int trans_table[256];
|
1995-09-13 14:39:06 -03:00
|
|
|
|
2001-05-14 23:14:44 -03:00
|
|
|
WARN;
|
2000-02-29 09:59:29 -04:00
|
|
|
if (!PyArg_ParseTuple(args, "St#|t#:translate", &input_obj,
|
1997-01-06 12:50:09 -04:00
|
|
|
&table1, &tablen, &del_table, &dellen))
|
1995-09-13 14:39:06 -03:00
|
|
|
return NULL;
|
|
|
|
if (tablen != 256) {
|
1996-12-09 14:35:56 -04:00
|
|
|
PyErr_SetString(PyExc_ValueError,
|
1997-01-03 18:45:34 -04:00
|
|
|
"translation table must be 256 characters long");
|
1995-09-13 14:39:06 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
1996-07-23 15:12:39 -03:00
|
|
|
|
1997-01-06 12:50:09 -04:00
|
|
|
table = table1;
|
|
|
|
inlen = PyString_Size(input_obj);
|
1996-07-23 15:12:39 -03:00
|
|
|
result = PyString_FromStringAndSize((char *)NULL, inlen);
|
1995-09-13 14:39:06 -03:00
|
|
|
if (result == NULL)
|
|
|
|
return NULL;
|
1996-07-23 15:12:39 -03:00
|
|
|
output_start = output = PyString_AsString(result);
|
1997-01-06 12:50:09 -04:00
|
|
|
input = PyString_AsString(input_obj);
|
|
|
|
|
|
|
|
if (dellen == 0) {
|
|
|
|
/* If no deletions are required, use faster code */
|
|
|
|
for (i = inlen; --i >= 0; ) {
|
|
|
|
c = Py_CHARMASK(*input++);
|
|
|
|
if (Py_CHARMASK((*output++ = table[c])) != c)
|
|
|
|
changed = 1;
|
1996-08-08 16:16:15 -03:00
|
|
|
}
|
1997-01-06 12:50:09 -04:00
|
|
|
if (changed)
|
|
|
|
return result;
|
|
|
|
Py_DECREF(result);
|
|
|
|
Py_INCREF(input_obj);
|
|
|
|
return input_obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < 256; i++)
|
|
|
|
trans_table[i] = Py_CHARMASK(table[i]);
|
|
|
|
|
1997-12-29 15:52:29 -04:00
|
|
|
for (i = 0; i < dellen; i++)
|
1997-04-29 18:34:16 -03:00
|
|
|
trans_table[(int) Py_CHARMASK(del_table[i])] = -1;
|
1997-01-06 12:50:09 -04:00
|
|
|
|
|
|
|
for (i = inlen; --i >= 0; ) {
|
|
|
|
c = Py_CHARMASK(*input++);
|
|
|
|
if (trans_table[c] != -1)
|
|
|
|
if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c)
|
|
|
|
continue;
|
|
|
|
changed = 1;
|
|
|
|
}
|
|
|
|
if (!changed) {
|
|
|
|
Py_DECREF(result);
|
|
|
|
Py_INCREF(input_obj);
|
|
|
|
return input_obj;
|
1995-09-13 14:39:06 -03:00
|
|
|
}
|
1997-01-06 12:50:09 -04:00
|
|
|
/* Fix the size of the resulting string */
|
2002-04-27 15:44:32 -03:00
|
|
|
if (inlen > 0)
|
|
|
|
_PyString_Resize(&result, output - output_start);
|
1995-09-13 14:39:06 -03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-04-02 02:11:18 -04:00
|
|
|
/* What follows is used for implementing replace(). Perry Stoll. */
|
|
|
|
|
|
|
|
/*
|
|
|
|
mymemfind
|
|
|
|
|
|
|
|
strstr replacement for arbitrary blocks of memory.
|
|
|
|
|
2000-03-20 12:36:48 -04:00
|
|
|
Locates the first occurrence in the memory pointed to by MEM of the
|
1997-04-02 02:11:18 -04:00
|
|
|
contents of memory pointed to by PAT. Returns the index into MEM if
|
|
|
|
found, or -1 if not found. If len of PAT is greater than length of
|
1997-12-29 15:52:29 -04:00
|
|
|
MEM, the function returns -1.
|
1997-04-02 02:11:18 -04:00
|
|
|
*/
|
2001-05-09 19:15:03 -03:00
|
|
|
static int
|
2001-05-09 21:05:33 -03:00
|
|
|
mymemfind(const char *mem, int len, const char *pat, int pat_len)
|
1997-04-02 02:11:18 -04:00
|
|
|
{
|
|
|
|
register int ii;
|
|
|
|
|
|
|
|
/* pattern can not occur in the last pat_len-1 chars */
|
|
|
|
len -= pat_len;
|
|
|
|
|
|
|
|
for (ii = 0; ii <= len; ii++) {
|
|
|
|
if (mem[ii] == pat[0] &&
|
|
|
|
(pat_len == 1 ||
|
|
|
|
memcmp(&mem[ii+1], &pat[1], pat_len-1) == 0)) {
|
|
|
|
return ii;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
mymemcnt
|
|
|
|
|
|
|
|
Return the number of distinct times PAT is found in MEM.
|
|
|
|
meaning mem=1111 and pat==11 returns 2.
|
|
|
|
mem=11111 and pat==11 also return 2.
|
|
|
|
*/
|
2001-05-09 19:15:03 -03:00
|
|
|
static int
|
2001-05-09 21:05:33 -03:00
|
|
|
mymemcnt(const char *mem, int len, const char *pat, int pat_len)
|
1997-04-02 02:11:18 -04:00
|
|
|
{
|
|
|
|
register int offset = 0;
|
|
|
|
int nfound = 0;
|
|
|
|
|
|
|
|
while (len >= 0) {
|
|
|
|
offset = mymemfind(mem, len, pat, pat_len);
|
|
|
|
if (offset == -1)
|
|
|
|
break;
|
|
|
|
mem += offset + pat_len;
|
|
|
|
len -= offset + pat_len;
|
|
|
|
nfound++;
|
|
|
|
}
|
|
|
|
return nfound;
|
|
|
|
}
|
|
|
|
|
1997-12-29 15:52:29 -04:00
|
|
|
/*
|
1997-04-02 02:11:18 -04:00
|
|
|
mymemreplace
|
|
|
|
|
2000-07-16 09:04:32 -03:00
|
|
|
Return a string in which all occurrences of PAT in memory STR are
|
1997-12-29 15:52:29 -04:00
|
|
|
replaced with SUB.
|
1997-04-02 02:11:18 -04:00
|
|
|
|
2000-07-16 09:04:32 -03:00
|
|
|
If length of PAT is less than length of STR or there are no occurrences
|
1997-04-02 02:11:18 -04:00
|
|
|
of PAT in STR, then the original string is returned. Otherwise, a new
|
|
|
|
string is allocated here and returned.
|
1997-12-29 15:52:29 -04:00
|
|
|
|
1997-04-02 02:11:18 -04:00
|
|
|
on return, out_len is:
|
|
|
|
the length of output string, or
|
|
|
|
-1 if the input string is returned, or
|
|
|
|
unchanged if an error occurs (no memory).
|
|
|
|
|
|
|
|
return value is:
|
|
|
|
the new string allocated locally, or
|
|
|
|
NULL if an error occurred.
|
|
|
|
*/
|
2001-05-09 19:15:03 -03:00
|
|
|
static char *
|
2001-05-09 21:05:33 -03:00
|
|
|
mymemreplace(const char *str, int len, /* input string */
|
|
|
|
const char *pat, int pat_len, /* pattern string to find */
|
|
|
|
const char *sub, int sub_len, /* substitution string */
|
|
|
|
int count, /* number of replacements */
|
|
|
|
int *out_len)
|
1997-04-02 02:11:18 -04:00
|
|
|
{
|
|
|
|
char *out_s;
|
|
|
|
char *new_s;
|
|
|
|
int nfound, offset, new_len;
|
|
|
|
|
|
|
|
if (len == 0 || pat_len > len)
|
|
|
|
goto return_same;
|
|
|
|
|
|
|
|
/* find length of output string */
|
|
|
|
nfound = mymemcnt(str, len, pat, pat_len);
|
2001-05-09 21:32:57 -03:00
|
|
|
if (count < 0)
|
|
|
|
count = INT_MAX;
|
|
|
|
else if (nfound > count)
|
|
|
|
nfound = count;
|
1997-04-02 02:11:18 -04:00
|
|
|
if (nfound == 0)
|
|
|
|
goto return_same;
|
2001-05-09 20:00:26 -03:00
|
|
|
|
1997-04-02 02:11:18 -04:00
|
|
|
new_len = len + nfound*(sub_len - pat_len);
|
2001-05-09 20:00:26 -03:00
|
|
|
if (new_len == 0) {
|
2001-05-09 21:05:33 -03:00
|
|
|
/* Have to allocate something for the caller to free(). */
|
|
|
|
out_s = (char *)PyMem_MALLOC(1);
|
2001-05-09 21:32:57 -03:00
|
|
|
if (out_s == NULL)
|
2001-05-09 21:05:33 -03:00
|
|
|
return NULL;
|
|
|
|
out_s[0] = '\0';
|
2001-05-09 20:00:26 -03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
assert(new_len > 0);
|
|
|
|
new_s = (char *)PyMem_MALLOC(new_len);
|
|
|
|
if (new_s == NULL)
|
|
|
|
return NULL;
|
|
|
|
out_s = new_s;
|
1997-04-02 02:11:18 -04:00
|
|
|
|
2001-05-09 21:32:57 -03:00
|
|
|
for (; count > 0 && len > 0; --count) {
|
2001-05-09 20:00:26 -03:00
|
|
|
/* find index of next instance of pattern */
|
|
|
|
offset = mymemfind(str, len, pat, pat_len);
|
|
|
|
if (offset == -1)
|
|
|
|
break;
|
1997-04-02 02:11:18 -04:00
|
|
|
|
2001-05-09 20:00:26 -03:00
|
|
|
/* copy non matching part of input string */
|
|
|
|
memcpy(new_s, str, offset);
|
|
|
|
str += offset + pat_len;
|
|
|
|
len -= offset + pat_len;
|
|
|
|
|
|
|
|
/* copy substitute into the output string */
|
|
|
|
new_s += offset;
|
|
|
|
memcpy(new_s, sub, sub_len);
|
|
|
|
new_s += sub_len;
|
|
|
|
}
|
|
|
|
/* copy any remaining values into output string */
|
|
|
|
if (len > 0)
|
|
|
|
memcpy(new_s, str, len);
|
1997-04-02 02:11:18 -04:00
|
|
|
}
|
2001-05-09 20:00:26 -03:00
|
|
|
*out_len = new_len;
|
1997-04-02 02:11:18 -04:00
|
|
|
return out_s;
|
|
|
|
|
|
|
|
return_same:
|
|
|
|
*out_len = -1;
|
2001-05-09 21:05:33 -03:00
|
|
|
return (char *)str; /* cast away const */
|
1997-04-02 02:11:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-12-29 15:52:29 -04:00
|
|
|
static char replace__doc__[] =
|
2001-05-09 19:15:03 -03:00
|
|
|
"replace (str, old, new[, maxsplit]) -> string\n"
|
|
|
|
"\n"
|
|
|
|
"Return a copy of string str with all occurrences of substring\n"
|
|
|
|
"old replaced by new. If the optional argument maxsplit is\n"
|
|
|
|
"given, only the first maxsplit occurrences are replaced.";
|
1997-12-29 15:52:29 -04:00
|
|
|
|
|
|
|
static PyObject *
|
2000-07-10 06:43:24 -03:00
|
|
|
strop_replace(PyObject *self, PyObject *args)
|
1997-04-02 02:11:18 -04:00
|
|
|
{
|
|
|
|
char *str, *pat,*sub,*new_s;
|
|
|
|
int len,pat_len,sub_len,out_len;
|
2001-05-09 21:59:45 -03:00
|
|
|
int count = -1;
|
1997-04-02 02:11:18 -04:00
|
|
|
PyObject *new;
|
|
|
|
|
2001-05-14 23:14:44 -03:00
|
|
|
WARN;
|
2000-02-29 09:59:29 -04:00
|
|
|
if (!PyArg_ParseTuple(args, "t#t#t#|i:replace",
|
1997-11-28 20:10:07 -04:00
|
|
|
&str, &len, &pat, &pat_len, &sub, &sub_len,
|
|
|
|
&count))
|
1997-04-02 02:11:18 -04:00
|
|
|
return NULL;
|
1998-05-13 23:36:29 -03:00
|
|
|
if (pat_len <= 0) {
|
|
|
|
PyErr_SetString(PyExc_ValueError, "empty pattern string");
|
|
|
|
return NULL;
|
|
|
|
}
|
2001-05-09 22:23:39 -03:00
|
|
|
/* CAUTION: strop treats a replace count of 0 as infinity, unlke
|
|
|
|
* current (2.1) string.py and string methods. Preserve this for
|
|
|
|
* ... well, hard to say for what <wink>.
|
|
|
|
*/
|
|
|
|
if (count == 0)
|
|
|
|
count = -1;
|
1997-11-28 20:10:07 -04:00
|
|
|
new_s = mymemreplace(str,len,pat,pat_len,sub,sub_len,count,&out_len);
|
1997-04-02 02:11:18 -04:00
|
|
|
if (new_s == NULL) {
|
|
|
|
PyErr_NoMemory();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (out_len == -1) {
|
|
|
|
/* we're returning another reference to the input string */
|
|
|
|
new = PyTuple_GetItem(args, 0);
|
|
|
|
Py_XINCREF(new);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
new = PyString_FromStringAndSize(new_s, out_len);
|
2000-05-03 20:44:39 -03:00
|
|
|
PyMem_FREE(new_s);
|
1997-04-02 02:11:18 -04:00
|
|
|
}
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1992-09-03 17:21:07 -03:00
|
|
|
/* List of functions defined in the module */
|
|
|
|
|
1997-01-03 18:45:34 -04:00
|
|
|
static PyMethodDef
|
|
|
|
strop_methods[] = {
|
2001-05-09 19:15:03 -03:00
|
|
|
{"atof", strop_atof, METH_VARARGS, atof__doc__},
|
|
|
|
{"atoi", strop_atoi, METH_VARARGS, atoi__doc__},
|
|
|
|
{"atol", strop_atol, METH_VARARGS, atol__doc__},
|
2002-04-02 14:17:57 -04:00
|
|
|
{"capitalize", strop_capitalize, METH_O, capitalize__doc__},
|
2001-05-09 19:15:03 -03:00
|
|
|
{"count", strop_count, METH_VARARGS, count__doc__},
|
|
|
|
{"expandtabs", strop_expandtabs, METH_VARARGS, expandtabs__doc__},
|
|
|
|
{"find", strop_find, METH_VARARGS, find__doc__},
|
|
|
|
{"join", strop_joinfields, METH_VARARGS, joinfields__doc__},
|
|
|
|
{"joinfields", strop_joinfields, METH_VARARGS, joinfields__doc__},
|
2002-04-02 14:17:57 -04:00
|
|
|
{"lstrip", strop_lstrip, METH_O, lstrip__doc__},
|
|
|
|
{"lower", strop_lower, METH_O, lower__doc__},
|
2001-05-09 19:15:03 -03:00
|
|
|
{"maketrans", strop_maketrans, METH_VARARGS, maketrans__doc__},
|
|
|
|
{"replace", strop_replace, METH_VARARGS, replace__doc__},
|
|
|
|
{"rfind", strop_rfind, METH_VARARGS, rfind__doc__},
|
2002-04-02 14:17:57 -04:00
|
|
|
{"rstrip", strop_rstrip, METH_O, rstrip__doc__},
|
2001-05-09 19:15:03 -03:00
|
|
|
{"split", strop_splitfields, METH_VARARGS, splitfields__doc__},
|
|
|
|
{"splitfields", strop_splitfields, METH_VARARGS, splitfields__doc__},
|
2002-04-02 14:17:57 -04:00
|
|
|
{"strip", strop_strip, METH_O, strip__doc__},
|
|
|
|
{"swapcase", strop_swapcase, METH_O, swapcase__doc__},
|
2001-05-09 19:15:03 -03:00
|
|
|
{"translate", strop_translate, METH_VARARGS, translate__doc__},
|
2002-04-02 14:17:57 -04:00
|
|
|
{"upper", strop_upper, METH_O, upper__doc__},
|
1992-09-03 17:21:07 -03:00
|
|
|
{NULL, NULL} /* sentinel */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
1998-12-04 14:50:17 -04:00
|
|
|
DL_EXPORT(void)
|
2000-07-21 03:00:07 -03:00
|
|
|
initstrop(void)
|
1992-09-03 17:21:07 -03:00
|
|
|
{
|
2002-04-01 10:53:37 -04:00
|
|
|
PyObject *m, *s;
|
1993-07-08 08:12:36 -03:00
|
|
|
char buf[256];
|
|
|
|
int c, n;
|
1997-12-29 15:52:29 -04:00
|
|
|
m = Py_InitModule4("strop", strop_methods, strop_module__doc__,
|
|
|
|
(PyObject*)NULL, PYTHON_API_VERSION);
|
1993-07-09 07:51:31 -03:00
|
|
|
|
|
|
|
/* Create 'whitespace' object */
|
1993-07-08 08:12:36 -03:00
|
|
|
n = 0;
|
1995-02-10 13:01:56 -04:00
|
|
|
for (c = 0; c < 256; c++) {
|
1993-07-08 08:12:36 -03:00
|
|
|
if (isspace(c))
|
|
|
|
buf[n++] = c;
|
|
|
|
}
|
1996-12-09 14:35:56 -04:00
|
|
|
s = PyString_FromStringAndSize(buf, n);
|
2002-04-01 10:53:37 -04:00
|
|
|
if (s)
|
|
|
|
PyModule_AddObject(m, "whitespace", s);
|
|
|
|
|
1993-07-09 07:51:31 -03:00
|
|
|
/* Create 'lowercase' object */
|
|
|
|
n = 0;
|
1995-02-10 13:01:56 -04:00
|
|
|
for (c = 0; c < 256; c++) {
|
1993-07-09 07:51:31 -03:00
|
|
|
if (islower(c))
|
|
|
|
buf[n++] = c;
|
|
|
|
}
|
1996-12-09 14:35:56 -04:00
|
|
|
s = PyString_FromStringAndSize(buf, n);
|
2002-04-01 10:53:37 -04:00
|
|
|
if (s)
|
|
|
|
PyModule_AddObject(m, "lowercase", s);
|
1993-07-09 07:51:31 -03:00
|
|
|
|
|
|
|
/* Create 'uppercase' object */
|
|
|
|
n = 0;
|
1995-02-10 13:01:56 -04:00
|
|
|
for (c = 0; c < 256; c++) {
|
1993-07-09 07:51:31 -03:00
|
|
|
if (isupper(c))
|
|
|
|
buf[n++] = c;
|
|
|
|
}
|
1996-12-09 14:35:56 -04:00
|
|
|
s = PyString_FromStringAndSize(buf, n);
|
2002-04-01 10:53:37 -04:00
|
|
|
if (s)
|
|
|
|
PyModule_AddObject(m, "uppercase", s);
|
1992-09-03 17:21:07 -03:00
|
|
|
}
|