annotate Objects/stringlib/find.h @ 39042:c02fa187e0f4 legacy-trunk

Part of SF patch #1484695. This removes dead code. The chksum was already verified in .frombuf() on the lines above. If there was a problem an exception is raised, so there was no way this condition could have been true.
author Neal Norwitz <nnorwitz@gmail.com>
date Mon, 10 Jul 2006 00:23:17 +0000
parents ecf7173e79ff
children e0c06caefbff 25a5f6a30292
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
38400
33c328896f52 needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1 /* stringlib: find/index implementation */
33c328896f52 needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
2
33c328896f52 needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
3 #ifndef STRINGLIB_FIND_H
33c328896f52 needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
4 #define STRINGLIB_FIND_H
33c328896f52 needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
5
33c328896f52 needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
6 #ifndef STRINGLIB_FASTSEARCH_H
33c328896f52 needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
7 #error must include "stringlib/fastsearch.h" before including this module
33c328896f52 needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
8 #endif
33c328896f52 needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
9
38440
5b0753b9d16a needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents: 38424
diff changeset
10 Py_LOCAL_INLINE(Py_ssize_t)
38400
33c328896f52 needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
11 stringlib_find(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
38404
52f2c9f74f5a needforspeed: stringlib refactoring: use stringlib/find for string find
Fredrik Lundh <fredrik@pythonware.com>
parents: 38400
diff changeset
12 const STRINGLIB_CHAR* sub, Py_ssize_t sub_len,
52f2c9f74f5a needforspeed: stringlib refactoring: use stringlib/find for string find
Fredrik Lundh <fredrik@pythonware.com>
parents: 38400
diff changeset
13 Py_ssize_t offset)
38400
33c328896f52 needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
14 {
38404
52f2c9f74f5a needforspeed: stringlib refactoring: use stringlib/find for string find
Fredrik Lundh <fredrik@pythonware.com>
parents: 38400
diff changeset
15 Py_ssize_t pos;
52f2c9f74f5a needforspeed: stringlib refactoring: use stringlib/find for string find
Fredrik Lundh <fredrik@pythonware.com>
parents: 38400
diff changeset
16
38514
ecf7173e79ff changed find/rfind to return -1 for matches outside the source string
Fredrik Lundh <fredrik@pythonware.com>
parents: 38441
diff changeset
17 if (sub_len == 0) {
ecf7173e79ff changed find/rfind to return -1 for matches outside the source string
Fredrik Lundh <fredrik@pythonware.com>
parents: 38441
diff changeset
18 if (str_len < 0)
ecf7173e79ff changed find/rfind to return -1 for matches outside the source string
Fredrik Lundh <fredrik@pythonware.com>
parents: 38441
diff changeset
19 return -1;
38404
52f2c9f74f5a needforspeed: stringlib refactoring: use stringlib/find for string find
Fredrik Lundh <fredrik@pythonware.com>
parents: 38400
diff changeset
20 return offset;
38514
ecf7173e79ff changed find/rfind to return -1 for matches outside the source string
Fredrik Lundh <fredrik@pythonware.com>
parents: 38441
diff changeset
21 }
38404
52f2c9f74f5a needforspeed: stringlib refactoring: use stringlib/find for string find
Fredrik Lundh <fredrik@pythonware.com>
parents: 38400
diff changeset
22
52f2c9f74f5a needforspeed: stringlib refactoring: use stringlib/find for string find
Fredrik Lundh <fredrik@pythonware.com>
parents: 38400
diff changeset
23 pos = fastsearch(str, str_len, sub, sub_len, FAST_SEARCH);
38400
33c328896f52 needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
24
38404
52f2c9f74f5a needforspeed: stringlib refactoring: use stringlib/find for string find
Fredrik Lundh <fredrik@pythonware.com>
parents: 38400
diff changeset
25 if (pos >= 0)
52f2c9f74f5a needforspeed: stringlib refactoring: use stringlib/find for string find
Fredrik Lundh <fredrik@pythonware.com>
parents: 38400
diff changeset
26 pos += offset;
52f2c9f74f5a needforspeed: stringlib refactoring: use stringlib/find for string find
Fredrik Lundh <fredrik@pythonware.com>
parents: 38400
diff changeset
27
52f2c9f74f5a needforspeed: stringlib refactoring: use stringlib/find for string find
Fredrik Lundh <fredrik@pythonware.com>
parents: 38400
diff changeset
28 return pos;
38400
33c328896f52 needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
29 }
33c328896f52 needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
30
38440
5b0753b9d16a needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents: 38424
diff changeset
31 Py_LOCAL_INLINE(Py_ssize_t)
38400
33c328896f52 needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
32 stringlib_rfind(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
38404
52f2c9f74f5a needforspeed: stringlib refactoring: use stringlib/find for string find
Fredrik Lundh <fredrik@pythonware.com>
parents: 38400
diff changeset
33 const STRINGLIB_CHAR* sub, Py_ssize_t sub_len,
52f2c9f74f5a needforspeed: stringlib refactoring: use stringlib/find for string find
Fredrik Lundh <fredrik@pythonware.com>
parents: 38400
diff changeset
34 Py_ssize_t offset)
38400
33c328896f52 needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
35 {
33c328896f52 needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
36 /* XXX - create reversefastsearch helper! */
38514
ecf7173e79ff changed find/rfind to return -1 for matches outside the source string
Fredrik Lundh <fredrik@pythonware.com>
parents: 38441
diff changeset
37 if (sub_len == 0) {
ecf7173e79ff changed find/rfind to return -1 for matches outside the source string
Fredrik Lundh <fredrik@pythonware.com>
parents: 38441
diff changeset
38 if (str_len < 0)
ecf7173e79ff changed find/rfind to return -1 for matches outside the source string
Fredrik Lundh <fredrik@pythonware.com>
parents: 38441
diff changeset
39 return -1;
ecf7173e79ff changed find/rfind to return -1 for matches outside the source string
Fredrik Lundh <fredrik@pythonware.com>
parents: 38441
diff changeset
40 return str_len + offset;
ecf7173e79ff changed find/rfind to return -1 for matches outside the source string
Fredrik Lundh <fredrik@pythonware.com>
parents: 38441
diff changeset
41 } else {
ecf7173e79ff changed find/rfind to return -1 for matches outside the source string
Fredrik Lundh <fredrik@pythonware.com>
parents: 38441
diff changeset
42 Py_ssize_t j, pos = -1;
38400
33c328896f52 needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
43 for (j = str_len - sub_len; j >= 0; --j)
33c328896f52 needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
44 if (STRINGLIB_CMP(str+j, sub, sub_len) == 0) {
38404
52f2c9f74f5a needforspeed: stringlib refactoring: use stringlib/find for string find
Fredrik Lundh <fredrik@pythonware.com>
parents: 38400
diff changeset
45 pos = j + offset;
38400
33c328896f52 needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
46 break;
33c328896f52 needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
47 }
38514
ecf7173e79ff changed find/rfind to return -1 for matches outside the source string
Fredrik Lundh <fredrik@pythonware.com>
parents: 38441
diff changeset
48 return pos;
38400
33c328896f52 needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
49 }
33c328896f52 needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
50 }
33c328896f52 needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
51
38441
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
52 Py_LOCAL_INLINE(Py_ssize_t)
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
53 stringlib_find_slice(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
54 const STRINGLIB_CHAR* sub, Py_ssize_t sub_len,
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
55 Py_ssize_t start, Py_ssize_t end)
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
56 {
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
57 if (start < 0)
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
58 start += str_len;
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
59 if (start < 0)
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
60 start = 0;
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
61 if (end > str_len)
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
62 end = str_len;
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
63 if (end < 0)
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
64 end += str_len;
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
65 if (end < 0)
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
66 end = 0;
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
67
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
68 return stringlib_find(
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
69 str + start, end - start,
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
70 sub, sub_len, start
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
71 );
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
72 }
38424
3d81faf47af7 needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents: 38404
diff changeset
73
38440
5b0753b9d16a needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents: 38424
diff changeset
74 Py_LOCAL_INLINE(Py_ssize_t)
38441
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
75 stringlib_rfind_slice(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
76 const STRINGLIB_CHAR* sub, Py_ssize_t sub_len,
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
77 Py_ssize_t start, Py_ssize_t end)
38424
3d81faf47af7 needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents: 38404
diff changeset
78 {
38441
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
79 if (start < 0)
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
80 start += str_len;
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
81 if (start < 0)
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
82 start = 0;
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
83 if (end > str_len)
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
84 end = str_len;
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
85 if (end < 0)
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
86 end += str_len;
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
87 if (end < 0)
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
88 end = 0;
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
89
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
90 return stringlib_rfind(str + start, end - start, sub, sub_len, start);
38424
3d81faf47af7 needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents: 38404
diff changeset
91 }
3d81faf47af7 needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents: 38404
diff changeset
92
38441
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
93 #ifdef STRINGLIB_STR
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
94
38440
5b0753b9d16a needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents: 38424
diff changeset
95 Py_LOCAL_INLINE(int)
38424
3d81faf47af7 needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents: 38404
diff changeset
96 stringlib_contains_obj(PyObject* str, PyObject* sub)
3d81faf47af7 needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents: 38404
diff changeset
97 {
3d81faf47af7 needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents: 38404
diff changeset
98 return stringlib_find(
3d81faf47af7 needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents: 38404
diff changeset
99 STRINGLIB_STR(str), STRINGLIB_LEN(str),
3d81faf47af7 needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents: 38404
diff changeset
100 STRINGLIB_STR(sub), STRINGLIB_LEN(sub), 0
3d81faf47af7 needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents: 38404
diff changeset
101 ) != -1;
3d81faf47af7 needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents: 38404
diff changeset
102 }
3d81faf47af7 needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents: 38404
diff changeset
103
38441
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
104 #endif /* STRINGLIB_STR */
38424
3d81faf47af7 needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents: 38404
diff changeset
105
38441
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
106 #endif /* STRINGLIB_FIND_H */
38400
33c328896f52 needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
107
33c328896f52 needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
108 /*
33c328896f52 needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
109 Local variables:
33c328896f52 needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
110 c-basic-offset: 4
33c328896f52 needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
111 indent-tabs-mode: nil
33c328896f52 needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
112 End:
33c328896f52 needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
113 */