commit dde8a9369d5f76e2dfeb0828c2e732993c21ccf6
parent 16ebac4e16de5a393f4d35061f24a7e248801dc9
Author: Ed van Bruggen <edvb@uw.edu>
Date: Thu, 26 Dec 2019 23:38:02 -0800
Add strlen function
Diffstat:
1 file changed, 15 insertions(+), 1 deletion(-)
diff --git a/tibs/string.c b/tibs/string.c
@@ -26,7 +26,7 @@
typedef Val (*MkFn)(Tsp, char*);
-/* TODO string tib: strlen lower upper strpos strsub */
+/* TODO string tib: lower upper strpos strsub */
/* TODO simplify by using fmemopen and tisp_print */
static Val
@@ -106,9 +106,23 @@ prim_symbol(Tsp st, Hash env, Val args)
return val_string(st, v, mk_sym);
}
+static Val
+prim_strlen(Tsp st, Hash env, Val args)
+{
+ Val v;
+ tsp_arg_num(args, "symbol", 1);
+ if (!(v = tisp_eval(st, env, car(args))))
+ return NULL;
+ if (!(v->t & (STRING|SYMBOL)))
+ tsp_warnf("strlen: expected string or symbol, received %s",
+ type_str(v->t));
+ return mk_int(strlen(v->v.s));
+}
+
void
tib_env_string(Tsp st)
{
tsp_env_fn(symbol);
tsp_env_fn(string);
+ tsp_env_fn(strlen);
}