Commit Diff
Diff:
bffa7daab8d1a54cf099287cbc0ce443fea4c0d0
d93c819182ba6decf8f7f1787c6ba416f76c315a
Commit:
d93c819182ba6decf8f7f1787c6ba416f76c315a
Tree:
030f3cbe8d2d65497b20715244f2d217c010c7ec
Author:
Omar Polo <op@omarpolo.com>
Date:
Fri Jul 9 06:30:55 2021 UTC
Message:
use bsearch to match the keywords

not that it's a bottleneck, but it's fancier this way.
commit - bffa7daab8d1a54cf099287cbc0ce443fea4c0d0
commit + d93c819182ba6decf8f7f1787c6ba416f76c315a
blob - 02ab8272bfbf564b3cfee0ac1ef66cb82dca3aa7
blob + a71c6bf8e593911e0538d542291af4b5b0712292
--- parse.y
+++ parse.y
@@ -63,6 +63,7 @@ static int yylex(void);
static struct location *new_location(void);
void yyerror(const char*, ...);
+int kw_cmp(const void *, const void *);
static int yylex(void);
int parse_portno(const char*);
void parse_conf(const char*);
@@ -340,6 +341,7 @@ static struct keyword {
const char *word;
int token;
} keywords[] = {
+ /* these MUST be sorted */
{"alias", TALIAS},
{"auto", TAUTO},
{"block", TBLOCK},
@@ -374,15 +376,22 @@ static struct keyword {
{"user", TUSER},
};
+int
+kw_cmp(const void *k, const void *e)
+{
+ return strcmp(k, ((struct keyword *)e)->word);
+}
+
/*
* Taken an adapted from doas' parse.y
*/
static int
yylex(void)
{
+ struct keyword *kw;
char buf[8096], *ebuf, *p, *str, *v, *val;
int c, quotes = 0, escape = 0, qpos = -1, nonkw = 0;
- size_t i, len;
+ size_t len;
p = buf;
ebuf = buf + sizeof(buf);
@@ -529,10 +538,10 @@ eow:
goto repeat;
}
if (!nonkw) {
- for (i = 0; i < sizeof(keywords) / sizeof(keywords[0]); ++i) {
- if (!strcmp(buf, keywords[i].word))
- return keywords[i].token;
- }
+ kw = bsearch(buf, keywords, sizeof(keywords)/sizeof(keywords[0]),
+ sizeof(keywords[0]), kw_cmp);
+ if (kw != NULL)
+ return kw->token;
}
c = *buf;
if (!nonkw && (c == '-' || isdigit(c))) {
Omar Polo