diff --git a/lib/token.cpp b/lib/token.cpp index f67706418..54ea93595 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -621,6 +621,15 @@ bool Token::Match(const Token *tok, const char pattern[], unsigned int varid) patternUnderstood = true; } break; + case 'c': + // Character (%char%) + { + if (tok->_type != eChar) + return false; + p += 6; + patternUnderstood = true; + } + break; case 's': // String (%str%) { diff --git a/lib/token.h b/lib/token.h index f60361d2f..a1b6268df 100644 --- a/lib/token.h +++ b/lib/token.h @@ -123,6 +123,7 @@ public: * - "%type%" Anything that can be a variable type, e.g. "int", but not "delete". * - "%num%" Any numeric token, e.g. "23" * - "%bool%" true or false + * - "%char%" Any token enclosed in '-character. * - "%str%" Any token starting with "-character (C-string). * - "%varid%" Match with parameter varid * - "%or%" A bitwise-or operator '|' diff --git a/test/testtoken.cpp b/test/testtoken.cpp index 1156b46b1..597bb965c 100644 --- a/test/testtoken.cpp +++ b/test/testtoken.cpp @@ -56,6 +56,7 @@ private: TEST_CASE(matchSingleChar); TEST_CASE(matchNothingOrAnyNotElse); TEST_CASE(matchType); + TEST_CASE(matchChar); TEST_CASE(matchStr); TEST_CASE(matchVarid); TEST_CASE(matchNumeric); @@ -276,6 +277,17 @@ private: ASSERT_EQUALS(false, Token::Match(noType.tokens(), "%type%")); } + void matchChar() { + givenACodeSampleToTokenize chr1("'a'", true); + ASSERT_EQUALS(true, Token::Match(chr1.tokens(), "%char%")); + + givenACodeSampleToTokenize chr2("'1'", true); + ASSERT_EQUALS(true, Token::Match(chr2.tokens(), "%char%")); + + givenACodeSampleToTokenize noChr("\"10\"", true); + ASSERT_EQUALS(false, Token::Match(noChr.tokens(), "%char%")); + } + void matchStr() { givenACodeSampleToTokenize noStr1("abc", true); ASSERT_EQUALS(false, Token::Match(noStr1.tokens(), "%str%"));