Coverage Report

Created: 2026-05-16 06:46

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
59.9k
{
11
59.9k
    *s = u2;
12
3.15M
    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.11M
        if (*s != u1) {
22
233k
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
466k
            while (1) {
25
466k
                if (++s == end)
26
19.8k
                    return;
27
446k
                if (*s == u1)
28
199k
                    break;
29
246k
                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.10k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
4.10k
                    if (s == NULL)
36
1.39k
                        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.16k
                        return;
44
6.42k
                    s += i;
45
6.42k
#endif
46
                    /* restart the dummy loop */
47
2.70k
                    break;
48
13.6k
                }
49
246k
            }
50
233k
        }
51
3.09M
        *s = u2;
52
3.09M
    }
53
59.9k
}
unicodeobject.c:ucs1lib_replace_1char_inplace
Line
Count
Source
10
33.3k
{
11
33.3k
    *s = u2;
12
958k
    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
935k
        if (*s != u1) {
22
33.2k
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
70.1k
            while (1) {
25
70.1k
                if (++s == end)
26
9.55k
                    return;
27
60.6k
                if (*s == u1)
28
21.2k
                    break;
29
39.3k
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
2.43k
#ifdef STRINGLIB_FAST_MEMCHR
33
2.43k
                    s++;
34
2.43k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
2.43k
                    if (s == NULL)
36
864
                        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.56k
                    break;
48
2.43k
                }
49
39.3k
            }
50
33.2k
        }
51
925k
        *s = u2;
52
925k
    }
53
33.3k
}
unicodeobject.c:ucs2lib_replace_1char_inplace
Line
Count
Source
10
22.0k
{
11
22.0k
    *s = u2;
12
1.71M
    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.70M
        if (*s != u1) {
22
161k
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
329k
            while (1) {
25
329k
                if (++s == end)
26
8.46k
                    return;
27
320k
                if (*s == u1)
28
143k
                    break;
29
177k
                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.16k
                        return;
44
6.42k
                    s += i;
45
6.42k
#endif
46
                    /* restart the dummy loop */
47
6.42k
                    break;
48
9.59k
                }
49
177k
            }
50
161k
        }
51
1.68M
        *s = u2;
52
1.68M
    }
53
22.0k
}
unicodeobject.c:ucs4lib_replace_1char_inplace
Line
Count
Source
10
4.61k
{
11
4.61k
    *s = u2;
12
482k
    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
480k
        if (*s != u1) {
22
38.3k
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
67.2k
            while (1) {
25
67.2k
                if (++s == end)
26
1.87k
                    return;
27
65.3k
                if (*s == u1)
28
34.7k
                    break;
29
30.6k
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
1.66k
#ifdef STRINGLIB_FAST_MEMCHR
33
1.66k
                    s++;
34
1.66k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
1.66k
                    if (s == NULL)
36
530
                        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.13k
                    break;
48
1.66k
                }
49
30.6k
            }
50
38.3k
        }
51
478k
        *s = u2;
52
478k
    }
53
4.61k
}