Ruby 4.1.0dev (2026-09-09 revision 213b1add1ff300be0e375b8c792c9b0eb9f53981)
memchr.c
1#include "prism/internal/memchr.h"
2
3#include <stdbool.h>
4#include <stdint.h>
5#include <string.h>
6
7#define TRAILING_BYTE_MINIMUM 0x40
8
14const void *
15pm_memchr(const void *memory, int character, size_t number, bool encoding_changed, const pm_encoding_t *encoding) {
16 if (encoding_changed && encoding->multibyte && character >= TRAILING_BYTE_MINIMUM) {
17 const uint8_t *source = (const uint8_t *) memory;
18 size_t index = 0;
19
20 while (index < number) {
21 if (source[index] == character) {
22 return (void *) (source + index);
23 }
24
25 size_t width = encoding->char_width(source + index, (ptrdiff_t) (number - index));
26 if (width == 0) {
27 return NULL;
28 }
29
30 index += width;
31 }
32
33 return NULL;
34 } else {
35 return memchr(memory, character, number);
36 }
37}
C99 shim for <stdbool.h>