common: use fnmatch() for pattern matching

Drop-in POSIX-compliant function that has a nice glob(7) manual page for
reference.
This commit is contained in:
Consus 2023-10-01 14:39:47 +03:00 committed by Johan Malm
parent e864419194
commit 3e5b988d38
2 changed files with 6 additions and 11 deletions

View file

@ -1,7 +1,7 @@
/* SPDX-License-Identifier: GPL-2.0-only */ /* SPDX-License-Identifier: GPL-2.0-only */
#ifndef LABWC_MATCH_H #ifndef LABWC_MATCH_H
#define LABWC_MATCH_H #define LABWC_MATCH_H
#include <glib.h>
#include <stdbool.h> #include <stdbool.h>
/** /**
@ -10,6 +10,6 @@
* @string: String to search. * @string: String to search.
* Note: Comparison case-insensitive. * Note: Comparison case-insensitive.
*/ */
bool match_glob(const gchar *pattern, const gchar *string); bool match_glob(const char *pattern, const char *string);
#endif /* LABWC_MATCH_H */ #endif /* LABWC_MATCH_H */

View file

@ -1,15 +1,10 @@
// SPDX-License-Identifier: GPL-2.0-only // SPDX-License-Identifier: GPL-2.0-only
#define _POSIX_C_SOURCE 200809L
#include <fnmatch.h>
#include "common/match.h" #include "common/match.h"
bool bool
match_glob(const gchar *pattern, const gchar *string) match_glob(const char *pattern, const char *string)
{ {
gchar *p = g_utf8_casefold(pattern, -1); return fnmatch(pattern, string, FNM_CASEFOLD) == 0;
gchar *s = g_utf8_casefold(string, -1);
bool ret = g_pattern_match_simple(p, s);
g_free(p);
g_free(s);
return ret;
} }