mirror of https://github.com/python/cpython
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
where appropriate
This commit is contained in:
parent
94b8c122fd
commit
c2d29c5a6d
|
@ -7,7 +7,7 @@
|
||||||
#error must include "stringlib/fastsearch.h" before including this module
|
#error must include "stringlib/fastsearch.h" before including this module
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
Py_LOCAL(Py_ssize_t)
|
Py_LOCAL_INLINE(Py_ssize_t)
|
||||||
stringlib_count(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
|
stringlib_count(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
|
||||||
const STRINGLIB_CHAR* sub, Py_ssize_t sub_len)
|
const STRINGLIB_CHAR* sub, Py_ssize_t sub_len)
|
||||||
{
|
{
|
||||||
|
|
|
@ -17,7 +17,7 @@
|
||||||
#define FAST_COUNT 0
|
#define FAST_COUNT 0
|
||||||
#define FAST_SEARCH 1
|
#define FAST_SEARCH 1
|
||||||
|
|
||||||
Py_LOCAL(Py_ssize_t)
|
Py_LOCAL_INLINE(Py_ssize_t)
|
||||||
fastsearch(const STRINGLIB_CHAR* s, Py_ssize_t n,
|
fastsearch(const STRINGLIB_CHAR* s, Py_ssize_t n,
|
||||||
const STRINGLIB_CHAR* p, Py_ssize_t m,
|
const STRINGLIB_CHAR* p, Py_ssize_t m,
|
||||||
int mode)
|
int mode)
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
#error must include "stringlib/fastsearch.h" before including this module
|
#error must include "stringlib/fastsearch.h" before including this module
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
Py_LOCAL(Py_ssize_t)
|
Py_LOCAL_INLINE(Py_ssize_t)
|
||||||
stringlib_find(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
|
stringlib_find(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
|
||||||
const STRINGLIB_CHAR* sub, Py_ssize_t sub_len,
|
const STRINGLIB_CHAR* sub, Py_ssize_t sub_len,
|
||||||
Py_ssize_t offset)
|
Py_ssize_t offset)
|
||||||
|
@ -25,7 +25,7 @@ stringlib_find(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
|
||||||
return pos;
|
return pos;
|
||||||
}
|
}
|
||||||
|
|
||||||
Py_LOCAL(Py_ssize_t)
|
Py_LOCAL_INLINE(Py_ssize_t)
|
||||||
stringlib_rfind(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
|
stringlib_rfind(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
|
||||||
const STRINGLIB_CHAR* sub, Py_ssize_t sub_len,
|
const STRINGLIB_CHAR* sub, Py_ssize_t sub_len,
|
||||||
Py_ssize_t offset)
|
Py_ssize_t offset)
|
||||||
|
@ -50,7 +50,7 @@ stringlib_rfind(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
|
||||||
|
|
||||||
#ifdef STRINGLIB_STR
|
#ifdef STRINGLIB_STR
|
||||||
|
|
||||||
Py_LOCAL(Py_ssize_t)
|
Py_LOCAL_INLINE(Py_ssize_t)
|
||||||
stringlib_find_obj(PyObject* str, PyObject* sub,
|
stringlib_find_obj(PyObject* str, PyObject* sub,
|
||||||
Py_ssize_t start, Py_ssize_t end)
|
Py_ssize_t start, Py_ssize_t end)
|
||||||
{
|
{
|
||||||
|
@ -60,7 +60,7 @@ stringlib_find_obj(PyObject* str, PyObject* sub,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Py_LOCAL(int)
|
Py_LOCAL_INLINE(int)
|
||||||
stringlib_contains_obj(PyObject* str, PyObject* sub)
|
stringlib_contains_obj(PyObject* str, PyObject* sub)
|
||||||
{
|
{
|
||||||
return stringlib_find(
|
return stringlib_find(
|
||||||
|
@ -69,7 +69,7 @@ stringlib_contains_obj(PyObject* str, PyObject* sub)
|
||||||
) != -1;
|
) != -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
Py_LOCAL(Py_ssize_t)
|
Py_LOCAL_INLINE(Py_ssize_t)
|
||||||
stringlib_rfind_obj(PyObject* str, PyObject* sub,
|
stringlib_rfind_obj(PyObject* str, PyObject* sub,
|
||||||
Py_ssize_t start, Py_ssize_t end)
|
Py_ssize_t start, Py_ssize_t end)
|
||||||
{
|
{
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
#error must include "stringlib/fastsearch.h" before including this module
|
#error must include "stringlib/fastsearch.h" before including this module
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
Py_LOCAL(PyObject*)
|
Py_LOCAL_INLINE(PyObject*)
|
||||||
stringlib_partition(
|
stringlib_partition(
|
||||||
PyObject* str_obj, const STRINGLIB_CHAR* str, Py_ssize_t str_len,
|
PyObject* str_obj, const STRINGLIB_CHAR* str, Py_ssize_t str_len,
|
||||||
PyObject* sep_obj, const STRINGLIB_CHAR* sep, Py_ssize_t sep_len
|
PyObject* sep_obj, const STRINGLIB_CHAR* sep, Py_ssize_t sep_len
|
||||||
|
@ -51,7 +51,7 @@ stringlib_partition(
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
Py_LOCAL(PyObject*)
|
Py_LOCAL_INLINE(PyObject*)
|
||||||
stringlib_rpartition(
|
stringlib_rpartition(
|
||||||
PyObject* str_obj, const STRINGLIB_CHAR* str, Py_ssize_t str_len,
|
PyObject* str_obj, const STRINGLIB_CHAR* str, Py_ssize_t str_len,
|
||||||
PyObject* sep_obj, const STRINGLIB_CHAR* sep, Py_ssize_t sep_len
|
PyObject* sep_obj, const STRINGLIB_CHAR* sep, Py_ssize_t sep_len
|
||||||
|
|
|
@ -1371,7 +1371,7 @@ static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"};
|
||||||
#define RSKIP_SPACE(s, i) { while (i>=0 && isspace(Py_CHARMASK(s[i]))) i--; }
|
#define RSKIP_SPACE(s, i) { while (i>=0 && isspace(Py_CHARMASK(s[i]))) i--; }
|
||||||
#define RSKIP_NONSPACE(s, i) { while (i>=0 && !isspace(Py_CHARMASK(s[i]))) i--; }
|
#define RSKIP_NONSPACE(s, i) { while (i>=0 && !isspace(Py_CHARMASK(s[i]))) i--; }
|
||||||
|
|
||||||
Py_LOCAL(PyObject *)
|
Py_LOCAL_INLINE(PyObject *)
|
||||||
split_whitespace(const char *s, Py_ssize_t len, Py_ssize_t maxsplit)
|
split_whitespace(const char *s, Py_ssize_t len, Py_ssize_t maxsplit)
|
||||||
{
|
{
|
||||||
Py_ssize_t i, j, count=0;
|
Py_ssize_t i, j, count=0;
|
||||||
|
@ -1405,7 +1405,7 @@ split_whitespace(const char *s, Py_ssize_t len, Py_ssize_t maxsplit)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
Py_LOCAL(PyObject *)
|
Py_LOCAL_INLINE(PyObject *)
|
||||||
split_char(const char *s, Py_ssize_t len, char ch, Py_ssize_t maxcount)
|
split_char(const char *s, Py_ssize_t len, char ch, Py_ssize_t maxcount)
|
||||||
{
|
{
|
||||||
register Py_ssize_t i, j, count=0;
|
register Py_ssize_t i, j, count=0;
|
||||||
|
@ -1578,7 +1578,7 @@ string_rpartition(PyStringObject *self, PyObject *sep_obj)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Py_LOCAL(PyObject *)
|
Py_LOCAL_INLINE(PyObject *)
|
||||||
rsplit_whitespace(const char *s, Py_ssize_t len, Py_ssize_t maxsplit)
|
rsplit_whitespace(const char *s, Py_ssize_t len, Py_ssize_t maxsplit)
|
||||||
{
|
{
|
||||||
Py_ssize_t i, j, count=0;
|
Py_ssize_t i, j, count=0;
|
||||||
|
@ -1614,7 +1614,7 @@ rsplit_whitespace(const char *s, Py_ssize_t len, Py_ssize_t maxsplit)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
Py_LOCAL(PyObject *)
|
Py_LOCAL_INLINE(PyObject *)
|
||||||
rsplit_char(const char *s, Py_ssize_t len, char ch, Py_ssize_t maxcount)
|
rsplit_char(const char *s, Py_ssize_t len, char ch, Py_ssize_t maxcount)
|
||||||
{
|
{
|
||||||
register Py_ssize_t i, j, count=0;
|
register Py_ssize_t i, j, count=0;
|
||||||
|
@ -1828,7 +1828,7 @@ _PyString_Join(PyObject *sep, PyObject *x)
|
||||||
return string_join((PyStringObject *)sep, x);
|
return string_join((PyStringObject *)sep, x);
|
||||||
}
|
}
|
||||||
|
|
||||||
Py_LOCAL(void)
|
Py_LOCAL_INLINE(void)
|
||||||
string_adjust_indices(Py_ssize_t *start, Py_ssize_t *end, Py_ssize_t len)
|
string_adjust_indices(Py_ssize_t *start, Py_ssize_t *end, Py_ssize_t len)
|
||||||
{
|
{
|
||||||
if (*end > len)
|
if (*end > len)
|
||||||
|
@ -1843,7 +1843,7 @@ string_adjust_indices(Py_ssize_t *start, Py_ssize_t *end, Py_ssize_t len)
|
||||||
*start = 0;
|
*start = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
Py_LOCAL(Py_ssize_t)
|
Py_LOCAL_INLINE(Py_ssize_t)
|
||||||
string_find_internal(PyStringObject *self, PyObject *args, int dir)
|
string_find_internal(PyStringObject *self, PyObject *args, int dir)
|
||||||
{
|
{
|
||||||
const char *s = PyString_AS_STRING(self), *sub;
|
const char *s = PyString_AS_STRING(self), *sub;
|
||||||
|
@ -1953,7 +1953,7 @@ string_rindex(PyStringObject *self, PyObject *args)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Py_LOCAL(PyObject *)
|
Py_LOCAL_INLINE(PyObject *)
|
||||||
do_xstrip(PyStringObject *self, int striptype, PyObject *sepobj)
|
do_xstrip(PyStringObject *self, int striptype, PyObject *sepobj)
|
||||||
{
|
{
|
||||||
char *s = PyString_AS_STRING(self);
|
char *s = PyString_AS_STRING(self);
|
||||||
|
@ -1986,7 +1986,7 @@ do_xstrip(PyStringObject *self, int striptype, PyObject *sepobj)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Py_LOCAL(PyObject *)
|
Py_LOCAL_INLINE(PyObject *)
|
||||||
do_strip(PyStringObject *self, int striptype)
|
do_strip(PyStringObject *self, int striptype)
|
||||||
{
|
{
|
||||||
char *s = PyString_AS_STRING(self);
|
char *s = PyString_AS_STRING(self);
|
||||||
|
@ -2016,7 +2016,7 @@ do_strip(PyStringObject *self, int striptype)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Py_LOCAL(PyObject *)
|
Py_LOCAL_INLINE(PyObject *)
|
||||||
do_argstrip(PyStringObject *self, int striptype, PyObject *args)
|
do_argstrip(PyStringObject *self, int striptype, PyObject *args)
|
||||||
{
|
{
|
||||||
PyObject *sep = NULL;
|
PyObject *sep = NULL;
|
||||||
|
@ -2460,7 +2460,7 @@ return_self(PyStringObject *self)
|
||||||
PyString_GET_SIZE(self));
|
PyString_GET_SIZE(self));
|
||||||
}
|
}
|
||||||
|
|
||||||
Py_LOCAL(Py_ssize_t)
|
Py_LOCAL_INLINE(Py_ssize_t)
|
||||||
countchar(char *target, int target_len, char c, Py_ssize_t maxcount)
|
countchar(char *target, int target_len, char c, Py_ssize_t maxcount)
|
||||||
{
|
{
|
||||||
Py_ssize_t count=0;
|
Py_ssize_t count=0;
|
||||||
|
@ -2514,7 +2514,7 @@ findstring(char *target, Py_ssize_t target_len,
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
Py_LOCAL(Py_ssize_t)
|
Py_LOCAL_INLINE(Py_ssize_t)
|
||||||
countstring(char *target, Py_ssize_t target_len,
|
countstring(char *target, Py_ssize_t target_len,
|
||||||
char *pattern, Py_ssize_t pattern_len,
|
char *pattern, Py_ssize_t pattern_len,
|
||||||
Py_ssize_t start,
|
Py_ssize_t start,
|
||||||
|
@ -3335,7 +3335,7 @@ string_expandtabs(PyStringObject *self, PyObject *args)
|
||||||
return u;
|
return u;
|
||||||
}
|
}
|
||||||
|
|
||||||
Py_LOCAL(PyObject *)
|
Py_LOCAL_INLINE(PyObject *)
|
||||||
pad(PyStringObject *self, Py_ssize_t left, Py_ssize_t right, char fill)
|
pad(PyStringObject *self, Py_ssize_t left, Py_ssize_t right, char fill)
|
||||||
{
|
{
|
||||||
PyObject *u;
|
PyObject *u;
|
||||||
|
@ -4096,7 +4096,7 @@ _PyString_Resize(PyObject **pv, Py_ssize_t newsize)
|
||||||
|
|
||||||
/* Helpers for formatstring */
|
/* Helpers for formatstring */
|
||||||
|
|
||||||
Py_LOCAL(PyObject *)
|
Py_LOCAL_INLINE(PyObject *)
|
||||||
getnextarg(PyObject *args, Py_ssize_t arglen, Py_ssize_t *p_argidx)
|
getnextarg(PyObject *args, Py_ssize_t arglen, Py_ssize_t *p_argidx)
|
||||||
{
|
{
|
||||||
Py_ssize_t argidx = *p_argidx;
|
Py_ssize_t argidx = *p_argidx;
|
||||||
|
@ -4125,7 +4125,7 @@ getnextarg(PyObject *args, Py_ssize_t arglen, Py_ssize_t *p_argidx)
|
||||||
#define F_ALT (1<<3)
|
#define F_ALT (1<<3)
|
||||||
#define F_ZERO (1<<4)
|
#define F_ZERO (1<<4)
|
||||||
|
|
||||||
Py_LOCAL(int)
|
Py_LOCAL_INLINE(int)
|
||||||
formatfloat(char *buf, size_t buflen, int flags,
|
formatfloat(char *buf, size_t buflen, int flags,
|
||||||
int prec, int type, PyObject *v)
|
int prec, int type, PyObject *v)
|
||||||
{
|
{
|
||||||
|
@ -4312,7 +4312,7 @@ _PyString_FormatLong(PyObject *val, int flags, int prec, int type,
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
Py_LOCAL(int)
|
Py_LOCAL_INLINE(int)
|
||||||
formatint(char *buf, size_t buflen, int flags,
|
formatint(char *buf, size_t buflen, int flags,
|
||||||
int prec, int type, PyObject *v)
|
int prec, int type, PyObject *v)
|
||||||
{
|
{
|
||||||
|
@ -4384,7 +4384,7 @@ formatint(char *buf, size_t buflen, int flags,
|
||||||
return (int)strlen(buf);
|
return (int)strlen(buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
Py_LOCAL(int)
|
Py_LOCAL_INLINE(int)
|
||||||
formatchar(char *buf, size_t buflen, PyObject *v)
|
formatchar(char *buf, size_t buflen, PyObject *v)
|
||||||
{
|
{
|
||||||
/* presume that the buffer is at least 2 characters long */
|
/* presume that the buffer is at least 2 characters long */
|
||||||
|
|
|
@ -141,7 +141,7 @@ static BLOOM_MASK bloom_linebreak;
|
||||||
#define BLOOM_LINEBREAK(ch)\
|
#define BLOOM_LINEBREAK(ch)\
|
||||||
(BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK((ch)))
|
(BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK((ch)))
|
||||||
|
|
||||||
Py_LOCAL(BLOOM_MASK) make_bloom_mask(Py_UNICODE* ptr, Py_ssize_t len)
|
Py_LOCAL_INLINE(BLOOM_MASK) make_bloom_mask(Py_UNICODE* ptr, Py_ssize_t len)
|
||||||
{
|
{
|
||||||
/* calculate simple bloom-style bitmask for a given unicode string */
|
/* calculate simple bloom-style bitmask for a given unicode string */
|
||||||
|
|
||||||
|
@ -155,7 +155,7 @@ Py_LOCAL(BLOOM_MASK) make_bloom_mask(Py_UNICODE* ptr, Py_ssize_t len)
|
||||||
return mask;
|
return mask;
|
||||||
}
|
}
|
||||||
|
|
||||||
Py_LOCAL(int) unicode_member(Py_UNICODE chr, Py_UNICODE* set, Py_ssize_t setlen)
|
Py_LOCAL_INLINE(int) unicode_member(Py_UNICODE chr, Py_UNICODE* set, Py_ssize_t setlen)
|
||||||
{
|
{
|
||||||
Py_ssize_t i;
|
Py_ssize_t i;
|
||||||
|
|
||||||
|
@ -2015,7 +2015,7 @@ onError:
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Py_LOCAL(const Py_UNICODE *) findchar(const Py_UNICODE *s,
|
Py_LOCAL_INLINE(const Py_UNICODE *) findchar(const Py_UNICODE *s,
|
||||||
Py_ssize_t size,
|
Py_ssize_t size,
|
||||||
Py_UNICODE ch)
|
Py_UNICODE ch)
|
||||||
{
|
{
|
||||||
|
@ -3860,7 +3860,7 @@ int PyUnicode_EncodeDecimal(Py_UNICODE *s,
|
||||||
#define STRINGLIB_NEW PyUnicode_FromUnicode
|
#define STRINGLIB_NEW PyUnicode_FromUnicode
|
||||||
#define STRINGLIB_STR PyUnicode_AS_UNICODE
|
#define STRINGLIB_STR PyUnicode_AS_UNICODE
|
||||||
|
|
||||||
Py_LOCAL(int)
|
Py_LOCAL_INLINE(int)
|
||||||
STRINGLIB_CMP(const Py_UNICODE* str, const Py_UNICODE* other, Py_ssize_t len)
|
STRINGLIB_CMP(const Py_UNICODE* str, const Py_UNICODE* other, Py_ssize_t len)
|
||||||
{
|
{
|
||||||
if (str[0] != other[0])
|
if (str[0] != other[0])
|
||||||
|
@ -4710,7 +4710,7 @@ PyObject *replace(PyUnicodeObject *self,
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
Py_ssize_t n, i;
|
Py_ssize_t n, i, j, e;
|
||||||
Py_ssize_t product, new_size, delta;
|
Py_ssize_t product, new_size, delta;
|
||||||
Py_UNICODE *p;
|
Py_UNICODE *p;
|
||||||
|
|
||||||
|
@ -4743,21 +4743,35 @@ PyObject *replace(PyUnicodeObject *self,
|
||||||
return NULL;
|
return NULL;
|
||||||
i = 0;
|
i = 0;
|
||||||
p = u->str;
|
p = u->str;
|
||||||
|
e = self->length - str1->length;
|
||||||
if (str1->length > 0) {
|
if (str1->length > 0) {
|
||||||
while (i <= self->length - str1->length)
|
while (n-- > 0) {
|
||||||
if (Py_UNICODE_MATCH(self, i, str1)) {
|
/* look for next match */
|
||||||
/* replace string segment */
|
j = i;
|
||||||
|
while (j <= e) {
|
||||||
|
if (Py_UNICODE_MATCH(self, j, str1))
|
||||||
|
break;
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
if (j > i) {
|
||||||
|
if (j > e)
|
||||||
|
break;
|
||||||
|
/* copy unchanged part [i:j] */
|
||||||
|
Py_UNICODE_COPY(p, self->str+i, j-i);
|
||||||
|
p += j - i;
|
||||||
|
}
|
||||||
|
/* copy substitution string */
|
||||||
|
if (str2->length > 0) {
|
||||||
Py_UNICODE_COPY(p, str2->str, str2->length);
|
Py_UNICODE_COPY(p, str2->str, str2->length);
|
||||||
p += str2->length;
|
p += str2->length;
|
||||||
i += str1->length;
|
}
|
||||||
if (--n <= 0) {
|
i = j + str1->length;
|
||||||
/* copy remaining part */
|
}
|
||||||
Py_UNICODE_COPY(p, self->str+i, self->length-i);
|
if (i < self->length)
|
||||||
break;
|
/* copy tail [i:] */
|
||||||
}
|
Py_UNICODE_COPY(p, self->str+i, self->length-i);
|
||||||
} else
|
|
||||||
*p++ = self->str[i++];
|
|
||||||
} else {
|
} else {
|
||||||
|
/* interleave */
|
||||||
while (n > 0) {
|
while (n > 0) {
|
||||||
Py_UNICODE_COPY(p, str2->str, str2->length);
|
Py_UNICODE_COPY(p, str2->str, str2->length);
|
||||||
p += str2->length;
|
p += str2->length;
|
||||||
|
|
Loading…
Reference in New Issue