[Python-checkins] python/dist/src/Objects stringobject.c,2.159,2.160 unicodeobject.c,2.143,2.144
doerwalter@sourceforge.net
doerwalter@sourceforge.net
Mon, 22 Apr 2002 10:42:40 -0700
Update of /cvsroot/python/python/dist/src/Objects
In directory usw-pr-cvs1:/tmp/cvs-serv30740/Objects
Modified Files:
stringobject.c unicodeobject.c
Log Message:
Apply patch diff.txt from SF feature request
http://www.python.org/sf/444708
This adds the optional argument for str.strip
to unicode.strip too and makes it possible
to call str.strip with a unicode argument
and unicode.strip with a str argument.
Index: stringobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/stringobject.c,v
retrieving revision 2.159
retrieving revision 2.160
diff -C2 -d -r2.159 -r2.160
*** stringobject.c 15 Apr 2002 18:42:15 -0000 2.159
--- stringobject.c 22 Apr 2002 17:42:37 -0000 2.160
***************
*** 1006,1010 ****
/* Arrays indexed by above */
! static const char *stripname[] = {"lstrip", "rstrip", "strip"};
--- 1006,1012 ----
/* Arrays indexed by above */
! static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"};
!
! #define STRIPNAME(i) (stripformat[i]+3)
***************
*** 1450,1462 ****
PyObject *sep = NULL;
! if (!PyArg_ParseTuple(args, "|O:[lr]strip", &sep))
return NULL;
if (sep != NULL && sep != Py_None) {
! /* XXX What about Unicode? */
! if (!PyString_Check(sep)) {
PyErr_Format(PyExc_TypeError,
! "%s arg must be None or string",
! stripname[striptype]);
return NULL;
}
--- 1452,1475 ----
PyObject *sep = NULL;
! if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep))
return NULL;
if (sep != NULL && sep != Py_None) {
! if (PyString_Check(sep))
! return do_xstrip(self, striptype, sep);
! else if (PyUnicode_Check(sep)) {
! PyObject *uniself = PyUnicode_FromObject((PyObject *)self);
! PyObject *res;
! if (uniself==NULL)
! return NULL;
! res = _PyUnicode_XStrip((PyUnicodeObject *)uniself,
! striptype, sep);
! Py_DECREF(uniself);
! return res;
! }
! else {
PyErr_Format(PyExc_TypeError,
! "%s arg must be None, str or unicode",
! STRIPNAME(striptype));
return NULL;
}
***************
*** 1469,1477 ****
static char strip__doc__[] =
! "S.strip([sep]) -> string\n\
\n\
Return a copy of the string S with leading and trailing\n\
whitespace removed.\n\
! If sep is given and not None, remove characters in sep instead.";
static PyObject *
--- 1482,1491 ----
static char strip__doc__[] =
! "S.strip([sep]) -> string or unicode\n\
\n\
Return a copy of the string S with leading and trailing\n\
whitespace removed.\n\
! If sep is given and not None, remove characters in sep instead.\n\
! If sep is unicode, S will be converted to unicode before stripping";
static PyObject *
***************
*** 1486,1493 ****
static char lstrip__doc__[] =
! "S.lstrip([sep]) -> string\n\
\n\
Return a copy of the string S with leading whitespace removed.\n\
! If sep is given and not None, remove characters in sep instead.";
static PyObject *
--- 1500,1508 ----
static char lstrip__doc__[] =
! "S.lstrip([sep]) -> string or unicode\n\
\n\
Return a copy of the string S with leading whitespace removed.\n\
! If sep is given and not None, remove characters in sep instead.\n\
! If sep is unicode, S will be converted to unicode before stripping";
static PyObject *
***************
*** 1502,1509 ****
static char rstrip__doc__[] =
! "S.rstrip([sep]) -> string\n\
\n\
Return a copy of the string S with trailing whitespace removed.\n\
! If sep is given and not None, remove characters in sep instead.";
static PyObject *
--- 1517,1525 ----
static char rstrip__doc__[] =
! "S.rstrip([sep]) -> string or unicode\n\
\n\
Return a copy of the string S with trailing whitespace removed.\n\
! If sep is given and not None, remove characters in sep instead.\n\
! If sep is unicode, S will be converted to unicode before stripping";
static PyObject *
Index: unicodeobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/unicodeobject.c,v
retrieving revision 2.143
retrieving revision 2.144
diff -C2 -d -r2.143 -r2.144
*** unicodeobject.c 21 Apr 2002 17:28:06 -0000 2.143
--- unicodeobject.c 22 Apr 2002 17:42:37 -0000 2.144
***************
*** 3505,3537 ****
static
- PyObject *strip(PyUnicodeObject *self,
- int left,
- int right)
- {
- Py_UNICODE *p = self->str;
- int start = 0;
- int end = self->length;
-
- if (left)
- while (start < end && Py_UNICODE_ISSPACE(p[start]))
- start++;
-
- if (right)
- while (end > start && Py_UNICODE_ISSPACE(p[end-1]))
- end--;
-
- if (start == 0 && end == self->length && PyUnicode_CheckExact(self)) {
- /* couldn't strip anything off, return original string */
- Py_INCREF(self);
- return (PyObject*) self;
- }
-
- return (PyObject*) PyUnicode_FromUnicode(
- self->str + start,
- end - start
- );
- }
-
- static
PyObject *replace(PyUnicodeObject *self,
PyUnicodeObject *str1,
--- 3505,3508 ----
***************
*** 4465,4479 ****
}
static char lstrip__doc__[] =
! "S.lstrip() -> unicode\n\
\n\
! Return a copy of the string S with leading whitespace removed.";
static PyObject *
! unicode_lstrip(PyUnicodeObject *self)
{
! return strip(self, 1, 0);
}
static PyObject*
unicode_repeat(PyUnicodeObject *str, int len)
--- 4436,4606 ----
}
+ #define LEFTSTRIP 0
+ #define RIGHTSTRIP 1
+ #define BOTHSTRIP 2
+
+ /* Arrays indexed by above */
+ static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"};
+
+ #define STRIPNAME(i) (stripformat[i]+3)
+
+ static const Py_UNICODE *
+ unicode_memchr(const Py_UNICODE *s, Py_UNICODE c, size_t n)
+ {
+ int i;
+ for (i = 0; i<n; ++i)
+ if (s[i]==c)
+ return s+i;
+ return NULL;
+ }
+
+ /* externally visible for str.strip(unicode) */
+ PyObject *
+ _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj)
+ {
+ Py_UNICODE *s = PyUnicode_AS_UNICODE(self);
+ int len = PyUnicode_GET_SIZE(self);
+ Py_UNICODE *sep = PyUnicode_AS_UNICODE(sepobj);
+ int seplen = PyUnicode_GET_SIZE(sepobj);
+ int i, j;
+
+ i = 0;
+ if (striptype != RIGHTSTRIP) {
+ while (i < len && unicode_memchr(sep, s[i], seplen)) {
+ i++;
+ }
+ }
+
+ j = len;
+ if (striptype != LEFTSTRIP) {
+ do {
+ j--;
+ } while (j >= i && unicode_memchr(sep, s[j], seplen));
+ j++;
+ }
+
+ if (i == 0 && j == len && PyUnicode_CheckExact(self)) {
+ Py_INCREF(self);
+ return (PyObject*)self;
+ }
+ else
+ return PyUnicode_FromUnicode(s+i, j-i);
+ }
+
+
+ static PyObject *
+ do_strip(PyUnicodeObject *self, int striptype)
+ {
+ Py_UNICODE *s = PyUnicode_AS_UNICODE(self);
+ int len = PyUnicode_GET_SIZE(self), i, j;
+
+ i = 0;
+ if (striptype != RIGHTSTRIP) {
+ while (i < len && Py_UNICODE_ISSPACE(s[i])) {
+ i++;
+ }
+ }
+
+ j = len;
+ if (striptype != LEFTSTRIP) {
+ do {
+ j--;
+ } while (j >= i && Py_UNICODE_ISSPACE(s[j]));
+ j++;
+ }
+
+ if (i == 0 && j == len && PyUnicode_CheckExact(self)) {
+ Py_INCREF(self);
+ return (PyObject*)self;
+ }
+ else
+ return PyUnicode_FromUnicode(s+i, j-i);
+ }
+
+
+ static PyObject *
+ do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args)
+ {
+ PyObject *sep = NULL;
+
+ if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep))
+ return NULL;
+
+ if (sep != NULL && sep != Py_None) {
+ if (PyUnicode_Check(sep))
+ return _PyUnicode_XStrip(self, striptype, sep);
+ else if (PyString_Check(sep)) {
+ PyObject *res;
+ sep = PyUnicode_FromObject(sep);
+ if (sep==NULL)
+ return NULL;
+ res = _PyUnicode_XStrip(self, striptype, sep);
+ Py_DECREF(sep);
+ return res;
+ }
+ else {
+ PyErr_Format(PyExc_TypeError,
+ "%s arg must be None, unicode or str",
+ STRIPNAME(striptype));
+ return NULL;
+ }
+ }
+
+ return do_strip(self, striptype);
+ }
+
+
+ static char strip__doc__[] =
+ "S.strip([sep]) -> unicode\n\
+ \n\
+ Return a copy of the string S with leading and trailing\n\
+ whitespace removed.\n\
+ If sep is given and not None, remove characters in sep instead.\n\
+ If sep is a str, it will be converted to unicode before stripping";
+
+ static PyObject *
+ unicode_strip(PyUnicodeObject *self, PyObject *args)
+ {
+ if (PyTuple_GET_SIZE(args) == 0)
+ return do_strip(self, BOTHSTRIP); /* Common case */
+ else
+ return do_argstrip(self, BOTHSTRIP, args);
+ }
+
+
static char lstrip__doc__[] =
! "S.lstrip([sep]) -> unicode\n\
\n\
! Return a copy of the string S with leading whitespace removed.\n\
! If sep is given and not None, remove characters in sep instead.\n\
! If sep is a str, it will be converted to unicode before stripping";
static PyObject *
! unicode_lstrip(PyUnicodeObject *self, PyObject *args)
{
! if (PyTuple_GET_SIZE(args) == 0)
! return do_strip(self, LEFTSTRIP); /* Common case */
! else
! return do_argstrip(self, LEFTSTRIP, args);
! }
!
!
! static char rstrip__doc__[] =
! "S.rstrip([sep]) -> unicode\n\
! \n\
! Return a copy of the string S with trailing whitespace removed.\n\
! If sep is given and not None, remove characters in sep instead.\n\
! If sep is a str, it will be converted to unicode before stripping";
!
! static PyObject *
! unicode_rstrip(PyUnicodeObject *self, PyObject *args)
! {
! if (PyTuple_GET_SIZE(args) == 0)
! return do_strip(self, RIGHTSTRIP); /* Common case */
! else
! return do_argstrip(self, RIGHTSTRIP, args);
}
+
static PyObject*
unicode_repeat(PyUnicodeObject *str, int len)
***************
*** 4678,4692 ****
}
- static char rstrip__doc__[] =
- "S.rstrip() -> unicode\n\
- \n\
- Return a copy of the string S with trailing whitespace removed.";
-
- static PyObject *
- unicode_rstrip(PyUnicodeObject *self)
- {
- return strip(self, 0, 1);
- }
-
static PyObject*
unicode_slice(PyUnicodeObject *self, int start, int end)
--- 4805,4808 ----
***************
*** 4784,4798 ****
}
- static char strip__doc__[] =
- "S.strip() -> unicode\n\
- \n\
- Return a copy of S with leading and trailing whitespace removed.";
-
- static PyObject *
- unicode_strip(PyUnicodeObject *self)
- {
- return strip(self, 1, 1);
- }
-
static char swapcase__doc__[] =
"S.swapcase() -> unicode\n\
--- 4900,4903 ----
***************
*** 4967,4978 ****
{"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__},
{"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__},
! {"lstrip", (PyCFunction) unicode_lstrip, METH_NOARGS, lstrip__doc__},
/* {"maketrans", (PyCFunction) unicode_maketrans, METH_VARARGS, maketrans__doc__}, */
{"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__},
{"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__},
{"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__},
! {"rstrip", (PyCFunction) unicode_rstrip, METH_NOARGS, rstrip__doc__},
{"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS, splitlines__doc__},
! {"strip", (PyCFunction) unicode_strip, METH_NOARGS, strip__doc__},
{"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__},
{"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__},
--- 5072,5083 ----
{"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__},
{"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__},
! {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__},
/* {"maketrans", (PyCFunction) unicode_maketrans, METH_VARARGS, maketrans__doc__}, */
{"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__},
{"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__},
{"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__},
! {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__},
{"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS, splitlines__doc__},
! {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__},
{"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__},
{"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__},