Use table for hex values
This commit is contained in:
@@ -514,16 +514,12 @@ inline PRESERVE_NONE WeaselJsonStatus n_string_following_escape(Parser3 *self,
|
|||||||
|
|
||||||
inline PRESERVE_NONE WeaselJsonStatus t_hex(Parser3 *self, char *buf,
|
inline PRESERVE_NONE WeaselJsonStatus t_hex(Parser3 *self, char *buf,
|
||||||
char *bufEnd) {
|
char *bufEnd) {
|
||||||
self->utf8Codepoint <<= 4;
|
auto hexVal = tables.hex[uint8_t(*buf)];
|
||||||
if (('0' <= *buf && *buf <= '9')) {
|
if (hexVal < 0) [[unlikely]] {
|
||||||
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]] {
|
|
||||||
return WeaselJson_REJECT;
|
return WeaselJson_REJECT;
|
||||||
}
|
}
|
||||||
|
self->utf8Codepoint <<= 4;
|
||||||
|
self->utf8Codepoint |= hexVal;
|
||||||
++buf;
|
++buf;
|
||||||
self->pop();
|
self->pop();
|
||||||
MUSTTAIL return Parser3::keepGoing(self, buf, bufEnd);
|
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,
|
inline PRESERVE_NONE WeaselJsonStatus t_hex2(Parser3 *self, char *buf,
|
||||||
char *bufEnd) {
|
char *bufEnd) {
|
||||||
self->utf8Codepoint <<= 4;
|
auto hexVal = tables.hex[uint8_t(*buf)];
|
||||||
if (('0' <= *buf && *buf <= '9')) {
|
if (hexVal < 0) [[unlikely]] {
|
||||||
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]] {
|
|
||||||
return WeaselJson_REJECT;
|
return WeaselJson_REJECT;
|
||||||
}
|
}
|
||||||
|
self->utf8Codepoint <<= 4;
|
||||||
|
self->utf8Codepoint |= hexVal;
|
||||||
++buf;
|
++buf;
|
||||||
|
|
||||||
// Write codepoint in utf-8 if there's room in the user provided buffer. If
|
// 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,
|
inline PRESERVE_NONE WeaselJsonStatus t_hex3(Parser3 *self, char *buf,
|
||||||
char *bufEnd) {
|
char *bufEnd) {
|
||||||
self->utf8Codepoint <<= 4;
|
auto hexVal = tables.hex[uint8_t(*buf)];
|
||||||
if (('0' <= *buf && *buf <= '9')) {
|
if (hexVal < 0) [[unlikely]] {
|
||||||
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]] {
|
|
||||||
return WeaselJson_REJECT;
|
return WeaselJson_REJECT;
|
||||||
}
|
}
|
||||||
|
self->utf8Codepoint <<= 4;
|
||||||
|
self->utf8Codepoint |= hexVal;
|
||||||
++buf;
|
++buf;
|
||||||
|
|
||||||
if (!(0xdc00 <= self->utf8Codepoint && self->utf8Codepoint <= 0xdfff))
|
if (!(0xdc00 <= self->utf8Codepoint && self->utf8Codepoint <= 0xdfff))
|
||||||
|
|||||||
13
src/tables.h
13
src/tables.h
@@ -17,9 +17,22 @@ constexpr inline struct Tables {
|
|||||||
unescape['f'] = '\f';
|
unescape['f'] = '\f';
|
||||||
unescape['\\'] = '\\';
|
unescape['\\'] = '\\';
|
||||||
unescape['/'] = '/';
|
unescape['/'] = '/';
|
||||||
|
for (int i = 0; i < 256; ++i) {
|
||||||
|
hex[i] = -1;
|
||||||
|
}
|
||||||
|
for (int i = '0'; i <= '9'; ++i) {
|
||||||
|
hex[i] = i - '0';
|
||||||
|
}
|
||||||
|
for (int i = 'a'; i <= 'f'; ++i) {
|
||||||
|
hex[i] = 10 + i - 'a';
|
||||||
|
}
|
||||||
|
for (int i = 'A'; i <= 'F'; ++i) {
|
||||||
|
hex[i] = 10 + i - 'A';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
bool whitespace[256]{};
|
bool whitespace[256]{};
|
||||||
char unescape[256]{};
|
char unescape[256]{};
|
||||||
|
int8_t hex[256]{};
|
||||||
} tables;
|
} tables;
|
||||||
|
|
||||||
// See https://gist.github.com/pervognsen/218ea17743e1442e59bb60d29b1aa725 for
|
// See https://gist.github.com/pervognsen/218ea17743e1442e59bb60d29b1aa725 for
|
||||||
|
|||||||
Reference in New Issue
Block a user