mirror of
				https://github.com/python/cpython.git
				synced 2025-10-31 05:31:20 +00:00 
			
		
		
		
	needforspeed: stringlib refactoring, continued. added count and
find helpers; updated unicodeobject to use stringlib_count
This commit is contained in:
		
							parent
							
								
									a26de2a80f
								
							
						
					
					
						commit
						58b5e84d52
					
				
					 5 changed files with 136 additions and 57 deletions
				
			
		
							
								
								
									
										34
									
								
								Objects/stringlib/count.h
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								Objects/stringlib/count.h
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,34 @@ | |||
| /* stringlib: count implementation */ | ||||
| 
 | ||||
| #ifndef STRINGLIB_COUNT_H | ||||
| #define STRINGLIB_COUNT_H | ||||
| 
 | ||||
| #ifndef STRINGLIB_FASTSEARCH_H | ||||
| #error must include "stringlib/fastsearch.h" before including this module | ||||
| #endif | ||||
| 
 | ||||
| Py_LOCAL(Py_ssize_t) | ||||
| stringlib_count(const STRINGLIB_CHAR* str, Py_ssize_t str_len, | ||||
|                 const STRINGLIB_CHAR* sub, Py_ssize_t sub_len) | ||||
| { | ||||
|     Py_ssize_t count; | ||||
| 
 | ||||
|     if (sub_len == 0) | ||||
|         return str_len + 1; | ||||
| 
 | ||||
|     count = fastsearch(str, str_len, sub, sub_len, FAST_COUNT); | ||||
| 
 | ||||
|     if (count < 0) | ||||
|         count = 0; /* no match */ | ||||
| 
 | ||||
|     return count; | ||||
| } | ||||
| 
 | ||||
| #endif | ||||
| 
 | ||||
| /*
 | ||||
| Local variables: | ||||
| c-basic-offset: 4 | ||||
| indent-tabs-mode: nil | ||||
| End: | ||||
| */ | ||||
							
								
								
									
										49
									
								
								Objects/stringlib/find.h
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										49
									
								
								Objects/stringlib/find.h
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,49 @@ | |||
| /* stringlib: find/index implementation */ | ||||
| 
 | ||||
| #ifndef STRINGLIB_FIND_H | ||||
| #define STRINGLIB_FIND_H | ||||
| 
 | ||||
| #ifndef STRINGLIB_FASTSEARCH_H | ||||
| #error must include "stringlib/fastsearch.h" before including this module | ||||
| #endif | ||||
| 
 | ||||
| Py_LOCAL(Py_ssize_t) | ||||
| stringlib_find(const STRINGLIB_CHAR* str, Py_ssize_t str_len, | ||||
|                const STRINGLIB_CHAR* sub, Py_ssize_t sub_len) | ||||
| { | ||||
|     if (sub_len == 0) | ||||
|         return 0; | ||||
| 
 | ||||
|     return fastsearch(str, str_len, sub, sub_len, FAST_SEARCH); | ||||
| } | ||||
| 
 | ||||
| Py_LOCAL(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 pos; | ||||
| 
 | ||||
|     /* XXX - create reversefastsearch helper! */ | ||||
|     if (sub_len == 0) | ||||
| 	pos = str_len; | ||||
|     else { | ||||
| 	Py_ssize_t j; | ||||
|         pos = -1; | ||||
| 	for (j = str_len - sub_len; j >= 0; --j) | ||||
|             if (STRINGLIB_CMP(str+j, sub, sub_len) == 0) { | ||||
|                 pos = j; | ||||
|                 break; | ||||
|             } | ||||
|     } | ||||
| 
 | ||||
|     return pos; | ||||
| } | ||||
| 
 | ||||
| #endif | ||||
| 
 | ||||
| /*
 | ||||
| Local variables: | ||||
| c-basic-offset: 4 | ||||
| indent-tabs-mode: nil | ||||
| End: | ||||
| */ | ||||
|  | @ -3,9 +3,15 @@ | |||
| #ifndef STRINGLIB_PARTITION_H | ||||
| #define STRINGLIB_PARTITION_H | ||||
| 
 | ||||
| #ifndef STRINGLIB_FASTSEARCH_H | ||||
| #error must include "stringlib/fastsearch.h" before including this module | ||||
| #endif | ||||
| 
 | ||||
| Py_LOCAL(PyObject*) | ||||
| 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) | ||||
| 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 | ||||
|     ) | ||||
| { | ||||
|     PyObject* out; | ||||
|     Py_ssize_t pos; | ||||
|  | @ -46,8 +52,10 @@ partition(PyObject* str_obj, const STRINGLIB_CHAR* str, Py_ssize_t str_len, | |||
| } | ||||
| 
 | ||||
| Py_LOCAL(PyObject*) | ||||
| 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) | ||||
| 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 | ||||
|     ) | ||||
| { | ||||
|     PyObject* out; | ||||
|     Py_ssize_t pos; | ||||
|  |  | |||
|  | @ -1548,7 +1548,7 @@ string_partition(PyStringObject *self, PyObject *sep_obj) | |||
| 	else if (PyObject_AsCharBuffer(sep_obj, &sep, &sep_len)) | ||||
| 		return NULL; | ||||
| 
 | ||||
| 	return partition( | ||||
| 	return stringlib_partition( | ||||
| 		(PyObject*) self, | ||||
| 		PyString_AS_STRING(self), PyString_GET_SIZE(self), | ||||
| 		sep_obj, sep, sep_len | ||||
|  | @ -1579,7 +1579,7 @@ string_rpartition(PyStringObject *self, PyObject *sep_obj) | |||
| 	else if (PyObject_AsCharBuffer(sep_obj, &sep, &sep_len)) | ||||
| 		return NULL; | ||||
| 
 | ||||
| 	return rpartition( | ||||
| 	return stringlib_rpartition( | ||||
| 		(PyObject*) self, | ||||
| 		PyString_AS_STRING(self), PyString_GET_SIZE(self), | ||||
| 		sep_obj, sep, sep_len | ||||
|  |  | |||
|  | @ -3869,63 +3869,47 @@ STRINGLIB_CMP(const Py_UNICODE* str, const Py_UNICODE* other, Py_ssize_t len) | |||
| #define STRINGLIB_EMPTY unicode_empty | ||||
| 
 | ||||
| #include "stringlib/fastsearch.h" | ||||
| 
 | ||||
| #include "stringlib/count.h" | ||||
| #include "stringlib/find.h" | ||||
| #include "stringlib/partition.h" | ||||
| 
 | ||||
| 
 | ||||
| Py_LOCAL(Py_ssize_t) count(PyUnicodeObject *self, | ||||
|                            Py_ssize_t start, | ||||
|                            Py_ssize_t end, | ||||
|                            PyUnicodeObject *substring) | ||||
| { | ||||
|     Py_ssize_t count = 0; | ||||
| 
 | ||||
|     if (start < 0) | ||||
|         start += self->length; | ||||
|     if (start < 0) | ||||
|         start = 0; | ||||
|     if (end > self->length) | ||||
|         end = self->length; | ||||
|     if (end < 0) | ||||
|         end += self->length; | ||||
|     if (end < 0) | ||||
|         end = 0; | ||||
| 
 | ||||
|     if (substring->length == 0) | ||||
| 	return (end - start + 1); | ||||
| 
 | ||||
|     count = fastsearch( | ||||
|         PyUnicode_AS_UNICODE(self) + start, end - start, | ||||
|         substring->str, substring->length, FAST_COUNT | ||||
|         ); | ||||
| 
 | ||||
|     if (count < 0) | ||||
|         count = 0; /* no match */ | ||||
| 
 | ||||
|     return count; | ||||
| } | ||||
| 
 | ||||
| Py_ssize_t PyUnicode_Count(PyObject *str, | ||||
| 		    PyObject *substr, | ||||
| 		    Py_ssize_t start, | ||||
| 		    Py_ssize_t end) | ||||
|                            PyObject *substr, | ||||
|                            Py_ssize_t start, | ||||
|                            Py_ssize_t end) | ||||
| { | ||||
|     Py_ssize_t result; | ||||
|     PyUnicodeObject* str_obj; | ||||
|     PyUnicodeObject* sub_obj; | ||||
| 
 | ||||
|     str = PyUnicode_FromObject(str); | ||||
|     if (str == NULL) | ||||
|     str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str); | ||||
|     if (!str_obj) | ||||
| 	return -1; | ||||
|     substr = PyUnicode_FromObject(substr); | ||||
|     if (substr == NULL) { | ||||
| 	Py_DECREF(str); | ||||
|     sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr); | ||||
|     if (!sub_obj) { | ||||
| 	Py_DECREF(str_obj); | ||||
| 	return -1; | ||||
|     } | ||||
| 
 | ||||
|     result = count((PyUnicodeObject *)str, | ||||
| 		   start, end, | ||||
| 		   (PyUnicodeObject *)substr); | ||||
|     if (start < 0) | ||||
|         start += str_obj->length; | ||||
|     if (start < 0) | ||||
|         start = 0; | ||||
|     if (end > str_obj->length) | ||||
|         end = str_obj->length; | ||||
|     if (end < 0) | ||||
|         end += str_obj->length; | ||||
|     if (end < 0) | ||||
|         end = 0; | ||||
| 
 | ||||
|     result = stringlib_count( | ||||
|         str_obj->str + start, end - start, sub_obj->str, sub_obj->length | ||||
|         ); | ||||
| 
 | ||||
|     Py_DECREF(sub_obj); | ||||
|     Py_DECREF(str_obj); | ||||
| 
 | ||||
|     Py_DECREF(str); | ||||
|     Py_DECREF(substr); | ||||
|     return result; | ||||
| } | ||||
| 
 | ||||
|  | @ -4767,7 +4751,7 @@ PyObject *replace(PyUnicodeObject *self, | |||
|         Py_UNICODE *p; | ||||
| 
 | ||||
|         /* replace strings */ | ||||
|         n = count(self, 0, self->length, str1); | ||||
|         n = stringlib_count(self->str, self->length, str1->str, str1->length); | ||||
|         if (n > maxcount) | ||||
|             n = maxcount; | ||||
|         if (n == 0) | ||||
|  | @ -5162,7 +5146,7 @@ unicode_count(PyUnicodeObject *self, PyObject *args) | |||
|         return NULL; | ||||
| 
 | ||||
|     substring = (PyUnicodeObject *)PyUnicode_FromObject( | ||||
| 						(PyObject *)substring); | ||||
|         (PyObject *)substring); | ||||
|     if (substring == NULL) | ||||
| 	return NULL; | ||||
| 
 | ||||
|  | @ -5177,9 +5161,13 @@ unicode_count(PyUnicodeObject *self, PyObject *args) | |||
|     if (end < 0) | ||||
|         end = 0; | ||||
| 
 | ||||
|     result = PyInt_FromSsize_t(count(self, start, end, substring)); | ||||
|     result = PyInt_FromSsize_t( | ||||
|         stringlib_count(self->str + start, end - start, | ||||
|                         substring->str, substring->length) | ||||
|         ); | ||||
| 
 | ||||
|     Py_DECREF(substring); | ||||
| 
 | ||||
|     return result; | ||||
| } | ||||
| 
 | ||||
|  | @ -6222,7 +6210,7 @@ PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) | |||
|         return NULL; | ||||
|     } | ||||
| 
 | ||||
|     out = partition( | ||||
|     out = stringlib_partition( | ||||
|         str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), | ||||
|         sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) | ||||
|         ); | ||||
|  | @ -6250,7 +6238,7 @@ PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) | |||
|         return NULL; | ||||
|     } | ||||
| 
 | ||||
|     out = rpartition( | ||||
|     out = stringlib_rpartition( | ||||
|         str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), | ||||
|         sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) | ||||
|         ); | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Fredrik Lundh
						Fredrik Lundh