Coverage Report

Created: 2026-04-12 06:54

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
72.2k
{
11
72.2k
    *s = u2;
12
3.64M
    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.60M
        if (*s != u1) {
22
283k
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
592k
            while (1) {
25
592k
                if (++s == end)
26
25.7k
                    return;
27
566k
                if (*s == u1)
28
241k
                    break;
29
325k
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
#ifdef STRINGLIB_FAST_MEMCHR
33
                    s++;
34
6.02k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
6.02k
                    if (s == NULL)
36
1.97k
                        return;
37
#else
38
                    Py_ssize_t i;
39
10.5k
                    STRINGLIB_CHAR ch1 = (STRINGLIB_CHAR) u1;
40
                    s++;
41
10.5k
                    i = FASTSEARCH(s, end - s, &ch1, 1, 0, FAST_SEARCH);
42
10.5k
                    if (i < 0)
43
3.62k
                        return;
44
6.88k
                    s += i;
45
6.88k
#endif
46
                    /* restart the dummy loop */
47
4.04k
                    break;
48
16.5k
                }
49
325k
            }
50
283k
        }
51
3.57M
        *s = u2;
52
3.57M
    }
53
72.2k
}
unicodeobject.c:ucs1lib_replace_1char_inplace
Line
Count
Source
10
35.2k
{
11
35.2k
    *s = u2;
12
898k
    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
875k
        if (*s != u1) {
22
55.3k
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
111k
            while (1) {
25
111k
                if (++s == end)
26
11.6k
                    return;
27
99.7k
                if (*s == u1)
28
40.7k
                    break;
29
59.0k
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
2.96k
#ifdef STRINGLIB_FAST_MEMCHR
33
2.96k
                    s++;
34
2.96k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
2.96k
                    if (s == NULL)
36
863
                        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.10k
                    break;
48
2.96k
                }
49
59.0k
            }
50
55.3k
        }
51
863k
        *s = u2;
52
863k
    }
53
35.2k
}
unicodeobject.c:ucs2lib_replace_1char_inplace
Line
Count
Source
10
29.3k
{
11
29.3k
    *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
168k
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
369k
            while (1) {
25
369k
                if (++s == end)
26
11.8k
                    return;
27
357k
                if (*s == u1)
28
146k
                    break;
29
211k
                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
10.5k
                    Py_ssize_t i;
39
10.5k
                    STRINGLIB_CHAR ch1 = (STRINGLIB_CHAR) u1;
40
10.5k
                    s++;
41
10.5k
                    i = FASTSEARCH(s, end - s, &ch1, 1, 0, FAST_SEARCH);
42
10.5k
                    if (i < 0)
43
3.62k
                        return;
44
6.88k
                    s += i;
45
6.88k
#endif
46
                    /* restart the dummy loop */
47
6.88k
                    break;
48
10.5k
                }
49
211k
            }
50
168k
        }
51
1.68M
        *s = u2;
52
1.68M
    }
53
29.3k
}
unicodeobject.c:ucs4lib_replace_1char_inplace
Line
Count
Source
10
7.67k
{
11
7.67k
    *s = u2;
12
1.03M
    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.02M
        if (*s != u1) {
22
59.4k
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
111k
            while (1) {
25
111k
                if (++s == end)
26
2.25k
                    return;
27
109k
                if (*s == u1)
28
54.1k
                    break;
29
55.3k
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
3.05k
#ifdef STRINGLIB_FAST_MEMCHR
33
3.05k
                    s++;
34
3.05k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
3.05k
                    if (s == NULL)
36
1.11k
                        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.94k
                    break;
48
3.05k
                }
49
55.3k
            }
50
59.4k
        }
51
1.02M
        *s = u2;
52
1.02M
    }
53
7.67k
}