| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent ccc6359 commit cda2ed2
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,3 +3,32 @@ possibly other modules, in a not too distant future). | |||
| 3 | 3 | ||
| 4 | 4 | the stuff in here is included into relevant places; see the individual | |
| 5 | 5 | 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) | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -48,6 +48,39 @@ stringlib_rfind(const STRINGLIB_CHAR* str, Py_ssize_t str_len, | |||
| 48 | 48 | return pos; | |
| 49 | 49 | } | |
| 50 | 50 | ||
| 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 | + | ||
| 51 | 84 | #endif | |
| 52 | 85 | ||
| 53 | 86 | /* | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -690,6 +690,9 @@ PyObject *PyString_DecodeEscape(const char *s, | |||
| 690 | 690 | return NULL; | |
| 691 | 691 | } | |
| 692 | 692 | ||
| 693 | + /* -------------------------------------------------------------------- */ | ||
| 694 | + /* object api */ | ||
| 695 | + | ||
| 693 | 696 | static Py_ssize_t | |
| 694 | 697 | string_getsize(register PyObject *op) | |
| 695 | 698 | { | |
@@ -765,22 +768,23 @@ PyString_AsStringAndSize(register PyObject *obj, | |||
| 765 | 768 | } | |
| 766 | 769 | ||
| 767 | 770 | /* -------------------------------------------------------------------- */ | |
| 768 | - /* stringlib components */ | ||
| 771 | + /* Methods */ | ||
| 769 | 772 | ||
| 770 | 773 | #define STRINGLIB_CHAR char | |
| 771 | 774 | ||
| 772 | - #define STRINGLIB_NEW PyString_FromStringAndSize | ||
| 773 | 775 | #define STRINGLIB_CMP memcmp | |
| 776 | + #define STRINGLIB_LEN PyString_GET_SIZE | ||
| 777 | + #define STRINGLIB_NEW PyString_FromStringAndSize | ||
| 778 | + #define STRINGLIB_STR PyString_AS_STRING | ||
| 774 | 779 | ||
| 775 | 780 | #define STRINGLIB_EMPTY nullstring | |
| 776 | 781 | ||
| 777 | 782 | #include "stringlib/fastsearch.h" | |
| 778 | 783 | ||
| 784 | + #include "stringlib/count.h" | ||
| 779 | 785 | #include "stringlib/find.h" | |
| 780 | 786 | #include "stringlib/partition.h" | |
| 781 | 787 | ||
| 782 | - /* -------------------------------------------------------------------- */ | ||
| 783 | - /* Methods */ | ||
| 784 | 788 | ||
| 785 | 789 | static int | |
| 786 | 790 | string_print(PyStringObject *op, FILE *fp, int flags) | |
@@ -1048,49 +1052,36 @@ string_slice(register PyStringObject *a, register Py_ssize_t i, | |||
| 1048 | 1052 | } | |
| 1049 | 1053 | ||
| 1050 | 1054 | static int | |
| 1051 | - string_contains(PyObject *a, PyObject *el) | ||
| 1055 | + string_contains(PyObject *str_obj, PyObject *sub_obj) | ||
| 1052 | 1056 | { | |
| 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)) { | ||
| 1059 | 1058 | #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); | ||
| 1062 | 1061 | #endif | |
| 1063 | - if (!PyString_Check(el)) { | ||
| 1062 | + if (!PyString_Check(sub_obj)) { | ||
| 1064 | 1063 | PyErr_SetString(PyExc_TypeError, | |
| 1065 | 1064 | "'in <string>' requires string as left operand"); | |
| 1066 | 1065 | return -1; | |
| 1067 | 1066 | } | |
| 1068 | 1067 | } | |
| 1069 | 1068 | ||
| 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); | ||
| 1079 | 1070 | } | |
| 1080 | 1071 | ||
| 1081 | 1072 | static PyObject * | |
| 1082 | 1073 | string_item(PyStringObject *a, register Py_ssize_t i) | |
| 1083 | 1074 | { | |
| 1075 | + char pchar; | ||
| 1084 | 1076 | PyObject *v; | |
| 1085 | - char *pchar; | ||
| 1086 | 1077 | if (i < 0 || i >= a->ob_size) { | |
| 1087 | 1078 | PyErr_SetString(PyExc_IndexError, "string index out of range"); | |
| 1088 | 1079 | return NULL; | |
| 1089 | 1080 | } | |
| 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]; | ||
| 1092 | 1083 | if (v == NULL) | |
| 1093 | - v = PyString_FromStringAndSize(pchar, 1); | ||
| 1084 | + v = PyString_FromStringAndSize(&pchar, 1); | ||
| 1094 | 1085 | else { | |
| 1095 | 1086 | #ifdef COUNT_ALLOCS | |
| 1096 | 1087 | one_strings++; | |
@@ -1166,9 +1157,8 @@ string_richcompare(PyStringObject *a, PyStringObject *b, int op) | |||
| 1166 | 1157 | int | |
| 1167 | 1158 | _PyString_Eq(PyObject *o1, PyObject *o2) | |
| 1168 | 1159 | { | |
| 1169 | - PyStringObject *a, *b; | ||
| 1170 | - a = (PyStringObject*)o1; | ||
| 1171 | - b = (PyStringObject*)o2; | ||
| 1160 | + PyStringObject *a = (PyStringObject*) o1; | ||
| 1161 | + PyStringObject *b = (PyStringObject*) o2; | ||
| 1172 | 1162 | return a->ob_size == b->ob_size | |
| 1173 | 1163 | && *a->ob_sval == *b->ob_sval | |
| 1174 | 1164 | && memcmp(a->ob_sval, b->ob_sval, a->ob_size) == 0; | |
@@ -2264,43 +2254,37 @@ as in slice notation."); | |||
| 2264 | 2254 | static PyObject * | |
| 2265 | 2255 | string_count(PyStringObject *self, PyObject *args) | |
| 2266 | 2256 | { | |
| 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; | ||
| 2272 | 2261 | ||
| 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)) | ||
| 2275 | 2264 | return NULL; | |
| 2276 | 2265 | ||
| 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); | ||
| 2280 | 2269 | } | |
| 2281 | 2270 | #ifdef Py_USING_UNICODE | |
| 2282 | - else if (PyUnicode_Check(subobj)) { | ||
| 2271 | + else if (PyUnicode_Check(sub_obj)) { | ||
| 2283 | 2272 | Py_ssize_t count; | |
| 2284 | - count = PyUnicode_Count((PyObject *)self, subobj, i, last); | ||
| 2273 | + count = PyUnicode_Count((PyObject *)self, sub_obj, start, end); | ||
| 2285 | 2274 | if (count == -1) | |
| 2286 | 2275 | return NULL; | |
| 2287 | 2276 | else | |
| 2288 | - return PyInt_FromLong((long) count); | ||
| 2277 | + return PyInt_FromSsize_t(count); | ||
| 2289 | 2278 | } | |
| 2290 | 2279 | #endif | |
| 2291 | - else if (PyObject_AsCharBuffer(subobj, &sub, &n)) | ||
| 2280 | + else if (PyObject_AsCharBuffer(sub_obj, &sub, &sub_len)) | ||
| 2292 | 2281 | return NULL; | |
| 2293 | 2282 | ||
| 2294 | - string_adjust_indices(&i, &last, len); | ||
| 2283 | + string_adjust_indices(&start, &end, PyString_GET_SIZE(self)); | ||
| 2295 | 2284 | ||
| 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 | + ); | ||
| 2304 | 2288 | } | |
| 2305 | 2289 | ||
| 2306 | 2290 | PyDoc_STRVAR(swapcase__doc__, | |
@@ -2477,7 +2461,7 @@ return_self(PyStringObject *self) | |||
| 2477 | 2461 | } | |
| 2478 | 2462 | ||
| 2479 | 2463 | 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) | ||
| 2481 | 2465 | { | |
| 2482 | 2466 | Py_ssize_t count=0; | |
| 2483 | 2467 | char *start=target; | |
@@ -2580,7 +2564,7 @@ countstring(char *target, Py_ssize_t target_len, | |||
| 2580 | 2564 | } | |
| 2581 | 2565 | ||
| 2582 | 2566 | ||
| 2583 | - /* Algorithms for difference cases of string replacement */ | ||
| 2567 | + /* Algorithms for different cases of string replacement */ | ||
| 2584 | 2568 | ||
| 2585 | 2569 | /* len(self)>=1, from="", len(to)>=1, maxcount>=1 */ | |
| 2586 | 2570 | Py_LOCAL(PyStringObject *) | |
| Back | FazBrowse Home | New Git URL |
0 commit comments