Coverage Report

Created: 2025-08-26 06:26

/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.35M
{
11
1.35M
    *s = u2;
12
175M
    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
175M
        if (*s != u1) {
22
7.30M
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
23.2M
            while (1) {
25
23.2M
                if (++s == end)
26
865k
                    return;
27
22.4M
                if (*s == u1)
28
5.87M
                    break;
29
16.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
95.7k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
95.7k
                    if (s == NULL)
36
28.7k
                        return;
37
#else
38
                    Py_ssize_t i;
39
464k
                    STRINGLIB_CHAR ch1 = (STRINGLIB_CHAR) u1;
40
                    s++;
41
464k
                    i = FASTSEARCH(s, end - s, &ch1, 1, 0, FAST_SEARCH);
42
464k
                    if (i < 0)
43
210k
                        return;
44
254k
                    s += i;
45
254k
#endif
46
                    /* restart the dummy loop */
47
66.9k
                    break;
48
560k
                }
49
16.5M
            }
50
7.30M
        }
51
174M
        *s = u2;
52
174M
    }
53
1.35M
}
unicodeobject.c:ucs1lib_replace_1char_inplace
Line
Count
Source
10
469k
{
11
469k
    *s = u2;
12
54.9M
    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
54.8M
        if (*s != u1) {
22
1.36M
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
3.91M
            while (1) {
25
3.91M
                if (++s == end)
26
304k
                    return;
27
3.60M
                if (*s == u1)
28
992k
                    break;
29
2.61M
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
67.0k
#ifdef STRINGLIB_FAST_MEMCHR
33
67.0k
                    s++;
34
67.0k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
67.0k
                    if (s == NULL)
36
23.2k
                        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
43.7k
                    break;
48
67.0k
                }
49
2.61M
            }
50
1.36M
        }
51
54.4M
        *s = u2;
52
54.4M
    }
53
469k
}
unicodeobject.c:ucs2lib_replace_1char_inplace
Line
Count
Source
10
873k
{
11
873k
    *s = u2;
12
88.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
88.3M
        if (*s != u1) {
22
4.72M
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
17.6M
            while (1) {
25
17.6M
                if (++s == end)
26
554k
                    return;
27
17.1M
                if (*s == u1)
28
3.70M
                    break;
29
13.4M
                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
464k
                    Py_ssize_t i;
39
464k
                    STRINGLIB_CHAR ch1 = (STRINGLIB_CHAR) u1;
40
464k
                    s++;
41
464k
                    i = FASTSEARCH(s, end - s, &ch1, 1, 0, FAST_SEARCH);
42
464k
                    if (i < 0)
43
210k
                        return;
44
254k
                    s += i;
45
254k
#endif
46
                    /* restart the dummy loop */
47
254k
                    break;
48
464k
                }
49
13.4M
            }
50
4.72M
        }
51
87.5M
        *s = u2;
52
87.5M
    }
53
873k
}
unicodeobject.c:ucs4lib_replace_1char_inplace
Line
Count
Source
10
15.0k
{
11
15.0k
    *s = u2;
12
32.0M
    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
32.0M
        if (*s != u1) {
22
1.21M
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
1.70M
            while (1) {
25
1.70M
                if (++s == end)
26
5.44k
                    return;
27
1.69M
                if (*s == u1)
28
1.17M
                    break;
29
520k
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
28.6k
#ifdef STRINGLIB_FAST_MEMCHR
33
28.6k
                    s++;
34
28.6k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
28.6k
                    if (s == NULL)
36
5.49k
                        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
23.1k
                    break;
48
28.6k
                }
49
520k
            }
50
1.21M
        }
51
32.0M
        *s = u2;
52
32.0M
    }
53
15.0k
}