From 40bcbd47d6c2d3882603d3b7fa963e91d7d1b271 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20St=C3=B6neberg?= Date: Tue, 31 May 2022 13:52:34 +0200 Subject: [PATCH] small `Token::Match()` optimizations (#4154) * token.cpp: fixed `readability-else-after-return` warnings in `Match()` * token.cpp: removed some duplicated code from `Match()` * token.cpp: use `strchr()` instead of loop in `Match()` * token.cpp: added early exit and removed unnecessary loop condition in `Match()` --- lib/token.cpp | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/lib/token.cpp b/lib/token.cpp index 3685dbfd2..4a68fc7a4 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -638,8 +638,11 @@ const char *Token::chrInFirstWord(const char *str, char c) bool Token::Match(const Token *tok, const char pattern[], nonneg int varid) { + if (!(*pattern)) + return true; + const char *p = pattern; - while (*p) { + while (true) { // Skip spaces in pattern.. while (*p == ' ') ++p; @@ -654,8 +657,9 @@ bool Token::Match(const Token *tok, const char pattern[], nonneg int varid) while (*p && *p != ' ') ++p; continue; - } else - return false; + } + + return false; } // [.. => search for a one-character token.. @@ -686,8 +690,6 @@ bool Token::Match(const Token *tok, const char pattern[], nonneg int varid) return false; p = temp; - while (*p && *p != ' ') - ++p; } // Parse "not" options. Token can be anything except the given one @@ -695,8 +697,6 @@ bool Token::Match(const Token *tok, const char pattern[], nonneg int varid) p += 2; if (firstWordEquals(p, tok->str().c_str())) return false; - while (*p && *p != ' ') - ++p; } // Parse multi options, such as void|int|char (accept token which is one of these 3) @@ -707,14 +707,16 @@ bool Token::Match(const Token *tok, const char pattern[], nonneg int varid) while (*p && *p != ' ') ++p; continue; - } else if (res == -1) { + } + if (res == -1) { // No match return false; } } - while (*p && *p != ' ') - ++p; + // using strchr() for the other instances leads to a performance decrease + if (!(p = strchr(p, ' '))) + break; tok = tok->next(); }