Use table for hex values

This commit is contained in:
2025-06-23 21:52:17 -04:00
parent 2299904557
commit 9803364adb
2 changed files with 25 additions and 24 deletions

View File

@@ -514,16 +514,12 @@ inline PRESERVE_NONE WeaselJsonStatus n_string_following_escape(Parser3 *self,
inline PRESERVE_NONE WeaselJsonStatus t_hex(Parser3 *self, char *buf,
char *bufEnd) {
self->utf8Codepoint <<= 4;
if (('0' <= *buf && *buf <= '9')) {
self->utf8Codepoint |= *buf - '0';
} else if ('a' <= *buf && *buf <= 'f') {
self->utf8Codepoint |= 10 + *buf - 'a';
} else if ('A' <= *buf && *buf <= 'F') {
self->utf8Codepoint |= 10 + *buf - 'A';
} else [[unlikely]] {
auto hexVal = tables.hex[uint8_t(*buf)];
if (hexVal < 0) [[unlikely]] {
return WeaselJson_REJECT;
}
self->utf8Codepoint <<= 4;
self->utf8Codepoint |= hexVal;
++buf;
self->pop();
MUSTTAIL return Parser3::keepGoing(self, buf, bufEnd);
@@ -531,16 +527,12 @@ inline PRESERVE_NONE WeaselJsonStatus t_hex(Parser3 *self, char *buf,
inline PRESERVE_NONE WeaselJsonStatus t_hex2(Parser3 *self, char *buf,
char *bufEnd) {
self->utf8Codepoint <<= 4;
if (('0' <= *buf && *buf <= '9')) {
self->utf8Codepoint |= *buf - '0';
} else if ('a' <= *buf && *buf <= 'f') {
self->utf8Codepoint |= 10 + *buf - 'a';
} else if ('A' <= *buf && *buf <= 'F') {
self->utf8Codepoint |= 10 + *buf - 'A';
} else [[unlikely]] {
auto hexVal = tables.hex[uint8_t(*buf)];
if (hexVal < 0) [[unlikely]] {
return WeaselJson_REJECT;
}
self->utf8Codepoint <<= 4;
self->utf8Codepoint |= hexVal;
++buf;
// Write codepoint in utf-8 if there's room in the user provided buffer. If
@@ -599,16 +591,12 @@ inline PRESERVE_NONE WeaselJsonStatus t_hex2(Parser3 *self, char *buf,
inline PRESERVE_NONE WeaselJsonStatus t_hex3(Parser3 *self, char *buf,
char *bufEnd) {
self->utf8Codepoint <<= 4;
if (('0' <= *buf && *buf <= '9')) {
self->utf8Codepoint |= *buf - '0';
} else if ('a' <= *buf && *buf <= 'f') {
self->utf8Codepoint |= 10 + *buf - 'a';
} else if ('A' <= *buf && *buf <= 'F') {
self->utf8Codepoint |= 10 + *buf - 'A';
} else [[unlikely]] {
auto hexVal = tables.hex[uint8_t(*buf)];
if (hexVal < 0) [[unlikely]] {
return WeaselJson_REJECT;
}
self->utf8Codepoint <<= 4;
self->utf8Codepoint |= hexVal;
++buf;
if (!(0xdc00 <= self->utf8Codepoint && self->utf8Codepoint <= 0xdfff))