Coverage Report

Created: 2026-06-21 06:15

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
61.0k
{
11
61.0k
    *s = u2;
12
3.00M
    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
2.97M
        if (*s != u1) {
22
230k
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
477k
            while (1) {
25
477k
                if (++s == end)
26
22.6k
                    return;
27
454k
                if (*s == u1)
28
193k
                    break;
29
261k
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
#ifdef STRINGLIB_FAST_MEMCHR
33
                    s++;
34
5.21k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
5.21k
                    if (s == NULL)
36
2.04k
                        return;
37
#else
38
                    Py_ssize_t i;
39
9.76k
                    STRINGLIB_CHAR ch1 = (STRINGLIB_CHAR) u1;
40
                    s++;
41
9.76k
                    i = FASTSEARCH(s, end - s, &ch1, 1, 0, FAST_SEARCH);
42
9.76k
                    if (i < 0)
43
3.66k
                        return;
44
6.09k
                    s += i;
45
6.09k
#endif
46
                    /* restart the dummy loop */
47
3.16k
                    break;
48
14.9k
                }
49
261k
            }
50
230k
        }
51
2.94M
        *s = u2;
52
2.94M
    }
53
61.0k
}
unicodeobject.c:ucs1lib_replace_1char_inplace
Line
Count
Source
10
36.1k
{
11
36.1k
    *s = u2;
12
906k
    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
882k
        if (*s != u1) {
22
34.6k
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
81.0k
            while (1) {
25
81.0k
                if (++s == end)
26
10.8k
                    return;
27
70.1k
                if (*s == u1)
28
20.8k
                    break;
29
49.3k
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
2.94k
#ifdef STRINGLIB_FAST_MEMCHR
33
2.94k
                    s++;
34
2.94k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
2.94k
                    if (s == NULL)
36
1.30k
                        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.64k
                    break;
48
2.94k
                }
49
49.3k
            }
50
34.6k
        }
51
870k
        *s = u2;
52
870k
    }
53
36.1k
}
unicodeobject.c:ucs2lib_replace_1char_inplace
Line
Count
Source
10
20.2k
{
11
20.2k
    *s = u2;
12
1.57M
    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.57M
        if (*s != u1) {
22
165k
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
332k
            while (1) {
25
332k
                if (++s == end)
26
8.75k
                    return;
27
323k
                if (*s == u1)
28
146k
                    break;
29
176k
                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.76k
                    Py_ssize_t i;
39
9.76k
                    STRINGLIB_CHAR ch1 = (STRINGLIB_CHAR) u1;
40
9.76k
                    s++;
41
9.76k
                    i = FASTSEARCH(s, end - s, &ch1, 1, 0, FAST_SEARCH);
42
9.76k
                    if (i < 0)
43
3.66k
                        return;
44
6.09k
                    s += i;
45
6.09k
#endif
46
                    /* restart the dummy loop */
47
6.09k
                    break;
48
9.76k
                }
49
176k
            }
50
165k
        }
51
1.55M
        *s = u2;
52
1.55M
    }
53
20.2k
}
unicodeobject.c:ucs4lib_replace_1char_inplace
Line
Count
Source
10
4.60k
{
11
4.60k
    *s = u2;
12
519k
    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
518k
        if (*s != u1) {
22
30.8k
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
63.8k
            while (1) {
25
63.8k
                if (++s == end)
26
3.02k
                    return;
27
60.8k
                if (*s == u1)
28
25.5k
                    break;
29
35.2k
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
2.27k
#ifdef STRINGLIB_FAST_MEMCHR
33
2.27k
                    s++;
34
2.27k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
2.27k
                    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.52k
                    break;
48
2.27k
                }
49
35.2k
            }
50
30.8k
        }
51
514k
        *s = u2;
52
514k
    }
53
4.60k
}