[utils] adding cstring_array_extend and string_tree_clear

This commit is contained in:
Al
2017-12-24 01:46:20 -05:00
parent 4e3d868bd0
commit c78566c241
2 changed files with 22 additions and 0 deletions

View File

@@ -1110,6 +1110,18 @@ cstring_array *cstring_array_from_strings(char **strings, size_t n) {
return array;
}
bool cstring_array_extend(cstring_array *array, cstring_array *other) {
if (array == NULL || other == NULL) return false;
size_t n = cstring_array_num_strings(other);
for (size_t i = 0; i < n; i++) {
char *s_i = cstring_array_get_string(other, i);
cstring_array_add_string(array, s_i);
}
return true;
}
inline size_t cstring_array_capacity(cstring_array *self) {
return self->str->m;
}
@@ -1318,6 +1330,12 @@ inline void string_tree_finalize_token(string_tree_t *self) {
uint32_array_push(self->token_indices, (uint32_t)cstring_array_num_strings(self->strings));
}
void string_tree_clear(string_tree_t *self) {
uint32_array_clear(self->token_indices);
uint32_array_push(self->token_indices, 0);
cstring_array_clear(self->strings);
}
// terminated
inline void string_tree_add_string(string_tree_t *self, char *str) {
cstring_array_add_string(self->strings, str);

View File

@@ -208,6 +208,8 @@ void cstring_array_clear(cstring_array *self);
cstring_array *cstring_array_from_char_array(char_array *str);
cstring_array *cstring_array_from_strings(char **strings, size_t n);
bool cstring_array_extend(cstring_array *array, cstring_array *other);
// Convert cstring_array to an array of n C strings and destroy the cstring_array
char **cstring_array_to_strings(cstring_array *self);
@@ -285,6 +287,8 @@ void string_tree_add_string_len(string_tree_t *self, char *str, size_t len);
void string_tree_append_string(string_tree_t *self, char *str);
void string_tree_append_string_len(string_tree_t *self, char *str, size_t len);
void string_tree_clear(string_tree_t *self);
uint32_t string_tree_num_tokens(string_tree_t *self);
uint32_t string_tree_num_strings(string_tree_t *self);