Simplify maybeSkipWs
This commit is contained in:
37
src/test.cpp
37
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');
|
||||
constexpr static struct Tables {
|
||||
constexpr Tables() {
|
||||
whitespace[' '] = true;
|
||||
whitespace['\n'] = true;
|
||||
whitespace['\r'] = true;
|
||||
whitespace['\t'] = true;
|
||||
}
|
||||
}
|
||||
alignas(16) bool notWhitespace[256]{};
|
||||
} table;
|
||||
|
||||
int i = 0;
|
||||
for (; i < len; ++i) {
|
||||
if (table.notWhitespace[buf[i]]) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
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) {
|
||||
|
||||
Reference in New Issue
Block a user