/src/cpython/Objects/stringlib/replace.h
Line | Count | Source |
1 | | /* stringlib: replace implementation */ |
2 | | |
3 | | #ifndef STRINGLIB_FASTSEARCH_H |
4 | | #error must include "stringlib/fastsearch.h" before including this module |
5 | | #endif |
6 | | |
7 | | Py_LOCAL_INLINE(void) |
8 | | STRINGLIB(replace_1char_inplace)(STRINGLIB_CHAR* s, STRINGLIB_CHAR* end, |
9 | | Py_UCS4 u1, Py_UCS4 u2, Py_ssize_t maxcount) |
10 | 1.69M | { |
11 | 1.69M | *s = u2; |
12 | 138M | while (--maxcount && ++s != end) { |
13 | | /* Find the next character to be replaced. |
14 | | |
15 | | If it occurs often, it is faster to scan for it using an inline |
16 | | loop. If it occurs seldom, it is faster to scan for it using a |
17 | | function call; the overhead of the function call is amortized |
18 | | across the many characters that call covers. We start with an |
19 | | inline loop and use a heuristic to determine whether to fall back |
20 | | to a function call. */ |
21 | 137M | if (*s != u1) { |
22 | 7.61M | int attempts = 10; |
23 | | /* search u1 in a dummy loop */ |
24 | 26.3M | while (1) { |
25 | 26.3M | if (++s == end) |
26 | 1.04M | return; |
27 | 25.2M | if (*s == u1) |
28 | 5.76M | break; |
29 | 19.5M | if (!--attempts) { |
30 | | /* if u1 was not found for attempts iterations, |
31 | | use FASTSEARCH() or memchr() */ |
32 | | #ifdef STRINGLIB_FAST_MEMCHR |
33 | | s++; |
34 | 130k | s = STRINGLIB_FAST_MEMCHR(s, u1, end - s); |
35 | 130k | if (s == NULL) |
36 | 23.2k | return; |
37 | | #else |
38 | | Py_ssize_t i; |
39 | 665k | STRINGLIB_CHAR ch1 = (STRINGLIB_CHAR) u1; |
40 | | s++; |
41 | 665k | i = FASTSEARCH(s, end - s, &ch1, 1, 0, FAST_SEARCH); |
42 | 665k | if (i < 0) |
43 | 295k | return; |
44 | 370k | s += i; |
45 | 370k | #endif |
46 | | /* restart the dummy loop */ |
47 | 107k | break; |
48 | 795k | } |
49 | 19.5M | } |
50 | 7.61M | } |
51 | 136M | *s = u2; |
52 | 136M | } |
53 | 1.69M | } unicodeobject.c:ucs1lib_replace_1char_inplace Line | Count | Source | 10 | 628k | { | 11 | 628k | *s = u2; | 12 | 43.2M | while (--maxcount && ++s != end) { | 13 | | /* Find the next character to be replaced. | 14 | | | 15 | | If it occurs often, it is faster to scan for it using an inline | 16 | | loop. If it occurs seldom, it is faster to scan for it using a | 17 | | function call; the overhead of the function call is amortized | 18 | | across the many characters that call covers. We start with an | 19 | | inline loop and use a heuristic to determine whether to fall back | 20 | | to a function call. */ | 21 | 42.9M | if (*s != u1) { | 22 | 1.48M | int attempts = 10; | 23 | | /* search u1 in a dummy loop */ | 24 | 4.22M | while (1) { | 25 | 4.22M | if (++s == end) | 26 | 394k | return; | 27 | 3.83M | if (*s == u1) | 28 | 989k | break; | 29 | 2.84M | if (!--attempts) { | 30 | | /* if u1 was not found for attempts iterations, | 31 | | use FASTSEARCH() or memchr() */ | 32 | 102k | #ifdef STRINGLIB_FAST_MEMCHR | 33 | 102k | s++; | 34 | 102k | s = STRINGLIB_FAST_MEMCHR(s, u1, end - s); | 35 | 102k | if (s == NULL) | 36 | 16.2k | return; | 37 | | #else | 38 | | Py_ssize_t i; | 39 | | STRINGLIB_CHAR ch1 = (STRINGLIB_CHAR) u1; | 40 | | s++; | 41 | | i = FASTSEARCH(s, end - s, &ch1, 1, 0, FAST_SEARCH); | 42 | | if (i < 0) | 43 | | return; | 44 | | s += i; | 45 | | #endif | 46 | | /* restart the dummy loop */ | 47 | 86.6k | break; | 48 | 102k | } | 49 | 2.84M | } | 50 | 1.48M | } | 51 | 42.5M | *s = u2; | 52 | 42.5M | } | 53 | 628k | } |
unicodeobject.c:ucs2lib_replace_1char_inplace Line | Count | Source | 10 | 1.05M | { | 11 | 1.05M | *s = u2; | 12 | 73.2M | while (--maxcount && ++s != end) { | 13 | | /* Find the next character to be replaced. | 14 | | | 15 | | If it occurs often, it is faster to scan for it using an inline | 16 | | loop. If it occurs seldom, it is faster to scan for it using a | 17 | | function call; the overhead of the function call is amortized | 18 | | across the many characters that call covers. We start with an | 19 | | inline loop and use a heuristic to determine whether to fall back | 20 | | to a function call. */ | 21 | 73.1M | if (*s != u1) { | 22 | 5.24M | int attempts = 10; | 23 | | /* search u1 in a dummy loop */ | 24 | 20.7M | while (1) { | 25 | 20.7M | if (++s == end) | 26 | 647k | return; | 27 | 20.1M | if (*s == u1) | 28 | 3.93M | break; | 29 | 16.1M | if (!--attempts) { | 30 | | /* if u1 was not found for attempts iterations, | 31 | | use FASTSEARCH() or memchr() */ | 32 | | #ifdef STRINGLIB_FAST_MEMCHR | 33 | | s++; | 34 | | s = STRINGLIB_FAST_MEMCHR(s, u1, end - s); | 35 | | if (s == NULL) | 36 | | return; | 37 | | #else | 38 | 665k | Py_ssize_t i; | 39 | 665k | STRINGLIB_CHAR ch1 = (STRINGLIB_CHAR) u1; | 40 | 665k | s++; | 41 | 665k | i = FASTSEARCH(s, end - s, &ch1, 1, 0, FAST_SEARCH); | 42 | 665k | if (i < 0) | 43 | 295k | return; | 44 | 370k | s += i; | 45 | 370k | #endif | 46 | | /* restart the dummy loop */ | 47 | 370k | break; | 48 | 665k | } | 49 | 16.1M | } | 50 | 5.24M | } | 51 | 72.2M | *s = u2; | 52 | 72.2M | } | 53 | 1.05M | } |
unicodeobject.c:ucs4lib_replace_1char_inplace Line | Count | Source | 10 | 17.4k | { | 11 | 17.4k | *s = u2; | 12 | 21.5M | while (--maxcount && ++s != end) { | 13 | | /* Find the next character to be replaced. | 14 | | | 15 | | If it occurs often, it is faster to scan for it using an inline | 16 | | loop. If it occurs seldom, it is faster to scan for it using a | 17 | | function call; the overhead of the function call is amortized | 18 | | across the many characters that call covers. We start with an | 19 | | inline loop and use a heuristic to determine whether to fall back | 20 | | to a function call. */ | 21 | 21.5M | if (*s != u1) { | 22 | 874k | int attempts = 10; | 23 | | /* search u1 in a dummy loop */ | 24 | 1.32M | while (1) { | 25 | 1.32M | if (++s == end) | 26 | 5.10k | return; | 27 | 1.32M | if (*s == u1) | 28 | 842k | break; | 29 | 477k | if (!--attempts) { | 30 | | /* if u1 was not found for attempts iterations, | 31 | | use FASTSEARCH() or memchr() */ | 32 | 27.3k | #ifdef STRINGLIB_FAST_MEMCHR | 33 | 27.3k | s++; | 34 | 27.3k | s = STRINGLIB_FAST_MEMCHR(s, u1, end - s); | 35 | 27.3k | if (s == NULL) | 36 | 6.98k | return; | 37 | | #else | 38 | | Py_ssize_t i; | 39 | | STRINGLIB_CHAR ch1 = (STRINGLIB_CHAR) u1; | 40 | | s++; | 41 | | i = FASTSEARCH(s, end - s, &ch1, 1, 0, FAST_SEARCH); | 42 | | if (i < 0) | 43 | | return; | 44 | | s += i; | 45 | | #endif | 46 | | /* restart the dummy loop */ | 47 | 20.3k | break; | 48 | 27.3k | } | 49 | 477k | } | 50 | 874k | } | 51 | 21.5M | *s = u2; | 52 | 21.5M | } | 53 | 17.4k | } |
|