@ast-grep/lang-yaml 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,200 @@
1
+ #include <stdlib.h>
2
+
3
+ #define SCH_STT_FRZ -1
4
+
5
+ typedef enum {
6
+ RS_STR,
7
+ RS_INT,
8
+ RS_NULL,
9
+ RS_BOOL,
10
+ RS_FLOAT,
11
+ } ResultSchema;
12
+
13
+ static int8_t adv_sch_stt(int8_t sch_stt, int32_t cur_chr, ResultSchema *rlt_sch) {
14
+ switch (sch_stt) {
15
+ case SCH_STT_FRZ:
16
+ break;
17
+ case 0:
18
+ if (cur_chr == '.') {*rlt_sch = RS_STR; return 6;}
19
+ if (cur_chr == '0') {*rlt_sch = RS_INT; return 37;}
20
+ if (cur_chr == 'F') {*rlt_sch = RS_STR; return 2;}
21
+ if (cur_chr == 'N') {*rlt_sch = RS_STR; return 16;}
22
+ if (cur_chr == 'T') {*rlt_sch = RS_STR; return 13;}
23
+ if (cur_chr == 'f') {*rlt_sch = RS_STR; return 17;}
24
+ if (cur_chr == 'n') {*rlt_sch = RS_STR; return 29;}
25
+ if (cur_chr == 't') {*rlt_sch = RS_STR; return 26;}
26
+ if (cur_chr == '~') {*rlt_sch = RS_NULL; return 35;}
27
+ if (cur_chr == '+') {*rlt_sch = RS_STR; return 1;}
28
+ if (cur_chr == '-') {*rlt_sch = RS_STR; return 1;}
29
+ if (('1' <= cur_chr && cur_chr <= '9')) {*rlt_sch = RS_INT; return 38;}
30
+ break;
31
+ case 1:
32
+ if (cur_chr == '.') {*rlt_sch = RS_STR; return 7;}
33
+ if (('0' <= cur_chr && cur_chr <= '9')) {*rlt_sch = RS_INT; return 38;}
34
+ break;
35
+ case 2:
36
+ if (cur_chr == 'A') {*rlt_sch = RS_STR; return 9;}
37
+ if (cur_chr == 'a') {*rlt_sch = RS_STR; return 22;}
38
+ break;
39
+ case 3:
40
+ if (cur_chr == 'A') {*rlt_sch = RS_STR; return 12;}
41
+ if (cur_chr == 'a') {*rlt_sch = RS_STR; return 12;}
42
+ break;
43
+ case 4:
44
+ if (cur_chr == 'E') {*rlt_sch = RS_BOOL; return 36;}
45
+ break;
46
+ case 5:
47
+ if (cur_chr == 'F') {*rlt_sch = RS_FLOAT; return 41;}
48
+ break;
49
+ case 6:
50
+ if (cur_chr == 'I') {*rlt_sch = RS_STR; return 11;}
51
+ if (cur_chr == 'N') {*rlt_sch = RS_STR; return 3;}
52
+ if (cur_chr == 'i') {*rlt_sch = RS_STR; return 24;}
53
+ if (cur_chr == 'n') {*rlt_sch = RS_STR; return 18;}
54
+ if (('0' <= cur_chr && cur_chr <= '9')) {*rlt_sch = RS_FLOAT; return 42;}
55
+ break;
56
+ case 7:
57
+ if (cur_chr == 'I') {*rlt_sch = RS_STR; return 11;}
58
+ if (cur_chr == 'i') {*rlt_sch = RS_STR; return 24;}
59
+ if (('0' <= cur_chr && cur_chr <= '9')) {*rlt_sch = RS_FLOAT; return 42;}
60
+ break;
61
+ case 8:
62
+ if (cur_chr == 'L') {*rlt_sch = RS_NULL; return 35;}
63
+ break;
64
+ case 9:
65
+ if (cur_chr == 'L') {*rlt_sch = RS_STR; return 14;}
66
+ break;
67
+ case 10:
68
+ if (cur_chr == 'L') {*rlt_sch = RS_STR; return 8;}
69
+ break;
70
+ case 11:
71
+ if (cur_chr == 'N') {*rlt_sch = RS_STR; return 5;}
72
+ if (cur_chr == 'n') {*rlt_sch = RS_STR; return 20;}
73
+ break;
74
+ case 12:
75
+ if (cur_chr == 'N') {*rlt_sch = RS_FLOAT; return 41;}
76
+ break;
77
+ case 13:
78
+ if (cur_chr == 'R') {*rlt_sch = RS_STR; return 15;}
79
+ if (cur_chr == 'r') {*rlt_sch = RS_STR; return 28;}
80
+ break;
81
+ case 14:
82
+ if (cur_chr == 'S') {*rlt_sch = RS_STR; return 4;}
83
+ break;
84
+ case 15:
85
+ if (cur_chr == 'U') {*rlt_sch = RS_STR; return 4;}
86
+ break;
87
+ case 16:
88
+ if (cur_chr == 'U') {*rlt_sch = RS_STR; return 10;}
89
+ if (cur_chr == 'u') {*rlt_sch = RS_STR; return 23;}
90
+ break;
91
+ case 17:
92
+ if (cur_chr == 'a') {*rlt_sch = RS_STR; return 22;}
93
+ break;
94
+ case 18:
95
+ if (cur_chr == 'a') {*rlt_sch = RS_STR; return 25;}
96
+ break;
97
+ case 19:
98
+ if (cur_chr == 'e') {*rlt_sch = RS_BOOL; return 36;}
99
+ break;
100
+ case 20:
101
+ if (cur_chr == 'f') {*rlt_sch = RS_FLOAT; return 41;}
102
+ break;
103
+ case 21:
104
+ if (cur_chr == 'l') {*rlt_sch = RS_NULL; return 35;}
105
+ break;
106
+ case 22:
107
+ if (cur_chr == 'l') {*rlt_sch = RS_STR; return 27;}
108
+ break;
109
+ case 23:
110
+ if (cur_chr == 'l') {*rlt_sch = RS_STR; return 21;}
111
+ break;
112
+ case 24:
113
+ if (cur_chr == 'n') {*rlt_sch = RS_STR; return 20;}
114
+ break;
115
+ case 25:
116
+ if (cur_chr == 'n') {*rlt_sch = RS_FLOAT; return 41;}
117
+ break;
118
+ case 26:
119
+ if (cur_chr == 'r') {*rlt_sch = RS_STR; return 28;}
120
+ break;
121
+ case 27:
122
+ if (cur_chr == 's') {*rlt_sch = RS_STR; return 19;}
123
+ break;
124
+ case 28:
125
+ if (cur_chr == 'u') {*rlt_sch = RS_STR; return 19;}
126
+ break;
127
+ case 29:
128
+ if (cur_chr == 'u') {*rlt_sch = RS_STR; return 23;}
129
+ break;
130
+ case 30:
131
+ if (cur_chr == '+' ||
132
+ cur_chr == '-') {*rlt_sch = RS_STR; return 32;}
133
+ if (('0' <= cur_chr && cur_chr <= '9')) {*rlt_sch = RS_FLOAT; return 43;}
134
+ break;
135
+ case 31:
136
+ if (('0' <= cur_chr && cur_chr <= '7')) {*rlt_sch = RS_INT; return 39;}
137
+ break;
138
+ case 32:
139
+ if (('0' <= cur_chr && cur_chr <= '9')) {*rlt_sch = RS_FLOAT; return 43;}
140
+ break;
141
+ case 33:
142
+ if (('0' <= cur_chr && cur_chr <= '9') ||
143
+ ('A' <= cur_chr && cur_chr <= 'F') ||
144
+ ('a' <= cur_chr && cur_chr <= 'f')) {*rlt_sch = RS_INT; return 40;}
145
+ break;
146
+ case 34:
147
+ abort();
148
+ break;
149
+ case 35:
150
+ *rlt_sch = RS_NULL;
151
+ break;
152
+ case 36:
153
+ *rlt_sch = RS_BOOL;
154
+ break;
155
+ case 37:
156
+ *rlt_sch = RS_INT;
157
+ if (cur_chr == '.') {*rlt_sch = RS_FLOAT; return 42;}
158
+ if (cur_chr == 'o') {*rlt_sch = RS_STR; return 31;}
159
+ if (cur_chr == 'x') {*rlt_sch = RS_STR; return 33;}
160
+ if (cur_chr == 'E' ||
161
+ cur_chr == 'e') {*rlt_sch = RS_STR; return 30;}
162
+ if (('0' <= cur_chr && cur_chr <= '9')) {*rlt_sch = RS_INT; return 38;}
163
+ break;
164
+ case 38:
165
+ *rlt_sch = RS_INT;
166
+ if (cur_chr == '.') {*rlt_sch = RS_FLOAT; return 42;}
167
+ if (cur_chr == 'E' ||
168
+ cur_chr == 'e') {*rlt_sch = RS_STR; return 30;}
169
+ if (('0' <= cur_chr && cur_chr <= '9')) {*rlt_sch = RS_INT; return 38;}
170
+ break;
171
+ case 39:
172
+ *rlt_sch = RS_INT;
173
+ if (('0' <= cur_chr && cur_chr <= '7')) {*rlt_sch = RS_INT; return 39;}
174
+ break;
175
+ case 40:
176
+ *rlt_sch = RS_INT;
177
+ if (('0' <= cur_chr && cur_chr <= '9') ||
178
+ ('A' <= cur_chr && cur_chr <= 'F') ||
179
+ ('a' <= cur_chr && cur_chr <= 'f')) {*rlt_sch = RS_INT; return 40;}
180
+ break;
181
+ case 41:
182
+ *rlt_sch = RS_FLOAT;
183
+ break;
184
+ case 42:
185
+ *rlt_sch = RS_FLOAT;
186
+ if (cur_chr == 'E' ||
187
+ cur_chr == 'e') {*rlt_sch = RS_STR; return 30;}
188
+ if (('0' <= cur_chr && cur_chr <= '9')) {*rlt_sch = RS_FLOAT; return 42;}
189
+ break;
190
+ case 43:
191
+ *rlt_sch = RS_FLOAT;
192
+ if (('0' <= cur_chr && cur_chr <= '9')) {*rlt_sch = RS_FLOAT; return 43;}
193
+ break;
194
+ default:
195
+ *rlt_sch = RS_STR;
196
+ return SCH_STT_FRZ;
197
+ }
198
+ if (cur_chr != '\r' && cur_chr != '\n' && cur_chr != ' ' && cur_chr != 0) *rlt_sch = RS_STR;
199
+ return SCH_STT_FRZ;
200
+ }
@@ -0,0 +1,102 @@
1
+ #include <stdlib.h>
2
+
3
+ #define SCH_STT_FRZ -1
4
+
5
+ typedef enum {
6
+ RS_STR,
7
+ RS_INT,
8
+ RS_BOOL,
9
+ RS_NULL,
10
+ RS_FLOAT,
11
+ } ResultSchema;
12
+
13
+ static int8_t adv_sch_stt(int8_t sch_stt, int32_t cur_chr, ResultSchema *rlt_sch) {
14
+ switch (sch_stt) {
15
+ case SCH_STT_FRZ:
16
+ break;
17
+ case 0:
18
+ if (cur_chr == '-') {*rlt_sch = RS_STR; return 1;}
19
+ if (cur_chr == '0') {*rlt_sch = RS_INT; return 16;}
20
+ if (cur_chr == 'f') {*rlt_sch = RS_STR; return 2;}
21
+ if (cur_chr == 'n') {*rlt_sch = RS_STR; return 10;}
22
+ if (cur_chr == 't') {*rlt_sch = RS_STR; return 7;}
23
+ if (('1' <= cur_chr && cur_chr <= '9')) {*rlt_sch = RS_INT; return 17;}
24
+ break;
25
+ case 1:
26
+ if (cur_chr == '0') {*rlt_sch = RS_INT; return 16;}
27
+ if (('1' <= cur_chr && cur_chr <= '9')) {*rlt_sch = RS_INT; return 17;}
28
+ break;
29
+ case 2:
30
+ if (cur_chr == 'a') {*rlt_sch = RS_STR; return 4;}
31
+ break;
32
+ case 3:
33
+ if (cur_chr == 'e') {*rlt_sch = RS_BOOL; return 15;}
34
+ break;
35
+ case 4:
36
+ if (cur_chr == 'l') {*rlt_sch = RS_STR; return 8;}
37
+ break;
38
+ case 5:
39
+ if (cur_chr == 'l') {*rlt_sch = RS_NULL; return 14;}
40
+ break;
41
+ case 6:
42
+ if (cur_chr == 'l') {*rlt_sch = RS_STR; return 5;}
43
+ break;
44
+ case 7:
45
+ if (cur_chr == 'r') {*rlt_sch = RS_STR; return 9;}
46
+ break;
47
+ case 8:
48
+ if (cur_chr == 's') {*rlt_sch = RS_STR; return 3;}
49
+ break;
50
+ case 9:
51
+ if (cur_chr == 'u') {*rlt_sch = RS_STR; return 3;}
52
+ break;
53
+ case 10:
54
+ if (cur_chr == 'u') {*rlt_sch = RS_STR; return 6;}
55
+ break;
56
+ case 11:
57
+ if (cur_chr == '+' ||
58
+ cur_chr == '-') {*rlt_sch = RS_STR; return 12;}
59
+ if (('0' <= cur_chr && cur_chr <= '9')) {*rlt_sch = RS_FLOAT; return 19;}
60
+ break;
61
+ case 12:
62
+ if (('0' <= cur_chr && cur_chr <= '9')) {*rlt_sch = RS_FLOAT; return 19;}
63
+ break;
64
+ case 13:
65
+ abort();
66
+ break;
67
+ case 14:
68
+ *rlt_sch = RS_NULL;
69
+ break;
70
+ case 15:
71
+ *rlt_sch = RS_BOOL;
72
+ break;
73
+ case 16:
74
+ *rlt_sch = RS_INT;
75
+ if (cur_chr == '.') {*rlt_sch = RS_FLOAT; return 18;}
76
+ if (cur_chr == 'E' ||
77
+ cur_chr == 'e') {*rlt_sch = RS_STR; return 11;}
78
+ break;
79
+ case 17:
80
+ *rlt_sch = RS_INT;
81
+ if (cur_chr == '.') {*rlt_sch = RS_FLOAT; return 18;}
82
+ if (cur_chr == 'E' ||
83
+ cur_chr == 'e') {*rlt_sch = RS_STR; return 11;}
84
+ if (('0' <= cur_chr && cur_chr <= '9')) {*rlt_sch = RS_INT; return 17;}
85
+ break;
86
+ case 18:
87
+ *rlt_sch = RS_FLOAT;
88
+ if (cur_chr == 'E' ||
89
+ cur_chr == 'e') {*rlt_sch = RS_STR; return 11;}
90
+ if (('0' <= cur_chr && cur_chr <= '9')) {*rlt_sch = RS_FLOAT; return 18;}
91
+ break;
92
+ case 19:
93
+ *rlt_sch = RS_FLOAT;
94
+ if (('0' <= cur_chr && cur_chr <= '9')) {*rlt_sch = RS_FLOAT; return 19;}
95
+ break;
96
+ default:
97
+ *rlt_sch = RS_STR;
98
+ return SCH_STT_FRZ;
99
+ }
100
+ if (cur_chr != '\r' && cur_chr != '\n' && cur_chr != ' ' && cur_chr != 0) *rlt_sch = RS_STR;
101
+ return SCH_STT_FRZ;
102
+ }
@@ -0,0 +1,54 @@
1
+ #ifndef TREE_SITTER_ALLOC_H_
2
+ #define TREE_SITTER_ALLOC_H_
3
+
4
+ #ifdef __cplusplus
5
+ extern "C" {
6
+ #endif
7
+
8
+ #include <stdbool.h>
9
+ #include <stdio.h>
10
+ #include <stdlib.h>
11
+
12
+ // Allow clients to override allocation functions
13
+ #ifdef TREE_SITTER_REUSE_ALLOCATOR
14
+
15
+ extern void *(*ts_current_malloc)(size_t size);
16
+ extern void *(*ts_current_calloc)(size_t count, size_t size);
17
+ extern void *(*ts_current_realloc)(void *ptr, size_t size);
18
+ extern void (*ts_current_free)(void *ptr);
19
+
20
+ #ifndef ts_malloc
21
+ #define ts_malloc ts_current_malloc
22
+ #endif
23
+ #ifndef ts_calloc
24
+ #define ts_calloc ts_current_calloc
25
+ #endif
26
+ #ifndef ts_realloc
27
+ #define ts_realloc ts_current_realloc
28
+ #endif
29
+ #ifndef ts_free
30
+ #define ts_free ts_current_free
31
+ #endif
32
+
33
+ #else
34
+
35
+ #ifndef ts_malloc
36
+ #define ts_malloc malloc
37
+ #endif
38
+ #ifndef ts_calloc
39
+ #define ts_calloc calloc
40
+ #endif
41
+ #ifndef ts_realloc
42
+ #define ts_realloc realloc
43
+ #endif
44
+ #ifndef ts_free
45
+ #define ts_free free
46
+ #endif
47
+
48
+ #endif
49
+
50
+ #ifdef __cplusplus
51
+ }
52
+ #endif
53
+
54
+ #endif // TREE_SITTER_ALLOC_H_
@@ -0,0 +1,290 @@
1
+ #ifndef TREE_SITTER_ARRAY_H_
2
+ #define TREE_SITTER_ARRAY_H_
3
+
4
+ #ifdef __cplusplus
5
+ extern "C" {
6
+ #endif
7
+
8
+ #include "./alloc.h"
9
+
10
+ #include <assert.h>
11
+ #include <stdbool.h>
12
+ #include <stdint.h>
13
+ #include <stdlib.h>
14
+ #include <string.h>
15
+
16
+ #ifdef _MSC_VER
17
+ #pragma warning(disable : 4101)
18
+ #elif defined(__GNUC__) || defined(__clang__)
19
+ #pragma GCC diagnostic push
20
+ #pragma GCC diagnostic ignored "-Wunused-variable"
21
+ #endif
22
+
23
+ #define Array(T) \
24
+ struct { \
25
+ T *contents; \
26
+ uint32_t size; \
27
+ uint32_t capacity; \
28
+ }
29
+
30
+ /// Initialize an array.
31
+ #define array_init(self) \
32
+ ((self)->size = 0, (self)->capacity = 0, (self)->contents = NULL)
33
+
34
+ /// Create an empty array.
35
+ #define array_new() \
36
+ { NULL, 0, 0 }
37
+
38
+ /// Get a pointer to the element at a given `index` in the array.
39
+ #define array_get(self, _index) \
40
+ (assert((uint32_t)(_index) < (self)->size), &(self)->contents[_index])
41
+
42
+ /// Get a pointer to the first element in the array.
43
+ #define array_front(self) array_get(self, 0)
44
+
45
+ /// Get a pointer to the last element in the array.
46
+ #define array_back(self) array_get(self, (self)->size - 1)
47
+
48
+ /// Clear the array, setting its size to zero. Note that this does not free any
49
+ /// memory allocated for the array's contents.
50
+ #define array_clear(self) ((self)->size = 0)
51
+
52
+ /// Reserve `new_capacity` elements of space in the array. If `new_capacity` is
53
+ /// less than the array's current capacity, this function has no effect.
54
+ #define array_reserve(self, new_capacity) \
55
+ _array__reserve((Array *)(self), array_elem_size(self), new_capacity)
56
+
57
+ /// Free any memory allocated for this array. Note that this does not free any
58
+ /// memory allocated for the array's contents.
59
+ #define array_delete(self) _array__delete((Array *)(self))
60
+
61
+ /// Push a new `element` onto the end of the array.
62
+ #define array_push(self, element) \
63
+ (_array__grow((Array *)(self), 1, array_elem_size(self)), \
64
+ (self)->contents[(self)->size++] = (element))
65
+
66
+ /// Increase the array's size by `count` elements.
67
+ /// New elements are zero-initialized.
68
+ #define array_grow_by(self, count) \
69
+ do { \
70
+ if ((count) == 0) break; \
71
+ _array__grow((Array *)(self), count, array_elem_size(self)); \
72
+ memset((self)->contents + (self)->size, 0, (count) * array_elem_size(self)); \
73
+ (self)->size += (count); \
74
+ } while (0)
75
+
76
+ /// Append all elements from one array to the end of another.
77
+ #define array_push_all(self, other) \
78
+ array_extend((self), (other)->size, (other)->contents)
79
+
80
+ /// Append `count` elements to the end of the array, reading their values from the
81
+ /// `contents` pointer.
82
+ #define array_extend(self, count, contents) \
83
+ _array__splice( \
84
+ (Array *)(self), array_elem_size(self), (self)->size, \
85
+ 0, count, contents \
86
+ )
87
+
88
+ /// Remove `old_count` elements from the array starting at the given `index`. At
89
+ /// the same index, insert `new_count` new elements, reading their values from the
90
+ /// `new_contents` pointer.
91
+ #define array_splice(self, _index, old_count, new_count, new_contents) \
92
+ _array__splice( \
93
+ (Array *)(self), array_elem_size(self), _index, \
94
+ old_count, new_count, new_contents \
95
+ )
96
+
97
+ /// Insert one `element` into the array at the given `index`.
98
+ #define array_insert(self, _index, element) \
99
+ _array__splice((Array *)(self), array_elem_size(self), _index, 0, 1, &(element))
100
+
101
+ /// Remove one element from the array at the given `index`.
102
+ #define array_erase(self, _index) \
103
+ _array__erase((Array *)(self), array_elem_size(self), _index)
104
+
105
+ /// Pop the last element off the array, returning the element by value.
106
+ #define array_pop(self) ((self)->contents[--(self)->size])
107
+
108
+ /// Assign the contents of one array to another, reallocating if necessary.
109
+ #define array_assign(self, other) \
110
+ _array__assign((Array *)(self), (const Array *)(other), array_elem_size(self))
111
+
112
+ /// Swap one array with another
113
+ #define array_swap(self, other) \
114
+ _array__swap((Array *)(self), (Array *)(other))
115
+
116
+ /// Get the size of the array contents
117
+ #define array_elem_size(self) (sizeof *(self)->contents)
118
+
119
+ /// Search a sorted array for a given `needle` value, using the given `compare`
120
+ /// callback to determine the order.
121
+ ///
122
+ /// If an existing element is found to be equal to `needle`, then the `index`
123
+ /// out-parameter is set to the existing value's index, and the `exists`
124
+ /// out-parameter is set to true. Otherwise, `index` is set to an index where
125
+ /// `needle` should be inserted in order to preserve the sorting, and `exists`
126
+ /// is set to false.
127
+ #define array_search_sorted_with(self, compare, needle, _index, _exists) \
128
+ _array__search_sorted(self, 0, compare, , needle, _index, _exists)
129
+
130
+ /// Search a sorted array for a given `needle` value, using integer comparisons
131
+ /// of a given struct field (specified with a leading dot) to determine the order.
132
+ ///
133
+ /// See also `array_search_sorted_with`.
134
+ #define array_search_sorted_by(self, field, needle, _index, _exists) \
135
+ _array__search_sorted(self, 0, _compare_int, field, needle, _index, _exists)
136
+
137
+ /// Insert a given `value` into a sorted array, using the given `compare`
138
+ /// callback to determine the order.
139
+ #define array_insert_sorted_with(self, compare, value) \
140
+ do { \
141
+ unsigned _index, _exists; \
142
+ array_search_sorted_with(self, compare, &(value), &_index, &_exists); \
143
+ if (!_exists) array_insert(self, _index, value); \
144
+ } while (0)
145
+
146
+ /// Insert a given `value` into a sorted array, using integer comparisons of
147
+ /// a given struct field (specified with a leading dot) to determine the order.
148
+ ///
149
+ /// See also `array_search_sorted_by`.
150
+ #define array_insert_sorted_by(self, field, value) \
151
+ do { \
152
+ unsigned _index, _exists; \
153
+ array_search_sorted_by(self, field, (value) field, &_index, &_exists); \
154
+ if (!_exists) array_insert(self, _index, value); \
155
+ } while (0)
156
+
157
+ // Private
158
+
159
+ typedef Array(void) Array;
160
+
161
+ /// This is not what you're looking for, see `array_delete`.
162
+ static inline void _array__delete(Array *self) {
163
+ if (self->contents) {
164
+ ts_free(self->contents);
165
+ self->contents = NULL;
166
+ self->size = 0;
167
+ self->capacity = 0;
168
+ }
169
+ }
170
+
171
+ /// This is not what you're looking for, see `array_erase`.
172
+ static inline void _array__erase(Array *self, size_t element_size,
173
+ uint32_t index) {
174
+ assert(index < self->size);
175
+ char *contents = (char *)self->contents;
176
+ memmove(contents + index * element_size, contents + (index + 1) * element_size,
177
+ (self->size - index - 1) * element_size);
178
+ self->size--;
179
+ }
180
+
181
+ /// This is not what you're looking for, see `array_reserve`.
182
+ static inline void _array__reserve(Array *self, size_t element_size, uint32_t new_capacity) {
183
+ if (new_capacity > self->capacity) {
184
+ if (self->contents) {
185
+ self->contents = ts_realloc(self->contents, new_capacity * element_size);
186
+ } else {
187
+ self->contents = ts_malloc(new_capacity * element_size);
188
+ }
189
+ self->capacity = new_capacity;
190
+ }
191
+ }
192
+
193
+ /// This is not what you're looking for, see `array_assign`.
194
+ static inline void _array__assign(Array *self, const Array *other, size_t element_size) {
195
+ _array__reserve(self, element_size, other->size);
196
+ self->size = other->size;
197
+ memcpy(self->contents, other->contents, self->size * element_size);
198
+ }
199
+
200
+ /// This is not what you're looking for, see `array_swap`.
201
+ static inline void _array__swap(Array *self, Array *other) {
202
+ Array swap = *other;
203
+ *other = *self;
204
+ *self = swap;
205
+ }
206
+
207
+ /// This is not what you're looking for, see `array_push` or `array_grow_by`.
208
+ static inline void _array__grow(Array *self, uint32_t count, size_t element_size) {
209
+ uint32_t new_size = self->size + count;
210
+ if (new_size > self->capacity) {
211
+ uint32_t new_capacity = self->capacity * 2;
212
+ if (new_capacity < 8) new_capacity = 8;
213
+ if (new_capacity < new_size) new_capacity = new_size;
214
+ _array__reserve(self, element_size, new_capacity);
215
+ }
216
+ }
217
+
218
+ /// This is not what you're looking for, see `array_splice`.
219
+ static inline void _array__splice(Array *self, size_t element_size,
220
+ uint32_t index, uint32_t old_count,
221
+ uint32_t new_count, const void *elements) {
222
+ uint32_t new_size = self->size + new_count - old_count;
223
+ uint32_t old_end = index + old_count;
224
+ uint32_t new_end = index + new_count;
225
+ assert(old_end <= self->size);
226
+
227
+ _array__reserve(self, element_size, new_size);
228
+
229
+ char *contents = (char *)self->contents;
230
+ if (self->size > old_end) {
231
+ memmove(
232
+ contents + new_end * element_size,
233
+ contents + old_end * element_size,
234
+ (self->size - old_end) * element_size
235
+ );
236
+ }
237
+ if (new_count > 0) {
238
+ if (elements) {
239
+ memcpy(
240
+ (contents + index * element_size),
241
+ elements,
242
+ new_count * element_size
243
+ );
244
+ } else {
245
+ memset(
246
+ (contents + index * element_size),
247
+ 0,
248
+ new_count * element_size
249
+ );
250
+ }
251
+ }
252
+ self->size += new_count - old_count;
253
+ }
254
+
255
+ /// A binary search routine, based on Rust's `std::slice::binary_search_by`.
256
+ /// This is not what you're looking for, see `array_search_sorted_with` or `array_search_sorted_by`.
257
+ #define _array__search_sorted(self, start, compare, suffix, needle, _index, _exists) \
258
+ do { \
259
+ *(_index) = start; \
260
+ *(_exists) = false; \
261
+ uint32_t size = (self)->size - *(_index); \
262
+ if (size == 0) break; \
263
+ int comparison; \
264
+ while (size > 1) { \
265
+ uint32_t half_size = size / 2; \
266
+ uint32_t mid_index = *(_index) + half_size; \
267
+ comparison = compare(&((self)->contents[mid_index] suffix), (needle)); \
268
+ if (comparison <= 0) *(_index) = mid_index; \
269
+ size -= half_size; \
270
+ } \
271
+ comparison = compare(&((self)->contents[*(_index)] suffix), (needle)); \
272
+ if (comparison == 0) *(_exists) = true; \
273
+ else if (comparison < 0) *(_index) += 1; \
274
+ } while (0)
275
+
276
+ /// Helper macro for the `_sorted_by` routines below. This takes the left (existing)
277
+ /// parameter by reference in order to work with the generic sorting function above.
278
+ #define _compare_int(a, b) ((int)*(a) - (int)(b))
279
+
280
+ #ifdef _MSC_VER
281
+ #pragma warning(default : 4101)
282
+ #elif defined(__GNUC__) || defined(__clang__)
283
+ #pragma GCC diagnostic pop
284
+ #endif
285
+
286
+ #ifdef __cplusplus
287
+ }
288
+ #endif
289
+
290
+ #endif // TREE_SITTER_ARRAY_H_