Fix string_split

This commit is contained in:
luboslenco
2025-09-07 21:17:02 +02:00
parent 1c8e8b7bee
commit 5b87b03a25
+41 -49
View File
@@ -95,7 +95,7 @@ void string_strip_trailing_zeros(char *str) {
int32_t string_index_of_pos(char *s, char *search, int pos) { int32_t string_index_of_pos(char *s, char *search, int pos) {
char *found = strstr(s + pos, search); char *found = strstr(s + pos, search);
if (found != NULL) { if (found != NULL) {
return found - s; return found - s;
} }
return -1; return -1;
} }
@@ -107,45 +107,37 @@ int32_t string_index_of(char *s, char *search) {
int32_t string_last_index_of(char *str, char *search) { int32_t string_last_index_of(char *str, char *search) {
char *s = str; char *s = str;
char *found = NULL; char *found = NULL;
while (1) { while (1) {
char *p = strstr(s, search); char *p = strstr(s, search);
if (p == NULL) { if (p == NULL) {
break; break;
} }
found = p; found = p;
s = p + 1; s = p + 1;
}
if (found != NULL) {
return found - str;
} }
return -1; if (found != NULL) {
return found - str;
}
return -1;
} }
any_array_t *string_split(char *s, char *sep) { any_array_t *string_split(char *s, char *sep) {
char *r = string_alloc(strlen(s) + 1); // Modified by strtok
any_array_t *a = gc_alloc(sizeof(any_array_t)); any_array_t *a = gc_alloc(sizeof(any_array_t));
int sep_len = strlen(sep);
strcpy(r, s); char *pos = s;
int count = 0; while (true) {
char *token = strtok(r, sep); char *next = strstr(pos, sep);
while (token != NULL) { if (next == NULL) {
count++; any_array_push(a, string_copy(pos));
token = strtok(NULL, sep); break;
}
int part_len = next - pos;
char *part = string_alloc(part_len + 1);
strncpy(part, pos, part_len);
part[part_len] = '\0';
any_array_push(a, part);
pos = next + sep_len;
} }
any_array_resize(a, count);
strcpy(r, s);
token = strtok(r, sep);
while (token != NULL) {
any_array_push(a, string_copy(token)); // Make a copy to keep gc ref
// TODO: this works only for single char sep
token = strtok(NULL, sep);
}
if (a->length == 0) { // Separator not found
any_array_push(a, r);
}
return a; return a;
} }
@@ -171,22 +163,22 @@ char *string_array_join(any_array_t *a, char *separator) {
char *string_replace_all(char *s, char *search, char *replace) { char *string_replace_all(char *s, char *search, char *replace) {
char *buffer = string_alloc(1024); char *buffer = string_alloc(1024);
char *buffer_pos = buffer; char *buffer_pos = buffer;
size_t search_len = strlen(search); size_t search_len = strlen(search);
size_t replace_len = strlen(replace); size_t replace_len = strlen(replace);
while (1) { while (1) {
char *p = strstr(s, search); char *p = strstr(s, search);
if (p == NULL) { if (p == NULL) {
strcpy(buffer_pos, s); strcpy(buffer_pos, s);
break; break;
} }
memcpy(buffer_pos, s, p - s); memcpy(buffer_pos, s, p - s);
buffer_pos += p - s; buffer_pos += p - s;
memcpy(buffer_pos, replace, replace_len); memcpy(buffer_pos, replace, replace_len);
buffer_pos += replace_len; buffer_pos += replace_len;
s = p + search_len; s = p + search_len;
} }
return buffer; return buffer;
} }
char *substring(char *s, int32_t start, int32_t end) { char *substring(char *s, int32_t start, int32_t end) {