Coverage Report

Created: 2025-11-02 06:30

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
2.13M
{
11
2.13M
    *s = u2;
12
126M
    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
125M
        if (*s != u1) {
22
8.44M
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
26.9M
            while (1) {
25
26.9M
                if (++s == end)
26
1.17M
                    return;
27
25.7M
                if (*s == u1)
28
6.46M
                    break;
29
19.3M
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
#ifdef STRINGLIB_FAST_MEMCHR
33
                    s++;
34
162k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
162k
                    if (s == NULL)
36
81.9k
                        return;
37
#else
38
                    Py_ssize_t i;
39
640k
                    STRINGLIB_CHAR ch1 = (STRINGLIB_CHAR) u1;
40
                    s++;
41
640k
                    i = FASTSEARCH(s, end - s, &ch1, 1, 0, FAST_SEARCH);
42
640k
                    if (i < 0)
43
282k
                        return;
44
358k
                    s += i;
45
358k
#endif
46
                    /* restart the dummy loop */
47
80.8k
                    break;
48
803k
                }
49
19.3M
            }
50
8.44M
        }
51
124M
        *s = u2;
52
124M
    }
53
2.13M
}
unicodeobject.c:ucs1lib_replace_1char_inplace
Line
Count
Source
10
939k
{
11
939k
    *s = u2;
12
38.6M
    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
38.2M
        if (*s != u1) {
22
1.82M
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
5.23M
            while (1) {
25
5.23M
                if (++s == end)
26
492k
                    return;
27
4.74M
                if (*s == u1)
28
1.19M
                    break;
29
3.54M
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
136k
#ifdef STRINGLIB_FAST_MEMCHR
33
136k
                    s++;
34
136k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
136k
                    if (s == NULL)
36
75.5k
                        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
60.5k
                    break;
48
136k
                }
49
3.54M
            }
50
1.82M
        }
51
37.6M
        *s = u2;
52
37.6M
    }
53
939k
}
unicodeobject.c:ucs2lib_replace_1char_inplace
Line
Count
Source
10
1.18M
{
11
1.18M
    *s = u2;
12
65.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
65.1M
        if (*s != u1) {
22
5.71M
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
20.3M
            while (1) {
25
20.3M
                if (++s == end)
26
678k
                    return;
27
19.7M
                if (*s == u1)
28
4.39M
                    break;
29
15.3M
                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
640k
                    Py_ssize_t i;
39
640k
                    STRINGLIB_CHAR ch1 = (STRINGLIB_CHAR) u1;
40
640k
                    s++;
41
640k
                    i = FASTSEARCH(s, end - s, &ch1, 1, 0, FAST_SEARCH);
42
640k
                    if (i < 0)
43
282k
                        return;
44
358k
                    s += i;
45
358k
#endif
46
                    /* restart the dummy loop */
47
358k
                    break;
48
640k
                }
49
15.3M
            }
50
5.71M
        }
51
64.1M
        *s = u2;
52
64.1M
    }
53
1.18M
}
unicodeobject.c:ucs4lib_replace_1char_inplace
Line
Count
Source
10
17.2k
{
11
17.2k
    *s = u2;
12
22.4M
    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
22.4M
        if (*s != u1) {
22
905k
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
1.34M
            while (1) {
25
1.34M
                if (++s == end)
26
6.00k
                    return;
27
1.34M
                if (*s == u1)
28
872k
                    break;
29
469k
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
26.7k
#ifdef STRINGLIB_FAST_MEMCHR
33
26.7k
                    s++;
34
26.7k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
26.7k
                    if (s == NULL)
36
6.34k
                        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
20.3k
                    break;
48
26.7k
                }
49
469k
            }
50
905k
        }
51
22.4M
        *s = u2;
52
22.4M
    }
53
17.2k
}