Coverage Report

Created: 2026-10-31 06:21

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
60.9k
{
11
60.9k
    *s = u2;
12
3.23M
    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.19M
        if (*s != u1) {
22
235k
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
430k
            while (1) {
25
430k
                if (++s == end)
26
22.8k
                    return;
27
408k
                if (*s == u1)
28
200k
                    break;
29
207k
                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.58k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
4.58k
                    if (s == NULL)
36
2.48k
                        return;
37
#else
38
                    Py_ssize_t i;
39
7.44k
                    STRINGLIB_CHAR ch1 = (STRINGLIB_CHAR) u1;
40
                    s++;
41
7.44k
                    i = FASTSEARCH(s, end - s, &ch1, 1, 0, FAST_SEARCH);
42
7.44k
                    if (i < 0)
43
3.17k
                        return;
44
4.27k
                    s += i;
45
4.27k
#endif
46
                    /* restart the dummy loop */
47
2.09k
                    break;
48
12.0k
                }
49
207k
            }
50
235k
        }
51
3.17M
        *s = u2;
52
3.17M
    }
53
60.9k
}
unicodeobject.c:ucs1lib_replace_1char_inplace
Line
Count
Source
10
37.6k
{
11
37.6k
    *s = u2;
12
920k
    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
896k
        if (*s != u1) {
22
45.8k
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
94.2k
            while (1) {
25
94.2k
                if (++s == end)
26
11.7k
                    return;
27
82.5k
                if (*s == u1)
28
30.9k
                    break;
29
51.5k
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
3.09k
#ifdef STRINGLIB_FAST_MEMCHR
33
3.09k
                    s++;
34
3.09k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
3.09k
                    if (s == NULL)
36
1.68k
                        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.40k
                    break;
48
3.09k
                }
49
51.5k
            }
50
45.8k
        }
51
883k
        *s = u2;
52
883k
    }
53
37.6k
}
unicodeobject.c:ucs2lib_replace_1char_inplace
Line
Count
Source
10
19.3k
{
11
19.3k
    *s = u2;
12
1.72M
    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.71M
        if (*s != u1) {
22
171k
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
297k
            while (1) {
25
297k
                if (++s == end)
26
9.71k
                    return;
27
287k
                if (*s == u1)
28
154k
                    break;
29
133k
                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.44k
                    Py_ssize_t i;
39
7.44k
                    STRINGLIB_CHAR ch1 = (STRINGLIB_CHAR) u1;
40
7.44k
                    s++;
41
7.44k
                    i = FASTSEARCH(s, end - s, &ch1, 1, 0, FAST_SEARCH);
42
7.44k
                    if (i < 0)
43
3.17k
                        return;
44
4.27k
                    s += i;
45
4.27k
#endif
46
                    /* restart the dummy loop */
47
4.27k
                    break;
48
7.44k
                }
49
133k
            }
50
171k
        }
51
1.70M
        *s = u2;
52
1.70M
    }
53
19.3k
}
unicodeobject.c:ucs4lib_replace_1char_inplace
Line
Count
Source
10
3.94k
{
11
3.94k
    *s = u2;
12
590k
    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
588k
        if (*s != u1) {
22
18.5k
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
39.0k
            while (1) {
25
39.0k
                if (++s == end)
26
1.38k
                    return;
27
37.7k
                if (*s == u1)
28
15.6k
                    break;
29
22.0k
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
1.48k
#ifdef STRINGLIB_FAST_MEMCHR
33
1.48k
                    s++;
34
1.48k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
1.48k
                    if (s == NULL)
36
793
                        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
691
                    break;
48
1.48k
                }
49
22.0k
            }
50
18.5k
        }
51
586k
        *s = u2;
52
586k
    }
53
3.94k
}