Coverage Report

Created: 2025-10-10 06:33

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.33M
{
11
1.33M
    *s = u2;
12
147M
    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
147M
        if (*s != u1) {
22
6.66M
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
20.6M
            while (1) {
25
20.6M
                if (++s == end)
26
855k
                    return;
27
19.8M
                if (*s == u1)
28
5.29M
                    break;
29
14.5M
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
#ifdef STRINGLIB_FAST_MEMCHR
33
                    s++;
34
105k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
105k
                    if (s == NULL)
36
39.2k
                        return;
37
#else
38
                    Py_ssize_t i;
39
413k
                    STRINGLIB_CHAR ch1 = (STRINGLIB_CHAR) u1;
40
                    s++;
41
413k
                    i = FASTSEARCH(s, end - s, &ch1, 1, 0, FAST_SEARCH);
42
413k
                    if (i < 0)
43
180k
                        return;
44
233k
                    s += i;
45
233k
#endif
46
                    /* restart the dummy loop */
47
65.7k
                    break;
48
518k
                }
49
14.5M
            }
50
6.66M
        }
51
146M
        *s = u2;
52
146M
    }
53
1.33M
}
unicodeobject.c:ucs1lib_replace_1char_inplace
Line
Count
Source
10
602k
{
11
602k
    *s = u2;
12
46.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
46.2M
        if (*s != u1) {
22
1.55M
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
4.26M
            while (1) {
25
4.26M
                if (++s == end)
26
412k
                    return;
27
3.85M
                if (*s == u1)
28
1.06M
                    break;
29
2.79M
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
80.8k
#ifdef STRINGLIB_FAST_MEMCHR
33
80.8k
                    s++;
34
80.8k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
80.8k
                    if (s == NULL)
36
33.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
46.9k
                    break;
48
80.8k
                }
49
2.79M
            }
50
1.55M
        }
51
45.7M
        *s = u2;
52
45.7M
    }
53
602k
}
unicodeobject.c:ucs2lib_replace_1char_inplace
Line
Count
Source
10
720k
{
11
720k
    *s = u2;
12
73.7M
    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
73.6M
        if (*s != u1) {
22
3.98M
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
14.7M
            while (1) {
25
14.7M
                if (++s == end)
26
438k
                    return;
27
14.2M
                if (*s == u1)
28
3.13M
                    break;
29
11.1M
                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
413k
                    Py_ssize_t i;
39
413k
                    STRINGLIB_CHAR ch1 = (STRINGLIB_CHAR) u1;
40
413k
                    s++;
41
413k
                    i = FASTSEARCH(s, end - s, &ch1, 1, 0, FAST_SEARCH);
42
413k
                    if (i < 0)
43
180k
                        return;
44
233k
                    s += i;
45
233k
#endif
46
                    /* restart the dummy loop */
47
233k
                    break;
48
413k
                }
49
11.1M
            }
50
3.98M
        }
51
73.0M
        *s = u2;
52
73.0M
    }
53
720k
}
unicodeobject.c:ucs4lib_replace_1char_inplace
Line
Count
Source
10
14.0k
{
11
14.0k
    *s = u2;
12
27.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
27.4M
        if (*s != u1) {
22
1.12M
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
1.68M
            while (1) {
25
1.68M
                if (++s == end)
26
4.13k
                    return;
27
1.68M
                if (*s == u1)
28
1.09M
                    break;
29
588k
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
24.1k
#ifdef STRINGLIB_FAST_MEMCHR
33
24.1k
                    s++;
34
24.1k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
24.1k
                    if (s == NULL)
36
5.26k
                        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
18.8k
                    break;
48
24.1k
                }
49
588k
            }
50
1.12M
        }
51
27.4M
        *s = u2;
52
27.4M
    }
53
14.0k
}