From e7cc8217d5f2ba69b8baca97ea9b6bf6fd028c8a Mon Sep 17 00:00:00 2001 From: ubq323 Date: Sat, 17 Aug 2024 23:13:35 +0100 Subject: add ssub? function --- lib.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'lib.c') 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 }, -- cgit v1.2.3