Modify Str8 substr functionality

This commit is contained in:
2025-02-22 13:39:03 +00:00
parent 99f7dcd794
commit 28ac9ed8c8
5 changed files with 107 additions and 45 deletions

View File

@@ -71,6 +71,32 @@ RETURN_ALLOC_STR8:
return output;
}
Str8 *wapp_str8_alloc_substr(const Allocator *allocator, Str8RO *str, u64 start, u64 end) {
Str8 *output = NULL;
if (!allocator || !str) {
goto RETURN_ALLOC_SUBSTR;
}
if (start >= str->size || start >= end) {
goto RETURN_ALLOC_SUBSTR;
}
if (end > str->size) {
end = str->size;
}
output = wapp_str8_alloc_buf(allocator, str->capacity);
if (!output) {
goto RETURN_ALLOC_SUBSTR;
}
output->size = end - start;
memcpy(output->buf, str->buf + start, output->size);
RETURN_ALLOC_SUBSTR:
return output;
}
c8 wapp_str8_get(const Str8 *str, u64 index) {
if (index >= str->size) {
return '\0';
@@ -103,7 +129,7 @@ bool wapp_str8_equal_to_count(Str8RO* s1, Str8RO* s2, u64 count) {
return memcmp(s1->buf, s2->buf, count) == 0;
}
Str8RO wapp_str8_substr(Str8RO *str, u64 start, u64 end) {
Str8 wapp_str8_slice(Str8RO *str, u64 start, u64 end) {
if (start >= str->size || start >= end) {
return (Str8RO){0};
}
@@ -268,8 +294,8 @@ Str8List *wapp_str8_split_with_max(const Allocator *allocator, Str8RO *str, Str8
break;
}
Str8RO before = wapp_str8_substr(str, start, start + end);
Str8RO after = wapp_str8_substr(str, start + end + delimiter->size, str->size);
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);
Str8Node *node = wapp_mem_allocator_alloc(allocator, sizeof(Str8Node));
@@ -285,7 +311,7 @@ Str8List *wapp_str8_split_with_max(const Allocator *allocator, Str8RO *str, Str8
++splits;
}
Str8RO last = wapp_str8_substr(str, start, str->size);
Str8RO last = wapp_str8_slice(str, start, str->size);
rest = wapp_str8_alloc_str8(allocator, &last);
Str8Node *node = wapp_mem_allocator_alloc(allocator, sizeof(Str8Node));
if (node) {
@@ -325,8 +351,8 @@ Str8List *wapp_str8_rsplit_with_max(const Allocator *allocator, Str8RO *str, Str
break;
}
Str8RO before = wapp_str8_substr(rest, 0, end);
Str8RO after = wapp_str8_substr(rest, end + delimiter->size, str->size);
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);
Str8Node *node = wapp_mem_allocator_alloc(allocator, sizeof(Str8Node));
@@ -341,7 +367,7 @@ Str8List *wapp_str8_rsplit_with_max(const Allocator *allocator, Str8RO *str, Str
++splits;
}
Str8RO last = wapp_str8_substr(str, 0, rest->size);
Str8RO last = wapp_str8_slice(str, 0, rest->size);
rest = wapp_str8_alloc_str8(allocator, &last);
Str8Node *node = wapp_mem_allocator_alloc(allocator, sizeof(Str8Node));
if (node) {

View File

@@ -60,6 +60,7 @@ struct str8_list {
Str8 *wapp_str8_alloc_buf(const Allocator *allocator, u64 capacity);
Str8 *wapp_str8_alloc_cstr(const Allocator *allocator, const char *str);
Str8 *wapp_str8_alloc_str8(const Allocator *allocator, Str8RO *str);
Str8 *wapp_str8_alloc_substr(const Allocator *allocator, Str8RO *str, u64 start, u64 end);
/**
* Str8 utilities
@@ -68,7 +69,7 @@ c8 wapp_str8_get(Str8RO *str, u64 index);
void wapp_str8_set(Str8 *str, u64 index, c8 c);
bool wapp_str8_equal(Str8RO *s1, Str8RO *s2);
bool wapp_str8_equal_to_count(Str8RO* s1, Str8RO* s2, u64 count);
Str8RO wapp_str8_substr(Str8RO *str, u64 start, u64 end);
Str8 wapp_str8_slice(Str8RO *str, u64 start, u64 end);
Str8 *wapp_str8_concat(const Allocator *allocator, Str8 *dst, Str8RO *src);
void wapp_str8_concat_capped(Str8 *dst, Str8RO *src);
void wapp_str8_copy_cstr_capped(Str8 *dst, const char *src);