diff --git a/src/core/strings/str8/str8.c b/src/core/strings/str8/str8.c index 6abb626..c5086ef 100644 --- a/src/core/strings/str8/str8.c +++ b/src/core/strings/str8/str8.c @@ -147,7 +147,8 @@ bool wapp_str8_equal_to_count(Str8RO* s1, Str8RO* s2, u64 count) { Str8 wapp_str8_slice(Str8RO *str, u64 start, u64 end) { if (start >= str->size || start >= end) { - return (Str8RO){0}; + start = str->size; + end = str->size; } if (end > str->size) { @@ -258,7 +259,7 @@ void wapp_str8_format(Str8 *dst, const char *format, ...) { } i64 wapp_str8_find(Str8RO *str, Str8RO substr) { - if (substr.size > str->size) { + if (!str || substr.size > str->size) { return -1; } @@ -280,7 +281,7 @@ i64 wapp_str8_find(Str8RO *str, Str8RO substr) { } i64 wapp_str8_rfind(Str8RO *str, Str8RO substr) { - if (substr.size > str->size) { + if (!str || substr.size > str->size) { return -1; } @@ -330,10 +331,7 @@ Str8List *wapp_str8_split_with_max(const Allocator *allocator, Str8RO *str, Str8 break; } - Str8RO before = wapp_str8_slice(str, start, start + end); - Str8RO after = wapp_str8_slice(str, start + end + delimiter->size, str->size); - - before_str = wapp_str8_alloc_str8(allocator, &before); + before_str = wapp_str8_alloc_substr(allocator, str, start, start + end); Str8Node *node = wapp_mem_allocator_alloc(allocator, sizeof(Str8Node)); if (node) { node->string = before_str; @@ -341,14 +339,14 @@ Str8List *wapp_str8_split_with_max(const Allocator *allocator, Str8RO *str, Str8 } wapp_mem_allocator_free(allocator, (void **)&rest, sizeof(Str8)); - rest = wapp_str8_alloc_str8(allocator, &after); + rest = wapp_str8_alloc_substr(allocator, str, start + end + delimiter->size, str->size); start += end + delimiter->size; ++splits; } - Str8RO last = wapp_str8_slice(str, start, str->size); - rest = wapp_str8_alloc_str8(allocator, &last); + // Ensure the last part of the string after the delimiter is added to the list + rest = wapp_str8_alloc_substr(allocator, str, start, str->size); Str8Node *node = wapp_mem_allocator_alloc(allocator, sizeof(Str8Node)); if (node) { node->string = rest; @@ -387,10 +385,7 @@ Str8List *wapp_str8_rsplit_with_max(const Allocator *allocator, Str8RO *str, Str break; } - Str8RO before = wapp_str8_slice(rest, 0, end); - Str8RO after = wapp_str8_slice(rest, end + delimiter->size, str->size); - - after_str = wapp_str8_alloc_str8(allocator, &after); + after_str = wapp_str8_alloc_substr(allocator, rest, end + delimiter->size, str->size); Str8Node *node = wapp_mem_allocator_alloc(allocator, sizeof(Str8Node)); if (node) { node->string = after_str; @@ -398,13 +393,12 @@ Str8List *wapp_str8_rsplit_with_max(const Allocator *allocator, Str8RO *str, Str } wapp_mem_allocator_free(allocator, (void **)&rest, sizeof(Str8)); - rest = wapp_str8_alloc_str8(allocator, &before); + rest = wapp_str8_alloc_substr(allocator, rest, 0, end); ++splits; } - Str8RO last = wapp_str8_slice(str, 0, rest->size); - rest = wapp_str8_alloc_str8(allocator, &last); + rest = wapp_str8_alloc_substr(allocator, str, 0, rest->size); Str8Node *node = wapp_mem_allocator_alloc(allocator, sizeof(Str8Node)); if (node) { node->string = rest;