}
/**
- * Like util_wildmatch, but allow to use a list of patterns,
+ * Compares given string against also given list of patterns.
+ *
+ * * Matches any sequence of characters.
+ * ? Matches any single character except of '/'.
+ * {foo,bar} Matches foo or bar - '{', ',' and '}' within this pattern must be
+ * escaped by '\'. '*' and '?' have no special meaning within the
+ * curly braces.
+ * *?{} these chars must always be escaped by '\' to match them literally
*/
-gboolean util_wildmatch_multi(const char *pattern, const char *subject)
+gboolean util_wildmatch(const char *pattern, const char *subject)
{
const char *end;
- int braces, patlen;
+ int braces, patlen, count;
/* loop through all pattens */
- for (; *pattern; pattern = (*end == ',' ? end + 1 : end)) {
+ for (count = 0; *pattern; pattern = (*end == ',' ? end + 1 : end), count++) {
/* find end of the pattern - but be careful with comma in curly braces */
braces = 0;
for (end = pattern; *end && (*end != ',' || braces || *(end - 1) == '\\'); ++end) {
}
}
- /* empty pattern matches only on empty subject */
- return !*subject;
-}
-
-/**
- * Compares given string against also given pattern.
- *
- * * Matches any sequence of characters.
- * ? Matches any single character except of '/'.
- * {foo,bar} Matches foo or bar - '{', ',' and '}' within this pattern must be
- * escaped by '\'. '*' and '?' have no special meaning within the
- * curly braces.
- * *?{} these chars must always be escaped by '\' to match them literally
- */
-gboolean util_wildmatch(const char *pattern, const char *subject)
-{
- return match(pattern, strlen(pattern), subject);
+ if (!count) {
+ /* empty pattern matches only on empty subject */
+ return !*subject;
+ }
+ /* there where one or more patterns but none of them matched */
+ return false;
}
/**
char *util_expand(const char *src, int expflags);
gboolean util_parse_expansion(const char **input, GString *str, int flags,
const char *quoteable);
-gboolean util_wildmatch_multi(const char *pattern, const char *subject);
-gboolean util_wildmatch(const char *pattern, const char *string);
+gboolean util_wildmatch(const char *pattern, const char *subject);
gboolean util_fill_completion(GtkListStore *store, const char *input, GList *src);
#endif /* end of include guard: _UTIL_H */
static void test_wildmatch_multi(void)
{
- /* check if sinlge pattern matching works */
- g_assert_true(util_wildmatch_multi("", ""));
- g_assert_true(util_wildmatch_multi("single", "single"));
- g_assert_true(util_wildmatch_multi("s*e", "single"));
-
- g_assert_true(util_wildmatch_multi("foo,b{a,o,}r,ba?", "foo"));
- g_assert_true(util_wildmatch_multi("foo,b{a,o,}r,ba?", "bar"));
- g_assert_true(util_wildmatch_multi("foo,b{a,o,}r,ba?", "bor"));
- g_assert_true(util_wildmatch_multi("foo,b{a,o,}r,ba?", "br"));
- g_assert_true(util_wildmatch_multi("foo,b{a,o,}r,ba?", "baz"));
- g_assert_true(util_wildmatch_multi("foo,b{a,o,}r,ba?", "bat"));
-
- g_assert_false(util_wildmatch_multi("foo,b{a,o,}r,ba?", "foo,"));
+ g_assert_true(util_wildmatch("foo,?", "foo"));
+ g_assert_true(util_wildmatch("foo,?", "f"));
+ g_assert_true(util_wildmatch("foo,b{a,o,}r,ba?", "foo"));
+ g_assert_true(util_wildmatch("foo,b{a,o,}r,ba?", "bar"));
+ g_assert_true(util_wildmatch("foo,b{a,o,}r,ba?", "bor"));
+ g_assert_true(util_wildmatch("foo,b{a,o,}r,ba?", "br"));
+ g_assert_true(util_wildmatch("foo,b{a,o,}r,ba?", "baz"));
+ g_assert_true(util_wildmatch("foo,b{a,o,}r,ba?", "bat"));
+
+ g_assert_false(util_wildmatch("foo,b{a,o,}r,ba?", "foo,"));
+ g_assert_false(util_wildmatch("foo,?", "fo"));
}
int main(int argc, char *argv[])