summaryrefslogtreecommitdiffstats
path: root/Objects/stringobject.c
diff options
context:
space:
mode:
authorFredrik Lundh <fredrik@pythonware.com>2006-05-26 18:15:38 (GMT)
committerFredrik Lundh <fredrik@pythonware.com>2006-05-26 18:15:38 (GMT)
commitb3167cbcd76652f9f5f64196ce371562bcd8d42c (patch)
treeacf09b264580d12fa754dafd9cee44eee5ac236f /Objects/stringobject.c
parentbe9f219e40fb0b817bd248234a0b89727b1295e3 (diff)
downloadcpython-b3167cbcd76652f9f5f64196ce371562bcd8d42c.zip
cpython-b3167cbcd76652f9f5f64196ce371562bcd8d42c.tar.gz
cpython-b3167cbcd76652f9f5f64196ce371562bcd8d42c.tar.bz2
needforspeed: added rpartition implementation
Diffstat (limited to 'Objects/stringobject.c')
-rw-r--r--Objects/stringobject.c36
1 files changed, 36 insertions, 0 deletions
diff --git a/Objects/stringobject.c b/Objects/stringobject.c
index ded1907..44deeed 100644
--- a/Objects/stringobject.c
+++ b/Objects/stringobject.c
@@ -768,7 +768,10 @@ PyString_AsStringAndSize(register PyObject *obj,
/* stringlib components */
#define STRINGLIB_CHAR char
+
#define STRINGLIB_NEW PyString_FromStringAndSize
+#define STRINGLIB_CMP memcmp
+
#define STRINGLIB_EMPTY nullstring
#include "stringlib/fastsearch.h"
@@ -1530,6 +1533,37 @@ string_partition(PyStringObject *self, PyObject *sep_obj)
);
}
+PyDoc_STRVAR(rpartition__doc__,
+"S.rpartition(sep) -> (head, sep, tail)\n\
+\n\
+Searches for the separator sep in S, starting at the end of S, and returns\n\
+the part before it, the separator itself, and the part after it. If the\n\
+separator is not found, returns S and two empty strings.");
+
+static PyObject *
+string_rpartition(PyStringObject *self, PyObject *sep_obj)
+{
+ const char *sep;
+ Py_ssize_t sep_len;
+
+ if (PyString_Check(sep_obj)) {
+ sep = PyString_AS_STRING(sep_obj);
+ sep_len = PyString_GET_SIZE(sep_obj);
+ }
+#ifdef Py_USING_UNICODE
+ else if (PyUnicode_Check(sep_obj))
+ return PyUnicode_Partition((PyObject *) self, sep_obj);
+#endif
+ else if (PyObject_AsCharBuffer(sep_obj, &sep, &sep_len))
+ return NULL;
+
+ return rpartition(
+ (PyObject*) self,
+ PyString_AS_STRING(self), PyString_GET_SIZE(self),
+ sep_obj, sep, sep_len
+ );
+}
+
Py_LOCAL(PyObject *)
rsplit_whitespace(const char *s, Py_ssize_t len, Py_ssize_t maxsplit)
{
@@ -3810,6 +3844,8 @@ string_methods[] = {
{"rfind", (PyCFunction)string_rfind, METH_VARARGS, rfind__doc__},
{"rindex", (PyCFunction)string_rindex, METH_VARARGS, rindex__doc__},
{"rstrip", (PyCFunction)string_rstrip, METH_VARARGS, rstrip__doc__},
+ {"rpartition", (PyCFunction)string_rpartition, METH_O,
+ rpartition__doc__},
{"startswith", (PyCFunction)string_startswith, METH_VARARGS,
startswith__doc__},
{"strip", (PyCFunction)string_strip, METH_VARARGS, strip__doc__},