Coverage Report

Created: 2026-02-09 07:07

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
1.14M
{
11
1.14M
    *s = u2;
12
74.3M
    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
74.0M
        if (*s != u1) {
22
6.01M
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
22.1M
            while (1) {
25
22.1M
                if (++s == end)
26
673k
                    return;
27
21.5M
                if (*s == u1)
28
4.63M
                    break;
29
16.8M
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
#ifdef STRINGLIB_FAST_MEMCHR
33
                    s++;
34
83.9k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
83.9k
                    if (s == NULL)
36
17.6k
                        return;
37
#else
38
                    Py_ssize_t i;
39
624k
                    STRINGLIB_CHAR ch1 = (STRINGLIB_CHAR) u1;
40
                    s++;
41
624k
                    i = FASTSEARCH(s, end - s, &ch1, 1, 0, FAST_SEARCH);
42
624k
                    if (i < 0)
43
233k
                        return;
44
391k
                    s += i;
45
391k
#endif
46
                    /* restart the dummy loop */
47
66.3k
                    break;
48
708k
                }
49
16.8M
            }
50
6.01M
        }
51
73.1M
        *s = u2;
52
73.1M
    }
53
1.14M
}
unicodeobject.c:ucs1lib_replace_1char_inplace
Line
Count
Source
10
364k
{
11
364k
    *s = u2;
12
23.8M
    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
23.6M
        if (*s != u1) {
22
898k
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
2.27M
            while (1) {
25
2.27M
                if (++s == end)
26
215k
                    return;
27
2.06M
                if (*s == u1)
28
646k
                    break;
29
1.41M
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
36.6k
#ifdef STRINGLIB_FAST_MEMCHR
33
36.6k
                    s++;
34
36.6k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
36.6k
                    if (s == NULL)
36
12.7k
                        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
23.9k
                    break;
48
36.6k
                }
49
1.41M
            }
50
898k
        }
51
23.4M
        *s = u2;
52
23.4M
    }
53
364k
}
unicodeobject.c:ucs2lib_replace_1char_inplace
Line
Count
Source
10
769k
{
11
769k
    *s = u2;
12
40.8M
    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
40.8M
        if (*s != u1) {
22
4.74M
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
18.8M
            while (1) {
25
18.8M
                if (++s == end)
26
453k
                    return;
27
18.3M
                if (*s == u1)
28
3.66M
                    break;
29
14.7M
                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
624k
                    Py_ssize_t i;
39
624k
                    STRINGLIB_CHAR ch1 = (STRINGLIB_CHAR) u1;
40
624k
                    s++;
41
624k
                    i = FASTSEARCH(s, end - s, &ch1, 1, 0, FAST_SEARCH);
42
624k
                    if (i < 0)
43
233k
                        return;
44
391k
                    s += i;
45
391k
#endif
46
                    /* restart the dummy loop */
47
391k
                    break;
48
624k
                }
49
14.7M
            }
50
4.74M
        }
51
40.1M
        *s = u2;
52
40.1M
    }
53
769k
}
unicodeobject.c:ucs4lib_replace_1char_inplace
Line
Count
Source
10
14.3k
{
11
14.3k
    *s = u2;
12
9.59M
    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
9.58M
        if (*s != u1) {
22
372k
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
1.06M
            while (1) {
25
1.06M
                if (++s == end)
26
4.31k
                    return;
27
1.05M
                if (*s == u1)
28
320k
                    break;
29
737k
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
47.2k
#ifdef STRINGLIB_FAST_MEMCHR
33
47.2k
                    s++;
34
47.2k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
47.2k
                    if (s == NULL)
36
4.92k
                        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
42.3k
                    break;
48
47.2k
                }
49
737k
            }
50
372k
        }
51
9.57M
        *s = u2;
52
9.57M
    }
53
14.3k
}