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