summaryrefslogtreecommitdiff
path: root/lib.c
diff options
context:
space:
mode:
authorubq323 <ubq323@ubq323.website>2024-08-17 23:13:35 +0100
committerubq323 <ubq323@ubq323.website>2024-08-17 23:13:35 +0100
commite7cc8217d5f2ba69b8baca97ea9b6bf6fd028c8a (patch)
tree91f5d89ecc31c935b36cc27305d06be48a887ac0 /lib.c
parent91d59f1ba812c7df783e459c8af8d1c1be1d27f4 (diff)
add ssub? function
Diffstat (limited to 'lib.c')
-rw-r--r--lib.c15
1 files changed, 15 insertions, 0 deletions
diff --git a/lib.c b/lib.c
index ba03c75..01f5110 100644
--- a/lib.c
+++ b/lib.c
@@ -123,6 +123,19 @@ static Val fn_spendsplat(State *S, int nargs, Val *args) {
ObjArr *a = AS_ARR(args[0]);
return fn_spend(S, a->len, a->d);
}
+static Val fn_ssub(State *S, int nargs, Val *args) {
+ CHECK(nargs == 2, "need exactly 2 args for ssub?");
+ CHECK(IS_STRING(args[0]) && IS_STRING(args[1]), "need 2 strings for ssub?");
+ ObjString *needle = AS_STRING(args[0]);
+ ObjString *haystack = AS_STRING(args[1]);
+ int maxoff = haystack->len - needle->len;
+ for (int i = 0; i <= maxoff; i++)
+ if (0 == memcmp(needle->d, haystack->d + i, needle->len))
+ return VAL_TRUE;
+ return VAL_FALSE;
+}
+
+
static Val fn_ssplit(State *S, int nargs, Val *args) {
CHECK(nargs==2,"need exactly 2 args for ssplit");
CHECK(IS_STRING(args[0]) && IS_STRING(args[1]), "need two strings for ssplit");
@@ -202,8 +215,10 @@ static BuiltinFunc builtin_funcs[] = {
{ "delete!", fn_delete },
{ "#", fn_len },
{ ",", fn_pend },
+
{ "s,", fn_spend },
{ "s,@", fn_spendsplat },
+ { "ssub?", fn_ssub },
{ "ssplit", fn_ssplit },
{ "schars-ascii", fn_schars_ascii },