Add str_join

This commit is contained in:
2026-07-31 00:27:04 +02:00
parent a94640e71d
commit 7f2713eeff
4 changed files with 93 additions and 8 deletions
+45 -4
View File
@@ -32,9 +32,7 @@ Str str_concat(Allocator *alloc, Str first, Str second)
Str str_push(Allocator *alloc, Str str)
{
U8 *mem = (U8 *)allocator_push(alloc, str.len, 1);
if (!str_is_empty(str)) {
MemCopy(mem, str.str, str.len);
}
MemCopy(mem, str.str, str.len);
return str_create(mem, str.len);
}
@@ -80,8 +78,21 @@ Str str_ascii_trim(Str str)
return new_str;
}
S64 str_substr(Str haystack, Str to_find)
S64 str_substr(Str haystack, Str needle)
{
if (needle.len == 0) {
return 0;
}
if (needle.len == 0) {
return str_find_byte(haystack, needle.str[0]);
}
U8 *found = MemFind(haystack.str, haystack.len, needle.str, needle.len);
if (found != NULL) {
return (S64)(found - haystack.str);
}
return -1;
}
@@ -112,3 +123,33 @@ void str_split(Str str, Str separators, StrVec *vec)
vec->len++;
}
}
Str str_join(Allocator *allocator, StrVec strings, Str separator)
{
U32 str_len = 0;
for (U32 i = 0; i < strings.len; i++) {
str_len += strings.strings[i].len;
}
if (strings.len > 0) {
str_len += separator.len * (strings.len - 1);
}
U8 *joined_str = allocator_push(allocator, str_len, 1);
if (strings.len > 0) {
U32 pos = 0;
Str *str = &strings.strings[0];
MemCopy(&joined_str[pos], str->str, str->len);
pos += str->len;
for (U32 i = 1; i < strings.len; i++) {
str = &strings.strings[i];
MemCopy(&joined_str[pos], separator.str, separator.len);
pos += separator.len;
MemCopy(&joined_str[pos], str->str, str->len);
pos += str->len;
}
}
return str_create(joined_str, str_len);
}