From 764e44790ff2dbc6bf0c09e86f7661e6d87e4ce0 Mon Sep 17 00:00:00 2001 From: Leandro Penz Date: Sat, 10 Jan 2009 21:13:10 +0000 Subject: [PATCH] match: skip initial !! patterns if on first token. --- src/token.cpp | 7 +++++++ test/testtokenize.cpp | 12 ++++++++++++ 2 files changed, 19 insertions(+) diff --git a/src/token.cpp b/src/token.cpp index 7d9ddb2fe..e78352095 100644 --- a/src/token.cpp +++ b/src/token.cpp @@ -167,6 +167,7 @@ bool Token::simpleMatch(const Token *tok, const char pattern[]) bool Token::Match(const Token *tok, const char pattern[], unsigned int varid) { const char *p = pattern; + bool firstpattern = true; while (*p) { // Skip spaces in pattern.. @@ -198,6 +199,12 @@ bool Token::Match(const Token *tok, const char pattern[], unsigned int varid) return false; } + // If we are in the first token, we skip all initial !! patterns + if (firstpattern && !tok->previous() && tok->next() && str[1] == '!' && str[0] == '!' && str[2] != '\0') + continue; + + firstpattern = false; + // Compare the first character of the string for optimization reasons // before doing more detailed checks. bool patternIdentified = false; diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 4c33e6d96..512a51530 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -504,6 +504,18 @@ private: ASSERT_EQUALS(false, Token::Match(tokenizer.tokens(), "!!else something")); } + { + const std::string code("if ;"); + + // tokenize.. + Tokenizer tokenizer; + std::istringstream istr(code); + tokenizer.tokenize(istr, "test.cpp"); + + // Match.. + ASSERT_EQUALS(true, Token::Match(tokenizer.tokens(), "!!return if")); + } + { const std::string code("if ;");