30 lines
982 B
C
30 lines
982 B
C
|
|
#include "include/array.h"
|
||
|
|
#include "include/mongoose.h"
|
||
|
|
|
||
|
|
void array_mg_str_append(array_mg_str* arr, struct mg_str item) {
|
||
|
|
if (arr->count >= arr->capacity) {
|
||
|
|
if (arr->capacity == 0) arr->capacity = 8;
|
||
|
|
else arr->capacity *= 2;
|
||
|
|
|
||
|
|
arr->items = realloc(arr->items, arr->capacity * sizeof(*arr->items));
|
||
|
|
}
|
||
|
|
arr->items[arr->count++] = item;
|
||
|
|
}
|
||
|
|
|
||
|
|
void split_mg_str(struct mg_str* string, const char token, array_mg_str* return_array) {
|
||
|
|
int start_index = 0;
|
||
|
|
|
||
|
|
for (int i = 0; i < string->len; i++) {
|
||
|
|
if (string->buf[i] == token || string->len == i + 1) {
|
||
|
|
if (i != start_index) {
|
||
|
|
int slice_len = i - start_index;
|
||
|
|
if (string->len == i + 1 && string->buf[i] != token) slice_len++;
|
||
|
|
|
||
|
|
struct mg_str tmp_mg_str = mg_str_n(string->buf + start_index, slice_len);
|
||
|
|
|
||
|
|
array_mg_str_append(return_array, tmp_mg_str);
|
||
|
|
}
|
||
|
|
start_index = i + 1;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|