Coverage Report

Created: 2025-10-12 06:48

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.40M
{
11
1.40M
    *s = u2;
12
144M
    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
144M
        if (*s != u1) {
22
6.89M
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
22.2M
            while (1) {
25
22.2M
                if (++s == end)
26
928k
                    return;
27
21.3M
                if (*s == u1)
28
5.39M
                    break;
29
15.9M
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
#ifdef STRINGLIB_FAST_MEMCHR
33
                    s++;
34
95.5k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
95.5k
                    if (s == NULL)
36
29.4k
                        return;
37
#else
38
                    Py_ssize_t i;
39
471k
                    STRINGLIB_CHAR ch1 = (STRINGLIB_CHAR) u1;
40
                    s++;
41
471k
                    i = FASTSEARCH(s, end - s, &ch1, 1, 0, FAST_SEARCH);
42
471k
                    if (i < 0)
43
207k
                        return;
44
264k
                    s += i;
45
264k
#endif
46
                    /* restart the dummy loop */
47
66.1k
                    break;
48
567k
                }
49
15.9M
            }
50
6.89M
        }
51
143M
        *s = u2;
52
143M
    }
53
1.40M
}
unicodeobject.c:ucs1lib_replace_1char_inplace
Line
Count
Source
10
601k
{
11
601k
    *s = u2;
12
46.2M
    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
46.1M
        if (*s != u1) {
22
1.55M
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
4.15M
            while (1) {
25
4.15M
                if (++s == end)
26
433k
                    return;
27
3.72M
                if (*s == u1)
28
1.04M
                    break;
29
2.67M
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
72.2k
#ifdef STRINGLIB_FAST_MEMCHR
33
72.2k
                    s++;
34
72.2k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
72.2k
                    if (s == NULL)
36
23.9k
                        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
48.3k
                    break;
48
72.2k
                }
49
2.67M
            }
50
1.55M
        }
51
45.6M
        *s = u2;
52
45.6M
    }
53
601k
}
unicodeobject.c:ucs2lib_replace_1char_inplace
Line
Count
Source
10
791k
{
11
791k
    *s = u2;
12
70.1M
    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
70.0M
        if (*s != u1) {
22
4.20M
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
16.3M
            while (1) {
25
16.3M
                if (++s == end)
26
490k
                    return;
27
15.8M
                if (*s == u1)
28
3.24M
                    break;
29
12.6M
                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
471k
                    Py_ssize_t i;
39
471k
                    STRINGLIB_CHAR ch1 = (STRINGLIB_CHAR) u1;
40
471k
                    s++;
41
471k
                    i = FASTSEARCH(s, end - s, &ch1, 1, 0, FAST_SEARCH);
42
471k
                    if (i < 0)
43
207k
                        return;
44
264k
                    s += i;
45
264k
#endif
46
                    /* restart the dummy loop */
47
264k
                    break;
48
471k
                }
49
12.6M
            }
50
4.20M
        }
51
69.3M
        *s = u2;
52
69.3M
    }
53
791k
}
unicodeobject.c:ucs4lib_replace_1char_inplace
Line
Count
Source
10
14.4k
{
11
14.4k
    *s = u2;
12
28.5M
    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
28.5M
        if (*s != u1) {
22
1.13M
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
1.78M
            while (1) {
25
1.78M
                if (++s == end)
26
4.21k
                    return;
27
1.78M
                if (*s == u1)
28
1.10M
                    break;
29
673k
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
23.3k
#ifdef STRINGLIB_FAST_MEMCHR
33
23.3k
                    s++;
34
23.3k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
23.3k
                    if (s == NULL)
36
5.53k
                        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
17.7k
                    break;
48
23.3k
                }
49
673k
            }
50
1.13M
        }
51
28.5M
        *s = u2;
52
28.5M
    }
53
14.4k
}