mirror of
https://github.com/python/cpython.git
synced 2024-11-24 18:34:43 +08:00
Greatly renamed.
This commit is contained in:
parent
ec775c52a2
commit
f52560197f
@ -31,15 +31,12 @@ PERFORMANCE OF THIS SOFTWARE.
|
|||||||
|
|
||||||
/* strop module */
|
/* strop module */
|
||||||
|
|
||||||
#include "allobjects.h"
|
#include "Python.h"
|
||||||
#include "modsupport.h"
|
|
||||||
|
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
/* XXX This file assumes that the <ctype.h> is*() functions
|
/* XXX This file assumes that the <ctype.h> is*() functions
|
||||||
XXX are defined for all 8-bit characters! */
|
XXX are defined for all 8-bit characters! */
|
||||||
|
|
||||||
#include <errno.h>
|
|
||||||
|
|
||||||
/* The lstrip(), rstrip() and strip() functions are implemented
|
/* The lstrip(), rstrip() and strip() functions are implemented
|
||||||
in do_strip(), which uses an additional parameter to indicate what
|
in do_strip(), which uses an additional parameter to indicate what
|
||||||
type of strip should occur. */
|
type of strip should occur. */
|
||||||
@ -49,7 +46,7 @@ PERFORMANCE OF THIS SOFTWARE.
|
|||||||
#define BOTHSTRIP 2
|
#define BOTHSTRIP 2
|
||||||
|
|
||||||
|
|
||||||
static object *
|
static PyObject *
|
||||||
split_whitespace(s, len, maxsplit)
|
split_whitespace(s, len, maxsplit)
|
||||||
char *s;
|
char *s;
|
||||||
int len;
|
int len;
|
||||||
@ -57,9 +54,9 @@ split_whitespace(s, len, maxsplit)
|
|||||||
{
|
{
|
||||||
int i, j, err;
|
int i, j, err;
|
||||||
int countsplit;
|
int countsplit;
|
||||||
object *list, *item;
|
PyObject *list, *item;
|
||||||
|
|
||||||
list = newlistobject(0);
|
list = PyList_New(0);
|
||||||
if (list == NULL)
|
if (list == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
@ -75,29 +72,30 @@ split_whitespace(s, len, maxsplit)
|
|||||||
i = i+1;
|
i = i+1;
|
||||||
}
|
}
|
||||||
if (j < i) {
|
if (j < i) {
|
||||||
item = newsizedstringobject(s+j, (int)(i-j));
|
item = PyString_FromStringAndSize(s+j, (int)(i-j));
|
||||||
if (item == NULL) {
|
if (item == NULL) {
|
||||||
DECREF(list);
|
Py_DECREF(list);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
err = addlistitem(list, item);
|
err = PyList_Append(list, item);
|
||||||
DECREF(item);
|
Py_DECREF(item);
|
||||||
if (err < 0) {
|
if (err < 0) {
|
||||||
DECREF(list);
|
Py_DECREF(list);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
countsplit++;
|
countsplit++;
|
||||||
if (maxsplit && (countsplit >= maxsplit)) {
|
if (maxsplit && (countsplit >= maxsplit)) {
|
||||||
item = newsizedstringobject(s+i, (int)(len - i));
|
item = PyString_FromStringAndSize(
|
||||||
|
s+i, (int)(len - i));
|
||||||
if (item == NULL) {
|
if (item == NULL) {
|
||||||
DECREF(list);
|
Py_DECREF(list);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
err = addlistitem(list, item);
|
err = PyList_Append(list, item);
|
||||||
DECREF(item);
|
Py_DECREF(item);
|
||||||
if (err < 0) {
|
if (err < 0) {
|
||||||
DECREF(list);
|
Py_DECREF(list);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
i = len;
|
i = len;
|
||||||
@ -110,41 +108,41 @@ split_whitespace(s, len, maxsplit)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static object *
|
static PyObject *
|
||||||
strop_splitfields(self, args)
|
strop_splitfields(self, args)
|
||||||
object *self; /* Not used */
|
PyObject *self; /* Not used */
|
||||||
object *args;
|
PyObject *args;
|
||||||
{
|
{
|
||||||
int len, n, i, j, err;
|
int len, n, i, j, err;
|
||||||
int splitcount, maxsplit;
|
int splitcount, maxsplit;
|
||||||
char *s, *sub;
|
char *s, *sub;
|
||||||
object *list, *item;
|
PyObject *list, *item;
|
||||||
|
|
||||||
sub = NULL;
|
sub = NULL;
|
||||||
n = 0;
|
n = 0;
|
||||||
splitcount = 0;
|
splitcount = 0;
|
||||||
maxsplit = 0;
|
maxsplit = 0;
|
||||||
if (!newgetargs(args, "s#|z#i", &s, &len, &sub, &n, &maxsplit))
|
if (!PyArg_ParseTuple(args, "s#|z#i", &s, &len, &sub, &n, &maxsplit))
|
||||||
return NULL;
|
return NULL;
|
||||||
if (sub == NULL)
|
if (sub == NULL)
|
||||||
return split_whitespace(s, len, maxsplit);
|
return split_whitespace(s, len, maxsplit);
|
||||||
if (n == 0) {
|
if (n == 0) {
|
||||||
err_setstr(ValueError, "empty separator");
|
PyErr_SetString(PyExc_ValueError, "empty separator");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
list = newlistobject(0);
|
list = PyList_New(0);
|
||||||
if (list == NULL)
|
if (list == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
i = j = 0;
|
i = j = 0;
|
||||||
while (i+n <= len) {
|
while (i+n <= len) {
|
||||||
if (s[i] == sub[0] && (n == 1 || memcmp(s+i, sub, n) == 0)) {
|
if (s[i] == sub[0] && (n == 1 || memcmp(s+i, sub, n) == 0)) {
|
||||||
item = newsizedstringobject(s+j, (int)(i-j));
|
item = PyString_FromStringAndSize(s+j, (int)(i-j));
|
||||||
if (item == NULL)
|
if (item == NULL)
|
||||||
goto fail;
|
goto fail;
|
||||||
err = addlistitem(list, item);
|
err = PyList_Append(list, item);
|
||||||
DECREF(item);
|
Py_DECREF(item);
|
||||||
if (err < 0)
|
if (err < 0)
|
||||||
goto fail;
|
goto fail;
|
||||||
i = j = i + n;
|
i = j = i + n;
|
||||||
@ -155,168 +153,170 @@ strop_splitfields(self, args)
|
|||||||
else
|
else
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
item = newsizedstringobject(s+j, (int)(len-j));
|
item = PyString_FromStringAndSize(s+j, (int)(len-j));
|
||||||
if (item == NULL)
|
if (item == NULL)
|
||||||
goto fail;
|
goto fail;
|
||||||
err = addlistitem(list, item);
|
err = PyList_Append(list, item);
|
||||||
DECREF(item);
|
Py_DECREF(item);
|
||||||
if (err < 0)
|
if (err < 0)
|
||||||
goto fail;
|
goto fail;
|
||||||
|
|
||||||
return list;
|
return list;
|
||||||
|
|
||||||
fail:
|
fail:
|
||||||
DECREF(list);
|
Py_DECREF(list);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static object *
|
static PyObject *
|
||||||
strop_joinfields(self, args)
|
strop_joinfields(self, args)
|
||||||
object *self; /* Not used */
|
PyObject *self; /* Not used */
|
||||||
object *args;
|
PyObject *args;
|
||||||
{
|
{
|
||||||
object *seq, *item, *res;
|
PyObject *seq, *item, *res;
|
||||||
object * (*getitem) FPROTO((object *, int));
|
PyObject * (*getitem) Py_FPROTO((PyObject *, int));
|
||||||
char *sep, *p;
|
char *sep, *p;
|
||||||
int seplen, seqlen, reslen, itemlen, i;
|
int seplen, seqlen, reslen, itemlen, i;
|
||||||
|
|
||||||
sep = NULL;
|
sep = NULL;
|
||||||
seplen = 0;
|
seplen = 0;
|
||||||
if (!newgetargs(args, "O|s#", &seq, &sep, &seplen))
|
if (!PyArg_ParseTuple(args, "O|s#", &seq, &sep, &seplen))
|
||||||
return NULL;
|
return NULL;
|
||||||
if (sep == NULL) {
|
if (sep == NULL) {
|
||||||
sep = " ";
|
sep = " ";
|
||||||
seplen = 1;
|
seplen = 1;
|
||||||
}
|
}
|
||||||
if (is_listobject(seq)) {
|
if (PyList_Check(seq)) {
|
||||||
getitem = getlistitem;
|
getitem = PyList_GetItem;
|
||||||
seqlen = getlistsize(seq);
|
seqlen = PyList_Size(seq);
|
||||||
}
|
}
|
||||||
else if (is_tupleobject(seq)) {
|
else if (PyTuple_Check(seq)) {
|
||||||
getitem = gettupleitem;
|
getitem = PyTuple_GetItem;
|
||||||
seqlen = gettuplesize(seq);
|
seqlen = PyTuple_Size(seq);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
err_setstr(TypeError, "first argument must be list/tuple");
|
PyErr_SetString(PyExc_TypeError,
|
||||||
|
"first argument must be list/tuple");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
reslen = 0;
|
reslen = 0;
|
||||||
for (i = 0; i < seqlen; i++) {
|
for (i = 0; i < seqlen; i++) {
|
||||||
item = getitem(seq, i);
|
item = getitem(seq, i);
|
||||||
if (!is_stringobject(item)) {
|
if (!PyString_Check(item)) {
|
||||||
err_setstr(TypeError,
|
PyErr_SetString(PyExc_TypeError,
|
||||||
"first argument must be list/tuple of strings");
|
"first argument must be list/tuple of strings");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (i > 0)
|
if (i > 0)
|
||||||
reslen = reslen + seplen;
|
reslen = reslen + seplen;
|
||||||
reslen = reslen + getstringsize(item);
|
reslen = reslen + PyString_Size(item);
|
||||||
}
|
}
|
||||||
if (seqlen == 1) {
|
if (seqlen == 1) {
|
||||||
/* Optimization if there's only one item */
|
/* Optimization if there's only one item */
|
||||||
item = getitem(seq, 0);
|
item = getitem(seq, 0);
|
||||||
INCREF(item);
|
Py_INCREF(item);
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
res = newsizedstringobject((char *)NULL, reslen);
|
res = PyString_FromStringAndSize((char *)NULL, reslen);
|
||||||
if (res == NULL)
|
if (res == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
p = getstringvalue(res);
|
p = PyString_AsString(res);
|
||||||
for (i = 0; i < seqlen; i++) {
|
for (i = 0; i < seqlen; i++) {
|
||||||
item = getitem(seq, i);
|
item = getitem(seq, i);
|
||||||
if (i > 0) {
|
if (i > 0) {
|
||||||
memcpy(p, sep, seplen);
|
memcpy(p, sep, seplen);
|
||||||
p += seplen;
|
p += seplen;
|
||||||
}
|
}
|
||||||
itemlen = getstringsize(item);
|
itemlen = PyString_Size(item);
|
||||||
memcpy(p, getstringvalue(item), itemlen);
|
memcpy(p, PyString_AsString(item), itemlen);
|
||||||
p += itemlen;
|
p += itemlen;
|
||||||
}
|
}
|
||||||
if (p != getstringvalue(res) + reslen) {
|
if (p != PyString_AsString(res) + reslen) {
|
||||||
err_setstr(SystemError, "strop.joinfields: assertion failed");
|
PyErr_SetString(PyExc_SystemError,
|
||||||
|
"strop.joinfields: assertion failed");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static object *
|
static PyObject *
|
||||||
strop_find(self, args)
|
strop_find(self, args)
|
||||||
object *self; /* Not used */
|
PyObject *self; /* Not used */
|
||||||
object *args;
|
PyObject *args;
|
||||||
{
|
{
|
||||||
char *s, *sub;
|
char *s, *sub;
|
||||||
int len, n, i;
|
int len, n, i;
|
||||||
|
|
||||||
if (getargs(args, "(s#s#i)", &s, &len, &sub, &n, &i)) {
|
if (PyArg_Parse(args, "(s#s#i)", &s, &len, &sub, &n, &i)) {
|
||||||
if (i < 0)
|
if (i < 0)
|
||||||
i += len;
|
i += len;
|
||||||
if (i < 0)
|
if (i < 0)
|
||||||
i = 0;
|
i = 0;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
err_clear();
|
PyErr_Clear();
|
||||||
if (!getargs(args, "(s#s#)", &s, &len, &sub, &n))
|
if (!PyArg_Parse(args, "(s#s#)", &s, &len, &sub, &n))
|
||||||
return NULL;
|
return NULL;
|
||||||
i = 0;
|
i = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (n == 0)
|
if (n == 0)
|
||||||
return newintobject((long)i);
|
return PyInt_FromLong((long)i);
|
||||||
|
|
||||||
len -= n;
|
len -= n;
|
||||||
for (; i <= len; ++i)
|
for (; i <= len; ++i)
|
||||||
if (s[i] == sub[0] &&
|
if (s[i] == sub[0] &&
|
||||||
(n == 1 || memcmp(&s[i+1], &sub[1], n-1) == 0))
|
(n == 1 || memcmp(&s[i+1], &sub[1], n-1) == 0))
|
||||||
return newintobject((long)i);
|
return PyInt_FromLong((long)i);
|
||||||
|
|
||||||
return newintobject(-1L);
|
return PyInt_FromLong(-1L);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static object *
|
static PyObject *
|
||||||
strop_rfind(self, args)
|
strop_rfind(self, args)
|
||||||
object *self; /* Not used */
|
PyObject *self; /* Not used */
|
||||||
object *args;
|
PyObject *args;
|
||||||
{
|
{
|
||||||
char *s, *sub;
|
char *s, *sub;
|
||||||
int len, n, i, j;
|
int len, n, i, j;
|
||||||
|
|
||||||
if (getargs(args, "(s#s#i)", &s, &len, &sub, &n, &i)) {
|
if (PyArg_Parse(args, "(s#s#i)", &s, &len, &sub, &n, &i)) {
|
||||||
if (i < 0)
|
if (i < 0)
|
||||||
i += len;
|
i += len;
|
||||||
if (i < 0)
|
if (i < 0)
|
||||||
i = 0;
|
i = 0;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
err_clear();
|
PyErr_Clear();
|
||||||
if (!getargs(args, "(s#s#)", &s, &len, &sub, &n))
|
if (!PyArg_Parse(args, "(s#s#)", &s, &len, &sub, &n))
|
||||||
return NULL;
|
return NULL;
|
||||||
i = 0;
|
i = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (n == 0)
|
if (n == 0)
|
||||||
return newintobject((long)len);
|
return PyInt_FromLong((long)len);
|
||||||
|
|
||||||
for (j = len-n; j >= i; --j)
|
for (j = len-n; j >= i; --j)
|
||||||
if (s[j] == sub[0] &&
|
if (s[j] == sub[0] &&
|
||||||
(n == 1 || memcmp(&s[j+1], &sub[1], n-1) == 0))
|
(n == 1 || memcmp(&s[j+1], &sub[1], n-1) == 0))
|
||||||
return newintobject((long)j);
|
return PyInt_FromLong((long)j);
|
||||||
|
|
||||||
return newintobject(-1L);
|
return PyInt_FromLong(-1L);
|
||||||
}
|
}
|
||||||
|
|
||||||
static object *
|
static PyObject *
|
||||||
do_strip(args, striptype)
|
do_strip(args, striptype)
|
||||||
object *args;
|
PyObject *args;
|
||||||
int striptype;
|
int striptype;
|
||||||
{
|
{
|
||||||
char *s;
|
char *s;
|
||||||
int len, i, j;
|
int len, i, j;
|
||||||
|
|
||||||
|
|
||||||
if (!getargs(args, "s#", &s, &len))
|
if (!PyArg_Parse(args, "s#", &s, &len))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
@ -336,55 +336,55 @@ do_strip(args, striptype)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (i == 0 && j == len) {
|
if (i == 0 && j == len) {
|
||||||
INCREF(args);
|
Py_INCREF(args);
|
||||||
return args;
|
return args;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
return newsizedstringobject(s+i, j-i);
|
return PyString_FromStringAndSize(s+i, j-i);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static object *
|
static PyObject *
|
||||||
strop_strip(self, args)
|
strop_strip(self, args)
|
||||||
object *self; /* Not used */
|
PyObject *self; /* Not used */
|
||||||
object *args;
|
PyObject *args;
|
||||||
{
|
{
|
||||||
return do_strip(args, BOTHSTRIP);
|
return do_strip(args, BOTHSTRIP);
|
||||||
}
|
}
|
||||||
|
|
||||||
static object *
|
static PyObject *
|
||||||
strop_lstrip(self, args)
|
strop_lstrip(self, args)
|
||||||
object *self; /* Not used */
|
PyObject *self; /* Not used */
|
||||||
object *args;
|
PyObject *args;
|
||||||
{
|
{
|
||||||
return do_strip(args, LEFTSTRIP);
|
return do_strip(args, LEFTSTRIP);
|
||||||
}
|
}
|
||||||
|
|
||||||
static object *
|
static PyObject *
|
||||||
strop_rstrip(self, args)
|
strop_rstrip(self, args)
|
||||||
object *self; /* Not used */
|
PyObject *self; /* Not used */
|
||||||
object *args;
|
PyObject *args;
|
||||||
{
|
{
|
||||||
return do_strip(args, RIGHTSTRIP);
|
return do_strip(args, RIGHTSTRIP);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static object *
|
static PyObject *
|
||||||
strop_lower(self, args)
|
strop_lower(self, args)
|
||||||
object *self; /* Not used */
|
PyObject *self; /* Not used */
|
||||||
object *args;
|
PyObject *args;
|
||||||
{
|
{
|
||||||
char *s, *s_new;
|
char *s, *s_new;
|
||||||
int i, n;
|
int i, n;
|
||||||
object *new;
|
PyObject *new;
|
||||||
int changed;
|
int changed;
|
||||||
|
|
||||||
if (!getargs(args, "s#", &s, &n))
|
if (!PyArg_Parse(args, "s#", &s, &n))
|
||||||
return NULL;
|
return NULL;
|
||||||
new = newsizedstringobject(NULL, n);
|
new = PyString_FromStringAndSize(NULL, n);
|
||||||
if (new == NULL)
|
if (new == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
s_new = getstringvalue(new);
|
s_new = PyString_AsString(new);
|
||||||
changed = 0;
|
changed = 0;
|
||||||
for (i = 0; i < n; i++) {
|
for (i = 0; i < n; i++) {
|
||||||
int c = Py_CHARMASK(*s++);
|
int c = Py_CHARMASK(*s++);
|
||||||
@ -396,30 +396,30 @@ strop_lower(self, args)
|
|||||||
s_new++;
|
s_new++;
|
||||||
}
|
}
|
||||||
if (!changed) {
|
if (!changed) {
|
||||||
DECREF(new);
|
Py_DECREF(new);
|
||||||
INCREF(args);
|
Py_INCREF(args);
|
||||||
return args;
|
return args;
|
||||||
}
|
}
|
||||||
return new;
|
return new;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static object *
|
static PyObject *
|
||||||
strop_upper(self, args)
|
strop_upper(self, args)
|
||||||
object *self; /* Not used */
|
PyObject *self; /* Not used */
|
||||||
object *args;
|
PyObject *args;
|
||||||
{
|
{
|
||||||
char *s, *s_new;
|
char *s, *s_new;
|
||||||
int i, n;
|
int i, n;
|
||||||
object *new;
|
PyObject *new;
|
||||||
int changed;
|
int changed;
|
||||||
|
|
||||||
if (!getargs(args, "s#", &s, &n))
|
if (!PyArg_Parse(args, "s#", &s, &n))
|
||||||
return NULL;
|
return NULL;
|
||||||
new = newsizedstringobject(NULL, n);
|
new = PyString_FromStringAndSize(NULL, n);
|
||||||
if (new == NULL)
|
if (new == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
s_new = getstringvalue(new);
|
s_new = PyString_AsString(new);
|
||||||
changed = 0;
|
changed = 0;
|
||||||
for (i = 0; i < n; i++) {
|
for (i = 0; i < n; i++) {
|
||||||
int c = Py_CHARMASK(*s++);
|
int c = Py_CHARMASK(*s++);
|
||||||
@ -431,30 +431,30 @@ strop_upper(self, args)
|
|||||||
s_new++;
|
s_new++;
|
||||||
}
|
}
|
||||||
if (!changed) {
|
if (!changed) {
|
||||||
DECREF(new);
|
Py_DECREF(new);
|
||||||
INCREF(args);
|
Py_INCREF(args);
|
||||||
return args;
|
return args;
|
||||||
}
|
}
|
||||||
return new;
|
return new;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static object *
|
static PyObject *
|
||||||
strop_capitalize(self, args)
|
strop_capitalize(self, args)
|
||||||
object *self; /* Not used */
|
PyObject *self; /* Not used */
|
||||||
object *args;
|
PyObject *args;
|
||||||
{
|
{
|
||||||
char *s, *s_new;
|
char *s, *s_new;
|
||||||
int i, n;
|
int i, n;
|
||||||
object *new;
|
PyObject *new;
|
||||||
int changed;
|
int changed;
|
||||||
|
|
||||||
if (!getargs(args, "s#", &s, &n))
|
if (!PyArg_Parse(args, "s#", &s, &n))
|
||||||
return NULL;
|
return NULL;
|
||||||
new = newsizedstringobject(NULL, n);
|
new = PyString_FromStringAndSize(NULL, n);
|
||||||
if (new == NULL)
|
if (new == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
s_new = getstringvalue(new);
|
s_new = PyString_AsString(new);
|
||||||
changed = 0;
|
changed = 0;
|
||||||
if (0 < n) {
|
if (0 < n) {
|
||||||
int c = Py_CHARMASK(*s++);
|
int c = Py_CHARMASK(*s++);
|
||||||
@ -475,30 +475,30 @@ strop_capitalize(self, args)
|
|||||||
s_new++;
|
s_new++;
|
||||||
}
|
}
|
||||||
if (!changed) {
|
if (!changed) {
|
||||||
DECREF(new);
|
Py_DECREF(new);
|
||||||
INCREF(args);
|
Py_INCREF(args);
|
||||||
return args;
|
return args;
|
||||||
}
|
}
|
||||||
return new;
|
return new;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static object *
|
static PyObject *
|
||||||
strop_swapcase(self, args)
|
strop_swapcase(self, args)
|
||||||
object *self; /* Not used */
|
PyObject *self; /* Not used */
|
||||||
object *args;
|
PyObject *args;
|
||||||
{
|
{
|
||||||
char *s, *s_new;
|
char *s, *s_new;
|
||||||
int i, n;
|
int i, n;
|
||||||
object *new;
|
PyObject *new;
|
||||||
int changed;
|
int changed;
|
||||||
|
|
||||||
if (!getargs(args, "s#", &s, &n))
|
if (!PyArg_Parse(args, "s#", &s, &n))
|
||||||
return NULL;
|
return NULL;
|
||||||
new = newsizedstringobject(NULL, n);
|
new = PyString_FromStringAndSize(NULL, n);
|
||||||
if (new == NULL)
|
if (new == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
s_new = getstringvalue(new);
|
s_new = PyString_AsString(new);
|
||||||
changed = 0;
|
changed = 0;
|
||||||
for (i = 0; i < n; i++) {
|
for (i = 0; i < n; i++) {
|
||||||
int c = Py_CHARMASK(*s++);
|
int c = Py_CHARMASK(*s++);
|
||||||
@ -515,90 +515,93 @@ strop_swapcase(self, args)
|
|||||||
s_new++;
|
s_new++;
|
||||||
}
|
}
|
||||||
if (!changed) {
|
if (!changed) {
|
||||||
DECREF(new);
|
Py_DECREF(new);
|
||||||
INCREF(args);
|
Py_INCREF(args);
|
||||||
return args;
|
return args;
|
||||||
}
|
}
|
||||||
return new;
|
return new;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static object *
|
static PyObject *
|
||||||
strop_atoi(self, args)
|
strop_atoi(self, args)
|
||||||
object *self; /* Not used */
|
PyObject *self; /* Not used */
|
||||||
object *args;
|
PyObject *args;
|
||||||
{
|
{
|
||||||
extern long mystrtol PROTO((const char *, char **, int));
|
extern long PyOS_strtol Py_PROTO((const char *, char **, int));
|
||||||
extern unsigned long mystrtoul PROTO((const char *, char **, int));
|
extern unsigned long
|
||||||
|
PyOS_strtoul Py_PROTO((const char *, char **, int));
|
||||||
char *s, *end;
|
char *s, *end;
|
||||||
int base = 10;
|
int base = 10;
|
||||||
long x;
|
long x;
|
||||||
char buffer[256]; /* For errors */
|
char buffer[256]; /* For errors */
|
||||||
|
|
||||||
if (args != NULL && is_tupleobject(args)) {
|
if (args != NULL && PyTuple_Check(args)) {
|
||||||
if (!getargs(args, "(si)", &s, &base))
|
if (!PyArg_Parse(args, "(si)", &s, &base))
|
||||||
return NULL;
|
return NULL;
|
||||||
if ((base != 0 && base < 2) || base > 36) {
|
if ((base != 0 && base < 2) || base > 36) {
|
||||||
err_setstr(ValueError, "invalid base for atoi()");
|
PyErr_SetString(PyExc_ValueError,
|
||||||
|
"invalid base for atoi()");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (!getargs(args, "s", &s))
|
else if (!PyArg_Parse(args, "s", &s))
|
||||||
return NULL;
|
return NULL;
|
||||||
while (*s && isspace(Py_CHARMASK(*s)))
|
while (*s && isspace(Py_CHARMASK(*s)))
|
||||||
s++;
|
s++;
|
||||||
if (s[0] == '\0') {
|
if (s[0] == '\0') {
|
||||||
err_setstr(ValueError, "empty string for atoi()");
|
PyErr_SetString(PyExc_ValueError, "empty string for atoi()");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
errno = 0;
|
errno = 0;
|
||||||
if (base == 0 && s[0] == '0')
|
if (base == 0 && s[0] == '0')
|
||||||
x = (long) mystrtoul(s, &end, base);
|
x = (long) PyOS_strtoul(s, &end, base);
|
||||||
else
|
else
|
||||||
x = mystrtol(s, &end, base);
|
x = PyOS_strtol(s, &end, base);
|
||||||
while (*end && isspace(Py_CHARMASK(*end)))
|
while (*end && isspace(Py_CHARMASK(*end)))
|
||||||
end++;
|
end++;
|
||||||
if (*end != '\0') {
|
if (*end != '\0') {
|
||||||
sprintf(buffer, "invalid literal for atoi(): %.200s", s);
|
sprintf(buffer, "invalid literal for atoi(): %.200s", s);
|
||||||
err_setstr(ValueError, buffer);
|
PyErr_SetString(PyExc_ValueError, buffer);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
else if (errno != 0) {
|
else if (errno != 0) {
|
||||||
sprintf(buffer, "atoi() literal too large: %.200s", s);
|
sprintf(buffer, "atoi() literal too large: %.200s", s);
|
||||||
err_setstr(ValueError, buffer);
|
PyErr_SetString(PyExc_ValueError, buffer);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
return newintobject(x);
|
return PyInt_FromLong(x);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static object *
|
static PyObject *
|
||||||
strop_atol(self, args)
|
strop_atol(self, args)
|
||||||
object *self; /* Not used */
|
PyObject *self; /* Not used */
|
||||||
object *args;
|
PyObject *args;
|
||||||
{
|
{
|
||||||
char *s, *end;
|
char *s, *end;
|
||||||
int base = 10;
|
int base = 10;
|
||||||
object *x;
|
PyObject *x;
|
||||||
char buffer[256]; /* For errors */
|
char buffer[256]; /* For errors */
|
||||||
|
|
||||||
if (args != NULL && is_tupleobject(args)) {
|
if (args != NULL && PyTuple_Check(args)) {
|
||||||
if (!getargs(args, "(si)", &s, &base))
|
if (!PyArg_Parse(args, "(si)", &s, &base))
|
||||||
return NULL;
|
return NULL;
|
||||||
if ((base != 0 && base < 2) || base > 36) {
|
if ((base != 0 && base < 2) || base > 36) {
|
||||||
err_setstr(ValueError, "invalid base for atol()");
|
PyErr_SetString(PyExc_ValueError,
|
||||||
|
"invalid base for atol()");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (!getargs(args, "s", &s))
|
else if (!PyArg_Parse(args, "s", &s))
|
||||||
return NULL;
|
return NULL;
|
||||||
while (*s && isspace(Py_CHARMASK(*s)))
|
while (*s && isspace(Py_CHARMASK(*s)))
|
||||||
s++;
|
s++;
|
||||||
if (s[0] == '\0') {
|
if (s[0] == '\0') {
|
||||||
err_setstr(ValueError, "empty string for atol()");
|
PyErr_SetString(PyExc_ValueError, "empty string for atol()");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
x = long_escan(s, &end, base);
|
x = PyLong_FromString(s, &end, base);
|
||||||
if (x == NULL)
|
if (x == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
if (base == 0 && (*end == 'l' || *end == 'L'))
|
if (base == 0 && (*end == 'l' || *end == 'L'))
|
||||||
@ -607,30 +610,30 @@ strop_atol(self, args)
|
|||||||
end++;
|
end++;
|
||||||
if (*end != '\0') {
|
if (*end != '\0') {
|
||||||
sprintf(buffer, "invalid literal for atol(): %.200s", s);
|
sprintf(buffer, "invalid literal for atol(): %.200s", s);
|
||||||
err_setstr(ValueError, buffer);
|
PyErr_SetString(PyExc_ValueError, buffer);
|
||||||
DECREF(x);
|
Py_DECREF(x);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
return x;
|
return x;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static object *
|
static PyObject *
|
||||||
strop_atof(self, args)
|
strop_atof(self, args)
|
||||||
object *self; /* Not used */
|
PyObject *self; /* Not used */
|
||||||
object *args;
|
PyObject *args;
|
||||||
{
|
{
|
||||||
extern double strtod PROTO((const char *, char **));
|
extern double strtod Py_PROTO((const char *, char **));
|
||||||
char *s, *end;
|
char *s, *end;
|
||||||
double x;
|
double x;
|
||||||
char buffer[256]; /* For errors */
|
char buffer[256]; /* For errors */
|
||||||
|
|
||||||
if (!getargs(args, "s", &s))
|
if (!PyArg_Parse(args, "s", &s))
|
||||||
return NULL;
|
return NULL;
|
||||||
while (*s && isspace(Py_CHARMASK(*s)))
|
while (*s && isspace(Py_CHARMASK(*s)))
|
||||||
s++;
|
s++;
|
||||||
if (s[0] == '\0') {
|
if (s[0] == '\0') {
|
||||||
err_setstr(ValueError, "empty string for atof()");
|
PyErr_SetString(PyExc_ValueError, "empty string for atof()");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
errno = 0;
|
errno = 0;
|
||||||
@ -639,15 +642,15 @@ strop_atof(self, args)
|
|||||||
end++;
|
end++;
|
||||||
if (*end != '\0') {
|
if (*end != '\0') {
|
||||||
sprintf(buffer, "invalid literal for atof(): %.200s", s);
|
sprintf(buffer, "invalid literal for atof(): %.200s", s);
|
||||||
err_setstr(ValueError, buffer);
|
PyErr_SetString(PyExc_ValueError, buffer);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
else if (errno != 0) {
|
else if (errno != 0) {
|
||||||
sprintf(buffer, "atof() literal too large: %.200s", s);
|
sprintf(buffer, "atof() literal too large: %.200s", s);
|
||||||
err_setstr(ValueError, buffer);
|
PyErr_SetString(PyExc_ValueError, buffer);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
return newfloatobject(x);
|
return PyFloat_FromDouble(x);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -666,7 +669,7 @@ strop_maketrans(self, args)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (fromlen!=tolen) {
|
if (fromlen!=tolen) {
|
||||||
PyErr_SetString(ValueError,
|
PyErr_SetString(PyExc_ValueError,
|
||||||
"maketrans arguments must have same length");
|
"maketrans arguments must have same length");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@ -679,10 +682,10 @@ strop_maketrans(self, args)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static object *
|
static PyObject *
|
||||||
strop_translate(self, args)
|
strop_translate(self, args)
|
||||||
object *self;
|
PyObject *self;
|
||||||
object *args;
|
PyObject *args;
|
||||||
{
|
{
|
||||||
char *input, *table, *output, *output_start, *delete=NULL;
|
char *input, *table, *output, *output_start, *delete=NULL;
|
||||||
int inlen, tablen, dellen;
|
int inlen, tablen, dellen;
|
||||||
@ -693,7 +696,7 @@ strop_translate(self, args)
|
|||||||
&table, &tablen, &delete, &dellen))
|
&table, &tablen, &delete, &dellen))
|
||||||
return NULL;
|
return NULL;
|
||||||
if (tablen != 256) {
|
if (tablen != 256) {
|
||||||
PyErr_SetString(ValueError,
|
PyErr_SetString(PyExc_ValueError,
|
||||||
"translation table must be 256 characters long");
|
"translation table must be 256 characters long");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@ -730,7 +733,7 @@ strop_translate(self, args)
|
|||||||
|
|
||||||
/* List of functions defined in the module */
|
/* List of functions defined in the module */
|
||||||
|
|
||||||
static struct methodlist strop_methods[] = {
|
static PyMethodDef strop_methods[] = {
|
||||||
{"atof", strop_atof},
|
{"atof", strop_atof},
|
||||||
{"atoi", strop_atoi},
|
{"atoi", strop_atoi},
|
||||||
{"atol", strop_atol},
|
{"atol", strop_atol},
|
||||||
@ -756,11 +759,11 @@ static struct methodlist strop_methods[] = {
|
|||||||
void
|
void
|
||||||
initstrop()
|
initstrop()
|
||||||
{
|
{
|
||||||
object *m, *d, *s;
|
PyObject *m, *d, *s;
|
||||||
char buf[256];
|
char buf[256];
|
||||||
int c, n;
|
int c, n;
|
||||||
m = initmodule("strop", strop_methods);
|
m = Py_InitModule("strop", strop_methods);
|
||||||
d = getmoduledict(m);
|
d = PyModule_GetDict(m);
|
||||||
|
|
||||||
/* Create 'whitespace' object */
|
/* Create 'whitespace' object */
|
||||||
n = 0;
|
n = 0;
|
||||||
@ -768,10 +771,10 @@ initstrop()
|
|||||||
if (isspace(c))
|
if (isspace(c))
|
||||||
buf[n++] = c;
|
buf[n++] = c;
|
||||||
}
|
}
|
||||||
s = newsizedstringobject(buf, n);
|
s = PyString_FromStringAndSize(buf, n);
|
||||||
if (s) {
|
if (s) {
|
||||||
dictinsert(d, "whitespace", s);
|
PyDict_SetItemString(d, "whitespace", s);
|
||||||
DECREF(s);
|
Py_DECREF(s);
|
||||||
}
|
}
|
||||||
/* Create 'lowercase' object */
|
/* Create 'lowercase' object */
|
||||||
n = 0;
|
n = 0;
|
||||||
@ -779,10 +782,10 @@ initstrop()
|
|||||||
if (islower(c))
|
if (islower(c))
|
||||||
buf[n++] = c;
|
buf[n++] = c;
|
||||||
}
|
}
|
||||||
s = newsizedstringobject(buf, n);
|
s = PyString_FromStringAndSize(buf, n);
|
||||||
if (s) {
|
if (s) {
|
||||||
dictinsert(d, "lowercase", s);
|
PyDict_SetItemString(d, "lowercase", s);
|
||||||
DECREF(s);
|
Py_DECREF(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Create 'uppercase' object */
|
/* Create 'uppercase' object */
|
||||||
@ -791,12 +794,12 @@ initstrop()
|
|||||||
if (isupper(c))
|
if (isupper(c))
|
||||||
buf[n++] = c;
|
buf[n++] = c;
|
||||||
}
|
}
|
||||||
s = newsizedstringobject(buf, n);
|
s = PyString_FromStringAndSize(buf, n);
|
||||||
if (s) {
|
if (s) {
|
||||||
dictinsert(d, "uppercase", s);
|
PyDict_SetItemString(d, "uppercase", s);
|
||||||
DECREF(s);
|
Py_DECREF(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (err_occurred())
|
if (PyErr_Occurred())
|
||||||
fatal("can't initialize module strop");
|
Py_FatalError("can't initialize module strop");
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user