rules: be more paranoid in scanner

This can't happen, but better safe than sorry. The optimizations were
noticeable but negligible.

Signed-off-by: Ran Benita <ran234@gmail.com>
master
Ran Benita 2013-03-14 12:45:34 +02:00 committed by Daniel Stone
parent 0e200bd507
commit 0513686b5d
1 changed files with 9 additions and 16 deletions

View File

@ -213,35 +213,26 @@ scanner_init(struct scanner *s, struct xkb_context *ctx,
static char static char
peek(struct scanner *s) peek(struct scanner *s)
{ {
return s->s[s->pos]; return s->pos < s->len ? s->s[s->pos] : '\0';
} }
static bool static bool
eof(struct scanner *s) eof(struct scanner *s)
{ {
return s->s[s->pos] == '\0' || s->pos >= s->len; return peek(s) == '\0';
} }
static bool static bool
eol(struct scanner *s) eol(struct scanner *s)
{ {
return s->s[s->pos] == '\n'; return peek(s) == '\n';
} }
/*
* Use the check_nl variant when the current char might be a new line;
* just an optimization.
*/
static char static char
next(struct scanner *s) next(struct scanner *s)
{ {
s->column++; if (eof(s))
return s->s[s->pos++]; return '\0';
}
static char
next_check_nl(struct scanner *s)
{
if (eol(s)) { if (eol(s)) {
s->line++; s->line++;
s->column = 1; s->column = 1;
@ -264,6 +255,8 @@ chr(struct scanner *s, char ch)
static bool static bool
str(struct scanner *s, const char *string, size_t len) str(struct scanner *s, const char *string, size_t len)
{ {
if (s->len - s->pos < len)
return false;
if (strncasecmp(s->s + s->pos, string, len) != 0) if (strncasecmp(s->s + s->pos, string, len) != 0)
return false; return false;
s->pos += len; s->column += len; s->pos += len; s->column += len;
@ -286,7 +279,7 @@ skip_more_whitespace_and_comments:
/* New line. */ /* New line. */
if (eol(s)) { if (eol(s)) {
while (eol(s)) next_check_nl(s); while (eol(s)) next(s);
return TOK_END_OF_LINE; return TOK_END_OF_LINE;
} }
@ -297,7 +290,7 @@ skip_more_whitespace_and_comments:
"illegal new line escape; must appear at end of line"); "illegal new line escape; must appear at end of line");
return TOK_ERROR; return TOK_ERROR;
} }
next_check_nl(s); next(s);
goto skip_more_whitespace_and_comments; goto skip_more_whitespace_and_comments;
} }