Skip to content

Commit 9aa4922

Browse files
committed
Fix fallback scanner
Fixes NLnetLabs#171.
1 parent d91fd9e commit 9aa4922

File tree

1 file changed

+39
-39
lines changed

1 file changed

+39
-39
lines changed

src/fallback/scanner.h

+39-39
Original file line numberDiff line numberDiff line change
@@ -17,84 +17,85 @@ nonnull_all
1717
static really_inline const char *scan_comment(
1818
parser_t *parser, const char *start, const char *end)
1919
{
20+
assert(!parser->file->state.is_escaped);
21+
2022
while (start < end) {
2123
if (unlikely(*start == '\n'))
2224
return start;
23-
start += 1;
25+
start++;
2426
}
2527

2628
parser->file->state.in_comment = 1;
27-
return end;
29+
return start;
2830
}
2931

3032
nonnull_all
3133
static really_inline const char *scan_quoted(
3234
parser_t *parser, const char *start, const char *end)
3335
{
36+
if (unlikely(parser->file->state.is_escaped))
37+
goto escaped;
38+
3439
while (start < end) {
3540
if (*start == '\\') {
36-
parser->file->lines.tail[0] += *(start + 1) == '\n';
37-
start += 2;
41+
escaped:
42+
if ((parser->file->state.is_escaped = (++start == end)))
43+
break;
44+
assert(start < end);
45+
*parser->file->lines.tail += (*start == '\n');
46+
start++;
3847
} else if (*start == '\"') {
48+
parser->file->state.in_quoted = 0;
3949
*parser->file->delimiters.tail++ = start;
40-
return start + 1;
41-
} else if (*start == '\n') {
42-
parser->file->lines.tail[0]++;
43-
start += 1;
50+
return ++start;
4451
} else {
45-
start += 1;
52+
*parser->file->lines.tail += (*start == '\n');
53+
start++;
4654
}
4755
}
4856

49-
parser->file->lines.tail[0] -= *end == '\n';
5057
parser->file->state.in_quoted = 1;
51-
parser->file->state.is_escaped = (start > end);
52-
return end;
58+
return start;
5359
}
5460

5561
nonnull_all
5662
static really_inline const char *scan_contiguous(
5763
parser_t *parser, const char *start, const char *end)
5864
{
65+
if (parser->file->state.is_escaped)
66+
goto escaped;
67+
5968
while (start < end) {
6069
if (likely(classify[ (uint8_t)*start ] == CONTIGUOUS)) {
61-
if (likely(*start != '\\')) {
62-
start += 1;
63-
} else {
64-
parser->file->lines.tail[0] += *(start + 1) == '\n';
65-
start += 2;
70+
if (unlikely(*start == '\\')) {
71+
escaped:
72+
if ((parser->file->state.is_escaped = (++start == end)))
73+
break;
74+
assert(start < end);
75+
parser->file->lines.tail[0] += (*start == '\n');
6676
}
77+
start++;
6778
} else {
79+
parser->file->state.follows_contiguous = 0;
6880
*parser->file->delimiters.tail++ = start;
6981
return start;
7082
}
7183
}
7284

73-
parser->file->lines.tail[0] -= *end == '\n';
74-
parser->file->state.is_escaped = (start > end);
7585
parser->file->state.follows_contiguous = 1;
76-
return end;
86+
return start;
7787
}
7888

7989
nonnull_all
8090
static really_inline void scan(
8191
parser_t *parser, const char *start, const char *end)
8292
{
83-
if (parser->file->state.is_escaped) {
84-
parser->file->state.is_escaped = 0;
85-
parser->file->lines.tail[0] += (*start++ == '\n');
86-
}
87-
88-
if (parser->file->state.follows_contiguous) {
89-
parser->file->state.follows_contiguous = 0;
93+
if (parser->file->state.follows_contiguous)
9094
start = scan_contiguous(parser, start, end);
91-
} if (parser->file->state.in_comment) {
92-
parser->file->state.in_comment = 0;
95+
else if (parser->file->state.in_comment)
9396
start = scan_comment(parser, start, end);
94-
} else if (parser->file->state.in_quoted) {
95-
parser->file->state.in_quoted = 0;
97+
else if (parser->file->state.in_quoted)
9698
start = scan_quoted(parser, start, end);
97-
}
9899

99100
while (start < end) {
100101
const int32_t code = classify[(uint8_t)*start];
@@ -104,18 +105,17 @@ static really_inline void scan(
104105
*parser->file->fields.tail++ = start;
105106
start = scan_contiguous(parser, start, end);
106107
} else if (code == LINE_FEED) {
107-
if (parser->file->lines.tail[0])
108+
if (*parser->file->lines.tail) {
108109
*parser->file->fields.tail++ = line_feed;
109-
else
110+
parser->file->lines.tail++;
111+
} else {
110112
*parser->file->fields.tail++ = start;
113+
}
111114
start++;
112115
} else if (code == QUOTED) {
113116
*parser->file->fields.tail++ = start;
114-
start = scan_quoted(parser, start+1, end);
115-
} else if (code == LEFT_PAREN) {
116-
*parser->file->fields.tail++ = start;
117-
start++;
118-
} else if (code == RIGHT_PAREN) {
117+
start = scan_quoted(parser, start + 1, end);
118+
} else if (code == LEFT_PAREN || code == RIGHT_PAREN) {
119119
*parser->file->fields.tail++ = start;
120120
start++;
121121
} else {

0 commit comments

Comments
 (0)