Coverage Report

Created: 2025-07-04 06:49

/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.27M
{
11
1.27M
    *s = u2;
12
131M
    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
131M
        if (*s != u1) {
22
6.90M
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
23.2M
            while (1) {
25
23.2M
                if (++s == end)
26
650k
                    return;
27
22.6M
                if (*s == u1)
28
5.54M
                    break;
29
17.0M
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
#ifdef STRINGLIB_FAST_MEMCHR
33
                    s++;
34
116k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
116k
                    if (s == NULL)
36
29.5k
                        return;
37
#else
38
                    Py_ssize_t i;
39
592k
                    STRINGLIB_CHAR ch1 = (STRINGLIB_CHAR) u1;
40
                    s++;
41
592k
                    i = FASTSEARCH(s, end - s, &ch1, 1, 0, FAST_SEARCH);
42
592k
                    if (i < 0)
43
247k
                        return;
44
344k
                    s += i;
45
344k
#endif
46
                    /* restart the dummy loop */
47
87.3k
                    break;
48
709k
                }
49
17.0M
            }
50
6.90M
        }
51
130M
        *s = u2;
52
130M
    }
53
1.27M
}
unicodeobject.c:ucs1lib_replace_1char_inplace
Line
Count
Source
10
471k
{
11
471k
    *s = u2;
12
40.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
40.1M
        if (*s != u1) {
22
1.12M
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
3.60M
            while (1) {
25
3.60M
                if (++s == end)
26
206k
                    return;
27
3.39M
                if (*s == u1)
28
824k
                    break;
29
2.57M
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
95.7k
#ifdef STRINGLIB_FAST_MEMCHR
33
95.7k
                    s++;
34
95.7k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
95.7k
                    if (s == NULL)
36
24.3k
                        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
71.4k
                    break;
48
95.7k
                }
49
2.57M
            }
50
1.12M
        }
51
39.9M
        *s = u2;
52
39.9M
    }
53
471k
}
unicodeobject.c:ucs2lib_replace_1char_inplace
Line
Count
Source
10
795k
{
11
795k
    *s = u2;
12
62.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
62.0M
        if (*s != u1) {
22
4.56M
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
17.9M
            while (1) {
25
17.9M
                if (++s == end)
26
439k
                    return;
27
17.5M
                if (*s == u1)
28
3.53M
                    break;
29
13.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
592k
                    Py_ssize_t i;
39
592k
                    STRINGLIB_CHAR ch1 = (STRINGLIB_CHAR) u1;
40
592k
                    s++;
41
592k
                    i = FASTSEARCH(s, end - s, &ch1, 1, 0, FAST_SEARCH);
42
592k
                    if (i < 0)
43
247k
                        return;
44
344k
                    s += i;
45
344k
#endif
46
                    /* restart the dummy loop */
47
344k
                    break;
48
592k
                }
49
13.9M
            }
50
4.56M
        }
51
61.4M
        *s = u2;
52
61.4M
    }
53
795k
}
unicodeobject.c:ucs4lib_replace_1char_inplace
Line
Count
Source
10
12.9k
{
11
12.9k
    *s = u2;
12
28.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
28.9M
        if (*s != u1) {
22
1.21M
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
1.69M
            while (1) {
25
1.69M
                if (++s == end)
26
4.80k
                    return;
27
1.69M
                if (*s == u1)
28
1.18M
                    break;
29
508k
                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
5.23k
                        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
15.8k
                    break;
48
21.1k
                }
49
508k
            }
50
1.21M
        }
51
28.9M
        *s = u2;
52
28.9M
    }
53
12.9k
}