/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.67M | { |
11 | 1.67M | *s = u2; |
12 | 171M | 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 | 170M | if (*s != u1) { |
22 | 7.03M | int attempts = 10; |
23 | | /* search u1 in a dummy loop */ |
24 | 25.7M | while (1) { |
25 | 25.7M | if (++s == end) |
26 | 1.00M | return; |
27 | 24.7M | if (*s == u1) |
28 | 5.24M | break; |
29 | 19.4M | if (!--attempts) { |
30 | | /* if u1 was not found for attempts iterations, |
31 | | use FASTSEARCH() or memchr() */ |
32 | | #ifdef STRINGLIB_FAST_MEMCHR |
33 | | s++; |
34 | 136k | s = STRINGLIB_FAST_MEMCHR(s, u1, end - s); |
35 | 136k | if (s == NULL) |
36 | 41.2k | return; |
37 | | #else |
38 | | Py_ssize_t i; |
39 | 652k | STRINGLIB_CHAR ch1 = (STRINGLIB_CHAR) u1; |
40 | | s++; |
41 | 652k | i = FASTSEARCH(s, end - s, &ch1, 1, 0, FAST_SEARCH); |
42 | 652k | if (i < 0) |
43 | 305k | return; |
44 | 347k | s += i; |
45 | 347k | #endif |
46 | | /* restart the dummy loop */ |
47 | 95.1k | break; |
48 | 789k | } |
49 | 19.4M | } |
50 | 7.03M | } |
51 | 169M | *s = u2; |
52 | 169M | } |
53 | 1.67M | } unicodeobject.c:ucs1lib_replace_1char_inplace Line | Count | Source | 10 | 585k | { | 11 | 585k | *s = u2; | 12 | 48.9M | 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 | 48.7M | if (*s != u1) { | 22 | 1.45M | int attempts = 10; | 23 | | /* search u1 in a dummy loop */ | 24 | 4.58M | while (1) { | 25 | 4.58M | if (++s == end) | 26 | 352k | return; | 27 | 4.23M | if (*s == u1) | 28 | 992k | break; | 29 | 3.24M | if (!--attempts) { | 30 | | /* if u1 was not found for attempts iterations, | 31 | | use FASTSEARCH() or memchr() */ | 32 | 112k | #ifdef STRINGLIB_FAST_MEMCHR | 33 | 112k | s++; | 34 | 112k | s = STRINGLIB_FAST_MEMCHR(s, u1, end - s); | 35 | 112k | if (s == NULL) | 36 | 34.5k | 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 | 77.9k | break; | 48 | 112k | } | 49 | 3.24M | } | 50 | 1.45M | } | 51 | 48.3M | *s = u2; | 52 | 48.3M | } | 53 | 585k | } |
unicodeobject.c:ucs2lib_replace_1char_inplace Line | Count | Source | 10 | 1.07M | { | 11 | 1.07M | *s = u2; | 12 | 93.4M | 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 | 93.3M | if (*s != u1) { | 22 | 4.78M | int attempts = 10; | 23 | | /* search u1 in a dummy loop */ | 24 | 19.9M | while (1) { | 25 | 19.9M | if (++s == end) | 26 | 648k | return; | 27 | 19.3M | if (*s == u1) | 28 | 3.47M | break; | 29 | 15.8M | 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 | 652k | Py_ssize_t i; | 39 | 652k | STRINGLIB_CHAR ch1 = (STRINGLIB_CHAR) u1; | 40 | 652k | s++; | 41 | 652k | i = FASTSEARCH(s, end - s, &ch1, 1, 0, FAST_SEARCH); | 42 | 652k | if (i < 0) | 43 | 305k | return; | 44 | 347k | s += i; | 45 | 347k | #endif | 46 | | /* restart the dummy loop */ | 47 | 347k | break; | 48 | 652k | } | 49 | 15.8M | } | 50 | 4.78M | } | 51 | 92.3M | *s = u2; | 52 | 92.3M | } | 53 | 1.07M | } |
unicodeobject.c:ucs4lib_replace_1char_inplace Line | Count | Source | 10 | 19.1k | { | 11 | 19.1k | *s = u2; | 12 | 28.8M | 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 | 28.8M | if (*s != u1) { | 22 | 800k | int attempts = 10; | 23 | | /* search u1 in a dummy loop */ | 24 | 1.19M | while (1) { | 25 | 1.19M | if (++s == end) | 26 | 5.15k | return; | 27 | 1.18M | if (*s == u1) | 28 | 771k | break; | 29 | 415k | if (!--attempts) { | 30 | | /* if u1 was not found for attempts iterations, | 31 | | use FASTSEARCH() or memchr() */ | 32 | 24.0k | #ifdef STRINGLIB_FAST_MEMCHR | 33 | 24.0k | s++; | 34 | 24.0k | s = STRINGLIB_FAST_MEMCHR(s, u1, end - s); | 35 | 24.0k | if (s == NULL) | 36 | 6.78k | 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 | 17.2k | break; | 48 | 24.0k | } | 49 | 415k | } | 50 | 800k | } | 51 | 28.7M | *s = u2; | 52 | 28.7M | } | 53 | 19.1k | } |
|