Add find and rfine functions

This commit is contained in:
Abdelrahman Said 2025-02-09 16:35:28 +00:00
parent f8e0804dd2
commit 093d0daf6f
2 changed files with 34 additions and 0 deletions

View File

@ -1,5 +1,6 @@
#include "str8.h"
#include "aliases.h"
#include <string.h>
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;
}

View File

@ -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