Return a TreeArray from ts_stack_pop

Since the capacity is now included in the return value, the buffer
can be reused in the ts_parser__accept function. Also, it's just
cleaner to use Array consistently, rather than a separate buffer
and size.
This commit is contained in:
Max Brunsfeld 2016-02-21 22:31:04 -08:00
parent f92b35e77e
commit b113dc8b0f
5 changed files with 142 additions and 129 deletions

View file

@ -43,6 +43,14 @@ extern "C" {
array_grow((self), (self)->capacity * 2)) && \
((self)->contents[(self)->size++] = (element), true))
#define array_splice(self, index, old_count, new_count, new_elements) \
array__splice((VoidArray *)(self), \
array__elem_size(self), \
index, \
old_count, \
new_count, \
new_elements) \
#define array_pop(self) ((self)->contents[--(self)->size])
#define array_reverse(self) \
@ -95,6 +103,30 @@ static inline bool array__grow(VoidArray *self, size_t element_size,
return true;
}
static inline bool array__splice(VoidArray *self, size_t element_size,
size_t index, size_t old_count,
size_t new_count, void *elements) {
assert(index + old_count <= self->size);
assert(index < self->size);
size_t new_size = self->size + new_count - old_count;
size_t old_end = index + old_count;
size_t new_end = index + new_count;
if (new_size >= self->capacity) {
if (!array__grow(self, element_size, new_size))
return false;
}
char *contents = (char *)self->contents;
if (self->size > old_end)
memmove(contents + new_end * element_size,
contents + old_end * element_size,
(self->size - old_end) * element_size);
if (new_count > 0)
memcpy((contents + index * element_size), elements, new_count * element_size);
self->size += new_count - old_count;
return true;
}
static inline void array__reverse(VoidArray *self, size_t element_size) {
char swap[element_size];
char *contents = (char *)self->contents;