annotate Objects/stringlib/find.h @ 87690:66c1c9f32567 default tip

Add use_weakref option to functools.lru_cache The use_weakref option makes lru_cache create weak references to the function arguments when creating the cache key, which prevents them from staying in the memory forever even after all other references to them are gone. This is a proof of concept for http://bugs.python.org/issue19859
author Radomir Dopieralski <devel@sheep.art.pl>
date Tue, 03 Dec 2013 16:02:48 +0100
parents 75648db1b3f3
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
1 /* stringlib: find/index implementation */
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
2
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
3 #ifndef STRINGLIB_FASTSEARCH_H
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
4 #error must include "stringlib/fastsearch.h" before including this module
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
5 #endif
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
6
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
7 Py_LOCAL_INLINE(Py_ssize_t)
72475
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 69473
diff changeset
8 STRINGLIB(find)(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
9 const STRINGLIB_CHAR* sub, Py_ssize_t sub_len,
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
10 Py_ssize_t offset)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
11 {
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
12 Py_ssize_t pos;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
13
49310
6403f1edecc1 Merged revisions 66631 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48886
diff changeset
14 if (str_len < 0)
6403f1edecc1 Merged revisions 66631 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48886
diff changeset
15 return -1;
6403f1edecc1 Merged revisions 66631 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48886
diff changeset
16 if (sub_len == 0)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
17 return offset;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
18
72475
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 69473
diff changeset
19 pos = FASTSEARCH(str, str_len, sub, sub_len, -1, FAST_SEARCH);
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
20
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
21 if (pos >= 0)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
22 pos += offset;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
23
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
24 return pos;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
25 }
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
26
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
27 Py_LOCAL_INLINE(Py_ssize_t)
72475
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 69473
diff changeset
28 STRINGLIB(rfind)(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
29 const STRINGLIB_CHAR* sub, Py_ssize_t sub_len,
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
30 Py_ssize_t offset)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
31 {
58095
a1035cb4a925 Merged revisions 77241 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49310
diff changeset
32 Py_ssize_t pos;
a1035cb4a925 Merged revisions 77241 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49310
diff changeset
33
a1035cb4a925 Merged revisions 77241 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49310
diff changeset
34 if (str_len < 0)
a1035cb4a925 Merged revisions 77241 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49310
diff changeset
35 return -1;
a1035cb4a925 Merged revisions 77241 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49310
diff changeset
36 if (sub_len == 0)
a1035cb4a925 Merged revisions 77241 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49310
diff changeset
37 return str_len + offset;
a1035cb4a925 Merged revisions 77241 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49310
diff changeset
38
72475
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 69473
diff changeset
39 pos = FASTSEARCH(str, str_len, sub, sub_len, -1, FAST_RSEARCH);
58095
a1035cb4a925 Merged revisions 77241 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49310
diff changeset
40
a1035cb4a925 Merged revisions 77241 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49310
diff changeset
41 if (pos >= 0)
a1035cb4a925 Merged revisions 77241 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49310
diff changeset
42 pos += offset;
a1035cb4a925 Merged revisions 77241 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49310
diff changeset
43
a1035cb4a925 Merged revisions 77241 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49310
diff changeset
44 return pos;
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
45 }
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
46
58289
5ca1948a8fd6 Merged revisions 77461 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58095
diff changeset
47 /* helper macro to fixup start/end slice values */
5ca1948a8fd6 Merged revisions 77461 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58095
diff changeset
48 #define ADJUST_INDICES(start, end, len) \
5ca1948a8fd6 Merged revisions 77461 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58095
diff changeset
49 if (end > len) \
5ca1948a8fd6 Merged revisions 77461 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58095
diff changeset
50 end = len; \
5ca1948a8fd6 Merged revisions 77461 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58095
diff changeset
51 else if (end < 0) { \
5ca1948a8fd6 Merged revisions 77461 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58095
diff changeset
52 end += len; \
5ca1948a8fd6 Merged revisions 77461 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58095
diff changeset
53 if (end < 0) \
5ca1948a8fd6 Merged revisions 77461 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58095
diff changeset
54 end = 0; \
5ca1948a8fd6 Merged revisions 77461 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58095
diff changeset
55 } \
5ca1948a8fd6 Merged revisions 77461 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58095
diff changeset
56 if (start < 0) { \
5ca1948a8fd6 Merged revisions 77461 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58095
diff changeset
57 start += len; \
5ca1948a8fd6 Merged revisions 77461 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58095
diff changeset
58 if (start < 0) \
5ca1948a8fd6 Merged revisions 77461 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58095
diff changeset
59 start = 0; \
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
60 }
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
61
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
62 Py_LOCAL_INLINE(Py_ssize_t)
72475
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 69473
diff changeset
63 STRINGLIB(find_slice)(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
64 const STRINGLIB_CHAR* sub, Py_ssize_t sub_len,
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
65 Py_ssize_t start, Py_ssize_t end)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
66 {
58289
5ca1948a8fd6 Merged revisions 77461 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58095
diff changeset
67 ADJUST_INDICES(start, end, str_len);
72475
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 69473
diff changeset
68 return STRINGLIB(find)(str + start, end - start, sub, sub_len, start);
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
69 }
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
70
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
71 Py_LOCAL_INLINE(Py_ssize_t)
72475
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 69473
diff changeset
72 STRINGLIB(rfind_slice)(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
73 const STRINGLIB_CHAR* sub, Py_ssize_t sub_len,
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
74 Py_ssize_t start, Py_ssize_t end)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
75 {
58289
5ca1948a8fd6 Merged revisions 77461 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58095
diff changeset
76 ADJUST_INDICES(start, end, str_len);
72475
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 69473
diff changeset
77 return STRINGLIB(rfind)(str + start, end - start, sub, sub_len, start);
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
78 }
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
79
48886
6ceb7c89e776 Removed merge glitch from stringlib/find.h as explained on the python committers list. The FROM_BYTEARRAY isn't required here.
Christian Heimes <christian@cheimes.de>
parents: 48870
diff changeset
80 #ifdef STRINGLIB_WANT_CONTAINS_OBJ
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
81
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
82 Py_LOCAL_INLINE(int)
72475
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 69473
diff changeset
83 STRINGLIB(contains_obj)(PyObject* str, PyObject* sub)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
84 {
72475
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 69473
diff changeset
85 return STRINGLIB(find)(
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
86 STRINGLIB_STR(str), STRINGLIB_LEN(str),
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
87 STRINGLIB_STR(sub), STRINGLIB_LEN(sub), 0
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
88 ) != -1;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
89 }
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
90
58289
5ca1948a8fd6 Merged revisions 77461 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58095
diff changeset
91 #endif /* STRINGLIB_WANT_CONTAINS_OBJ */
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
diff changeset
92
43897
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43576
diff changeset
93 /*
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43576
diff changeset
94 This function is a helper for the "find" family (find, rfind, index,
69472
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 49310
diff changeset
95 rindex) and for count, startswith and endswith, because they all have
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 49310
diff changeset
96 the same behaviour for the arguments.
43897
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43576
diff changeset
97
73185
f61db5671cc8 Replace PyUnicodeObject* by PyObject* where it was irrevelant
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73038
diff changeset
98 It does not touch the variables received until it knows everything
43897
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43576
diff changeset
99 is ok.
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43576
diff changeset
100 */
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43576
diff changeset
101
69472
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 49310
diff changeset
102 #define FORMAT_BUFFER_SIZE 50
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 49310
diff changeset
103
43897
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43576
diff changeset
104 Py_LOCAL_INLINE(int)
72475
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 69473
diff changeset
105 STRINGLIB(parse_args_finds)(const char * function_name, PyObject *args,
69472
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 49310
diff changeset
106 PyObject **subobj,
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 49310
diff changeset
107 Py_ssize_t *start, Py_ssize_t *end)
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 49310
diff changeset
108 {
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 49310
diff changeset
109 PyObject *tmp_subobj;
43897
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43576
diff changeset
110 Py_ssize_t tmp_start = 0;
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43576
diff changeset
111 Py_ssize_t tmp_end = PY_SSIZE_T_MAX;
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43576
diff changeset
112 PyObject *obj_start=Py_None, *obj_end=Py_None;
69472
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 49310
diff changeset
113 char format[FORMAT_BUFFER_SIZE] = "O|OO:";
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 49310
diff changeset
114 size_t len = strlen(format);
43897
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43576
diff changeset
115
69472
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 49310
diff changeset
116 strncpy(format + len, function_name, FORMAT_BUFFER_SIZE - len - 1);
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 49310
diff changeset
117 format[FORMAT_BUFFER_SIZE - 1] = '\0';
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 49310
diff changeset
118
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 49310
diff changeset
119 if (!PyArg_ParseTuple(args, format, &tmp_subobj, &obj_start, &obj_end))
43897
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43576
diff changeset
120 return 0;
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43576
diff changeset
121
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43576
diff changeset
122 /* To support None in "start" and "end" arguments, meaning
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43576
diff changeset
123 the same as if they were not passed.
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43576
diff changeset
124 */
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43576
diff changeset
125 if (obj_start != Py_None)
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43576
diff changeset
126 if (!_PyEval_SliceIndex(obj_start, &tmp_start))
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43576
diff changeset
127 return 0;
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43576
diff changeset
128 if (obj_end != Py_None)
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43576
diff changeset
129 if (!_PyEval_SliceIndex(obj_end, &tmp_end))
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43576
diff changeset
130 return 0;
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43576
diff changeset
131
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43576
diff changeset
132 *start = tmp_start;
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43576
diff changeset
133 *end = tmp_end;
69472
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 49310
diff changeset
134 *subobj = tmp_subobj;
43897
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43576
diff changeset
135 return 1;
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43576
diff changeset
136 }
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43576
diff changeset
137
69472
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 49310
diff changeset
138 #undef FORMAT_BUFFER_SIZE
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 49310
diff changeset
139
69473
e5f11efe89a6 MERGE: startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 58289 69472
diff changeset
140 #if STRINGLIB_IS_UNICODE
69472
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 49310
diff changeset
141
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 49310
diff changeset
142 /*
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 49310
diff changeset
143 Wraps stringlib_parse_args_finds() and additionally ensures that the
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 49310
diff changeset
144 first argument is a unicode object.
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 49310
diff changeset
145
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 49310
diff changeset
146 Note that we receive a pointer to the pointer of the substring object,
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 49310
diff changeset
147 so when we create that object in this function we don't DECREF it,
73185
f61db5671cc8 Replace PyUnicodeObject* by PyObject* where it was irrevelant
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73038
diff changeset
148 because it continues living in the caller functions (those functions,
69472
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 49310
diff changeset
149 after finishing using the substring, must DECREF it).
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 49310
diff changeset
150 */
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 49310
diff changeset
151
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 49310
diff changeset
152 Py_LOCAL_INLINE(int)
72475
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 69473
diff changeset
153 STRINGLIB(parse_args_finds_unicode)(const char * function_name, PyObject *args,
73185
f61db5671cc8 Replace PyUnicodeObject* by PyObject* where it was irrevelant
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73038
diff changeset
154 PyObject **substring,
69472
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 49310
diff changeset
155 Py_ssize_t *start, Py_ssize_t *end)
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 49310
diff changeset
156 {
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 49310
diff changeset
157 PyObject *tmp_substring;
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 49310
diff changeset
158
72475
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 69473
diff changeset
159 if(STRINGLIB(parse_args_finds)(function_name, args, &tmp_substring,
69472
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 49310
diff changeset
160 start, end)) {
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 49310
diff changeset
161 tmp_substring = PyUnicode_FromObject(tmp_substring);
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 49310
diff changeset
162 if (!tmp_substring)
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 49310
diff changeset
163 return 0;
73185
f61db5671cc8 Replace PyUnicodeObject* by PyObject* where it was irrevelant
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73038
diff changeset
164 *substring = tmp_substring;
69472
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 49310
diff changeset
165 return 1;
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 49310
diff changeset
166 }
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 49310
diff changeset
167 return 0;
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 49310
diff changeset
168 }
77c657e47b5c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 49310
diff changeset
169
73038
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 72475
diff changeset
170 #else /* !STRINGLIB_IS_UNICODE */
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 72475
diff changeset
171
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 72475
diff changeset
172 /*
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 72475
diff changeset
173 Wraps stringlib_parse_args_finds() and additionally checks whether the
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 72475
diff changeset
174 first argument is an integer in range(0, 256).
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 72475
diff changeset
175
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 72475
diff changeset
176 If this is the case, writes the integer value to the byte parameter
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 72475
diff changeset
177 and sets subobj to NULL. Otherwise, sets the first argument to subobj
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 72475
diff changeset
178 and doesn't touch byte. The other parameters are similar to those of
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 72475
diff changeset
179 stringlib_parse_args_finds().
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 72475
diff changeset
180 */
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 72475
diff changeset
181
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 72475
diff changeset
182 Py_LOCAL_INLINE(int)
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 72475
diff changeset
183 STRINGLIB(parse_args_finds_byte)(const char *function_name, PyObject *args,
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 72475
diff changeset
184 PyObject **subobj, char *byte,
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 72475
diff changeset
185 Py_ssize_t *start, Py_ssize_t *end)
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 72475
diff changeset
186 {
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 72475
diff changeset
187 PyObject *tmp_subobj;
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 72475
diff changeset
188 Py_ssize_t ival;
74022
75648db1b3f3 Issue #13623: Fix a performance regression introduced by issue #12170 in
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73185
diff changeset
189 PyObject *err;
73038
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 72475
diff changeset
190
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 72475
diff changeset
191 if(!STRINGLIB(parse_args_finds)(function_name, args, &tmp_subobj,
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 72475
diff changeset
192 start, end))
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 72475
diff changeset
193 return 0;
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 72475
diff changeset
194
74022
75648db1b3f3 Issue #13623: Fix a performance regression introduced by issue #12170 in
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73185
diff changeset
195 if (!PyNumber_Check(tmp_subobj)) {
73038
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 72475
diff changeset
196 *subobj = tmp_subobj;
74022
75648db1b3f3 Issue #13623: Fix a performance regression introduced by issue #12170 in
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73185
diff changeset
197 return 1;
73038
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 72475
diff changeset
198 }
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 72475
diff changeset
199
74022
75648db1b3f3 Issue #13623: Fix a performance regression introduced by issue #12170 in
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73185
diff changeset
200 ival = PyNumber_AsSsize_t(tmp_subobj, PyExc_OverflowError);
75648db1b3f3 Issue #13623: Fix a performance regression introduced by issue #12170 in
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73185
diff changeset
201 if (ival == -1) {
75648db1b3f3 Issue #13623: Fix a performance regression introduced by issue #12170 in
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73185
diff changeset
202 err = PyErr_Occurred();
75648db1b3f3 Issue #13623: Fix a performance regression introduced by issue #12170 in
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73185
diff changeset
203 if (err && !PyErr_GivenExceptionMatches(err, PyExc_OverflowError)) {
75648db1b3f3 Issue #13623: Fix a performance regression introduced by issue #12170 in
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73185
diff changeset
204 PyErr_Clear();
75648db1b3f3 Issue #13623: Fix a performance regression introduced by issue #12170 in
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73185
diff changeset
205 *subobj = tmp_subobj;
75648db1b3f3 Issue #13623: Fix a performance regression introduced by issue #12170 in
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73185
diff changeset
206 return 1;
75648db1b3f3 Issue #13623: Fix a performance regression introduced by issue #12170 in
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73185
diff changeset
207 }
75648db1b3f3 Issue #13623: Fix a performance regression introduced by issue #12170 in
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73185
diff changeset
208 }
75648db1b3f3 Issue #13623: Fix a performance regression introduced by issue #12170 in
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73185
diff changeset
209
75648db1b3f3 Issue #13623: Fix a performance regression introduced by issue #12170 in
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73185
diff changeset
210 if (ival < 0 || ival > 255) {
75648db1b3f3 Issue #13623: Fix a performance regression introduced by issue #12170 in
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73185
diff changeset
211 PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)");
75648db1b3f3 Issue #13623: Fix a performance regression introduced by issue #12170 in
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73185
diff changeset
212 return 0;
75648db1b3f3 Issue #13623: Fix a performance regression introduced by issue #12170 in
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73185
diff changeset
213 }
75648db1b3f3 Issue #13623: Fix a performance regression introduced by issue #12170 in
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73185
diff changeset
214
75648db1b3f3 Issue #13623: Fix a performance regression introduced by issue #12170 in
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73185
diff changeset
215 *subobj = NULL;
75648db1b3f3 Issue #13623: Fix a performance regression introduced by issue #12170 in
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73185
diff changeset
216 *byte = (char)ival;
73038
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 72475
diff changeset
217 return 1;
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 72475
diff changeset
218 }
c1effa2cdd20 Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents: 72475
diff changeset
219
58289
5ca1948a8fd6 Merged revisions 77461 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58095
diff changeset
220 #endif /* STRINGLIB_IS_UNICODE */