Skip to content

Commit a6ca51e

Browse files
author
fredrik.lundh
committed
needforspeed: stringlib refactoring (in progress)
git-svn-id: http://svn.python.org/projects/python/trunk@46364 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent c4ca3f8 commit a6ca51e

File tree

3 files changed

+71
-77
lines changed

3 files changed

+71
-77
lines changed

Objects/stringlib/partition.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/* stringlib: partition implementation */
2+
3+
#ifndef STRINGLIB_PARTITION_H
4+
#define STRINGLIB_PARTITION_H
5+
6+
#include "stringlib/fastsearch.h"
7+
8+
Py_LOCAL(PyObject*)
9+
partition(PyObject* str_obj, const STRINGLIB_CHAR* str, Py_ssize_t str_len,
10+
PyObject* sep_obj, const STRINGLIB_CHAR* sep, Py_ssize_t sep_len)
11+
{
12+
PyObject* out;
13+
Py_ssize_t pos;
14+
15+
if (sep_len == 0) {
16+
PyErr_SetString(PyExc_ValueError, "empty separator");
17+
return NULL;
18+
}
19+
20+
out = PyTuple_New(3);
21+
if (!out)
22+
return NULL;
23+
24+
pos = fastsearch(str, str_len, sep, sep_len, FAST_SEARCH);
25+
26+
if (pos < 0) {
27+
Py_INCREF(str_obj);
28+
PyTuple_SET_ITEM(out, 0, (PyObject*) str_obj);
29+
Py_INCREF(STRINGLIB_EMPTY);
30+
PyTuple_SET_ITEM(out, 1, (PyObject*) STRINGLIB_EMPTY);
31+
Py_INCREF(STRINGLIB_EMPTY);
32+
PyTuple_SET_ITEM(out, 2, (PyObject*) STRINGLIB_EMPTY);
33+
return out;
34+
}
35+
36+
PyTuple_SET_ITEM(out, 0, STRINGLIB_NEW(str, pos));
37+
Py_INCREF(sep_obj);
38+
PyTuple_SET_ITEM(out, 1, sep_obj);
39+
pos += sep_len;
40+
PyTuple_SET_ITEM(out, 2, STRINGLIB_NEW(str + pos, str_len - pos));
41+
42+
if (PyErr_Occurred()) {
43+
Py_DECREF(out);
44+
return NULL;
45+
}
46+
47+
return out;
48+
}
49+
50+
#endif

Objects/stringobject.c

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -772,8 +772,11 @@ PyString_AsStringAndSize(register PyObject *obj,
772772
#ifdef USE_FAST
773773

774774
#define STRINGLIB_CHAR char
775+
#define STRINGLIB_NEW PyString_FromStringAndSize
776+
#define STRINGLIB_EMPTY nullstring
775777

776778
#include "stringlib/fastsearch.h"
779+
#include "stringlib/partition.h"
777780

778781
#endif
779782

@@ -1541,9 +1544,8 @@ found, returns S and two empty strings.");
15411544
static PyObject *
15421545
string_partition(PyStringObject *self, PyObject *sep_obj)
15431546
{
1544-
Py_ssize_t len = PyString_GET_SIZE(self), sep_len, pos;
1547+
Py_ssize_t str_len = PyString_GET_SIZE(self), sep_len;
15451548
const char *str = PyString_AS_STRING(self), *sep;
1546-
PyObject * out;
15471549

15481550
if (PyString_Check(sep_obj)) {
15491551
sep = PyString_AS_STRING(sep_obj);
@@ -1556,38 +1558,7 @@ string_partition(PyStringObject *self, PyObject *sep_obj)
15561558
else if (PyObject_AsCharBuffer(sep_obj, &sep, &sep_len))
15571559
return NULL;
15581560

1559-
if (sep_len == 0) {
1560-
PyErr_SetString(PyExc_ValueError, "empty separator");
1561-
return NULL;
1562-
}
1563-
1564-
out = PyTuple_New(3);
1565-
if (!out)
1566-
return NULL;
1567-
1568-
pos = fastsearch(str, len, sep, sep_len, FAST_SEARCH);
1569-
if (pos < 0) {
1570-
Py_INCREF(self);
1571-
PyTuple_SET_ITEM(out, 0, (PyObject*) self);
1572-
Py_INCREF(nullstring);
1573-
PyTuple_SET_ITEM(out, 1, (PyObject*) nullstring);
1574-
Py_INCREF(nullstring);
1575-
PyTuple_SET_ITEM(out, 2, (PyObject*) nullstring);
1576-
} else {
1577-
PyObject* obj;
1578-
PyTuple_SET_ITEM(out, 0, PyString_FromStringAndSize(str, pos));
1579-
Py_INCREF(sep_obj);
1580-
PyTuple_SET_ITEM(out, 1, sep_obj);
1581-
pos += sep_len;
1582-
obj = PyString_FromStringAndSize(str + pos, len - pos);
1583-
PyTuple_SET_ITEM(out, 2, obj);
1584-
if (PyErr_Occurred()) {
1585-
Py_DECREF(out);
1586-
return NULL;
1587-
}
1588-
}
1589-
1590-
return out;
1561+
return partition((PyObject*)self, str, str_len, sep_obj, sep, sep_len);
15911562
}
15921563

15931564
Py_LOCAL(PyObject *)

Objects/unicodeobject.c

Lines changed: 16 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3856,7 +3856,13 @@ int PyUnicode_EncodeDecimal(Py_UNICODE *s,
38563856

38573857
#define STRINGLIB_CHAR Py_UNICODE
38583858

3859+
#define STRINGLIB_NEW PyUnicode_FromUnicode
3860+
3861+
#define STRINGLIB_EMPTY unicode_empty
3862+
38593863
#include "stringlib/fastsearch.h"
3864+
#include "stringlib/partition.h"
3865+
38603866

38613867
Py_LOCAL(Py_ssize_t) count(PyUnicodeObject *self,
38623868
Py_ssize_t start,
@@ -6197,59 +6203,26 @@ PyUnicode_Partition(PyObject *str_in, PyObject *sep_in)
61976203
{
61986204
PyObject* str_obj;
61996205
PyObject* sep_obj;
6200-
Py_UNICODE *str, *sep;
6201-
Py_ssize_t len, sep_len, pos;
62026206
PyObject* out;
6203-
6207+
62046208
str_obj = PyUnicode_FromObject(str_in);
62056209
if (!str_obj)
62066210
return NULL;
62076211
sep_obj = PyUnicode_FromObject(sep_in);
6208-
if (!sep_obj)
6209-
goto error;
6210-
6211-
str = PyUnicode_AS_UNICODE(str_obj);
6212-
len = PyUnicode_GET_SIZE(str_obj);
6213-
6214-
sep = PyUnicode_AS_UNICODE(sep_obj);
6215-
sep_len = PyUnicode_GET_SIZE(sep_obj);
6216-
6217-
if (sep_len == 0) {
6218-
PyErr_SetString(PyExc_ValueError, "empty separator");
6219-
goto error;
6212+
if (!sep_obj) {
6213+
Py_DECREF(str_obj);
6214+
return NULL;
62206215
}
62216216

6222-
out = PyTuple_New(3);
6223-
if (!out)
6224-
goto error;
6217+
out = partition(
6218+
str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj),
6219+
sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj)
6220+
);
62256221

6226-
pos = fastsearch(str, len, sep, sep_len, FAST_SEARCH);
6227-
if (pos < 0) {
6228-
Py_INCREF(str_obj);
6229-
PyTuple_SET_ITEM(out, 0, (PyObject*) str_obj);
6230-
Py_INCREF(unicode_empty);
6231-
PyTuple_SET_ITEM(out, 1, (PyObject*) unicode_empty);
6232-
Py_INCREF(unicode_empty);
6233-
PyTuple_SET_ITEM(out, 2, (PyObject*) unicode_empty);
6234-
} else {
6235-
PyObject* obj;
6236-
PyTuple_SET_ITEM(out, 0, PyUnicode_FromUnicode(str, pos));
6237-
Py_INCREF(sep_obj);
6238-
PyTuple_SET_ITEM(out, 1, sep_obj);
6239-
obj = PyUnicode_FromUnicode(str + sep_len + pos, len - sep_len - pos);
6240-
PyTuple_SET_ITEM(out, 2, obj);
6241-
if (PyErr_Occurred()) {
6242-
Py_DECREF(out);
6243-
goto error;
6244-
}
6245-
}
6222+
Py_DECREF(sep_obj);
6223+
Py_DECREF(str_obj);
62466224

62476225
return out;
6248-
6249-
error:
6250-
Py_XDECREF(sep_obj);
6251-
Py_DECREF(str_obj);
6252-
return NULL;
62536226
}
62546227

62556228
PyDoc_STRVAR(partition__doc__,

0 commit comments

Comments
 (0)