1 | n/a | #define PY_SSIZE_T_CLEAN |
---|
2 | n/a | #include "Python.h" |
---|
3 | n/a | #include "bytes_methods.h" |
---|
4 | n/a | |
---|
5 | n/a | PyDoc_STRVAR_shared(_Py_isspace__doc__, |
---|
6 | n/a | "B.isspace() -> bool\n\ |
---|
7 | n/a | \n\ |
---|
8 | n/a | Return True if all characters in B are whitespace\n\ |
---|
9 | n/a | and there is at least one character in B, False otherwise."); |
---|
10 | n/a | |
---|
11 | n/a | PyObject* |
---|
12 | n/a | _Py_bytes_isspace(const char *cptr, Py_ssize_t len) |
---|
13 | n/a | { |
---|
14 | n/a | const unsigned char *p |
---|
15 | n/a | = (unsigned char *) cptr; |
---|
16 | n/a | const unsigned char *e; |
---|
17 | n/a | |
---|
18 | n/a | /* Shortcut for single character strings */ |
---|
19 | n/a | if (len == 1 && Py_ISSPACE(*p)) |
---|
20 | n/a | Py_RETURN_TRUE; |
---|
21 | n/a | |
---|
22 | n/a | /* Special case for empty strings */ |
---|
23 | n/a | if (len == 0) |
---|
24 | n/a | Py_RETURN_FALSE; |
---|
25 | n/a | |
---|
26 | n/a | e = p + len; |
---|
27 | n/a | for (; p < e; p++) { |
---|
28 | n/a | if (!Py_ISSPACE(*p)) |
---|
29 | n/a | Py_RETURN_FALSE; |
---|
30 | n/a | } |
---|
31 | n/a | Py_RETURN_TRUE; |
---|
32 | n/a | } |
---|
33 | n/a | |
---|
34 | n/a | |
---|
35 | n/a | PyDoc_STRVAR_shared(_Py_isalpha__doc__, |
---|
36 | n/a | "B.isalpha() -> bool\n\ |
---|
37 | n/a | \n\ |
---|
38 | n/a | Return True if all characters in B are alphabetic\n\ |
---|
39 | n/a | and there is at least one character in B, False otherwise."); |
---|
40 | n/a | |
---|
41 | n/a | PyObject* |
---|
42 | n/a | _Py_bytes_isalpha(const char *cptr, Py_ssize_t len) |
---|
43 | n/a | { |
---|
44 | n/a | const unsigned char *p |
---|
45 | n/a | = (unsigned char *) cptr; |
---|
46 | n/a | const unsigned char *e; |
---|
47 | n/a | |
---|
48 | n/a | /* Shortcut for single character strings */ |
---|
49 | n/a | if (len == 1 && Py_ISALPHA(*p)) |
---|
50 | n/a | Py_RETURN_TRUE; |
---|
51 | n/a | |
---|
52 | n/a | /* Special case for empty strings */ |
---|
53 | n/a | if (len == 0) |
---|
54 | n/a | Py_RETURN_FALSE; |
---|
55 | n/a | |
---|
56 | n/a | e = p + len; |
---|
57 | n/a | for (; p < e; p++) { |
---|
58 | n/a | if (!Py_ISALPHA(*p)) |
---|
59 | n/a | Py_RETURN_FALSE; |
---|
60 | n/a | } |
---|
61 | n/a | Py_RETURN_TRUE; |
---|
62 | n/a | } |
---|
63 | n/a | |
---|
64 | n/a | |
---|
65 | n/a | PyDoc_STRVAR_shared(_Py_isalnum__doc__, |
---|
66 | n/a | "B.isalnum() -> bool\n\ |
---|
67 | n/a | \n\ |
---|
68 | n/a | Return True if all characters in B are alphanumeric\n\ |
---|
69 | n/a | and there is at least one character in B, False otherwise."); |
---|
70 | n/a | |
---|
71 | n/a | PyObject* |
---|
72 | n/a | _Py_bytes_isalnum(const char *cptr, Py_ssize_t len) |
---|
73 | n/a | { |
---|
74 | n/a | const unsigned char *p |
---|
75 | n/a | = (unsigned char *) cptr; |
---|
76 | n/a | const unsigned char *e; |
---|
77 | n/a | |
---|
78 | n/a | /* Shortcut for single character strings */ |
---|
79 | n/a | if (len == 1 && Py_ISALNUM(*p)) |
---|
80 | n/a | Py_RETURN_TRUE; |
---|
81 | n/a | |
---|
82 | n/a | /* Special case for empty strings */ |
---|
83 | n/a | if (len == 0) |
---|
84 | n/a | Py_RETURN_FALSE; |
---|
85 | n/a | |
---|
86 | n/a | e = p + len; |
---|
87 | n/a | for (; p < e; p++) { |
---|
88 | n/a | if (!Py_ISALNUM(*p)) |
---|
89 | n/a | Py_RETURN_FALSE; |
---|
90 | n/a | } |
---|
91 | n/a | Py_RETURN_TRUE; |
---|
92 | n/a | } |
---|
93 | n/a | |
---|
94 | n/a | |
---|
95 | n/a | PyDoc_STRVAR_shared(_Py_isdigit__doc__, |
---|
96 | n/a | "B.isdigit() -> bool\n\ |
---|
97 | n/a | \n\ |
---|
98 | n/a | Return True if all characters in B are digits\n\ |
---|
99 | n/a | and there is at least one character in B, False otherwise."); |
---|
100 | n/a | |
---|
101 | n/a | PyObject* |
---|
102 | n/a | _Py_bytes_isdigit(const char *cptr, Py_ssize_t len) |
---|
103 | n/a | { |
---|
104 | n/a | const unsigned char *p |
---|
105 | n/a | = (unsigned char *) cptr; |
---|
106 | n/a | const unsigned char *e; |
---|
107 | n/a | |
---|
108 | n/a | /* Shortcut for single character strings */ |
---|
109 | n/a | if (len == 1 && Py_ISDIGIT(*p)) |
---|
110 | n/a | Py_RETURN_TRUE; |
---|
111 | n/a | |
---|
112 | n/a | /* Special case for empty strings */ |
---|
113 | n/a | if (len == 0) |
---|
114 | n/a | Py_RETURN_FALSE; |
---|
115 | n/a | |
---|
116 | n/a | e = p + len; |
---|
117 | n/a | for (; p < e; p++) { |
---|
118 | n/a | if (!Py_ISDIGIT(*p)) |
---|
119 | n/a | Py_RETURN_FALSE; |
---|
120 | n/a | } |
---|
121 | n/a | Py_RETURN_TRUE; |
---|
122 | n/a | } |
---|
123 | n/a | |
---|
124 | n/a | |
---|
125 | n/a | PyDoc_STRVAR_shared(_Py_islower__doc__, |
---|
126 | n/a | "B.islower() -> bool\n\ |
---|
127 | n/a | \n\ |
---|
128 | n/a | Return True if all cased characters in B are lowercase and there is\n\ |
---|
129 | n/a | at least one cased character in B, False otherwise."); |
---|
130 | n/a | |
---|
131 | n/a | PyObject* |
---|
132 | n/a | _Py_bytes_islower(const char *cptr, Py_ssize_t len) |
---|
133 | n/a | { |
---|
134 | n/a | const unsigned char *p |
---|
135 | n/a | = (unsigned char *) cptr; |
---|
136 | n/a | const unsigned char *e; |
---|
137 | n/a | int cased; |
---|
138 | n/a | |
---|
139 | n/a | /* Shortcut for single character strings */ |
---|
140 | n/a | if (len == 1) |
---|
141 | n/a | return PyBool_FromLong(Py_ISLOWER(*p)); |
---|
142 | n/a | |
---|
143 | n/a | /* Special case for empty strings */ |
---|
144 | n/a | if (len == 0) |
---|
145 | n/a | Py_RETURN_FALSE; |
---|
146 | n/a | |
---|
147 | n/a | e = p + len; |
---|
148 | n/a | cased = 0; |
---|
149 | n/a | for (; p < e; p++) { |
---|
150 | n/a | if (Py_ISUPPER(*p)) |
---|
151 | n/a | Py_RETURN_FALSE; |
---|
152 | n/a | else if (!cased && Py_ISLOWER(*p)) |
---|
153 | n/a | cased = 1; |
---|
154 | n/a | } |
---|
155 | n/a | return PyBool_FromLong(cased); |
---|
156 | n/a | } |
---|
157 | n/a | |
---|
158 | n/a | |
---|
159 | n/a | PyDoc_STRVAR_shared(_Py_isupper__doc__, |
---|
160 | n/a | "B.isupper() -> bool\n\ |
---|
161 | n/a | \n\ |
---|
162 | n/a | Return True if all cased characters in B are uppercase and there is\n\ |
---|
163 | n/a | at least one cased character in B, False otherwise."); |
---|
164 | n/a | |
---|
165 | n/a | PyObject* |
---|
166 | n/a | _Py_bytes_isupper(const char *cptr, Py_ssize_t len) |
---|
167 | n/a | { |
---|
168 | n/a | const unsigned char *p |
---|
169 | n/a | = (unsigned char *) cptr; |
---|
170 | n/a | const unsigned char *e; |
---|
171 | n/a | int cased; |
---|
172 | n/a | |
---|
173 | n/a | /* Shortcut for single character strings */ |
---|
174 | n/a | if (len == 1) |
---|
175 | n/a | return PyBool_FromLong(Py_ISUPPER(*p)); |
---|
176 | n/a | |
---|
177 | n/a | /* Special case for empty strings */ |
---|
178 | n/a | if (len == 0) |
---|
179 | n/a | Py_RETURN_FALSE; |
---|
180 | n/a | |
---|
181 | n/a | e = p + len; |
---|
182 | n/a | cased = 0; |
---|
183 | n/a | for (; p < e; p++) { |
---|
184 | n/a | if (Py_ISLOWER(*p)) |
---|
185 | n/a | Py_RETURN_FALSE; |
---|
186 | n/a | else if (!cased && Py_ISUPPER(*p)) |
---|
187 | n/a | cased = 1; |
---|
188 | n/a | } |
---|
189 | n/a | return PyBool_FromLong(cased); |
---|
190 | n/a | } |
---|
191 | n/a | |
---|
192 | n/a | |
---|
193 | n/a | PyDoc_STRVAR_shared(_Py_istitle__doc__, |
---|
194 | n/a | "B.istitle() -> bool\n\ |
---|
195 | n/a | \n\ |
---|
196 | n/a | Return True if B is a titlecased string and there is at least one\n\ |
---|
197 | n/a | character in B, i.e. uppercase characters may only follow uncased\n\ |
---|
198 | n/a | characters and lowercase characters only cased ones. Return False\n\ |
---|
199 | n/a | otherwise."); |
---|
200 | n/a | |
---|
201 | n/a | PyObject* |
---|
202 | n/a | _Py_bytes_istitle(const char *cptr, Py_ssize_t len) |
---|
203 | n/a | { |
---|
204 | n/a | const unsigned char *p |
---|
205 | n/a | = (unsigned char *) cptr; |
---|
206 | n/a | const unsigned char *e; |
---|
207 | n/a | int cased, previous_is_cased; |
---|
208 | n/a | |
---|
209 | n/a | /* Shortcut for single character strings */ |
---|
210 | n/a | if (len == 1) |
---|
211 | n/a | return PyBool_FromLong(Py_ISUPPER(*p)); |
---|
212 | n/a | |
---|
213 | n/a | /* Special case for empty strings */ |
---|
214 | n/a | if (len == 0) |
---|
215 | n/a | Py_RETURN_FALSE; |
---|
216 | n/a | |
---|
217 | n/a | e = p + len; |
---|
218 | n/a | cased = 0; |
---|
219 | n/a | previous_is_cased = 0; |
---|
220 | n/a | for (; p < e; p++) { |
---|
221 | n/a | const unsigned char ch = *p; |
---|
222 | n/a | |
---|
223 | n/a | if (Py_ISUPPER(ch)) { |
---|
224 | n/a | if (previous_is_cased) |
---|
225 | n/a | Py_RETURN_FALSE; |
---|
226 | n/a | previous_is_cased = 1; |
---|
227 | n/a | cased = 1; |
---|
228 | n/a | } |
---|
229 | n/a | else if (Py_ISLOWER(ch)) { |
---|
230 | n/a | if (!previous_is_cased) |
---|
231 | n/a | Py_RETURN_FALSE; |
---|
232 | n/a | previous_is_cased = 1; |
---|
233 | n/a | cased = 1; |
---|
234 | n/a | } |
---|
235 | n/a | else |
---|
236 | n/a | previous_is_cased = 0; |
---|
237 | n/a | } |
---|
238 | n/a | return PyBool_FromLong(cased); |
---|
239 | n/a | } |
---|
240 | n/a | |
---|
241 | n/a | |
---|
242 | n/a | PyDoc_STRVAR_shared(_Py_lower__doc__, |
---|
243 | n/a | "B.lower() -> copy of B\n\ |
---|
244 | n/a | \n\ |
---|
245 | n/a | Return a copy of B with all ASCII characters converted to lowercase."); |
---|
246 | n/a | |
---|
247 | n/a | void |
---|
248 | n/a | _Py_bytes_lower(char *result, const char *cptr, Py_ssize_t len) |
---|
249 | n/a | { |
---|
250 | n/a | Py_ssize_t i; |
---|
251 | n/a | |
---|
252 | n/a | for (i = 0; i < len; i++) { |
---|
253 | n/a | result[i] = Py_TOLOWER((unsigned char) cptr[i]); |
---|
254 | n/a | } |
---|
255 | n/a | } |
---|
256 | n/a | |
---|
257 | n/a | |
---|
258 | n/a | PyDoc_STRVAR_shared(_Py_upper__doc__, |
---|
259 | n/a | "B.upper() -> copy of B\n\ |
---|
260 | n/a | \n\ |
---|
261 | n/a | Return a copy of B with all ASCII characters converted to uppercase."); |
---|
262 | n/a | |
---|
263 | n/a | void |
---|
264 | n/a | _Py_bytes_upper(char *result, const char *cptr, Py_ssize_t len) |
---|
265 | n/a | { |
---|
266 | n/a | Py_ssize_t i; |
---|
267 | n/a | |
---|
268 | n/a | for (i = 0; i < len; i++) { |
---|
269 | n/a | result[i] = Py_TOUPPER((unsigned char) cptr[i]); |
---|
270 | n/a | } |
---|
271 | n/a | } |
---|
272 | n/a | |
---|
273 | n/a | |
---|
274 | n/a | PyDoc_STRVAR_shared(_Py_title__doc__, |
---|
275 | n/a | "B.title() -> copy of B\n\ |
---|
276 | n/a | \n\ |
---|
277 | n/a | Return a titlecased version of B, i.e. ASCII words start with uppercase\n\ |
---|
278 | n/a | characters, all remaining cased characters have lowercase."); |
---|
279 | n/a | |
---|
280 | n/a | void |
---|
281 | n/a | _Py_bytes_title(char *result, const char *s, Py_ssize_t len) |
---|
282 | n/a | { |
---|
283 | n/a | Py_ssize_t i; |
---|
284 | n/a | int previous_is_cased = 0; |
---|
285 | n/a | |
---|
286 | n/a | for (i = 0; i < len; i++) { |
---|
287 | n/a | int c = Py_CHARMASK(*s++); |
---|
288 | n/a | if (Py_ISLOWER(c)) { |
---|
289 | n/a | if (!previous_is_cased) |
---|
290 | n/a | c = Py_TOUPPER(c); |
---|
291 | n/a | previous_is_cased = 1; |
---|
292 | n/a | } else if (Py_ISUPPER(c)) { |
---|
293 | n/a | if (previous_is_cased) |
---|
294 | n/a | c = Py_TOLOWER(c); |
---|
295 | n/a | previous_is_cased = 1; |
---|
296 | n/a | } else |
---|
297 | n/a | previous_is_cased = 0; |
---|
298 | n/a | *result++ = c; |
---|
299 | n/a | } |
---|
300 | n/a | } |
---|
301 | n/a | |
---|
302 | n/a | |
---|
303 | n/a | PyDoc_STRVAR_shared(_Py_capitalize__doc__, |
---|
304 | n/a | "B.capitalize() -> copy of B\n\ |
---|
305 | n/a | \n\ |
---|
306 | n/a | Return a copy of B with only its first character capitalized (ASCII)\n\ |
---|
307 | n/a | and the rest lower-cased."); |
---|
308 | n/a | |
---|
309 | n/a | void |
---|
310 | n/a | _Py_bytes_capitalize(char *result, const char *s, Py_ssize_t len) |
---|
311 | n/a | { |
---|
312 | n/a | Py_ssize_t i; |
---|
313 | n/a | |
---|
314 | n/a | if (0 < len) { |
---|
315 | n/a | int c = Py_CHARMASK(*s++); |
---|
316 | n/a | if (Py_ISLOWER(c)) |
---|
317 | n/a | *result = Py_TOUPPER(c); |
---|
318 | n/a | else |
---|
319 | n/a | *result = c; |
---|
320 | n/a | result++; |
---|
321 | n/a | } |
---|
322 | n/a | for (i = 1; i < len; i++) { |
---|
323 | n/a | int c = Py_CHARMASK(*s++); |
---|
324 | n/a | if (Py_ISUPPER(c)) |
---|
325 | n/a | *result = Py_TOLOWER(c); |
---|
326 | n/a | else |
---|
327 | n/a | *result = c; |
---|
328 | n/a | result++; |
---|
329 | n/a | } |
---|
330 | n/a | } |
---|
331 | n/a | |
---|
332 | n/a | |
---|
333 | n/a | PyDoc_STRVAR_shared(_Py_swapcase__doc__, |
---|
334 | n/a | "B.swapcase() -> copy of B\n\ |
---|
335 | n/a | \n\ |
---|
336 | n/a | Return a copy of B with uppercase ASCII characters converted\n\ |
---|
337 | n/a | to lowercase ASCII and vice versa."); |
---|
338 | n/a | |
---|
339 | n/a | void |
---|
340 | n/a | _Py_bytes_swapcase(char *result, const char *s, Py_ssize_t len) |
---|
341 | n/a | { |
---|
342 | n/a | Py_ssize_t i; |
---|
343 | n/a | |
---|
344 | n/a | for (i = 0; i < len; i++) { |
---|
345 | n/a | int c = Py_CHARMASK(*s++); |
---|
346 | n/a | if (Py_ISLOWER(c)) { |
---|
347 | n/a | *result = Py_TOUPPER(c); |
---|
348 | n/a | } |
---|
349 | n/a | else if (Py_ISUPPER(c)) { |
---|
350 | n/a | *result = Py_TOLOWER(c); |
---|
351 | n/a | } |
---|
352 | n/a | else |
---|
353 | n/a | *result = c; |
---|
354 | n/a | result++; |
---|
355 | n/a | } |
---|
356 | n/a | } |
---|
357 | n/a | |
---|
358 | n/a | |
---|
359 | n/a | PyDoc_STRVAR_shared(_Py_maketrans__doc__, |
---|
360 | n/a | "B.maketrans(frm, to) -> translation table\n\ |
---|
361 | n/a | \n\ |
---|
362 | n/a | Return a translation table (a bytes object of length 256) suitable\n\ |
---|
363 | n/a | for use in the bytes or bytearray translate method where each byte\n\ |
---|
364 | n/a | in frm is mapped to the byte at the same position in to.\n\ |
---|
365 | n/a | The bytes objects frm and to must be of the same length."); |
---|
366 | n/a | |
---|
367 | n/a | PyObject * |
---|
368 | n/a | _Py_bytes_maketrans(Py_buffer *frm, Py_buffer *to) |
---|
369 | n/a | { |
---|
370 | n/a | PyObject *res = NULL; |
---|
371 | n/a | Py_ssize_t i; |
---|
372 | n/a | char *p; |
---|
373 | n/a | |
---|
374 | n/a | if (frm->len != to->len) { |
---|
375 | n/a | PyErr_Format(PyExc_ValueError, |
---|
376 | n/a | "maketrans arguments must have same length"); |
---|
377 | n/a | return NULL; |
---|
378 | n/a | } |
---|
379 | n/a | res = PyBytes_FromStringAndSize(NULL, 256); |
---|
380 | n/a | if (!res) |
---|
381 | n/a | return NULL; |
---|
382 | n/a | p = PyBytes_AS_STRING(res); |
---|
383 | n/a | for (i = 0; i < 256; i++) |
---|
384 | n/a | p[i] = (char) i; |
---|
385 | n/a | for (i = 0; i < frm->len; i++) { |
---|
386 | n/a | p[((unsigned char *)frm->buf)[i]] = ((char *)to->buf)[i]; |
---|
387 | n/a | } |
---|
388 | n/a | |
---|
389 | n/a | return res; |
---|
390 | n/a | } |
---|
391 | n/a | |
---|
392 | n/a | #define FASTSEARCH fastsearch |
---|
393 | n/a | #define STRINGLIB(F) stringlib_##F |
---|
394 | n/a | #define STRINGLIB_CHAR char |
---|
395 | n/a | #define STRINGLIB_SIZEOF_CHAR 1 |
---|
396 | n/a | |
---|
397 | n/a | #include "stringlib/fastsearch.h" |
---|
398 | n/a | #include "stringlib/count.h" |
---|
399 | n/a | #include "stringlib/find.h" |
---|
400 | n/a | |
---|
401 | n/a | /* |
---|
402 | n/a | Wraps stringlib_parse_args_finds() and additionally checks whether the |
---|
403 | n/a | first argument is an integer in range(0, 256). |
---|
404 | n/a | |
---|
405 | n/a | If this is the case, writes the integer value to the byte parameter |
---|
406 | n/a | and sets subobj to NULL. Otherwise, sets the first argument to subobj |
---|
407 | n/a | and doesn't touch byte. The other parameters are similar to those of |
---|
408 | n/a | stringlib_parse_args_finds(). |
---|
409 | n/a | */ |
---|
410 | n/a | |
---|
411 | n/a | Py_LOCAL_INLINE(int) |
---|
412 | n/a | parse_args_finds_byte(const char *function_name, PyObject *args, |
---|
413 | n/a | PyObject **subobj, char *byte, |
---|
414 | n/a | Py_ssize_t *start, Py_ssize_t *end) |
---|
415 | n/a | { |
---|
416 | n/a | PyObject *tmp_subobj; |
---|
417 | n/a | Py_ssize_t ival; |
---|
418 | n/a | PyObject *err; |
---|
419 | n/a | |
---|
420 | n/a | if(!stringlib_parse_args_finds(function_name, args, &tmp_subobj, |
---|
421 | n/a | start, end)) |
---|
422 | n/a | return 0; |
---|
423 | n/a | |
---|
424 | n/a | if (!PyNumber_Check(tmp_subobj)) { |
---|
425 | n/a | *subobj = tmp_subobj; |
---|
426 | n/a | return 1; |
---|
427 | n/a | } |
---|
428 | n/a | |
---|
429 | n/a | ival = PyNumber_AsSsize_t(tmp_subobj, PyExc_OverflowError); |
---|
430 | n/a | if (ival == -1) { |
---|
431 | n/a | err = PyErr_Occurred(); |
---|
432 | n/a | if (err && !PyErr_GivenExceptionMatches(err, PyExc_OverflowError)) { |
---|
433 | n/a | PyErr_Clear(); |
---|
434 | n/a | *subobj = tmp_subobj; |
---|
435 | n/a | return 1; |
---|
436 | n/a | } |
---|
437 | n/a | } |
---|
438 | n/a | |
---|
439 | n/a | if (ival < 0 || ival > 255) { |
---|
440 | n/a | PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)"); |
---|
441 | n/a | return 0; |
---|
442 | n/a | } |
---|
443 | n/a | |
---|
444 | n/a | *subobj = NULL; |
---|
445 | n/a | *byte = (char)ival; |
---|
446 | n/a | return 1; |
---|
447 | n/a | } |
---|
448 | n/a | |
---|
449 | n/a | /* helper macro to fixup start/end slice values */ |
---|
450 | n/a | #define ADJUST_INDICES(start, end, len) \ |
---|
451 | n/a | if (end > len) \ |
---|
452 | n/a | end = len; \ |
---|
453 | n/a | else if (end < 0) { \ |
---|
454 | n/a | end += len; \ |
---|
455 | n/a | if (end < 0) \ |
---|
456 | n/a | end = 0; \ |
---|
457 | n/a | } \ |
---|
458 | n/a | if (start < 0) { \ |
---|
459 | n/a | start += len; \ |
---|
460 | n/a | if (start < 0) \ |
---|
461 | n/a | start = 0; \ |
---|
462 | n/a | } |
---|
463 | n/a | |
---|
464 | n/a | Py_LOCAL_INLINE(Py_ssize_t) |
---|
465 | n/a | find_internal(const char *str, Py_ssize_t len, |
---|
466 | n/a | const char *function_name, PyObject *args, int dir) |
---|
467 | n/a | { |
---|
468 | n/a | PyObject *subobj; |
---|
469 | n/a | char byte; |
---|
470 | n/a | Py_buffer subbuf; |
---|
471 | n/a | const char *sub; |
---|
472 | n/a | Py_ssize_t sub_len; |
---|
473 | n/a | Py_ssize_t start = 0, end = PY_SSIZE_T_MAX; |
---|
474 | n/a | Py_ssize_t res; |
---|
475 | n/a | |
---|
476 | n/a | if (!parse_args_finds_byte(function_name, args, |
---|
477 | n/a | &subobj, &byte, &start, &end)) |
---|
478 | n/a | return -2; |
---|
479 | n/a | |
---|
480 | n/a | if (subobj) { |
---|
481 | n/a | if (PyObject_GetBuffer(subobj, &subbuf, PyBUF_SIMPLE) != 0) |
---|
482 | n/a | return -2; |
---|
483 | n/a | |
---|
484 | n/a | sub = subbuf.buf; |
---|
485 | n/a | sub_len = subbuf.len; |
---|
486 | n/a | } |
---|
487 | n/a | else { |
---|
488 | n/a | sub = &byte; |
---|
489 | n/a | sub_len = 1; |
---|
490 | n/a | } |
---|
491 | n/a | |
---|
492 | n/a | ADJUST_INDICES(start, end, len); |
---|
493 | n/a | if (end - start < sub_len) |
---|
494 | n/a | res = -1; |
---|
495 | n/a | else if (sub_len == 1) { |
---|
496 | n/a | if (dir > 0) |
---|
497 | n/a | res = stringlib_find_char( |
---|
498 | n/a | str + start, end - start, |
---|
499 | n/a | *sub); |
---|
500 | n/a | else |
---|
501 | n/a | res = stringlib_rfind_char( |
---|
502 | n/a | str + start, end - start, |
---|
503 | n/a | *sub); |
---|
504 | n/a | if (res >= 0) |
---|
505 | n/a | res += start; |
---|
506 | n/a | } |
---|
507 | n/a | else { |
---|
508 | n/a | if (dir > 0) |
---|
509 | n/a | res = stringlib_find_slice( |
---|
510 | n/a | str, len, |
---|
511 | n/a | sub, sub_len, start, end); |
---|
512 | n/a | else |
---|
513 | n/a | res = stringlib_rfind_slice( |
---|
514 | n/a | str, len, |
---|
515 | n/a | sub, sub_len, start, end); |
---|
516 | n/a | } |
---|
517 | n/a | |
---|
518 | n/a | if (subobj) |
---|
519 | n/a | PyBuffer_Release(&subbuf); |
---|
520 | n/a | |
---|
521 | n/a | return res; |
---|
522 | n/a | } |
---|
523 | n/a | |
---|
524 | n/a | PyDoc_STRVAR_shared(_Py_find__doc__, |
---|
525 | n/a | "B.find(sub[, start[, end]]) -> int\n\ |
---|
526 | n/a | \n\ |
---|
527 | n/a | Return the lowest index in B where subsection sub is found,\n\ |
---|
528 | n/a | such that sub is contained within B[start,end]. Optional\n\ |
---|
529 | n/a | arguments start and end are interpreted as in slice notation.\n\ |
---|
530 | n/a | \n\ |
---|
531 | n/a | Return -1 on failure."); |
---|
532 | n/a | |
---|
533 | n/a | PyObject * |
---|
534 | n/a | _Py_bytes_find(const char *str, Py_ssize_t len, PyObject *args) |
---|
535 | n/a | { |
---|
536 | n/a | Py_ssize_t result = find_internal(str, len, "find", args, +1); |
---|
537 | n/a | if (result == -2) |
---|
538 | n/a | return NULL; |
---|
539 | n/a | return PyLong_FromSsize_t(result); |
---|
540 | n/a | } |
---|
541 | n/a | |
---|
542 | n/a | PyDoc_STRVAR_shared(_Py_index__doc__, |
---|
543 | n/a | "B.index(sub[, start[, end]]) -> int\n\ |
---|
544 | n/a | \n\ |
---|
545 | n/a | Like B.find() but raise ValueError when the subsection is not found."); |
---|
546 | n/a | |
---|
547 | n/a | PyObject * |
---|
548 | n/a | _Py_bytes_index(const char *str, Py_ssize_t len, PyObject *args) |
---|
549 | n/a | { |
---|
550 | n/a | Py_ssize_t result = find_internal(str, len, "index", args, +1); |
---|
551 | n/a | if (result == -2) |
---|
552 | n/a | return NULL; |
---|
553 | n/a | if (result == -1) { |
---|
554 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
555 | n/a | "subsection not found"); |
---|
556 | n/a | return NULL; |
---|
557 | n/a | } |
---|
558 | n/a | return PyLong_FromSsize_t(result); |
---|
559 | n/a | } |
---|
560 | n/a | |
---|
561 | n/a | PyDoc_STRVAR_shared(_Py_rfind__doc__, |
---|
562 | n/a | "B.rfind(sub[, start[, end]]) -> int\n\ |
---|
563 | n/a | \n\ |
---|
564 | n/a | Return the highest index in B where subsection sub is found,\n\ |
---|
565 | n/a | such that sub is contained within B[start,end]. Optional\n\ |
---|
566 | n/a | arguments start and end are interpreted as in slice notation.\n\ |
---|
567 | n/a | \n\ |
---|
568 | n/a | Return -1 on failure."); |
---|
569 | n/a | |
---|
570 | n/a | PyObject * |
---|
571 | n/a | _Py_bytes_rfind(const char *str, Py_ssize_t len, PyObject *args) |
---|
572 | n/a | { |
---|
573 | n/a | Py_ssize_t result = find_internal(str, len, "rfind", args, -1); |
---|
574 | n/a | if (result == -2) |
---|
575 | n/a | return NULL; |
---|
576 | n/a | return PyLong_FromSsize_t(result); |
---|
577 | n/a | } |
---|
578 | n/a | |
---|
579 | n/a | PyDoc_STRVAR_shared(_Py_rindex__doc__, |
---|
580 | n/a | "B.rindex(sub[, start[, end]]) -> int\n\ |
---|
581 | n/a | \n\ |
---|
582 | n/a | Like B.rfind() but raise ValueError when the subsection is not found."); |
---|
583 | n/a | |
---|
584 | n/a | PyObject * |
---|
585 | n/a | _Py_bytes_rindex(const char *str, Py_ssize_t len, PyObject *args) |
---|
586 | n/a | { |
---|
587 | n/a | Py_ssize_t result = find_internal(str, len, "rindex", args, -1); |
---|
588 | n/a | if (result == -2) |
---|
589 | n/a | return NULL; |
---|
590 | n/a | if (result == -1) { |
---|
591 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
592 | n/a | "subsection not found"); |
---|
593 | n/a | return NULL; |
---|
594 | n/a | } |
---|
595 | n/a | return PyLong_FromSsize_t(result); |
---|
596 | n/a | } |
---|
597 | n/a | |
---|
598 | n/a | PyDoc_STRVAR_shared(_Py_count__doc__, |
---|
599 | n/a | "B.count(sub[, start[, end]]) -> int\n\ |
---|
600 | n/a | \n\ |
---|
601 | n/a | Return the number of non-overlapping occurrences of subsection sub in\n\ |
---|
602 | n/a | bytes B[start:end]. Optional arguments start and end are interpreted\n\ |
---|
603 | n/a | as in slice notation."); |
---|
604 | n/a | |
---|
605 | n/a | PyObject * |
---|
606 | n/a | _Py_bytes_count(const char *str, Py_ssize_t len, PyObject *args) |
---|
607 | n/a | { |
---|
608 | n/a | PyObject *sub_obj; |
---|
609 | n/a | const char *sub; |
---|
610 | n/a | Py_ssize_t sub_len; |
---|
611 | n/a | char byte; |
---|
612 | n/a | Py_ssize_t start = 0, end = PY_SSIZE_T_MAX; |
---|
613 | n/a | |
---|
614 | n/a | Py_buffer vsub; |
---|
615 | n/a | PyObject *count_obj; |
---|
616 | n/a | |
---|
617 | n/a | if (!parse_args_finds_byte("count", args, |
---|
618 | n/a | &sub_obj, &byte, &start, &end)) |
---|
619 | n/a | return NULL; |
---|
620 | n/a | |
---|
621 | n/a | if (sub_obj) { |
---|
622 | n/a | if (PyObject_GetBuffer(sub_obj, &vsub, PyBUF_SIMPLE) != 0) |
---|
623 | n/a | return NULL; |
---|
624 | n/a | |
---|
625 | n/a | sub = vsub.buf; |
---|
626 | n/a | sub_len = vsub.len; |
---|
627 | n/a | } |
---|
628 | n/a | else { |
---|
629 | n/a | sub = &byte; |
---|
630 | n/a | sub_len = 1; |
---|
631 | n/a | } |
---|
632 | n/a | |
---|
633 | n/a | ADJUST_INDICES(start, end, len); |
---|
634 | n/a | |
---|
635 | n/a | count_obj = PyLong_FromSsize_t( |
---|
636 | n/a | stringlib_count(str + start, end - start, sub, sub_len, PY_SSIZE_T_MAX) |
---|
637 | n/a | ); |
---|
638 | n/a | |
---|
639 | n/a | if (sub_obj) |
---|
640 | n/a | PyBuffer_Release(&vsub); |
---|
641 | n/a | |
---|
642 | n/a | return count_obj; |
---|
643 | n/a | } |
---|
644 | n/a | |
---|
645 | n/a | int |
---|
646 | n/a | _Py_bytes_contains(const char *str, Py_ssize_t len, PyObject *arg) |
---|
647 | n/a | { |
---|
648 | n/a | Py_ssize_t ival = PyNumber_AsSsize_t(arg, NULL); |
---|
649 | n/a | if (ival == -1 && PyErr_Occurred()) { |
---|
650 | n/a | Py_buffer varg; |
---|
651 | n/a | Py_ssize_t pos; |
---|
652 | n/a | PyErr_Clear(); |
---|
653 | n/a | if (PyObject_GetBuffer(arg, &varg, PyBUF_SIMPLE) != 0) |
---|
654 | n/a | return -1; |
---|
655 | n/a | pos = stringlib_find(str, len, |
---|
656 | n/a | varg.buf, varg.len, 0); |
---|
657 | n/a | PyBuffer_Release(&varg); |
---|
658 | n/a | return pos >= 0; |
---|
659 | n/a | } |
---|
660 | n/a | if (ival < 0 || ival >= 256) { |
---|
661 | n/a | PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)"); |
---|
662 | n/a | return -1; |
---|
663 | n/a | } |
---|
664 | n/a | |
---|
665 | n/a | return memchr(str, (int) ival, len) != NULL; |
---|
666 | n/a | } |
---|
667 | n/a | |
---|
668 | n/a | |
---|
669 | n/a | /* Matches the end (direction >= 0) or start (direction < 0) of the buffer |
---|
670 | n/a | * against substr, using the start and end arguments. Returns |
---|
671 | n/a | * -1 on error, 0 if not found and 1 if found. |
---|
672 | n/a | */ |
---|
673 | n/a | static int |
---|
674 | n/a | tailmatch(const char *str, Py_ssize_t len, PyObject *substr, |
---|
675 | n/a | Py_ssize_t start, Py_ssize_t end, int direction) |
---|
676 | n/a | { |
---|
677 | n/a | Py_buffer sub_view = {NULL, NULL}; |
---|
678 | n/a | const char *sub; |
---|
679 | n/a | Py_ssize_t slen; |
---|
680 | n/a | |
---|
681 | n/a | if (PyBytes_Check(substr)) { |
---|
682 | n/a | sub = PyBytes_AS_STRING(substr); |
---|
683 | n/a | slen = PyBytes_GET_SIZE(substr); |
---|
684 | n/a | } |
---|
685 | n/a | else { |
---|
686 | n/a | if (PyObject_GetBuffer(substr, &sub_view, PyBUF_SIMPLE) != 0) |
---|
687 | n/a | return -1; |
---|
688 | n/a | sub = sub_view.buf; |
---|
689 | n/a | slen = sub_view.len; |
---|
690 | n/a | } |
---|
691 | n/a | |
---|
692 | n/a | ADJUST_INDICES(start, end, len); |
---|
693 | n/a | |
---|
694 | n/a | if (direction < 0) { |
---|
695 | n/a | /* startswith */ |
---|
696 | n/a | if (start + slen > len) |
---|
697 | n/a | goto notfound; |
---|
698 | n/a | } else { |
---|
699 | n/a | /* endswith */ |
---|
700 | n/a | if (end - start < slen || start > len) |
---|
701 | n/a | goto notfound; |
---|
702 | n/a | |
---|
703 | n/a | if (end - slen > start) |
---|
704 | n/a | start = end - slen; |
---|
705 | n/a | } |
---|
706 | n/a | if (end - start < slen) |
---|
707 | n/a | goto notfound; |
---|
708 | n/a | if (memcmp(str + start, sub, slen) != 0) |
---|
709 | n/a | goto notfound; |
---|
710 | n/a | |
---|
711 | n/a | PyBuffer_Release(&sub_view); |
---|
712 | n/a | return 1; |
---|
713 | n/a | |
---|
714 | n/a | notfound: |
---|
715 | n/a | PyBuffer_Release(&sub_view); |
---|
716 | n/a | return 0; |
---|
717 | n/a | } |
---|
718 | n/a | |
---|
719 | n/a | static PyObject * |
---|
720 | n/a | _Py_bytes_tailmatch(const char *str, Py_ssize_t len, |
---|
721 | n/a | const char *function_name, PyObject *args, |
---|
722 | n/a | int direction) |
---|
723 | n/a | { |
---|
724 | n/a | Py_ssize_t start = 0; |
---|
725 | n/a | Py_ssize_t end = PY_SSIZE_T_MAX; |
---|
726 | n/a | PyObject *subobj; |
---|
727 | n/a | int result; |
---|
728 | n/a | |
---|
729 | n/a | if (!stringlib_parse_args_finds(function_name, args, &subobj, &start, &end)) |
---|
730 | n/a | return NULL; |
---|
731 | n/a | if (PyTuple_Check(subobj)) { |
---|
732 | n/a | Py_ssize_t i; |
---|
733 | n/a | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
---|
734 | n/a | result = tailmatch(str, len, PyTuple_GET_ITEM(subobj, i), |
---|
735 | n/a | start, end, direction); |
---|
736 | n/a | if (result == -1) |
---|
737 | n/a | return NULL; |
---|
738 | n/a | else if (result) { |
---|
739 | n/a | Py_RETURN_TRUE; |
---|
740 | n/a | } |
---|
741 | n/a | } |
---|
742 | n/a | Py_RETURN_FALSE; |
---|
743 | n/a | } |
---|
744 | n/a | result = tailmatch(str, len, subobj, start, end, direction); |
---|
745 | n/a | if (result == -1) { |
---|
746 | n/a | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
---|
747 | n/a | PyErr_Format(PyExc_TypeError, |
---|
748 | n/a | "%s first arg must be bytes or a tuple of bytes, " |
---|
749 | n/a | "not %s", |
---|
750 | n/a | function_name, Py_TYPE(subobj)->tp_name); |
---|
751 | n/a | return NULL; |
---|
752 | n/a | } |
---|
753 | n/a | else |
---|
754 | n/a | return PyBool_FromLong(result); |
---|
755 | n/a | } |
---|
756 | n/a | |
---|
757 | n/a | PyDoc_STRVAR_shared(_Py_startswith__doc__, |
---|
758 | n/a | "B.startswith(prefix[, start[, end]]) -> bool\n\ |
---|
759 | n/a | \n\ |
---|
760 | n/a | Return True if B starts with the specified prefix, False otherwise.\n\ |
---|
761 | n/a | With optional start, test B beginning at that position.\n\ |
---|
762 | n/a | With optional end, stop comparing B at that position.\n\ |
---|
763 | n/a | prefix can also be a tuple of bytes to try."); |
---|
764 | n/a | |
---|
765 | n/a | PyObject * |
---|
766 | n/a | _Py_bytes_startswith(const char *str, Py_ssize_t len, PyObject *args) |
---|
767 | n/a | { |
---|
768 | n/a | return _Py_bytes_tailmatch(str, len, "startswith", args, -1); |
---|
769 | n/a | } |
---|
770 | n/a | |
---|
771 | n/a | PyDoc_STRVAR_shared(_Py_endswith__doc__, |
---|
772 | n/a | "B.endswith(suffix[, start[, end]]) -> bool\n\ |
---|
773 | n/a | \n\ |
---|
774 | n/a | Return True if B ends with the specified suffix, False otherwise.\n\ |
---|
775 | n/a | With optional start, test B beginning at that position.\n\ |
---|
776 | n/a | With optional end, stop comparing B at that position.\n\ |
---|
777 | n/a | suffix can also be a tuple of bytes to try."); |
---|
778 | n/a | |
---|
779 | n/a | PyObject * |
---|
780 | n/a | _Py_bytes_endswith(const char *str, Py_ssize_t len, PyObject *args) |
---|
781 | n/a | { |
---|
782 | n/a | return _Py_bytes_tailmatch(str, len, "endswith", args, +1); |
---|
783 | n/a | } |
---|
784 | n/a | |
---|
785 | n/a | PyDoc_STRVAR_shared(_Py_expandtabs__doc__, |
---|
786 | n/a | "B.expandtabs(tabsize=8) -> copy of B\n\ |
---|
787 | n/a | \n\ |
---|
788 | n/a | Return a copy of B where all tab characters are expanded using spaces.\n\ |
---|
789 | n/a | If tabsize is not given, a tab size of 8 characters is assumed."); |
---|
790 | n/a | |
---|
791 | n/a | PyDoc_STRVAR_shared(_Py_ljust__doc__, |
---|
792 | n/a | "B.ljust(width[, fillchar]) -> copy of B\n" |
---|
793 | n/a | "\n" |
---|
794 | n/a | "Return B left justified in a string of length width. Padding is\n" |
---|
795 | n/a | "done using the specified fill character (default is a space)."); |
---|
796 | n/a | |
---|
797 | n/a | PyDoc_STRVAR_shared(_Py_rjust__doc__, |
---|
798 | n/a | "B.rjust(width[, fillchar]) -> copy of B\n" |
---|
799 | n/a | "\n" |
---|
800 | n/a | "Return B right justified in a string of length width. Padding is\n" |
---|
801 | n/a | "done using the specified fill character (default is a space)"); |
---|
802 | n/a | |
---|
803 | n/a | PyDoc_STRVAR_shared(_Py_center__doc__, |
---|
804 | n/a | "B.center(width[, fillchar]) -> copy of B\n" |
---|
805 | n/a | "\n" |
---|
806 | n/a | "Return B centered in a string of length width. Padding is\n" |
---|
807 | n/a | "done using the specified fill character (default is a space)."); |
---|
808 | n/a | |
---|
809 | n/a | PyDoc_STRVAR_shared(_Py_zfill__doc__, |
---|
810 | n/a | "B.zfill(width) -> copy of B\n" |
---|
811 | n/a | "\n" |
---|
812 | n/a | "Pad a numeric string B with zeros on the left, to fill a field\n" |
---|
813 | n/a | "of the specified width. B is never truncated."); |
---|
814 | n/a | |
---|