static int getPaths(lua_State * L) { TRY PathPtr path = getInstance(L); std::string matchesPattern(luaL_checkstring(L, 2)); PathListPtr paths = path->GetPaths(matchesPattern); pushPathListPtrOntoStack(L, paths); return 1; CATCH }
static bool matchesPattern(const char *string, int stringPos, const char *pattern, int patternPos) { matchesPattern_START: // match all non-wildcard characters, as far as possible while ((pattern[patternPos] != '?') && (pattern[patternPos] != '*') && (pattern[patternPos] != 0)) { if (string[stringPos] != pattern[patternPos]) return false; stringPos++; patternPos++; } // if string is over, either return true or false, depending on whether // the pattern equals "*" or not if (string[stringPos] == 0) { while (pattern[patternPos] != 0) { if (pattern[patternPos] != '*') return false; patternPos++; } return true; } // if we still have string, but no more pattern, return false if (pattern[patternPos] == 0) return false; // if current pattern character is '?', match that against the current character // of the string and go back to START if (pattern[patternPos] == '?') { patternPos++; stringPos++; goto matchesPattern_START; } // if we get here, this means the current pattern character is '*' assert(pattern[patternPos] == '*'); // contract consecutive '*' instances while (pattern[patternPos + 1] == '*') patternPos++; patternPos++; if (pattern[patternPos] == 0) return true; // try all possibilities of matching this '*' with the remainder of "string" while (string[stringPos] != 0) { if (matchesPattern(string, stringPos, pattern, patternPos)) return true; stringPos++; } return false; } // end of matchesPattern(char*, int, char*, int)
bool matchesPattern(const char *string, const char *pattern) { // refuse to process anything with more than 3 "*" in it int cnt = 0; for (int i = 0; pattern[i] != 0; i++) if (pattern[i] == '*') if (pattern[i + 1] != '*') cnt++; if (pow(strlen(string), cnt) > 100000) return false; else return matchesPattern(string, 0, pattern, 0); } // end of matchesPattern(char*, char*)
bool KFileItemModelFilter::matches(const KFileItem& item) const { const bool hasPatternFilter = !m_pattern.isEmpty(); const bool hasMimeTypesFilter = !m_mimeTypes.isEmpty(); // If no filter is set, return true. if (!hasPatternFilter && !hasMimeTypesFilter) { return true; } // If both filters are set, return true when both filters are matched if (hasPatternFilter && hasMimeTypesFilter) { return (matchesPattern(item) && matchesType(item)); } // If only one filter is set, return true when that filter is matched if (hasPatternFilter) { return matchesPattern(item); } return matchesType(item); }
TEST(StringUtilsTest, matchesPattern) { ASSERT_TRUE(matchesPattern("", "")); ASSERT_TRUE(matchesPattern("", "*")); ASSERT_FALSE(matchesPattern("", "?")); ASSERT_TRUE(matchesPattern("asdf", "asdf")); ASSERT_TRUE(matchesPattern("asdf", "*")); ASSERT_TRUE(matchesPattern("asdf", "a??f")); ASSERT_FALSE(matchesPattern("asdf", "a?f")); ASSERT_TRUE(matchesPattern("asdf", "*f")); ASSERT_TRUE(matchesPattern("asdf", "a*f")); ASSERT_TRUE(matchesPattern("asdf", "?s?f")); ASSERT_TRUE(matchesPattern("asdfjkl", "a*f*l")); ASSERT_TRUE(matchesPattern("asdfjkl", "*a*f*l*")); ASSERT_TRUE(matchesPattern("asd*fjkl", "*a*f*l*")); ASSERT_TRUE(matchesPattern("asd*fjkl", "asd\\*fjkl")); ASSERT_TRUE(matchesPattern("asd*?fj\\kl", "asd\\*\\?fj\\\\kl")); }