LCOV - code coverage report
Current view: | top level - Objects/stringlib - replace.h (source / functions) |  |  | Hit | Total | Coverage
Test: | CPython 3.12 LCOV report [commit 5e6661bce9] |  | Lines: | 16 | 22 | 72.7 %
Date: | 2023-03-20 08:15:36 |  | Functions: | 1 | 3 | 33.3 %
|  |  | Branches: | 11 | 16 | 68.8 %

               Branch data     Line data    Source code
           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                 :         98 : STRINGLIB(replace_1char_inplace)(STRINGLIB_CHAR* s, STRINGLIB_CHAR* end,
           9                 :            :                                  Py_UCS4 u1, Py_UCS4 u2, Py_ssize_t maxcount)
          10                 :            : {
          11                 :         98 :     *s = u2;
          12   [ +  -  +  - ]:        128 :     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         [ +  - ]:        128 :         if (*s != u1) {
          22                 :        128 :             int attempts = 10;
          23                 :            :             /* search u1 in a dummy loop */
          24                 :            :             while (1) {
          25         [ +  + ]:       1016 :                 if (++s == end)
          26                 :         55 :                     return;
          27         [ +  + ]:        961 :                 if (*s == u1)
          28                 :         24 :                     break;
          29         [ +  + ]:        937 :                 if (!--attempts) {
          30                 :            :                     /* if u1 was not found for attempts iterations,
          31                 :            :                        use FASTSEARCH() or memchr() */
          32                 :            : #ifdef STRINGLIB_FAST_MEMCHR
          33                 :         49 :                     s++;
          34                 :         49 :                     s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
          35         [ +  + ]:         49 :                     if (s == NULL)
          36                 :         43 :                         return;
          37                 :            : #else
          38                 :            :                     Py_ssize_t i;
          39                 :          0 :                     STRINGLIB_CHAR ch1 = (STRINGLIB_CHAR) u1;
          40                 :          0 :                     s++;
          41                 :          0 :                     i = FASTSEARCH(s, end - s, &ch1, 1, 0, FAST_SEARCH);
          42         [ #  # ]:          0 :                     if (i < 0)
          43                 :          0 :                         return;
          44                 :          0 :                     s += i;
          45                 :            : #endif
          46                 :            :                     /* restart the dummy loop */
          47                 :          6 :                     break;
          48                 :            :                 }
          49                 :            :             }
          50                 :            :         }
          51                 :         30 :         *s = u2;
          52                 :            :     }
          53                 :            : }

Generated by: LCOV version 1.14