Showing the changes to sys/src/cmd/python/Objects/stringlib (5 of 3706 files). Show all
Unified · Split
| @@ -0,0 +1,34 @@ | |||
| 1 | bits shared by the stringobject and unicodeobject implementations (and | ||
| 2 | possibly other modules, in a not too distant future). | ||
| 3 | |||
| 4 | the stuff in here is included into relevant places; see the individual | ||
| 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) | ||
| @@ -0,0 +1,37 @@ | |||
| 1 | /* stringlib: count implementation */ | ||
| 2 | |||
| 3 | #ifndef STRINGLIB_COUNT_H | ||
| 4 | #define STRINGLIB_COUNT_H | ||
| 5 | |||
| 6 | #ifndef STRINGLIB_FASTSEARCH_H | ||
| 7 | #error must include "stringlib/fastsearch.h" before including this module | ||
| 8 | #endif | ||
| 9 | |||
| 10 | Py_LOCAL_INLINE(Py_ssize_t) | ||
| 11 | stringlib_count(const STRINGLIB_CHAR* str, Py_ssize_t str_len, | ||
| 12 | const STRINGLIB_CHAR* sub, Py_ssize_t sub_len) | ||
| 13 | { | ||
| 14 | Py_ssize_t count; | ||
| 15 | |||
| 16 | if (sub_len == 0) { | ||
| 17 | if (str_len < 0) | ||
| 18 | return 0; /* start > len(str) */ | ||
| 19 | return str_len + 1; | ||
| 20 | } | ||
| 21 | |||
| 22 | count = fastsearch(str, str_len, sub, sub_len, FAST_COUNT); | ||
| 23 | |||
| 24 | if (count < 0) | ||
| 25 | count = 0; /* no match */ | ||
| 26 | |||
| 27 | return count; | ||
| 28 | } | ||
| 29 | |||
| 30 | #endif | ||
| 31 | |||
| 32 | /* | ||
| 33 | Local variables: | ||
| 34 | c-basic-offset: 4 | ||
| 35 | indent-tabs-mode: nil | ||
| 36 | End: | ||
| 37 | */ | ||
| @@ -0,0 +1,104 @@ | |||
| 1 | /* stringlib: fastsearch implementation */ | ||
| 2 | |||
| 3 | #ifndef STRINGLIB_FASTSEARCH_H | ||
| 4 | #define STRINGLIB_FASTSEARCH_H | ||
| 5 | |||
| 6 | /* fast search/count implementation, based on a mix between boyer- | ||
| 7 | moore and horspool, with a few more bells and whistles on the top. | ||
| 8 | for some more background, see: http://effbot.org/stringlib */ | ||
| 9 | |||
| 10 | /* note: fastsearch may access s[n], which isn't a problem when using | ||
| 11 | Python's ordinary string types, but may cause problems if you're | ||
| 12 | using this code in other contexts. also, the count mode returns -1 | ||
| 13 | if there cannot possible be a match in the target string, and 0 if | ||
| 14 | it has actually checked for matches, but didn't find any. callers | ||
| 15 | beware! */ | ||
| 16 | |||
| 17 | #define FAST_COUNT 0 | ||
| 18 | #define FAST_SEARCH 1 | ||
| 19 | |||
| 20 | Py_LOCAL_INLINE(Py_ssize_t) | ||
| 21 | fastsearch(const STRINGLIB_CHAR* s, Py_ssize_t n, | ||
| 22 | const STRINGLIB_CHAR* p, Py_ssize_t m, | ||
| 23 | int mode) | ||
| 24 | { | ||
| 25 | long mask; | ||
| 26 | Py_ssize_t skip, count = 0; | ||
| 27 | Py_ssize_t i, j, mlast, w; | ||
| 28 | |||
| 29 | w = n - m; | ||
| 30 | |||
| 31 | if (w < 0) | ||
| 32 | return -1; | ||
| 33 | |||
| 34 | /* look for special cases */ | ||
| 35 | if (m <= 1) { | ||
| 36 | if (m <= 0) | ||
| 37 | return -1; | ||
| 38 | /* use special case for 1-character strings */ | ||
| 39 | if (mode == FAST_COUNT) { | ||
| 40 | for (i = 0; i < n; i++) | ||
| 41 | if (s[i] == p[0]) | ||
| 42 | count++; | ||
| 43 | return count; | ||
| 44 | } else { | ||
| 45 | for (i = 0; i < n; i++) | ||
| 46 | if (s[i] == p[0]) | ||
| 47 | return i; | ||
| 48 | } | ||
| 49 | return -1; | ||
| 50 | } | ||
| 51 | |||
| 52 | mlast = m - 1; | ||
| 53 | |||
| 54 | /* create compressed boyer-moore delta 1 table */ | ||
| 55 | skip = mlast - 1; | ||
| 56 | /* process pattern[:-1] */ | ||
| 57 | for (mask = i = 0; i < mlast; i++) { | ||
| 58 | mask |= (1 << (p[i] & 0x1F)); | ||
| 59 | if (p[i] == p[mlast]) | ||
| 60 | skip = mlast - i - 1; | ||
| 61 | } | ||
| 62 | /* process pattern[-1] outside the loop */ | ||
| 63 | mask |= (1 << (p[mlast] & 0x1F)); | ||
| 64 | |||
| 65 | for (i = 0; i <= w; i++) { | ||
| 66 | /* note: using mlast in the skip path slows things down on x86 */ | ||
| 67 | if (s[i+m-1] == p[m-1]) { | ||
| 68 | /* candidate match */ | ||
| 69 | for (j = 0; j < mlast; j++) | ||
| 70 | if (s[i+j] != p[j]) | ||
| 71 | break; | ||
| 72 | if (j == mlast) { | ||
| 73 | /* got a match! */ | ||
| 74 | if (mode != FAST_COUNT) | ||
| 75 | return i; | ||
| 76 | count++; | ||
| 77 | i = i + mlast; | ||
| 78 | continue; | ||
| 79 | } | ||
| 80 | /* miss: check if next character is part of pattern */ | ||
| 81 | if (!(mask & (1 << (s[i+m] & 0x1F)))) | ||
| 82 | i = i + m; | ||
| 83 | else | ||
| 84 | i = i + skip; | ||
| 85 | } else { | ||
| 86 | /* skip: check if next character is part of pattern */ | ||
| 87 | if (!(mask & (1 << (s[i+m] & 0x1F)))) | ||
| 88 | i = i + m; | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 | if (mode != FAST_COUNT) | ||
| 93 | return -1; | ||
| 94 | return count; | ||
| 95 | } | ||
| 96 | |||
| 97 | #endif | ||
| 98 | |||
| 99 | /* | ||
| 100 | Local variables: | ||
| 101 | c-basic-offset: 4 | ||
| 102 | indent-tabs-mode: nil | ||
| 103 | End: | ||
| 104 | */ | ||
| @@ -0,0 +1,113 @@ | |||
| 1 | /* stringlib: find/index implementation */ | ||
| 2 | |||
| 3 | #ifndef STRINGLIB_FIND_H | ||
| 4 | #define STRINGLIB_FIND_H | ||
| 5 | |||
| 6 | #ifndef STRINGLIB_FASTSEARCH_H | ||
| 7 | #error must include "stringlib/fastsearch.h" before including this module | ||
| 8 | #endif | ||
| 9 | |||
| 10 | Py_LOCAL_INLINE(Py_ssize_t) | ||
| 11 | stringlib_find(const STRINGLIB_CHAR* str, Py_ssize_t str_len, | ||
| 12 | const STRINGLIB_CHAR* sub, Py_ssize_t sub_len, | ||
| 13 | Py_ssize_t offset) | ||
| 14 | { | ||
| 15 | Py_ssize_t pos; | ||
| 16 | |||
| 17 | if (sub_len == 0) { | ||
| 18 | if (str_len < 0) | ||
| 19 | return -1; | ||
| 20 | return offset; | ||
| 21 | } | ||
| 22 | |||
| 23 | pos = fastsearch(str, str_len, sub, sub_len, FAST_SEARCH); | ||
| 24 | |||
| 25 | if (pos >= 0) | ||
| 26 | pos += offset; | ||
| 27 | |||
| 28 | return pos; | ||
| 29 | } | ||
| 30 | |||
| 31 | Py_LOCAL_INLINE(Py_ssize_t) | ||
| 32 | stringlib_rfind(const STRINGLIB_CHAR* str, Py_ssize_t str_len, | ||
| 33 | const STRINGLIB_CHAR* sub, Py_ssize_t sub_len, | ||
| 34 | Py_ssize_t offset) | ||
| 35 | { | ||
| 36 | /* XXX - create reversefastsearch helper! */ | ||
| 37 | if (sub_len == 0) { | ||
| 38 | if (str_len < 0) | ||
| 39 | return -1; | ||
| 40 | return str_len + offset; | ||
| 41 | } else { | ||
| 42 | Py_ssize_t j, pos = -1; | ||
| 43 | for (j = str_len - sub_len; j >= 0; --j) | ||
| 44 | if (STRINGLIB_CMP(str+j, sub, sub_len) == 0) { | ||
| 45 | pos = j + offset; | ||
| 46 | break; | ||
| 47 | } | ||
| 48 | return pos; | ||
| 49 | } | ||
| 50 | } | ||
| 51 | |||
| 52 | Py_LOCAL_INLINE(Py_ssize_t) | ||
| 53 | stringlib_find_slice(const STRINGLIB_CHAR* str, Py_ssize_t str_len, | ||
| 54 | const STRINGLIB_CHAR* sub, Py_ssize_t sub_len, | ||
| 55 | Py_ssize_t start, Py_ssize_t end) | ||
| 56 | { | ||
| 57 | if (start < 0) | ||
| 58 | start += str_len; | ||
| 59 | if (start < 0) | ||
| 60 | start = 0; | ||
| 61 | if (end > str_len) | ||
| 62 | end = str_len; | ||
| 63 | if (end < 0) | ||
| 64 | end += str_len; | ||
| 65 | if (end < 0) | ||
| 66 | end = 0; | ||
| 67 | |||
| 68 | return stringlib_find( | ||
| 69 | str + start, end - start, | ||
| 70 | sub, sub_len, start | ||
| 71 | ); | ||
| 72 | } | ||
| 73 | |||
| 74 | Py_LOCAL_INLINE(Py_ssize_t) | ||
| 75 | stringlib_rfind_slice(const STRINGLIB_CHAR* str, Py_ssize_t str_len, | ||
| 76 | const STRINGLIB_CHAR* sub, Py_ssize_t sub_len, | ||
| 77 | Py_ssize_t start, Py_ssize_t end) | ||
| 78 | { | ||
| 79 | if (start < 0) | ||
| 80 | start += str_len; | ||
| 81 | if (start < 0) | ||
| 82 | start = 0; | ||
| 83 | if (end > str_len) | ||
| 84 | end = str_len; | ||
| 85 | if (end < 0) | ||
| 86 | end += str_len; | ||
| 87 | if (end < 0) | ||
| 88 | end = 0; | ||
| 89 | |||
| 90 | return stringlib_rfind(str + start, end - start, sub, sub_len, start); | ||
| 91 | } | ||
| 92 | |||
| 93 | #ifdef STRINGLIB_STR | ||
| 94 | |||
| 95 | Py_LOCAL_INLINE(int) | ||
| 96 | stringlib_contains_obj(PyObject* str, PyObject* sub) | ||
| 97 | { | ||
| 98 | return stringlib_find( | ||
| 99 | STRINGLIB_STR(str), STRINGLIB_LEN(str), | ||
| 100 | STRINGLIB_STR(sub), STRINGLIB_LEN(sub), 0 | ||
| 101 | ) != -1; | ||
| 102 | } | ||
| 103 | |||
| 104 | #endif /* STRINGLIB_STR */ | ||
| 105 | |||
| 106 | #endif /* STRINGLIB_FIND_H */ | ||
| 107 | |||
| 108 | /* | ||
| 109 | Local variables: | ||
| 110 | c-basic-offset: 4 | ||
| 111 | indent-tabs-mode: nil | ||
| 112 | End: | ||
| 113 | */ | ||
| @@ -0,0 +1,111 @@ | |||
| 1 | /* stringlib: partition implementation */ | ||
| 2 | |||
| 3 | #ifndef STRINGLIB_PARTITION_H | ||
| 4 | #define STRINGLIB_PARTITION_H | ||
| 5 | |||
| 6 | #ifndef STRINGLIB_FASTSEARCH_H | ||
| 7 | #error must include "stringlib/fastsearch.h" before including this module | ||
| 8 | #endif | ||
| 9 | |||
| 10 | Py_LOCAL_INLINE(PyObject*) | ||
| 11 | stringlib_partition( | ||
| 12 | PyObject* str_obj, const STRINGLIB_CHAR* str, Py_ssize_t str_len, | ||
| 13 | PyObject* sep_obj, const STRINGLIB_CHAR* sep, Py_ssize_t sep_len | ||
| 14 | ) | ||
| 15 | { | ||
| 16 | PyObject* out; | ||
| 17 | Py_ssize_t pos; | ||
| 18 | |||
| 19 | if (sep_len == 0) { | ||
| 20 | PyErr_SetString(PyExc_ValueError, "empty separator"); | ||
| 21 | return NULL; | ||
| 22 | } | ||
| 23 | |||
| 24 | out = PyTuple_New(3); | ||
| 25 | if (!out) | ||
| 26 | return NULL; | ||
| 27 | |||
| 28 | pos = fastsearch(str, str_len, sep, sep_len, FAST_SEARCH); | ||
| 29 | |||
| 30 | if (pos < 0) { | ||
| 31 | Py_INCREF(str_obj); | ||
| 32 | PyTuple_SET_ITEM(out, 0, (PyObject*) str_obj); | ||
| 33 | Py_INCREF(STRINGLIB_EMPTY); | ||
| 34 | PyTuple_SET_ITEM(out, 1, (PyObject*) STRINGLIB_EMPTY); | ||
| 35 | Py_INCREF(STRINGLIB_EMPTY); | ||
| 36 | PyTuple_SET_ITEM(out, 2, (PyObject*) STRINGLIB_EMPTY); | ||
| 37 | return out; | ||
| 38 | } | ||
| 39 | |||
| 40 | PyTuple_SET_ITEM(out, 0, STRINGLIB_NEW(str, pos)); | ||
| 41 | Py_INCREF(sep_obj); | ||
| 42 | PyTuple_SET_ITEM(out, 1, sep_obj); | ||
| 43 | pos += sep_len; | ||
| 44 | PyTuple_SET_ITEM(out, 2, STRINGLIB_NEW(str + pos, str_len - pos)); | ||
| 45 | |||
| 46 | if (PyErr_Occurred()) { | ||
| 47 | Py_DECREF(out); | ||
| 48 | return NULL; | ||
| 49 | } | ||
| 50 | |||
| 51 | return out; | ||
| 52 | } | ||
| 53 | |||
| 54 | Py_LOCAL_INLINE(PyObject*) | ||
| 55 | stringlib_rpartition( | ||
| 56 | PyObject* str_obj, const STRINGLIB_CHAR* str, Py_ssize_t str_len, | ||
| 57 | PyObject* sep_obj, const STRINGLIB_CHAR* sep, Py_ssize_t sep_len | ||
| 58 | ) | ||
| 59 | { | ||
| 60 | PyObject* out; | ||
| 61 | Py_ssize_t pos, j; | ||
| 62 | |||
| 63 | if (sep_len == 0) { | ||
| 64 | PyErr_SetString(PyExc_ValueError, "empty separator"); | ||
| 65 | return NULL; | ||
| 66 | } | ||
| 67 | |||
| 68 | out = PyTuple_New(3); | ||
| 69 | if (!out) | ||
| 70 | return NULL; | ||
| 71 | |||
| 72 | /* XXX - create reversefastsearch helper! */ | ||
| 73 | pos = -1; | ||
| 74 | for (j = str_len - sep_len; j >= 0; --j) | ||
| 75 | if (STRINGLIB_CMP(str+j, sep, sep_len) == 0) { | ||
| 76 | pos = j; | ||
| 77 | break; | ||
| 78 | } | ||
| 79 | |||
| 80 | if (pos < 0) { | ||
| 81 | Py_INCREF(STRINGLIB_EMPTY); | ||
| 82 | PyTuple_SET_ITEM(out, 0, (PyObject*) STRINGLIB_EMPTY); | ||
| 83 | Py_INCREF(STRINGLIB_EMPTY); | ||
| 84 | PyTuple_SET_ITEM(out, 1, (PyObject*) STRINGLIB_EMPTY); | ||
| 85 | Py_INCREF(str_obj); | ||
| 86 | PyTuple_SET_ITEM(out, 2, (PyObject*) str_obj); | ||
| 87 | return out; | ||
| 88 | } | ||
| 89 | |||
| 90 | PyTuple_SET_ITEM(out, 0, STRINGLIB_NEW(str, pos)); | ||
| 91 | Py_INCREF(sep_obj); | ||
| 92 | PyTuple_SET_ITEM(out, 1, sep_obj); | ||
| 93 | pos += sep_len; | ||
| 94 | PyTuple_SET_ITEM(out, 2, STRINGLIB_NEW(str + pos, str_len - pos)); | ||
| 95 | |||
| 96 | if (PyErr_Occurred()) { | ||
| 97 | Py_DECREF(out); | ||
| 98 | return NULL; | ||
| 99 | } | ||
| 100 | |||
| 101 | return out; | ||
| 102 | } | ||
| 103 | |||
| 104 | #endif | ||
| 105 | |||
| 106 | /* | ||
| 107 | Local variables: | ||
| 108 | c-basic-offset: 4 | ||
| 109 | indent-tabs-mode: nil | ||
| 110 | End: | ||
| 111 | */ | ||