Coverage Report

Created: 2026-12-01 06:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
65.2k
{
11
65.2k
    *s = u2;
12
3.40M
    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
3.37M
        if (*s != u1) {
22
266k
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
474k
            while (1) {
25
474k
                if (++s == end)
26
24.7k
                    return;
27
449k
                if (*s == u1)
28
228k
                    break;
29
221k
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
#ifdef STRINGLIB_FAST_MEMCHR
33
                    s++;
34
5.50k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
5.50k
                    if (s == NULL)
36
2.31k
                        return;
37
#else
38
                    Py_ssize_t i;
39
7.72k
                    STRINGLIB_CHAR ch1 = (STRINGLIB_CHAR) u1;
40
                    s++;
41
7.72k
                    i = FASTSEARCH(s, end - s, &ch1, 1, 0, FAST_SEARCH);
42
7.72k
                    if (i < 0)
43
3.31k
                        return;
44
4.40k
                    s += i;
45
4.40k
#endif
46
                    /* restart the dummy loop */
47
3.18k
                    break;
48
13.2k
                }
49
221k
            }
50
266k
        }
51
3.34M
        *s = u2;
52
3.34M
    }
53
65.2k
}
unicodeobject.c:ucs1lib_replace_1char_inplace
Line
Count
Source
10
39.2k
{
11
39.2k
    *s = u2;
12
945k
    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
919k
        if (*s != u1) {
22
66.1k
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
123k
            while (1) {
25
123k
                if (++s == end)
26
11.6k
                    return;
27
111k
                if (*s == u1)
28
50.4k
                    break;
29
61.3k
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
4.02k
#ifdef STRINGLIB_FAST_MEMCHR
33
4.02k
                    s++;
34
4.02k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
4.02k
                    if (s == NULL)
36
1.56k
                        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
2.46k
                    break;
48
4.02k
                }
49
61.3k
            }
50
66.1k
        }
51
905k
        *s = u2;
52
905k
    }
53
39.2k
}
unicodeobject.c:ucs2lib_replace_1char_inplace
Line
Count
Source
10
20.4k
{
11
20.4k
    *s = u2;
12
1.81M
    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.80M
        if (*s != u1) {
22
174k
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
303k
            while (1) {
25
303k
                if (++s == end)
26
10.3k
                    return;
27
293k
                if (*s == u1)
28
156k
                    break;
29
137k
                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
7.72k
                    Py_ssize_t i;
39
7.72k
                    STRINGLIB_CHAR ch1 = (STRINGLIB_CHAR) u1;
40
7.72k
                    s++;
41
7.72k
                    i = FASTSEARCH(s, end - s, &ch1, 1, 0, FAST_SEARCH);
42
7.72k
                    if (i < 0)
43
3.31k
                        return;
44
4.40k
                    s += i;
45
4.40k
#endif
46
                    /* restart the dummy loop */
47
4.40k
                    break;
48
7.72k
                }
49
137k
            }
50
174k
        }
51
1.78M
        *s = u2;
52
1.78M
    }
53
20.4k
}
unicodeobject.c:ucs4lib_replace_1char_inplace
Line
Count
Source
10
5.52k
{
11
5.52k
    *s = u2;
12
653k
    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
651k
        if (*s != u1) {
22
26.5k
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
47.6k
            while (1) {
25
47.6k
                if (++s == end)
26
2.68k
                    return;
27
44.9k
                if (*s == u1)
28
22.3k
                    break;
29
22.5k
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
1.47k
#ifdef STRINGLIB_FAST_MEMCHR
33
1.47k
                    s++;
34
1.47k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
1.47k
                    if (s == NULL)
36
758
                        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
719
                    break;
48
1.47k
                }
49
22.5k
            }
50
26.5k
        }
51
648k
        *s = u2;
52
648k
    }
53
5.52k
}