diff --git a/src/token.cpp b/src/token.cpp index 536cfeb41..9a5b57c79 100644 --- a/src/token.cpp +++ b/src/token.cpp @@ -111,50 +111,68 @@ const char *Token::strAt(int index) const return tok ? tok->_cstr : ""; } -int Token::multiCompare(const char *needle, const char *haystack) +int Token::multiCompare(const char *haystack, const char *needle) { bool emptyStringFound = false; - bool findNextOr = false; - const char *haystackPointer = haystack; - for (; *needle; ++needle) + bool noMatch = false; + const char *needlePointer = needle; + for (; *haystack; ++haystack) { - if (*needle == '|') + if (*haystack == '|') { - // If needle and haystack are both at the end, we have a match. - if (*haystackPointer == 0) + if (noMatch) + { + // We didn't have a match at this round + noMatch = false; + } + else if (*needlePointer == 0) + { + // If needle and haystack are both at the end, we have a match. return 1; - - haystackPointer = haystack; - if (findNextOr) - findNextOr = false; - else + } + else if (needlePointer == needle) + { + // If needlePointer was not increased at all, we had a empty + // string in the haystack emptyStringFound = true; + } + needlePointer = needle; continue; } - if (findNextOr) + if (noMatch) continue; - // If haystack and needle don't share the same character, reset - // haystackpointer and find next '|' character. - if (*haystackPointer != *needle) + // If haystack and needle don't share the same character, + // find next '|' character. + if (*needlePointer != *haystack) { - haystackPointer = haystack; - findNextOr = true; + noMatch = true; continue; } // All characters in haystack and needle have matched this far - ++haystackPointer; + ++needlePointer; } - // If both needle and haystack are at the end, then we have a match. - if (*haystackPointer == 0) - return 1; - // If empty string was found or if last character in needle was '|' - if (emptyStringFound || findNextOr == false) + if (!noMatch) + { + if (*needlePointer == 0) + { + // If both needle and haystack are at the end, then we have a match. + return 1; + } + else if (needlePointer == needle) + { + // Last string in haystack was empty string e.g. "one|two|" + return 0; + } + } + + // If empty string was found earlier from the haystack + if (emptyStringFound) return 0; return -1; diff --git a/src/token.h b/src/token.h index 3b0f8048a..4fb65ab2e 100644 --- a/src/token.h +++ b/src/token.h @@ -128,13 +128,13 @@ public: * string, return value is 0. If needle was not found, return * value is -1. * - * @param needle e.g. "one|two" or "|one|two" - * @param haystack e.g. "one", "two" or "invalid" + * @param haystack e.g. "one|two" or "|one|two" + * @param needle e.g. "one", "two" or "invalid" * @return 1 if needle is found from the haystack * 0 if needle was empty string * -1 if needle was not found */ - static int multiCompare(const char *needle, const char *haystack); + static int multiCompare(const char *haystack, const char *needle); unsigned int linenr() const; diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 849f04566..153adc4a6 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -464,6 +464,8 @@ private: ASSERT_EQUALS(-1, Token::multiCompare("verybig|two", "s")); ASSERT_EQUALS(-1, Token::multiCompare("one|two", "ne")); ASSERT_EQUALS(-1, Token::multiCompare("abc|def", "a")); + ASSERT_EQUALS(-1, Token::multiCompare("abc|def", "abcd")); + ASSERT_EQUALS(-1, Token::multiCompare("abc|def", "default")); } void match1()