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",
|
"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 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 {
|
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) {
|
||||||
|
|||||||
Reference in New Issue
Block a user