tree_sitter

Constant ARRAY_HEADER

source
pub const ARRAY_HEADER: &str = "#ifndef TREE_SITTER_ARRAY_H_\n#define TREE_SITTER_ARRAY_H_\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n#include \"./alloc.h\"\n\n#include <assert.h>\n#include <stdbool.h>\n#include <stdint.h>\n#include <stdlib.h>\n#include <string.h>\n\n#ifdef _MSC_VER\n#pragma warning(disable : 4101)\n#elif defined(__GNUC__) || defined(__clang__)\n#pragma GCC diagnostic push\n#pragma GCC diagnostic ignored \"-Wunused-variable\"\n#endif\n\n#define Array(T)       \\\n  struct {             \\\n    T *contents;       \\\n    uint32_t size;     \\\n    uint32_t capacity; \\\n  }\n\n/// Initialize an array.\n#define array_init(self) \\\n  ((self)->size = 0, (self)->capacity = 0, (self)->contents = NULL)\n\n/// Create an empty array.\n#define array_new() \\\n  { NULL, 0, 0 }\n\n/// Get a pointer to the element at a given `index` in the array.\n#define array_get(self, _index) \\\n  (assert((uint32_t)(_index) < (self)->size), &(self)->contents[_index])\n\n/// Get a pointer to the first element in the array.\n#define array_front(self) array_get(self, 0)\n\n/// Get a pointer to the last element in the array.\n#define array_back(self) array_get(self, (self)->size - 1)\n\n/// Clear the array, setting its size to zero. Note that this does not free any\n/// memory allocated for the array\'s contents.\n#define array_clear(self) ((self)->size = 0)\n\n/// Reserve `new_capacity` elements of space in the array. If `new_capacity` is\n/// less than the array\'s current capacity, this function has no effect.\n#define array_reserve(self, new_capacity) \\\n  _array__reserve((Array *)(self), array_elem_size(self), new_capacity)\n\n/// Free any memory allocated for this array. Note that this does not free any\n/// memory allocated for the array\'s contents.\n#define array_delete(self) _array__delete((Array *)(self))\n\n/// Push a new `element` onto the end of the array.\n#define array_push(self, element)                            \\\n  (_array__grow((Array *)(self), 1, array_elem_size(self)), \\\n   (self)->contents[(self)->size++] = (element))\n\n/// Increase the array\'s size by `count` elements.\n/// New elements are zero-initialized.\n#define array_grow_by(self, count) \\\n  do { \\\n    if ((count) == 0) break; \\\n    _array__grow((Array *)(self), count, array_elem_size(self)); \\\n    memset((self)->contents + (self)->size, 0, (count) * array_elem_size(self)); \\\n    (self)->size += (count); \\\n  } while (0)\n\n/// Append all elements from one array to the end of another.\n#define array_push_all(self, other)                                       \\\n  array_extend((self), (other)->size, (other)->contents)\n\n/// Append `count` elements to the end of the array, reading their values from the\n/// `contents` pointer.\n#define array_extend(self, count, contents)                    \\\n  _array__splice(                                               \\\n    (Array *)(self), array_elem_size(self), (self)->size, \\\n    0, count,  contents                                        \\\n  )\n\n/// Remove `old_count` elements from the array starting at the given `index`. At\n/// the same index, insert `new_count` new elements, reading their values from the\n/// `new_contents` pointer.\n#define array_splice(self, _index, old_count, new_count, new_contents)  \\\n  _array__splice(                                                       \\\n    (Array *)(self), array_elem_size(self), _index,                \\\n    old_count, new_count, new_contents                                 \\\n  )\n\n/// Insert one `element` into the array at the given `index`.\n#define array_insert(self, _index, element) \\\n  _array__splice((Array *)(self), array_elem_size(self), _index, 0, 1, &(element))\n\n/// Remove one element from the array at the given `index`.\n#define array_erase(self, _index) \\\n  _array__erase((Array *)(self), array_elem_size(self), _index)\n\n/// Pop the last element off the array, returning the element by value.\n#define array_pop(self) ((self)->contents[--(self)->size])\n\n/// Assign the contents of one array to another, reallocating if necessary.\n#define array_assign(self, other) \\\n  _array__assign((Array *)(self), (const Array *)(other), array_elem_size(self))\n\n/// Swap one array with another\n#define array_swap(self, other) \\\n  _array__swap((Array *)(self), (Array *)(other))\n\n/// Get the size of the array contents\n#define array_elem_size(self) (sizeof *(self)->contents)\n\n/// Search a sorted array for a given `needle` value, using the given `compare`\n/// callback to determine the order.\n///\n/// If an existing element is found to be equal to `needle`, then the `index`\n/// out-parameter is set to the existing value\'s index, and the `exists`\n/// out-parameter is set to true. Otherwise, `index` is set to an index where\n/// `needle` should be inserted in order to preserve the sorting, and `exists`\n/// is set to false.\n#define array_search_sorted_with(self, compare, needle, _index, _exists) \\\n  _array__search_sorted(self, 0, compare, , needle, _index, _exists)\n\n/// Search a sorted array for a given `needle` value, using integer comparisons\n/// of a given struct field (specified with a leading dot) to determine the order.\n///\n/// See also `array_search_sorted_with`.\n#define array_search_sorted_by(self, field, needle, _index, _exists) \\\n  _array__search_sorted(self, 0, _compare_int, field, needle, _index, _exists)\n\n/// Insert a given `value` into a sorted array, using the given `compare`\n/// callback to determine the order.\n#define array_insert_sorted_with(self, compare, value) \\\n  do { \\\n    unsigned _index, _exists; \\\n    array_search_sorted_with(self, compare, &(value), &_index, &_exists); \\\n    if (!_exists) array_insert(self, _index, value); \\\n  } while (0)\n\n/// Insert a given `value` into a sorted array, using integer comparisons of\n/// a given struct field (specified with a leading dot) to determine the order.\n///\n/// See also `array_search_sorted_by`.\n#define array_insert_sorted_by(self, field, value) \\\n  do { \\\n    unsigned _index, _exists; \\\n    array_search_sorted_by(self, field, (value) field, &_index, &_exists); \\\n    if (!_exists) array_insert(self, _index, value); \\\n  } while (0)\n\n// Private\n\ntypedef Array(void) Array;\n\n/// This is not what you\'re looking for, see `array_delete`.\nstatic inline void _array__delete(Array *self) {\n  if (self->contents) {\n    ts_free(self->contents);\n    self->contents = NULL;\n    self->size = 0;\n    self->capacity = 0;\n  }\n}\n\n/// This is not what you\'re looking for, see `array_erase`.\nstatic inline void _array__erase(Array *self, size_t element_size,\n                                uint32_t index) {\n  assert(index < self->size);\n  char *contents = (char *)self->contents;\n  memmove(contents + index * element_size, contents + (index + 1) * element_size,\n          (self->size - index - 1) * element_size);\n  self->size--;\n}\n\n/// This is not what you\'re looking for, see `array_reserve`.\nstatic inline void _array__reserve(Array *self, size_t element_size, uint32_t new_capacity) {\n  if (new_capacity > self->capacity) {\n    if (self->contents) {\n      self->contents = ts_realloc(self->contents, new_capacity * element_size);\n    } else {\n      self->contents = ts_malloc(new_capacity * element_size);\n    }\n    self->capacity = new_capacity;\n  }\n}\n\n/// This is not what you\'re looking for, see `array_assign`.\nstatic inline void _array__assign(Array *self, const Array *other, size_t element_size) {\n  _array__reserve(self, element_size, other->size);\n  self->size = other->size;\n  memcpy(self->contents, other->contents, self->size * element_size);\n}\n\n/// This is not what you\'re looking for, see `array_swap`.\nstatic inline void _array__swap(Array *self, Array *other) {\n  Array swap = *other;\n  *other = *self;\n  *self = swap;\n}\n\n/// This is not what you\'re looking for, see `array_push` or `array_grow_by`.\nstatic inline void _array__grow(Array *self, uint32_t count, size_t element_size) {\n  uint32_t new_size = self->size + count;\n  if (new_size > self->capacity) {\n    uint32_t new_capacity = self->capacity * 2;\n    if (new_capacity < 8) new_capacity = 8;\n    if (new_capacity < new_size) new_capacity = new_size;\n    _array__reserve(self, element_size, new_capacity);\n  }\n}\n\n/// This is not what you\'re looking for, see `array_splice`.\nstatic inline void _array__splice(Array *self, size_t element_size,\n                                 uint32_t index, uint32_t old_count,\n                                 uint32_t new_count, const void *elements) {\n  uint32_t new_size = self->size + new_count - old_count;\n  uint32_t old_end = index + old_count;\n  uint32_t new_end = index + new_count;\n  assert(old_end <= self->size);\n\n  _array__reserve(self, element_size, new_size);\n\n  char *contents = (char *)self->contents;\n  if (self->size > old_end) {\n    memmove(\n      contents + new_end * element_size,\n      contents + old_end * element_size,\n      (self->size - old_end) * element_size\n    );\n  }\n  if (new_count > 0) {\n    if (elements) {\n      memcpy(\n        (contents + index * element_size),\n        elements,\n        new_count * element_size\n      );\n    } else {\n      memset(\n        (contents + index * element_size),\n        0,\n        new_count * element_size\n      );\n    }\n  }\n  self->size += new_count - old_count;\n}\n\n/// A binary search routine, based on Rust\'s `std::slice::binary_search_by`.\n/// This is not what you\'re looking for, see `array_search_sorted_with` or `array_search_sorted_by`.\n#define _array__search_sorted(self, start, compare, suffix, needle, _index, _exists) \\\n  do { \\\n    *(_index) = start; \\\n    *(_exists) = false; \\\n    uint32_t size = (self)->size - *(_index); \\\n    if (size == 0) break; \\\n    int comparison; \\\n    while (size > 1) { \\\n      uint32_t half_size = size / 2; \\\n      uint32_t mid_index = *(_index) + half_size; \\\n      comparison = compare(&((self)->contents[mid_index] suffix), (needle)); \\\n      if (comparison <= 0) *(_index) = mid_index; \\\n      size -= half_size; \\\n    } \\\n    comparison = compare(&((self)->contents[*(_index)] suffix), (needle)); \\\n    if (comparison == 0) *(_exists) = true; \\\n    else if (comparison < 0) *(_index) += 1; \\\n  } while (0)\n\n/// Helper macro for the `_sorted_by` routines below. This takes the left (existing)\n/// parameter by reference in order to work with the generic sorting function above.\n#define _compare_int(a, b) ((int)*(a) - (int)(b))\n\n#ifdef _MSC_VER\n#pragma warning(default : 4101)\n#elif defined(__GNUC__) || defined(__clang__)\n#pragma GCC diagnostic pop\n#endif\n\n#ifdef __cplusplus\n}\n#endif\n\n#endif  // TREE_SITTER_ARRAY_H_\n";