From 4b59682077deaa76fe039c856c29a898c332bc2d Mon Sep 17 00:00:00 2001 From: jules Date: Fri, 30 Sep 2011 09:44:18 +0100 Subject: [PATCH] Wildcard matching fix for strings containing "*". --- modules/juce_core/text/juce_String.cpp | 41 +++++++++++++++----------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/modules/juce_core/text/juce_String.cpp b/modules/juce_core/text/juce_String.cpp index 605d638cc7..93b968941f 100644 --- a/modules/juce_core/text/juce_String.cpp +++ b/modules/juce_core/text/juce_String.cpp @@ -991,34 +991,30 @@ struct WildCardMatcher for (;;) { const juce_wchar wc = wildcard.getAndAdvance(); - const juce_wchar tc = *test; - if (wc == tc - || (ignoreCase && CharacterFunctions::toLowerCase (wc) == CharacterFunctions::toLowerCase (tc)) - || (wc == '?' && tc != 0)) - { - if (wc == 0) - return true; + if (wc == '*') + return wildcard.isEmpty() || matchesAnywhere (wildcard, test, ignoreCase); - ++test; - } - else - { - return wc == '*' && (wildcard.isEmpty() || matchesAnywhere (wildcard, test, ignoreCase)); - } + if (! characterMatches (wc, test.getAndAdvance(), ignoreCase)) + return false; + + if (wc == 0) + return true; } } + static bool characterMatches (const juce_wchar wc, const juce_wchar tc, const bool ignoreCase) noexcept + { + return (wc == tc) || (wc == '?' && tc != 0) + || (ignoreCase && CharacterFunctions::toLowerCase (wc) == CharacterFunctions::toLowerCase (tc)); + } + static bool matchesAnywhere (const CharPointer& wildcard, CharPointer test, const bool ignoreCase) noexcept { - while (! test.isEmpty()) - { + for (; ! test.isEmpty(); ++test) if (matches (wildcard, test, ignoreCase)) return true; - ++test; - } - return false; } }; @@ -2305,6 +2301,15 @@ public: expect (s5.matchesWildcard (L"*word3", true)); expect (s5.matchesWildcard ("*word?", true)); expect (s5.matchesWildcard (L"Word*3", true)); + expect (! s5.matchesWildcard (L"*34", true)); + expect (String ("xx**y").matchesWildcard ("*y", true)); + expect (String ("xx**y").matchesWildcard ("x*y", true)); + expect (String ("xx**y").matchesWildcard ("xx*y", true)); + expect (String ("xx**y").matchesWildcard ("xx*", true)); + expect (String ("xx?y").matchesWildcard ("x??y", true)); + expect (String ("xx?y").matchesWildcard ("xx?y", true)); + expect (! String ("xx?y").matchesWildcard ("xx?y?", true)); + expect (String ("xx?y").matchesWildcard ("xx??", true)); expectEquals (s5.fromFirstOccurrenceOf (String::empty, true, false), s5); expectEquals (s5.fromFirstOccurrenceOf ("xword2", true, false), s5.substring (100));