Commit 8a0acf2c67633a5569124d6928c17c9a885949a5

Ran Benita 2014-10-07T23:42:08

scanner-utils: optimize one-line comments Compose files have a lot of those. Signed-off-by: Ran Benita <ran234@gmail.com>

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;
     }