untested string.contains(haystack, needle)

This commit is contained in:
koniifer 2024-11-07 02:22:58 +00:00
parent f13c682171
commit ba59233ce7

View file

@ -82,3 +82,40 @@ equals := fn(lhs: ^u8, rhs: ^u8): bool {
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
}