Coverage Report

Created: 2026-05-30 06:18

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
56.8k
{
11
56.8k
    *s = u2;
12
3.19M
    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.15M
        if (*s != u1) {
22
237k
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
473k
            while (1) {
25
473k
                if (++s == end)
26
19.9k
                    return;
27
453k
                if (*s == u1)
28
202k
                    break;
29
250k
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
#ifdef STRINGLIB_FAST_MEMCHR
33
                    s++;
34
4.83k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
4.83k
                    if (s == NULL)
36
1.86k
                        return;
37
#else
38
                    Py_ssize_t i;
39
9.59k
                    STRINGLIB_CHAR ch1 = (STRINGLIB_CHAR) u1;
40
                    s++;
41
9.59k
                    i = FASTSEARCH(s, end - s, &ch1, 1, 0, FAST_SEARCH);
42
9.59k
                    if (i < 0)
43
3.32k
                        return;
44
6.26k
                    s += i;
45
6.26k
#endif
46
                    /* restart the dummy loop */
47
2.97k
                    break;
48
14.4k
                }
49
250k
            }
50
237k
        }
51
3.13M
        *s = u2;
52
3.13M
    }
53
56.8k
}
unicodeobject.c:ucs1lib_replace_1char_inplace
Line
Count
Source
10
31.3k
{
11
31.3k
    *s = u2;
12
924k
    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
904k
        if (*s != u1) {
22
40.2k
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
83.0k
            while (1) {
25
83.0k
                if (++s == end)
26
10.0k
                    return;
27
72.9k
                if (*s == u1)
28
27.3k
                    break;
29
45.5k
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
2.73k
#ifdef STRINGLIB_FAST_MEMCHR
33
2.73k
                    s++;
34
2.73k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
2.73k
                    if (s == NULL)
36
1.07k
                        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
1.66k
                    break;
48
2.73k
                }
49
45.5k
            }
50
40.2k
        }
51
893k
        *s = u2;
52
893k
    }
53
31.3k
}
unicodeobject.c:ucs2lib_replace_1char_inplace
Line
Count
Source
10
20.4k
{
11
20.4k
    *s = u2;
12
1.77M
    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.76M
        if (*s != u1) {
22
160k
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
321k
            while (1) {
25
321k
                if (++s == end)
26
7.87k
                    return;
27
314k
                if (*s == u1)
28
143k
                    break;
29
170k
                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
9.59k
                    Py_ssize_t i;
39
9.59k
                    STRINGLIB_CHAR ch1 = (STRINGLIB_CHAR) u1;
40
9.59k
                    s++;
41
9.59k
                    i = FASTSEARCH(s, end - s, &ch1, 1, 0, FAST_SEARCH);
42
9.59k
                    if (i < 0)
43
3.32k
                        return;
44
6.26k
                    s += i;
45
6.26k
#endif
46
                    /* restart the dummy loop */
47
6.26k
                    break;
48
9.59k
                }
49
170k
            }
50
160k
        }
51
1.75M
        *s = u2;
52
1.75M
    }
53
20.4k
}
unicodeobject.c:ucs4lib_replace_1char_inplace
Line
Count
Source
10
5.12k
{
11
5.12k
    *s = u2;
12
487k
    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
485k
        if (*s != u1) {
22
36.2k
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
68.8k
            while (1) {
25
68.8k
                if (++s == end)
26
1.98k
                    return;
27
66.8k
                if (*s == u1)
28
32.2k
                    break;
29
34.6k
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
2.09k
#ifdef STRINGLIB_FAST_MEMCHR
33
2.09k
                    s++;
34
2.09k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
2.09k
                    if (s == NULL)
36
785
                        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
1.31k
                    break;
48
2.09k
                }
49
34.6k
            }
50
36.2k
        }
51
482k
        *s = u2;
52
482k
    }
53
5.12k
}