summaryrefslogtreecommitdiff
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
parent91d59f1ba812c7df783e459c8af8d1c1be1d27f4 (diff)
add ssub? function
-rw-r--r--lib.c15
-rw-r--r--tests/ssub.bth8
-rw-r--r--tests/ssub.out7
3 files changed, 30 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 },
diff --git a/tests/ssub.bth b/tests/ssub.bth
new file mode 100644
index 0000000..4c69a28
--- /dev/null
+++ b/tests/ssub.bth
@@ -0,0 +1,8 @@
+(defn (f n h) (say (ssub? n h)))
+(f 'ee 'needle)
+(f 'l 'flucloxacillin)
+(f 'l 'fucoxaciin)
+(f "really really long thethethethe" "h")
+(f 'start "start the at")
+(f 'end "at the end")
+(f 'entire 'entire)
diff --git a/tests/ssub.out b/tests/ssub.out
new file mode 100644
index 0000000..1d000fa
--- /dev/null
+++ b/tests/ssub.out
@@ -0,0 +1,7 @@
+true
+true
+false
+false
+true
+true
+true