From ba59233ce729d9712c24bc3ff02e14ad64262cb7 Mon Sep 17 00:00:00 2001 From: koniifer Date: Thu, 7 Nov 2024 02:22:58 +0000 Subject: [PATCH] untested string.contains(haystack, needle) --- sysdata/libraries/stn/src/string.hb | 37 +++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/sysdata/libraries/stn/src/string.hb b/sysdata/libraries/stn/src/string.hb index 41a5535c..efea8702 100644 --- a/sysdata/libraries/stn/src/string.hb +++ b/sysdata/libraries/stn/src/string.hb @@ -81,4 +81,41 @@ equals := fn(lhs: ^u8, rhs: ^u8): bool { } else { i += 1 } +} + +contains := fn(haystack: ^u8, needle: ^u8): bool { + haystack_len := @inline(length, haystack) + needle_len := @inline(length, needle) + + if needle_len == 0 { + return true + } + if haystack_len < needle_len { + return false + } + + max_start := haystack_len - needle_len + + pos := 0 + loop if pos > max_start break else { + is_match := true + offset := 0 + + loop if offset >= needle_len break else { + if *(haystack + pos + offset) != *(needle + offset) { + is_match = false + } + if is_match == false { + break + } + offset += 1 + } + + if is_match { + return true + } + pos += 1 + } + + return false } \ No newline at end of file