ZJIT: Stop padding side exits (#13295)
[ruby.git] / prism / util / pm_memchr.c
blob7ea20ace6d529070f8d8fca69dee73207f65805f
1 #include "prism/util/pm_memchr.h"
3 #define PRISM_MEMCHR_TRAILING_BYTE_MINIMUM 0x40
5 /**
6 * We need to roll our own memchr to handle cases where the encoding changes and
7 * we need to search for a character in a buffer that could be the trailing byte
8 * of a multibyte character.
9 */
10 void *
11 pm_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;
16 while (index < number) {
17 if (source[index] == character) {
18 return (void *) (source + index);
21 size_t width = encoding->char_width(source + index, (ptrdiff_t) (number - index));
22 if (width == 0) {
23 return NULL;
26 index += width;
29 return NULL;
30 } else {
31 return memchr(memory, character, number);
35 #undef PRISM_MEMCHR_TRAILING_BYTE_MINIMUM