Simplify maybeSkipWs

This commit is contained in:
2025-05-14 19:52:11 -04:00
parent c30e3e6713
commit b15a598f2d

View File

@@ -160,24 +160,15 @@ const char *symbolNames[] = {
"N_OBJECT_MAYBE_CONTINUE", "N_OBJECT_MAYBE_CONTINUE",
}; };
int leadingWhitespaceCount(const char *buf, int len) { constexpr static struct Tables {
constexpr static struct Table { constexpr Tables() {
constexpr Table() { whitespace[' '] = true;
for (int i = 0; i < 256; ++i) { whitespace['\n'] = true;
notWhitespace[i] = !(i == ' ' || i == '\n' || i == '\t' || i == '\r'); whitespace['\r'] = true;
whitespace['\t'] = true;
} }
} alignas(16) bool whitespace[256]{};
alignas(16) bool notWhitespace[256]{}; } tables;
} table;
int i = 0;
for (; i < len; ++i) {
if (table.notWhitespace[buf[i]]) {
break;
}
}
return i;
}
namespace { namespace {
@@ -204,7 +195,11 @@ private:
int len() const { return bufEnd - buf; } int len() const { return bufEnd - buf; }
// Helpers // Helpers
void maybeSkipWs() { buf += leadingWhitespaceCount(buf, len()); } void maybeSkipWs() {
while (buf != bufEnd && tables.whitespace[*buf]) {
++buf;
}
}
bool parseLiteral(const char *literal) { bool parseLiteral(const char *literal) {
const int litLen = strlen(literal); const int litLen = strlen(literal);
if (len() < litLen) { if (len() < litLen) {
@@ -440,7 +435,11 @@ struct Parser2 {
private: private:
// Helpers // Helpers
void maybeSkipWs() { buf += leadingWhitespaceCount(buf, len()); } void maybeSkipWs() {
while (buf != bufEnd && tables.whitespace[*buf]) {
++buf;
}
}
bool parseLiteral(const char *literal) { bool parseLiteral(const char *literal) {
const int litLen = strlen(literal); const int litLen = strlen(literal);
if (len() < litLen) { if (len() < litLen) {