Coverage Report

Created: 2026-09-03 06:36

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cpython3/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
74
{
11
74
    *s = u2;
12
1.22k
    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
1.19k
        if (*s != u1) {
22
615
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
3.10k
            while (1) {
25
3.10k
                if (++s == end)
26
19
                    return;
27
3.08k
                if (*s == u1)
28
401
                    break;
29
2.68k
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
#ifdef STRINGLIB_FAST_MEMCHR
33
                    s++;
34
195
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
195
                    if (s == NULL)
36
26
                        return;
37
#else
38
                    Py_ssize_t i;
39
0
                    STRINGLIB_CHAR ch1 = (STRINGLIB_CHAR) u1;
40
                    s++;
41
0
                    i = FASTSEARCH(s, end - s, &ch1, 1, 0, FAST_SEARCH);
42
0
                    if (i < 0)
43
0
                        return;
44
0
                    s += i;
45
0
#endif
46
                    /* restart the dummy loop */
47
169
                    break;
48
195
                }
49
2.68k
            }
50
615
        }
51
1.14k
        *s = u2;
52
1.14k
    }
53
74
}
unicodeobject.c:ucs1lib_replace_1char_inplace
Line
Count
Source
10
74
{
11
74
    *s = u2;
12
1.22k
    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
1.19k
        if (*s != u1) {
22
615
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
3.10k
            while (1) {
25
3.10k
                if (++s == end)
26
19
                    return;
27
3.08k
                if (*s == u1)
28
401
                    break;
29
2.68k
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
195
#ifdef STRINGLIB_FAST_MEMCHR
33
195
                    s++;
34
195
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
195
                    if (s == NULL)
36
26
                        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
169
                    break;
48
195
                }
49
2.68k
            }
50
615
        }
51
1.14k
        *s = u2;
52
1.14k
    }
53
74
}
Unexecuted instantiation: unicodeobject.c:ucs2lib_replace_1char_inplace
Unexecuted instantiation: unicodeobject.c:ucs4lib_replace_1char_inplace