Coverage Report

Created: 2025-11-30 06:38

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.64M
{
11
1.64M
    *s = u2;
12
132M
    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
132M
        if (*s != u1) {
22
7.60M
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
27.2M
            while (1) {
25
27.2M
                if (++s == end)
26
1.06M
                    return;
27
26.1M
                if (*s == u1)
28
5.79M
                    break;
29
20.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
133k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
133k
                    if (s == NULL)
36
36.0k
                        return;
37
#else
38
                    Py_ssize_t i;
39
617k
                    STRINGLIB_CHAR ch1 = (STRINGLIB_CHAR) u1;
40
                    s++;
41
617k
                    i = FASTSEARCH(s, end - s, &ch1, 1, 0, FAST_SEARCH);
42
617k
                    if (i < 0)
43
259k
                        return;
44
358k
                    s += i;
45
358k
#endif
46
                    /* restart the dummy loop */
47
97.8k
                    break;
48
751k
                }
49
20.3M
            }
50
7.60M
        }
51
130M
        *s = u2;
52
130M
    }
53
1.64M
}
unicodeobject.c:ucs1lib_replace_1char_inplace
Line
Count
Source
10
627k
{
11
627k
    *s = u2;
12
42.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
42.2M
        if (*s != u1) {
22
1.54M
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
4.26M
            while (1) {
25
4.26M
                if (++s == end)
26
402k
                    return;
27
3.86M
                if (*s == u1)
28
1.03M
                    break;
29
2.82M
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
102k
#ifdef STRINGLIB_FAST_MEMCHR
33
102k
                    s++;
34
102k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
102k
                    if (s == NULL)
36
29.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
73.2k
                    break;
48
102k
                }
49
2.82M
            }
50
1.54M
        }
51
41.8M
        *s = u2;
52
41.8M
    }
53
627k
}
unicodeobject.c:ucs2lib_replace_1char_inplace
Line
Count
Source
10
1.00M
{
11
1.00M
    *s = u2;
12
69.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
69.1M
        if (*s != u1) {
22
5.11M
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
21.4M
            while (1) {
25
21.4M
                if (++s == end)
26
654k
                    return;
27
20.7M
                if (*s == u1)
28
3.84M
                    break;
29
16.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
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
                    if (s == NULL)
36
                        return;
37
#else
38
617k
                    Py_ssize_t i;
39
617k
                    STRINGLIB_CHAR ch1 = (STRINGLIB_CHAR) u1;
40
617k
                    s++;
41
617k
                    i = FASTSEARCH(s, end - s, &ch1, 1, 0, FAST_SEARCH);
42
617k
                    if (i < 0)
43
259k
                        return;
44
358k
                    s += i;
45
358k
#endif
46
                    /* restart the dummy loop */
47
358k
                    break;
48
617k
                }
49
16.9M
            }
50
5.11M
        }
51
68.2M
        *s = u2;
52
68.2M
    }
53
1.00M
}
unicodeobject.c:ucs4lib_replace_1char_inplace
Line
Count
Source
10
15.6k
{
11
15.6k
    *s = u2;
12
20.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
20.7M
        if (*s != u1) {
22
944k
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
1.55M
            while (1) {
25
1.55M
                if (++s == end)
26
4.45k
                    return;
27
1.55M
                if (*s == u1)
28
909k
                    break;
29
645k
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
30.9k
#ifdef STRINGLIB_FAST_MEMCHR
33
30.9k
                    s++;
34
30.9k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
30.9k
                    if (s == NULL)
36
6.29k
                        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
24.6k
                    break;
48
30.9k
                }
49
645k
            }
50
944k
        }
51
20.7M
        *s = u2;
52
20.7M
    }
53
15.6k
}