forked from bminor/bash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray.hh
293 lines (222 loc) · 6.12 KB
/
array.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/* array.h -- definitions for the interface exported by array.c that allows
the rest of the shell to manipulate array variables. */
/* Copyright (C) 1997-2020 Free Software Foundation, Inc.
This file is part of GNU Bash, the Bourne Again SHell.
Bash is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Bash is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Bash. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _ARRAY_H_
#define _ARRAY_H_
#include "bashtypes.hh"
#include "command.hh"
namespace bash
{
// Type of array index.
typedef int64_t arrayind_t;
struct ARRAY_ELEMENT
{
// Construct a new array element with index and value.
ARRAY_ELEMENT (arrayind_t ind_, const std::string &value_ = std::string ())
: ind (ind_), value (value_)
{
}
arrayind_t ind;
std::string value;
#ifndef ALT_ARRAY_IMPLEMENTATION
ARRAY_ELEMENT *next, *prev;
#endif
};
typedef int (*sh_ae_map_func_t) (ARRAY_ELEMENT *, void *);
/* Flags for array_shift */
#define AS_DISPOSE 0x01
struct ARRAY
{
#ifndef ALT_ARRAY_IMPLEMENTATION
ARRAY () : max_index (-1)
{
head = new ARRAY_ELEMENT (-1); /* dummy head */
head->prev = head->next = head;
}
~ARRAY ()
{
flush ();
delete head;
}
// Clone the array elements.
ARRAY (const ARRAY &);
arrayind_t
first_index ()
{
return head->next->ind;
}
// Walk the array, calling FUNC once for each element, with the array
// element and user data pointer as the arguments.
void
walk (sh_ae_map_func_t func, void *udata)
{
if (empty ())
return;
for (ARRAY_ELEMENT *ae = head->next; ae != head; ae = ae->next)
{
if ((*func) (ae, udata) < 0)
return;
}
}
ARRAY *slice (ARRAY_ELEMENT *, ARRAY_ELEMENT *);
ARRAY_ELEMENT *shift (size_t, int);
ARRAY_ELEMENT *
unshift_element ()
{
return shift (1, 0);
}
size_t
shift_element (string_view v)
{
return rshift (1, v);
}
void
push (string_view v)
{
(void)rshift (1, v);
}
void
pop ()
{
(void)shift (1, AS_DISPOSE);
}
#else // ALT_ARRAY_IMPLEMENTATION
ARRAY () : max_index (-1), num_elements (0), first_index_ (-1) {}
~ARRAY ()
{
std::vector<ARRAY_ELEMENT *>::iterator it;
for (it = elements.begin (); it != elements.end (); ++it)
delete *it;
}
ARRAY (const ARRAY &);
arrayind_t
first_index ()
{
return first_index_;
}
void
resize (size_t n)
{
if (elements.size () > 0 && n >= static_cast<size_t> (max_index)
&& n <= elements.size ())
return;
elements.resize (n);
}
void expand (size_t);
ARRAY *slice (arrayind_t, arrayind_t);
// Walk the array, calling FUNC once for each element, with the array
// element and user data pointer as the arguments.
void
walk (sh_ae_map_func_t func, void *udata)
{
size_t i;
ARRAY_ELEMENT *ae;
if (empty ())
return;
for (i = static_cast<size_t> (first_index_);
i <= static_cast<size_t> (max_index); i++)
{
if ((ae = elements[i]) == nullptr)
continue;
if ((*func) (ae, udata) < 0)
return;
}
}
ARRAY_ELEMENT **shift (size_t, int);
ARRAY_ELEMENT *
unshift_element ()
{
ARRAY_ELEMENT **r, *ret;
r = shift (1, 0);
ret = r[0];
delete[] r;
return ret;
}
size_t
shift_element (string_view v)
{
return rshift (1, v);
}
// Return the next non-null array element after A[IND]
arrayind_t
element_forw (arrayind_t ind)
{
size_t i;
for (i = static_cast<size_t> (ind + 1);
i <= static_cast<size_t> (max_index); i++)
if (elements[i])
break;
if (elements[i])
return static_cast<arrayind_t> (i);
return max_index;
}
// Return the previous non-null array element before A[IND]
arrayind_t
element_back (arrayind_t ind)
{
size_t i;
for (i = static_cast<size_t> (ind - 1);
i >= static_cast<size_t> (first_index_); i--)
if (elements[i])
break;
if (elements[i] && i >= static_cast<size_t> (first_index_))
return static_cast<arrayind_t> (i);
return first_index_;
}
#endif
void flush ();
bool
empty ()
{
return num_elements == 0;
}
size_t rshift (size_t, string_view);
std::string *reference (arrayind_t);
int insert (arrayind_t, string_view);
ARRAY_ELEMENT *remove (arrayind_t);
arrayind_t max_index;
size_t num_elements;
#ifdef ALT_ARRAY_IMPLEMENTATION
arrayind_t first_index_;
std::vector<ARRAY_ELEMENT *> elements;
#else
ARRAY_ELEMENT *head;
ARRAY_ELEMENT *lastref;
#endif
};
#define ARRAY_DEFAULT_SIZE 1024
/* Converting to and from arrays */
WORD_LIST *array_to_word_list (ARRAY *);
ARRAY *array_from_word_list (WORD_LIST *);
WORD_LIST *array_keys_to_word_list (ARRAY *);
WORD_LIST *array_to_kvpair_list (ARRAY *);
ARRAY *array_assign_list (ARRAY *, WORD_LIST *);
char **array_to_argv (ARRAY *, int *);
ARRAY *array_from_argv (ARRAY *, char **, int);
std::string array_to_kvpair (ARRAY *, bool);
std::string array_to_assign (ARRAY *, bool);
std::string array_to_string (ARRAY *, string_view, bool);
ARRAY *array_from_string (char *, char *);
/* Convenience */
#define GET_ARRAY_FROM_VAR(n, v, a) \
do \
{ \
(v) = find_variable (n); \
(a) = ((v) && ((v)->array ())) ? (v)->array_value () : nullptr; \
} \
while (0)
#define ALL_ELEMENT_SUB(c) ((c) == '@' || (c) == '*')
} // namespace bash
#endif /* _ARRAY_H_ */