diff options
author | Jo-Philipp Wich <jo@mein.io> | 2021-05-17 19:57:58 +0200 |
---|---|---|
committer | Jo-Philipp Wich <jo@mein.io> | 2021-05-18 13:11:33 +0200 |
commit | 5803d8605b84ef362cc7f96f9e523eff5d0d81bc (patch) | |
tree | d56d07c53dc1c549ea8b04ce0c3f9517ed5b6300 /lib.c | |
parent | 29591422d602ec6b5a3808c3dead91cfbcdbbcf0 (diff) |
lib: implement wildcard() function
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'lib.c')
-rw-r--r-- | lib.c | 29 |
1 files changed, 28 insertions, 1 deletions
@@ -30,6 +30,7 @@ #include <sys/stat.h> #include <sys/types.h> #include <sys/wait.h> +#include <fnmatch.h> #include "lexer.h" #include "compiler.h" @@ -2525,6 +2526,31 @@ uc_regexp(uc_vm *vm, size_t nargs) return regex; } +static uc_value_t * +uc_wildcard(uc_vm *vm, size_t nargs) +{ + uc_value_t *subject = uc_get_arg(0); + uc_value_t *pattern = uc_get_arg(1); + uc_value_t *icase = uc_get_arg(2); + int flags = 0, rv; + bool freeable; + char *s; + + if (!subject || ucv_type(pattern) != UC_STRING) + return NULL; + + if (uc_val_is_truish(icase)) + flags |= FNM_CASEFOLD; + + s = uc_cast_string(vm, &subject, &freeable); + rv = fnmatch(ucv_string_get(pattern), s, flags); + + if (freeable) + free(s); + + return ucv_boolean_new(rv == 0); +} + static const uc_cfunction_list functions[] = { { "chr", uc_chr }, { "delete", uc_delete }, @@ -2577,7 +2603,8 @@ static const uc_cfunction_list functions[] = { { "sleep", uc_sleep }, { "assert", uc_assert }, { "render", uc_render }, - { "regexp", uc_regexp } + { "regexp", uc_regexp }, + { "wildcard", uc_wildcard } }; |