Coverage Report

Created: 2026-04-20 06:11

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
62.3k
{
11
62.3k
    *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
256k
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
511k
            while (1) {
25
511k
                if (++s == end)
26
22.0k
                    return;
27
489k
                if (*s == u1)
28
220k
                    break;
29
269k
                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.84k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
4.84k
                    if (s == NULL)
36
1.51k
                        return;
37
#else
38
                    Py_ssize_t i;
39
9.35k
                    STRINGLIB_CHAR ch1 = (STRINGLIB_CHAR) u1;
40
                    s++;
41
9.35k
                    i = FASTSEARCH(s, end - s, &ch1, 1, 0, FAST_SEARCH);
42
9.35k
                    if (i < 0)
43
3.48k
                        return;
44
5.86k
                    s += i;
45
5.86k
#endif
46
                    /* restart the dummy loop */
47
3.32k
                    break;
48
14.2k
                }
49
269k
            }
50
256k
        }
51
3.13M
        *s = u2;
52
3.13M
    }
53
62.3k
}
unicodeobject.c:ucs1lib_replace_1char_inplace
Line
Count
Source
10
32.2k
{
11
32.2k
    *s = u2;
12
790k
    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
769k
        if (*s != u1) {
22
41.2k
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
83.8k
            while (1) {
25
83.8k
                if (++s == end)
26
9.91k
                    return;
27
73.9k
                if (*s == u1)
28
29.1k
                    break;
29
44.7k
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
2.18k
#ifdef STRINGLIB_FAST_MEMCHR
33
2.18k
                    s++;
34
2.18k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
2.18k
                    if (s == NULL)
36
767
                        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.42k
                    break;
48
2.18k
                }
49
44.7k
            }
50
41.2k
        }
51
758k
        *s = u2;
52
758k
    }
53
32.2k
}
unicodeobject.c:ucs2lib_replace_1char_inplace
Line
Count
Source
10
24.7k
{
11
24.7k
    *s = u2;
12
1.76M
    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.75M
        if (*s != u1) {
22
159k
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
326k
            while (1) {
25
326k
                if (++s == end)
26
10.2k
                    return;
27
315k
                if (*s == u1)
28
139k
                    break;
29
175k
                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.35k
                    Py_ssize_t i;
39
9.35k
                    STRINGLIB_CHAR ch1 = (STRINGLIB_CHAR) u1;
40
9.35k
                    s++;
41
9.35k
                    i = FASTSEARCH(s, end - s, &ch1, 1, 0, FAST_SEARCH);
42
9.35k
                    if (i < 0)
43
3.48k
                        return;
44
5.86k
                    s += i;
45
5.86k
#endif
46
                    /* restart the dummy loop */
47
5.86k
                    break;
48
9.35k
                }
49
175k
            }
50
159k
        }
51
1.74M
        *s = u2;
52
1.74M
    }
53
24.7k
}
unicodeobject.c:ucs4lib_replace_1char_inplace
Line
Count
Source
10
5.37k
{
11
5.37k
    *s = u2;
12
633k
    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
631k
        if (*s != u1) {
22
55.9k
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
102k
            while (1) {
25
102k
                if (++s == end)
26
1.88k
                    return;
27
100k
                if (*s == u1)
28
51.4k
                    break;
29
48.7k
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
2.65k
#ifdef STRINGLIB_FAST_MEMCHR
33
2.65k
                    s++;
34
2.65k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
2.65k
                    if (s == NULL)
36
749
                        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.90k
                    break;
48
2.65k
                }
49
48.7k
            }
50
55.9k
        }
51
628k
        *s = u2;
52
628k
    }
53
5.37k
}