scanner-utils: optimize one-line comments Compose files have a lot of those. Signed-off-by: Ran Benita <ran234@gmail.com>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
diff --git a/src/compose/parser.c b/src/compose/parser.c
index 29229f3..ff0b006 100644
--- a/src/compose/parser.c
+++ b/src/compose/parser.c
@@ -134,7 +134,7 @@ skip_more_whitespace_and_comments:
/* Skip comments. */
if (chr(s, '#')) {
- while (!eof(s) && !eol(s)) next(s);
+ skip_to_eol(s);
goto skip_more_whitespace_and_comments;
}
diff --git a/src/scanner-utils.h b/src/scanner-utils.h
index ba47f69..914e11f 100644
--- a/src/scanner-utils.h
+++ b/src/scanner-utils.h
@@ -102,6 +102,15 @@ eol(struct scanner *s)
return peek(s) == '\n';
}
+static inline void
+skip_to_eol(struct scanner *s)
+{
+ const char *nl = memchr(s->s + s->pos, '\n', s->len - s->pos);
+ const size_t new_pos = nl ? (size_t) (nl - s->s) : s->len;
+ s->column += new_pos - s->pos;
+ s->pos = new_pos;
+}
+
static inline char
next(struct scanner *s)
{
diff --git a/src/xkbcomp/rules.c b/src/xkbcomp/rules.c
index 3a6f57c..f4b475a 100644
--- a/src/xkbcomp/rules.c
+++ b/src/xkbcomp/rules.c
@@ -85,7 +85,7 @@ skip_more_whitespace_and_comments:
/* Skip comments. */
if (lit(s, "//")) {
- while (!eof(s) && !eol(s)) next(s);
+ skip_to_eol(s);
}
/* New line. */
diff --git a/src/xkbcomp/scanner.c b/src/xkbcomp/scanner.c
index ead20ab..8f24721 100644
--- a/src/xkbcomp/scanner.c
+++ b/src/xkbcomp/scanner.c
@@ -69,7 +69,7 @@ skip_more_whitespace_and_comments:
/* Skip comments. */
if (lit(s, "//") || chr(s, '#')) {
- while (!eof(s) && !eol(s)) next(s);
+ skip_to_eol(s);
goto skip_more_whitespace_and_comments;
}