[Python-checkins] r46468 - in python/trunk/Objects: stringlib/count.h stringlib/fastsearch.h stringlib/find.h stringlib/partition.h stringobject.c unicodeobject.c

fredrik.lundh python-checkins at python.org
Sat May 27 16:58:21 CEST 2006


Author: fredrik.lundh
Date: Sat May 27 16:58:20 2006
New Revision: 46468

Modified:
   python/trunk/Objects/stringlib/count.h
   python/trunk/Objects/stringlib/fastsearch.h
   python/trunk/Objects/stringlib/find.h
   python/trunk/Objects/stringlib/partition.h
   python/trunk/Objects/stringobject.c
   python/trunk/Objects/unicodeobject.c
Log:
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
where appropriate



Modified: python/trunk/Objects/stringlib/count.h
==============================================================================
--- python/trunk/Objects/stringlib/count.h	(original)
+++ python/trunk/Objects/stringlib/count.h	Sat May 27 16:58:20 2006
@@ -7,7 +7,7 @@
 #error must include "stringlib/fastsearch.h" before including this module
 #endif
 
-Py_LOCAL(Py_ssize_t)
+Py_LOCAL_INLINE(Py_ssize_t)
 stringlib_count(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
                 const STRINGLIB_CHAR* sub, Py_ssize_t sub_len)
 {

Modified: python/trunk/Objects/stringlib/fastsearch.h
==============================================================================
--- python/trunk/Objects/stringlib/fastsearch.h	(original)
+++ python/trunk/Objects/stringlib/fastsearch.h	Sat May 27 16:58:20 2006
@@ -17,7 +17,7 @@
 #define FAST_COUNT 0
 #define FAST_SEARCH 1
 
-Py_LOCAL(Py_ssize_t)
+Py_LOCAL_INLINE(Py_ssize_t)
 fastsearch(const STRINGLIB_CHAR* s, Py_ssize_t n,
            const STRINGLIB_CHAR* p, Py_ssize_t m,
            int mode)

Modified: python/trunk/Objects/stringlib/find.h
==============================================================================
--- python/trunk/Objects/stringlib/find.h	(original)
+++ python/trunk/Objects/stringlib/find.h	Sat May 27 16:58:20 2006
@@ -7,7 +7,7 @@
 #error must include "stringlib/fastsearch.h" before including this module
 #endif
 
-Py_LOCAL(Py_ssize_t)
+Py_LOCAL_INLINE(Py_ssize_t)
 stringlib_find(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
                const STRINGLIB_CHAR* sub, Py_ssize_t sub_len,
                Py_ssize_t offset)
@@ -25,7 +25,7 @@
     return pos;
 }
 
-Py_LOCAL(Py_ssize_t)
+Py_LOCAL_INLINE(Py_ssize_t)
 stringlib_rfind(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
                 const STRINGLIB_CHAR* sub, Py_ssize_t sub_len,
                 Py_ssize_t offset)
@@ -50,7 +50,7 @@
 
 #ifdef STRINGLIB_STR
 
-Py_LOCAL(Py_ssize_t)
+Py_LOCAL_INLINE(Py_ssize_t)
 stringlib_find_obj(PyObject* str, PyObject* sub,
                    Py_ssize_t start, Py_ssize_t end)
 {
@@ -60,7 +60,7 @@
         );
 }
 
-Py_LOCAL(int)
+Py_LOCAL_INLINE(int)
 stringlib_contains_obj(PyObject* str, PyObject* sub)
 {
     return stringlib_find(
@@ -69,7 +69,7 @@
         ) != -1;
 }
 
-Py_LOCAL(Py_ssize_t)
+Py_LOCAL_INLINE(Py_ssize_t)
 stringlib_rfind_obj(PyObject* str, PyObject* sub,
                     Py_ssize_t start, Py_ssize_t end)
 {

Modified: python/trunk/Objects/stringlib/partition.h
==============================================================================
--- python/trunk/Objects/stringlib/partition.h	(original)
+++ python/trunk/Objects/stringlib/partition.h	Sat May 27 16:58:20 2006
@@ -7,7 +7,7 @@
 #error must include "stringlib/fastsearch.h" before including this module
 #endif
 
-Py_LOCAL(PyObject*)
+Py_LOCAL_INLINE(PyObject*)
 stringlib_partition(
     PyObject* str_obj, const STRINGLIB_CHAR* str, Py_ssize_t str_len,
     PyObject* sep_obj, const STRINGLIB_CHAR* sep, Py_ssize_t sep_len
@@ -51,7 +51,7 @@
     return out;
 }
 
-Py_LOCAL(PyObject*)
+Py_LOCAL_INLINE(PyObject*)
 stringlib_rpartition(
     PyObject* str_obj, const STRINGLIB_CHAR* str, Py_ssize_t str_len,
     PyObject* sep_obj, const STRINGLIB_CHAR* sep, Py_ssize_t sep_len

Modified: python/trunk/Objects/stringobject.c
==============================================================================
--- python/trunk/Objects/stringobject.c	(original)
+++ python/trunk/Objects/stringobject.c	Sat May 27 16:58:20 2006
@@ -1371,7 +1371,7 @@
 #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--; }
 
-Py_LOCAL(PyObject *)
+Py_LOCAL_INLINE(PyObject *)
 split_whitespace(const char *s, Py_ssize_t len, Py_ssize_t maxsplit)
 {
 	Py_ssize_t i, j, count=0;
@@ -1405,7 +1405,7 @@
 	return NULL;
 }
 
-Py_LOCAL(PyObject *)
+Py_LOCAL_INLINE(PyObject *)
 split_char(const char *s, Py_ssize_t len, char ch, Py_ssize_t maxcount)
 {
 	register Py_ssize_t i, j, count=0;
@@ -1578,7 +1578,7 @@
 		);
 }
 
-Py_LOCAL(PyObject *)
+Py_LOCAL_INLINE(PyObject *)
 rsplit_whitespace(const char *s, Py_ssize_t len, Py_ssize_t maxsplit)
 {
 	Py_ssize_t i, j, count=0;
@@ -1614,7 +1614,7 @@
 	return NULL;
 }
 
-Py_LOCAL(PyObject *)
+Py_LOCAL_INLINE(PyObject *)
 rsplit_char(const char *s, Py_ssize_t len, char ch, Py_ssize_t maxcount)
 {
 	register Py_ssize_t i, j, count=0;
@@ -1828,7 +1828,7 @@
 	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)
 {
 	if (*end > len)
@@ -1843,7 +1843,7 @@
 		*start = 0;
 }
 
-Py_LOCAL(Py_ssize_t)
+Py_LOCAL_INLINE(Py_ssize_t)
 string_find_internal(PyStringObject *self, PyObject *args, int dir)
 {
 	const char *s = PyString_AS_STRING(self), *sub;
@@ -1953,7 +1953,7 @@
 }
 
 
-Py_LOCAL(PyObject *)
+Py_LOCAL_INLINE(PyObject *)
 do_xstrip(PyStringObject *self, int striptype, PyObject *sepobj)
 {
 	char *s = PyString_AS_STRING(self);
@@ -1986,7 +1986,7 @@
 }
 
 
-Py_LOCAL(PyObject *)
+Py_LOCAL_INLINE(PyObject *)
 do_strip(PyStringObject *self, int striptype)
 {
 	char *s = PyString_AS_STRING(self);
@@ -2016,7 +2016,7 @@
 }
 
 
-Py_LOCAL(PyObject *)
+Py_LOCAL_INLINE(PyObject *)
 do_argstrip(PyStringObject *self, int striptype, PyObject *args)
 {
 	PyObject *sep = NULL;
@@ -2460,7 +2460,7 @@
 		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)
 {
 	Py_ssize_t count=0;
@@ -2514,7 +2514,7 @@
 	return -1;
 }
 
-Py_LOCAL(Py_ssize_t)
+Py_LOCAL_INLINE(Py_ssize_t)
 countstring(char *target, Py_ssize_t target_len,
 	    char *pattern, Py_ssize_t pattern_len,
 	    Py_ssize_t start,
@@ -3335,7 +3335,7 @@
     return u;
 }
 
-Py_LOCAL(PyObject *)
+Py_LOCAL_INLINE(PyObject *)
 pad(PyStringObject *self, Py_ssize_t left, Py_ssize_t right, char fill)
 {
     PyObject *u;
@@ -4096,7 +4096,7 @@
 
 /* Helpers for formatstring */
 
-Py_LOCAL(PyObject *)
+Py_LOCAL_INLINE(PyObject *)
 getnextarg(PyObject *args, Py_ssize_t arglen, Py_ssize_t *p_argidx)
 {
 	Py_ssize_t argidx = *p_argidx;
@@ -4125,7 +4125,7 @@
 #define F_ALT	(1<<3)
 #define F_ZERO	(1<<4)
 
-Py_LOCAL(int)
+Py_LOCAL_INLINE(int)
 formatfloat(char *buf, size_t buflen, int flags,
             int prec, int type, PyObject *v)
 {
@@ -4312,7 +4312,7 @@
 	return result;
 }
 
-Py_LOCAL(int)
+Py_LOCAL_INLINE(int)
 formatint(char *buf, size_t buflen, int flags,
           int prec, int type, PyObject *v)
 {
@@ -4384,7 +4384,7 @@
 	return (int)strlen(buf);
 }
 
-Py_LOCAL(int)
+Py_LOCAL_INLINE(int)
 formatchar(char *buf, size_t buflen, PyObject *v)
 {
 	/* presume that the buffer is at least 2 characters long */

Modified: python/trunk/Objects/unicodeobject.c
==============================================================================
--- python/trunk/Objects/unicodeobject.c	(original)
+++ python/trunk/Objects/unicodeobject.c	Sat May 27 16:58:20 2006
@@ -141,7 +141,7 @@
 #define BLOOM_LINEBREAK(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 */
 
@@ -155,7 +155,7 @@
     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;
 
@@ -2015,7 +2015,7 @@
 
 */
 
-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_UNICODE ch)
 {
@@ -3860,7 +3860,7 @@
 #define STRINGLIB_NEW PyUnicode_FromUnicode
 #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)
 {
     if (str[0] != other[0])
@@ -4710,7 +4710,7 @@
         }
     } else {
 
-        Py_ssize_t n, i;
+        Py_ssize_t n, i, j, e;
         Py_ssize_t product, new_size, delta;
         Py_UNICODE *p;
 
@@ -4743,21 +4743,35 @@
             return NULL;
         i = 0;
         p = u->str;
+        e = self->length - str1->length;
         if (str1->length > 0) {
-            while (i <= self->length - str1->length)
-                if (Py_UNICODE_MATCH(self, i, str1)) {
-                    /* replace string segment */
+            while (n-- > 0) {
+                /* look for next match */
+                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);
                     p += str2->length;
-                    i += str1->length;
-                    if (--n <= 0) {
-                        /* copy remaining part */
-                        Py_UNICODE_COPY(p, self->str+i, self->length-i);
-                        break;
-                    }
-                } else
-                    *p++ = self->str[i++];
+                }
+                i = j + str1->length;
+            }
+            if (i < self->length)
+                /* copy tail [i:] */
+                Py_UNICODE_COPY(p, self->str+i, self->length-i);
         } else {
+            /* interleave */
             while (n > 0) {
                 Py_UNICODE_COPY(p, str2->str, str2->length);
                 p += str2->length;


More information about the Python-checkins mailing list