Ruby: prism/util/pm_memchr.c Source File

Ruby 3.3.6p108 (2024-11-05 revision 75015d4c1f6965b5e85e96fb309f1f2129f933c0)

pm_memchr.c
1#include "prism/util/pm_memchr.h"
2
3#define PRISM_MEMCHR_TRAILING_BYTE_MINIMUM 0x40
4
10void *
11pm_memchr(const void *memory, int character, size_t number, bool encoding_changed, const pm_encoding_t *encoding) {
12 if (encoding_changed && encoding->multibyte && character >= PRISM_MEMCHR_TRAILING_BYTE_MINIMUM) {
13 const uint8_t *source = (const uint8_t *) memory;
14 size_t index = 0;
15
16 while (index < number) {
17 if (source[index] == character) {
18 return (void *) (source + index);
19 }
20
21 size_t width = encoding->char_width(source + index, (ptrdiff_t) (number - index));
22 if (width == 0) {
23 return NULL;
24 }
25
26 index += width;
27 }
28
29 return NULL;
30 } else {
31 return memchr(memory, character, number);
32 }
33}
34
35#undef PRISM_MEMCHR_TRAILING_BYTE_MINIMUM
pm_memchr.h
A custom memchr implementation.
pm_encoding_t
This struct defines the functions necessary to implement the encoding interface so we can determine h...
Definition encoding.h:23
pm_encoding_t::char_width
size_t(* char_width)(const uint8_t *b, ptrdiff_t n)
Return the number of bytes that the next character takes if it is valid in the encoding.
Definition encoding.h:29
pm_encoding_t::multibyte
bool multibyte
Return true if the encoding is a multibyte encoding.
Definition encoding.h:61