summaryrefslogtreecommitdiffhomepage
path: root/lib.c
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2021-06-07 17:42:59 +0200
committerJo-Philipp Wich <jo@mein.io>2021-06-07 18:19:41 +0200
commit86fb1300a500f6f49f1c6bb4f68c111106c862a5 (patch)
tree8a40fb958ed9d86eb5d449e7099848fd4819bc7d /lib.c
parent3e893e68e33651067a77ff4ceeb8bb28020376dc (diff)
lib: implement min() and max() functions
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'lib.c')
-rw-r--r--lib.c35
1 files changed, 34 insertions, 1 deletions
diff --git a/lib.c b/lib.c
index 4d09ab0..c9ebcb2 100644
--- a/lib.c
+++ b/lib.c
@@ -2583,6 +2583,37 @@ uc_sourcepath(uc_vm *vm, size_t nargs)
return rv;
}
+static uc_value_t *
+uc_min_max(uc_vm *vm, size_t nargs, int cmp)
+{
+ uc_value_t *rv = NULL, *val;
+ bool set = false;
+ size_t i;
+
+ for (i = 0; i < nargs; i++) {
+ val = uc_get_arg(i);
+
+ if (!set || uc_cmp(cmp, val, rv)) {
+ set = true;
+ rv = val;
+ }
+ }
+
+ return ucv_get(rv);
+}
+
+static uc_value_t *
+uc_min(uc_vm *vm, size_t nargs)
+{
+ return uc_min_max(vm, nargs, TK_LT);
+}
+
+static uc_value_t *
+uc_max(uc_vm *vm, size_t nargs)
+{
+ return uc_min_max(vm, nargs, TK_GT);
+}
+
static const uc_cfunction_list functions[] = {
{ "chr", uc_chr },
{ "die", uc_die },
@@ -2636,7 +2667,9 @@ static const uc_cfunction_list functions[] = {
{ "render", uc_render },
{ "regexp", uc_regexp },
{ "wildcard", uc_wildcard },
- { "sourcepath", uc_sourcepath }
+ { "sourcepath", uc_sourcepath },
+ { "min", uc_min },
+ { "max", uc_max }
};