From b15a598f2d5cc576adf24bbfa81720e8783fc4f8 Mon Sep 17 00:00:00 2001 From: Andrew Noyes Date: Wed, 14 May 2025 19:52:11 -0400 Subject: [PATCH] Simplify maybeSkipWs --- src/test.cpp | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/src/test.cpp b/src/test.cpp index 909e7e7..8a2ad0d 100644 --- a/src/test.cpp +++ b/src/test.cpp @@ -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) {