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