FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

needforspeed: more stringlib refactoring · broncodans/python@cda2ed2 · GitHub

forked from glix/python

Commit cda2ed2

Browse files
fredrik.lundh
committed
needforspeed: more stringlib refactoring
git-svn-id: http://svn.python.org/projects/python/trunk@46436 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent ccc6359 commit cda2ed2

4 files changed

Lines changed: 161 additions & 147 deletions

File tree

‎Objects/stringlib/README.txt‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,32 @@ possibly other modules, in a not too distant future).
33

44
the stuff in here is included into relevant places; see the individual
55
source files for details.
6+
7+
--------------------------------------------------------------------
8+
the following defines used by the different modules:
9+
10+
STRINGLIB_CHAR
11+
12+
the type used to hold a character (char or Py_UNICODE)
13+
14+
STRINGLIB_EMPTY
15+
16+
a PyObject representing the empty string
17+
18+
int STRINGLIB_CMP(STRINGLIB_CHAR*, STRINGLIB_CHAR*, Py_ssize_t)
19+
20+
compares two strings. returns 0 if they match, and non-zero if not.
21+
22+
Py_ssize_t STRINGLIB_LEN(PyObject*)
23+
24+
returns the length of the given string object (which must be of the
25+
right type)
26+
27+
PyObject* STRINGLIB_NEW(STRINGLIB_CHAR*, Py_ssize_t)
28+
29+
creates a new string object
30+
31+
STRINGLIB_CHAR* STRINGLIB_STR(PyObject*)
32+
33+
returns the pointer to the character data for the given string
34+
object (which must be of the right type)

‎Objects/stringlib/find.h‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,39 @@ stringlib_rfind(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
4848
return pos;
4949
}
5050

51+
#ifdef STRINGLIB_STR
52+
53+
Py_LOCAL(Py_ssize_t)
54+
stringlib_find_obj(PyObject* str, PyObject* sub,
55+
Py_ssize_t start, Py_ssize_t end)
56+
{
57+
return stringlib_find(
58+
STRINGLIB_STR(str) + start, end - start,
59+
STRINGLIB_STR(sub), STRINGLIB_LEN(sub), start
60+
);
61+
}
62+
63+
Py_LOCAL(int)
64+
stringlib_contains_obj(PyObject* str, PyObject* sub)
65+
{
66+
return stringlib_find(
67+
STRINGLIB_STR(str), STRINGLIB_LEN(str),
68+
STRINGLIB_STR(sub), STRINGLIB_LEN(sub), 0
69+
) != -1;
70+
}
71+
72+
Py_LOCAL(Py_ssize_t)
73+
stringlib_rfind_obj(PyObject* str, PyObject* sub,
74+
Py_ssize_t start, Py_ssize_t end)
75+
{
76+
return stringlib_rfind(
77+
STRINGLIB_STR(str) + start, end - start,
78+
STRINGLIB_STR(sub), STRINGLIB_LEN(sub), start
79+
);
80+
}
81+
82+
#endif
83+
5184
#endif
5285

5386
/*

‎Objects/stringobject.c‎

Lines changed: 39 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,9 @@ PyObject *PyString_DecodeEscape(const char *s,
690690
return NULL;
691691
}
692692

693+
/* -------------------------------------------------------------------- */
694+
/* object api */
695+
693696
static Py_ssize_t
694697
string_getsize(register PyObject *op)
695698
{
@@ -765,22 +768,23 @@ PyString_AsStringAndSize(register PyObject *obj,
765768
}
766769

767770
/* -------------------------------------------------------------------- */
768-
/* stringlib components */
771+
/* Methods */
769772

770773
#define STRINGLIB_CHAR char
771774

772-
#define STRINGLIB_NEW PyString_FromStringAndSize
773775
#define STRINGLIB_CMP memcmp
776+
#define STRINGLIB_LEN PyString_GET_SIZE
777+
#define STRINGLIB_NEW PyString_FromStringAndSize
778+
#define STRINGLIB_STR PyString_AS_STRING
774779

775780
#define STRINGLIB_EMPTY nullstring
776781

777782
#include "stringlib/fastsearch.h"
778783

784+
#include "stringlib/count.h"
779785
#include "stringlib/find.h"
780786
#include "stringlib/partition.h"
781787

782-
/* -------------------------------------------------------------------- */
783-
/* Methods */
784788

785789
static int
786790
string_print(PyStringObject *op, FILE *fp, int flags)
@@ -1048,49 +1052,36 @@ string_slice(register PyStringObject *a, register Py_ssize_t i,
10481052
}
10491053

10501054
static int
1051-
string_contains(PyObject *a, PyObject *el)
1055+
string_contains(PyObject *str_obj, PyObject *sub_obj)
10521056
{
1053-
char *s = PyString_AS_STRING(a);
1054-
const char *sub = PyString_AS_STRING(el);
1055-
Py_ssize_t len_sub = PyString_GET_SIZE(el);
1056-
Py_ssize_t pos;
1057-
1058-
if (!PyString_CheckExact(el)) {
1057+
if (!PyString_CheckExact(sub_obj)) {
10591058
#ifdef Py_USING_UNICODE
1060-
if (PyUnicode_Check(el))
1061-
return PyUnicode_Contains(a, el);
1059+
if (PyUnicode_Check(sub_obj))
1060+
return PyUnicode_Contains(str_obj, sub_obj);
10621061
#endif
1063-
if (!PyString_Check(el)) {
1062+
if (!PyString_Check(sub_obj)) {
10641063
PyErr_SetString(PyExc_TypeError,
10651064
"'in <string>' requires string as left operand");
10661065
return -1;
10671066
}
10681067
}
10691068

1070-
if (len_sub == 0)
1071-
return 1;
1072-
1073-
pos = fastsearch(
1074-
s, PyString_GET_SIZE(a),
1075-
sub, len_sub, FAST_SEARCH
1076-
);
1077-
1078-
return (pos != -1);
1069+
return stringlib_contains_obj(str_obj, sub_obj);
10791070
}
10801071

10811072
static PyObject *
10821073
string_item(PyStringObject *a, register Py_ssize_t i)
10831074
{
1075+
char pchar;
10841076
PyObject *v;
1085-
char *pchar;
10861077
if (i < 0 || i >= a->ob_size) {
10871078
PyErr_SetString(PyExc_IndexError, "string index out of range");
10881079
return NULL;
10891080
}
1090-
pchar = a->ob_sval + i;
1091-
v = (PyObject *)characters[*pchar & UCHAR_MAX];
1081+
pchar = a->ob_sval[i];
1082+
v = (PyObject *)characters[pchar & UCHAR_MAX];
10921083
if (v == NULL)
1093-
v = PyString_FromStringAndSize(pchar, 1);
1084+
v = PyString_FromStringAndSize(&pchar, 1);
10941085
else {
10951086
#ifdef COUNT_ALLOCS
10961087
one_strings++;
@@ -1166,9 +1157,8 @@ string_richcompare(PyStringObject *a, PyStringObject *b, int op)
11661157
int
11671158
_PyString_Eq(PyObject *o1, PyObject *o2)
11681159
{
1169-
PyStringObject *a, *b;
1170-
a = (PyStringObject*)o1;
1171-
b = (PyStringObject*)o2;
1160+
PyStringObject *a = (PyStringObject*) o1;
1161+
PyStringObject *b = (PyStringObject*) o2;
11721162
return a->ob_size == b->ob_size
11731163
&& *a->ob_sval == *b->ob_sval
11741164
&& memcmp(a->ob_sval, b->ob_sval, a->ob_size) == 0;
@@ -2264,43 +2254,37 @@ as in slice notation.");
22642254
static PyObject *
22652255
string_count(PyStringObject *self, PyObject *args)
22662256
{
2267-
const char *s = PyString_AS_STRING(self), *sub;
2268-
Py_ssize_t len = PyString_GET_SIZE(self), n;
2269-
Py_ssize_t i = 0, last = PY_SSIZE_T_MAX;
2270-
Py_ssize_t m, r;
2271-
PyObject *subobj;
2257+
PyObject *sub_obj;
2258+
const char *str = PyString_AS_STRING(self), *sub;
2259+
Py_ssize_t sub_len;
2260+
Py_ssize_t start = 0, end = PY_SSIZE_T_MAX;
22722261

2273-
if (!PyArg_ParseTuple(args, "O|O&O&:count", &subobj,
2274-
_PyEval_SliceIndex, &i, _PyEval_SliceIndex, &last))
2262+
if (!PyArg_ParseTuple(args, "O|O&O&:count", &sub_obj,
2263+
_PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end))
22752264
return NULL;
22762265

2277-
if (PyString_Check(subobj)) {
2278-
sub = PyString_AS_STRING(subobj);
2279-
n = PyString_GET_SIZE(subobj);
2266+
if (PyString_Check(sub_obj)) {
2267+
sub = PyString_AS_STRING(sub_obj);
2268+
sub_len = PyString_GET_SIZE(sub_obj);
22802269
}
22812270
#ifdef Py_USING_UNICODE
2282-
else if (PyUnicode_Check(subobj)) {
2271+
else if (PyUnicode_Check(sub_obj)) {
22832272
Py_ssize_t count;
2284-
count = PyUnicode_Count((PyObject *)self, subobj, i, last);
2273+
count = PyUnicode_Count((PyObject *)self, sub_obj, start, end);
22852274
if (count == -1)
22862275
return NULL;
22872276
else
2288-
return PyInt_FromLong((long) count);
2277+
return PyInt_FromSsize_t(count);
22892278
}
22902279
#endif
2291-
else if (PyObject_AsCharBuffer(subobj, &sub, &n))
2280+
else if (PyObject_AsCharBuffer(sub_obj, &sub, &sub_len))
22922281
return NULL;
22932282

2294-
string_adjust_indices(&i, &last, len);
2283+
string_adjust_indices(&start, &end, PyString_GET_SIZE(self));
22952284

2296-
m = last + 1 - n;
2297-
if (n == 0)
2298-
return PyInt_FromSsize_t(m-i);
2299-
2300-
r = fastsearch(s + i, last - i, sub, n, FAST_COUNT);
2301-
if (r < 0)
2302-
r = 0; /* no match */
2303-
return PyInt_FromSsize_t(r);
2285+
return PyInt_FromSsize_t(
2286+
stringlib_count(str + start, end - start, sub, sub_len)
2287+
);
23042288
}
23052289

23062290
PyDoc_STRVAR(swapcase__doc__,
@@ -2477,7 +2461,7 @@ return_self(PyStringObject *self)
24772461
}
24782462

24792463
Py_LOCAL(Py_ssize_t)
2480-
countchar(char *target, int target_len, char c, Py_ssize_t maxcount)
2464+
countchar(char *target, int target_len, char c, Py_ssize_t maxcount)
24812465
{
24822466
Py_ssize_t count=0;
24832467
char *start=target;
@@ -2580,7 +2564,7 @@ countstring(char *target, Py_ssize_t target_len,
25802564
}
25812565

25822566

2583-
/* Algorithms for difference cases of string replacement */
2567+
/* Algorithms for different cases of string replacement */
25842568

25852569
/* len(self)>=1, from="", len(to)>=1, maxcount>=1 */
25862570
Py_LOCAL(PyStringObject *)

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL