diff --git a/src/core/strings/str8/str8.c b/src/core/strings/str8/str8.c index b204e36..8340228 100644 --- a/src/core/strings/str8/str8.c +++ b/src/core/strings/str8/str8.c @@ -1,5 +1,6 @@ #include "str8.h" #include "aliases.h" +#include c8 wapp_str8_get(const Str8 *str, u64 index) { if (index >= str->size) { @@ -16,3 +17,33 @@ void wapp_str8_set(Str8 *str, u64 index, c8 c) { str->buf[index] = c; } + +i64 wapp_str8_find(Str8RO *str, Str8RO substr) { + if (substr.size > str->size) { + return -1; + } + + for (u64 i = 0; i < str->size; ++i) { + const c8 *sub = str->buf + i; + if (memcmp(sub, substr.buf, substr.size) == 0) { + return i; + } + } + + return -1; +} + +i64 wapp_str8_rfind(Str8RO *str, Str8RO substr) { + if (substr.size > str->size) { + return -1; + } + + for (i64 i = str->size - substr.size; i >= 0; --i) { + const c8 *sub = str->buf + i; + if (memcmp(sub, substr.buf, substr.size) == 0) { + return i; + } + } + + return -1; +} diff --git a/src/core/strings/str8/str8.h b/src/core/strings/str8/str8.h index 8557db0..7e6f98f 100644 --- a/src/core/strings/str8/str8.h +++ b/src/core/strings/str8/str8.h @@ -35,6 +35,9 @@ typedef const Str8 Str8RO; c8 wapp_str8_get(Str8RO *str, u64 index); void wapp_str8_set(Str8 *str, u64 index, c8 c); +i64 wapp_str8_find(Str8RO *str, Str8RO substr); +i64 wapp_str8_rfind(Str8RO *str, Str8RO substr); + #ifdef __cplusplus END_C_LINKAGE #endif // !__cplusplus