Line | Count | Source |
1 | | #include "prism/internal/memchr.h" |
2 | | |
3 | | #include <stdbool.h> |
4 | | #include <stdint.h> |
5 | | #include <string.h> |
6 | | |
7 | 3.55k | #define TRAILING_BYTE_MINIMUM 0x40 |
8 | | |
9 | | /** |
10 | | * We need to roll our own memchr to handle cases where the encoding changes and |
11 | | * we need to search for a character in a buffer that could be the trailing byte |
12 | | * of a multibyte character. |
13 | | */ |
14 | | const void * |
15 | 31.6k | pm_memchr(const void *memory, int character, size_t number, bool encoding_changed, const pm_encoding_t *encoding) { |
16 | 31.6k | if (encoding_changed && encoding->multibyte7.22k && character >= 3.55k TRAILING_BYTE_MINIMUM3.55k ) { Branch (16:9): [True: 7.22k, False: 24.4k]
Branch (16:29): [True: 3.55k, False: 3.66k]
Branch (16:52): [True: 576, False: 2.97k]
|
17 | 576 | const uint8_t *source = (const uint8_t *) memory; |
18 | 576 | size_t index = 0; |
19 | | |
20 | 2.51k | while (index < number) { Branch (20:16): [True: 2.30k, False: 210]
|
21 | 2.30k | if (source[index] == character) { Branch (21:17): [True: 305, False: 2.00k]
|
22 | 305 | return (void *) (source + index); |
23 | 305 | } |
24 | | |
25 | 2.00k | size_t width = encoding->char_width(source + index, (ptrdiff_t) (number - index)); |
26 | 2.00k | if (width == 0) { Branch (26:17): [True: 61, False: 1.94k]
|
27 | 61 | return NULL; |
28 | 61 | } |
29 | | |
30 | 1.94k | index += width; |
31 | 1.94k | } |
32 | | |
33 | 210 | return NULL; |
34 | 31.0k | } else { |
35 | 31.0k | return memchr(memory, character, number); |
36 | 31.0k | } |
37 | 31.6k | } |