Coverage Report

Created: 2025-07-11 06:24

/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.05M
{
11
1.05M
    *s = u2;
12
124M
    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
123M
        if (*s != u1) {
22
5.40M
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
18.0M
            while (1) {
25
18.0M
                if (++s == end)
26
600k
                    return;
27
17.4M
                if (*s == u1)
28
4.32M
                    break;
29
13.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
93.7k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
93.7k
                    if (s == NULL)
36
23.9k
                        return;
37
#else
38
                    Py_ssize_t i;
39
387k
                    STRINGLIB_CHAR ch1 = (STRINGLIB_CHAR) u1;
40
                    s++;
41
387k
                    i = FASTSEARCH(s, end - s, &ch1, 1, 0, FAST_SEARCH);
42
387k
                    if (i < 0)
43
178k
                        return;
44
208k
                    s += i;
45
208k
#endif
46
                    /* restart the dummy loop */
47
69.8k
                    break;
48
481k
                }
49
13.1M
            }
50
5.40M
        }
51
123M
        *s = u2;
52
123M
    }
53
1.05M
}
unicodeobject.c:ucs1lib_replace_1char_inplace
Line
Count
Source
10
372k
{
11
372k
    *s = u2;
12
39.8M
    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
39.6M
        if (*s != u1) {
22
953k
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
2.76M
            while (1) {
25
2.76M
                if (++s == end)
26
162k
                    return;
27
2.60M
                if (*s == u1)
28
718k
                    break;
29
1.88M
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
72.5k
#ifdef STRINGLIB_FAST_MEMCHR
33
72.5k
                    s++;
34
72.5k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
72.5k
                    if (s == NULL)
36
19.6k
                        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
52.9k
                    break;
48
72.5k
                }
49
1.88M
            }
50
953k
        }
51
39.4M
        *s = u2;
52
39.4M
    }
53
372k
}
unicodeobject.c:ucs2lib_replace_1char_inplace
Line
Count
Source
10
672k
{
11
672k
    *s = u2;
12
58.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
58.7M
        if (*s != u1) {
22
3.38M
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
13.8M
            while (1) {
25
13.8M
                if (++s == end)
26
433k
                    return;
27
13.4M
                if (*s == u1)
28
2.56M
                    break;
29
10.8M
                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
387k
                    Py_ssize_t i;
39
387k
                    STRINGLIB_CHAR ch1 = (STRINGLIB_CHAR) u1;
40
387k
                    s++;
41
387k
                    i = FASTSEARCH(s, end - s, &ch1, 1, 0, FAST_SEARCH);
42
387k
                    if (i < 0)
43
178k
                        return;
44
208k
                    s += i;
45
208k
#endif
46
                    /* restart the dummy loop */
47
208k
                    break;
48
387k
                }
49
10.8M
            }
50
3.38M
        }
51
58.0M
        *s = u2;
52
58.0M
    }
53
672k
}
unicodeobject.c:ucs4lib_replace_1char_inplace
Line
Count
Source
10
12.5k
{
11
12.5k
    *s = u2;
12
25.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
25.4M
        if (*s != u1) {
22
1.07M
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
1.42M
            while (1) {
25
1.42M
                if (++s == end)
26
4.67k
                    return;
27
1.41M
                if (*s == u1)
28
1.04M
                    break;
29
372k
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
21.1k
#ifdef STRINGLIB_FAST_MEMCHR
33
21.1k
                    s++;
34
21.1k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
21.1k
                    if (s == NULL)
36
4.34k
                        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
16.8k
                    break;
48
21.1k
                }
49
372k
            }
50
1.07M
        }
51
25.4M
        *s = u2;
52
25.4M
    }
53
12.5k
}