[ruby/json] json_string_unescape: use memchr to search for backslashes

https://github.com/ruby/json/commit/5e6cfcf724
This commit is contained in:
Jean Boussier 2025-01-16 18:31:18 +01:00 committed by Hiroshi SHIBATA
parent ba8f22c040
commit df8f93848e
Notes: git 2025-01-20 07:09:20 +00:00

View File

@ -592,18 +592,12 @@ static VALUE json_string_unescape(JSON_ParserState *state, const char *string, c
}
}
pe = memchr(p, '\\', bufferSize);
if (RB_UNLIKELY(pe == NULL)) {
return build_string(string, stringEnd, intern, symbolize);
}
VALUE result = rb_str_buf_new(bufferSize);
rb_enc_associate_index(result, utf8_encindex);
buffer = RSTRING_PTR(result);
bufferStart = buffer;
while (pe < stringEnd) {
if (*pe == '\\') {
while ((pe = memchr(pe, '\\', stringEnd - pe))) {
unescape = (char *) "?";
unescape_len = 1;
if (pe > p) {
@ -674,14 +668,11 @@ static VALUE json_string_unescape(JSON_ParserState *state, const char *string, c
MEMCPY(buffer, unescape, char, unescape_len);
buffer += unescape_len;
p = ++pe;
} else {
pe++;
}
}
if (pe > p) {
MEMCPY(buffer, p, char, pe - p);
buffer += pe - p;
if (stringEnd > p) {
MEMCPY(buffer, p, char, stringEnd - p);
buffer += stringEnd - p;
}
rb_str_set_len(result, buffer - bufferStart);