Bugfixes
This commit is contained in:
+5
-5
@@ -433,7 +433,7 @@ void string_split_test_1(void)
|
||||
{
|
||||
TRACEF("\n");
|
||||
|
||||
Str str1 = S("a:bb|ccc");
|
||||
Str str1 = S("|a:bb|ccc|");
|
||||
Str sep = S("|:");
|
||||
STRVEC_DEFINE(strings, 16);
|
||||
|
||||
@@ -470,13 +470,13 @@ void string_test_misc(void)
|
||||
Str str4_trim = str_ascii_trim(str4);
|
||||
printf("str4_trim: '%.*s'\n", str4_trim.len, str4_trim.str);
|
||||
|
||||
S64 found = str_substr(str1, S("World"));
|
||||
S64 found = str_index(str1, S("World"));
|
||||
printf("1. found %ld\n", found);
|
||||
found = str_substr(str_empty(), S("World"));
|
||||
found = str_index(str_empty(), S("World"));
|
||||
printf("2. found %ld\n", found);
|
||||
found = str_substr(str_empty(), S(""));
|
||||
found = str_index(str_empty(), S(""));
|
||||
printf("3. found %ld\n", found);
|
||||
found = str_substr(str1, S(" "));
|
||||
found = str_index(str1, S(" "));
|
||||
printf("4. found %ld\n", found);
|
||||
|
||||
}
|
||||
|
||||
+21
-11
@@ -9,8 +9,8 @@ Str str_empty(void)
|
||||
|
||||
Str str_slice(Str str, U32 start, U32 len)
|
||||
{
|
||||
Assert(start < str.len);
|
||||
Assert(len <= str.len);
|
||||
Assert(start <= str.len);
|
||||
Assert(len <= str.len - start);
|
||||
|
||||
Str new_str = str_create(&str.str[start], len);
|
||||
return new_str;
|
||||
@@ -78,15 +78,18 @@ Str str_ascii_trim(Str str)
|
||||
return new_str;
|
||||
}
|
||||
|
||||
S64 str_substr(Str haystack, Str needle)
|
||||
S64 str_index(Str haystack, Str needle)
|
||||
{
|
||||
if (needle.len == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (needle.len == 0) {
|
||||
if (needle.len == 1) {
|
||||
return str_find_byte(haystack, needle.str[0]);
|
||||
}
|
||||
if (needle.len > haystack.len) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
U8 *found = MemFind(haystack.str, haystack.len, needle.str, needle.len);
|
||||
if (found != NULL) {
|
||||
@@ -117,7 +120,7 @@ void str_split(Str str, Str separators, StrVec *vec)
|
||||
start_pos = i + 1;
|
||||
}
|
||||
}
|
||||
if (start_pos < str.len && vec->len < vec->max_len) {
|
||||
if (start_pos <= str.len && vec->len < vec->max_len) {
|
||||
Str sub_str = str_slice(str, start_pos, str.len - start_pos);
|
||||
vec->strings[vec->len] = sub_str;
|
||||
vec->len++;
|
||||
@@ -127,21 +130,28 @@ void str_split(Str str, Str separators, StrVec *vec)
|
||||
Str str_join(Allocator *allocator, StrVec strings, Str separator)
|
||||
{
|
||||
|
||||
U32 str_len = 0;
|
||||
U32 joined_str_len = 0;
|
||||
|
||||
for (U32 i = 0; i < strings.len; i++) {
|
||||
str_len += strings.strings[i].len;
|
||||
U32 part_len = strings.strings[i].len;
|
||||
Assert(joined_str_len <= U32_Max - part_len); // overflow
|
||||
joined_str_len += part_len;
|
||||
}
|
||||
if (strings.len > 0) {
|
||||
str_len += separator.len * (strings.len - 1);
|
||||
if (strings.len > 1) {
|
||||
U32 separator_count = strings.len - 1;
|
||||
Assert(separator.len <= U32_Max / separator_count); // overflow
|
||||
Assert(joined_str_len <= U32_Max - separator.len * separator_count); // overflow
|
||||
joined_str_len += separator.len * separator_count;
|
||||
}
|
||||
|
||||
U8 *joined_str = allocator_push(allocator, str_len, 1);
|
||||
U8 *joined_str = allocator_push(allocator, joined_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);
|
||||
@@ -151,5 +161,5 @@ Str str_join(Allocator *allocator, StrVec strings, Str separator)
|
||||
}
|
||||
}
|
||||
|
||||
return str_create(joined_str, str_len);
|
||||
return str_create(joined_str, joined_str_len);
|
||||
}
|
||||
|
||||
+1
-1
@@ -9,7 +9,7 @@ struct Str {
|
||||
|
||||
Str str_empty(void);
|
||||
Str str_slice(Str str, U32 start, U32 len);
|
||||
S64 str_substr(Str haystack, Str needle);
|
||||
S64 str_index(Str haystack, Str needle);
|
||||
|
||||
Str str_concat(Allocator *alloc, Str first, Str second);
|
||||
Str str_push(Allocator *alloc, Str str);
|
||||
|
||||
Reference in New Issue
Block a user