-
Notifications
You must be signed in to change notification settings - Fork 0
/
string_encoded_array.h
400 lines (361 loc) · 12.4 KB
/
string_encoded_array.h
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#pragma once
#include <regex>
#include "exception.h"
#include "../../pfc/pfc.h"
#define LIST_START 1
#define LIST_SEP 2
#define LIST_END 3
#define LIST_START_PRETTY 91
#define LIST_SEP_PRETTY 44
#define LIST_END_PRETTY 93
#define DEFAULT_JOIN_DELIM ", "
class string_encoding_exception : public foo_discogs_exception
{};
class string_encoded_array
{
private:
pfc::array_t<string_encoded_array> sub_array;
pfc::string8 value;
bool dirty = false;
size_t m_depth = ~0;
bool branch_execute(bool(string_encoded_array::*)(), size_t depth = 0);
bool branch_execute(bool(string_encoded_array::*)(const string_encoded_array&), const string_encoded_array &other1, size_t depth = 0, size_t other_depth = 0);
bool branch_execute(bool(string_encoded_array::*)(const string_encoded_array&, const string_encoded_array&), const string_encoded_array &other1, const string_encoded_array &other2, size_t depth = 0, size_t other_depth = 0);
bool _join(const string_encoded_array&);
bool _joinnames(const string_encoded_array&);
bool _zip(const string_encoded_array&);
bool _zip2(const string_encoded_array&);
bool _replace(const string_encoded_array&, const string_encoded_array&);
bool _replace_exp(const string_encoded_array&, const string_encoded_array&);
bool _search_exp(const string_encoded_array&);
bool _trim();
bool _wrap(const string_encoded_array&);
bool _pad(const string_encoded_array&, const string_encoded_array&);
bool _pad_right(const string_encoded_array&, const string_encoded_array&);
bool _strcmp(const string_encoded_array&);
bool _istrcmp(const string_encoded_array&);
bool _strstr(const string_encoded_array&);
bool _any();
bool _all();
bool _count();
bool _multi_if(const string_encoded_array&, const string_encoded_array&);
bool _multi_or(const string_encoded_array&);
bool _multi_and(const string_encoded_array&);
bool _multi_not();
bool _extend(const string_encoded_array&);
bool _append(const string_encoded_array&);
bool _contains(const string_encoded_array&);
bool _filter(const string_encoded_array&);
//bool _reduce();
bool _multi_len();
bool _multi_longest(const string_encoded_array&);
bool _multi_cut(const string_encoded_array&);
bool _multi_left(const string_encoded_array&);
bool _multi_right(const string_encoded_array&);
bool _multi_greater(const string_encoded_array&);
bool _multi_longer(const string_encoded_array&);
bool _sum();
bool _multi_add(const string_encoded_array&);
bool _multi_sub(const string_encoded_array&);
bool _multi_mul(const string_encoded_array&);
bool _multi_div(const string_encoded_array&);
bool _multi_divd(const string_encoded_array&);
bool _multi_mod(const string_encoded_array&);
bool _multi_min(const string_encoded_array&);
bool _multi_max(const string_encoded_array&);
bool _multi_round(const string_encoded_array&);
void array_param_too_deep(size_t pos) {
string_encoding_exception ex;
ex << "Array parameter (" << pos << ") is too deep.";
throw ex;
}
void array_param_too_shallow(size_t pos, size_t depth, size_t required) {
string_encoding_exception ex;
ex << "Array parameter (" << pos << ") is too shallow. (" << depth << " need " << required << ")";
throw ex;
}
void array_param_wrong_width(size_t pos, size_t depth, size_t width, size_t required) {
string_encoding_exception ex;
ex << "Array parameter (" << pos << ") wrong width at depth " << depth << ". (" << width << " need " << required << ")";
throw ex;
}
public:
string_encoded_array();
string_encoded_array(const char c);
string_encoded_array(const pfc::string8 &str);
string_encoded_array(const pfc::array_t<pfc::string8> &arr);
string_encoded_array::string_encoded_array(int i) : m_depth(0) {
value << i;
}
string_encoded_array::string_encoded_array(unsigned i) : m_depth(0) {
value << i;
}
string_encoded_array::string_encoded_array(const char *i) : m_depth(0) {
value << i;
}
operator const char* () {
if (dirty) {
encode();
}
return value.get_ptr();
}
operator const char* () const {
PFC_ASSERT(!dirty);
return value.get_ptr();
}
inline pfc::string8 get_value() {
if (dirty) {
encode();
}
return value;
}
inline const pfc::string8& get_cvalue() {
if (dirty) {
encode();
}
return value;
}
/* First newline offset or the length of text */
static int newline_offset(const char* text)
{
const char* newline = strchr(text, '\n');
if (!newline)
return strlen(text);
else
return (int)(newline - text);
}
inline void get_cvalue_lf(pfc::string8& out) {
if (dirty) {
encode();
}
out.set_string(value);
if (!value.is_empty()) {
t_size lfpos = newline_offset(value.toString());
if (lfpos != value.get_length()) {
out.set_string(pfc::lineEndingsToWin(out));
}
}
}
inline const pfc::string8& get_pure_cvalue() const {
PFC_ASSERT(!dirty);
return value;
}
inline string_encoded_array get_item(size_t i) const {
PFC_ASSERT(has_array() && i < sub_array.get_size());
return sub_array[i];
}
inline const string_encoded_array& get_citem(size_t i) const {
PFC_ASSERT(has_array() && i < sub_array.get_size());
return sub_array[i];
}
inline string_encoded_array& get_item_unsafe(size_t i) {
PFC_ASSERT(has_array() && i < sub_array.get_size());
return sub_array[i];
}
inline const pfc::array_t<string_encoded_array>& get_citems() const {
return sub_array;
}
inline int get_numeric_value() const {
if (!has_array()) {
if (!value.get_length()) {
return 0;
}
else if (pfc::string_is_numeric(value)) {
return std::stoi(value.get_ptr());
}
}
string_encoding_exception ex;
ex << "Invalid integer parameter: " << value;
throw ex;
}
inline double get_numeric_double_value() const {
try {
if (!has_array()) {
if (!value.get_length()) {
return 0;
}
else {
return std::stod(value.get_ptr());
}
}
}
catch (const std::invalid_argument&) {
}
string_encoding_exception ex;
ex << "Invalid numeric parameter: " << value;
throw ex;
}
void set_value(const char c);
void set_value(const char *str);
void set_value(const pfc::string8 &str);
void set_value(const pfc::array_t<pfc::string8> &arr);
void append_item(const string_encoded_array &v);
void append_item_val(const string_encoded_array v);
void set_item(size_t i, string_encoded_array &v);
// encodes array into value string
void encode();
inline bool has_array() const {
return m_depth > 0 && m_depth != ~0;
//return sub_array.get_size() != 0;
}
inline size_t get_width() const {
return sub_array.get_size();
}
inline size_t get_depth() const {
return m_depth;
}
inline void force_reset() {
sub_array.force_reset();
value = "";
m_depth = ~0;
dirty = false;
}
// limits depth of array by squashing it with ", " delimiter
bool limit_depth(size_t depth);
// expands depth to fill in empty values
bool expand_depth(size_t depth);
void split(const pfc::string8 &delim = DEFAULT_JOIN_DELIM);
void increase_width(size_t length);
void unique();
void flatten();
void force_array(size_t depth=1);
bool has_diffs(string_encoded_array new_value);
bool has_blank();
inline void join(const pfc::string8 &delim = DEFAULT_JOIN_DELIM) {
branch_execute(&string_encoded_array::_join, delim, 1);
}
inline void join(const string_encoded_array &other) {
branch_execute(&string_encoded_array::_join, other, 1);
}
inline void joinnames(const string_encoded_array &other) {
branch_execute(&string_encoded_array::_joinnames, other, 1);
}
inline void zip(const string_encoded_array &other) {
branch_execute(&string_encoded_array::_zip, other, 0);
}
inline void zip2(const string_encoded_array &other) {
branch_execute(&string_encoded_array::_zip2, other, 0);
}
inline void replace(const string_encoded_array &find, const string_encoded_array &with) {
branch_execute(&string_encoded_array::_replace, find, with, 0);
}
inline void replace_exp(const string_encoded_array& find, const string_encoded_array& with) {
branch_execute(&string_encoded_array::_replace_exp, find, with, 0);
}
inline void search_exp(const string_encoded_array& find) {
branch_execute(&string_encoded_array::_search_exp, find, 0);
}
inline void trim() {
branch_execute(&string_encoded_array::_trim, 0);
}
inline void wrap(const string_encoded_array &other) {
branch_execute(&string_encoded_array::_wrap, other);
}
inline void pad(const string_encoded_array &max_len, const string_encoded_array &padchar = ' ') {
branch_execute(&string_encoded_array::_pad, max_len, padchar);
}
inline void pad_right(const string_encoded_array &max_len, const string_encoded_array &padchar = ' ') {
branch_execute(&string_encoded_array::_pad_right, max_len, padchar);
}
inline void strcmp(const string_encoded_array &other) {
branch_execute(&string_encoded_array::_strcmp, other, 0);
}
inline void istrcmp(const string_encoded_array &other) {
branch_execute(&string_encoded_array::_istrcmp, other, 0);
}
inline void strstr(const string_encoded_array &other) {
branch_execute(&string_encoded_array::_strstr, other, 0);
}
inline void any() {
branch_execute(&string_encoded_array::_any, 1);
}
inline void all() {
branch_execute(&string_encoded_array::_all, 1);
}
inline void count() {
branch_execute(&string_encoded_array::_count, 1);
}
inline void multi_if(const string_encoded_array &yes, const string_encoded_array &no) {
branch_execute(&string_encoded_array::_multi_if, yes, no);
}
inline void multi_or(const string_encoded_array &other) {
branch_execute(&string_encoded_array::_multi_or, other);
}
inline void multi_and(const string_encoded_array &other) {
branch_execute(&string_encoded_array::_multi_and, other);
}
inline void multi_not() {
branch_execute(&string_encoded_array::_multi_not);
}
inline void extend(const string_encoded_array &other) {
branch_execute(&string_encoded_array::_extend, other, 1);
}
inline void append(const string_encoded_array &other) {
branch_execute(&string_encoded_array::_append, other, 1);
}
inline void shallow_extend(const string_encoded_array &other) {
branch_execute(&string_encoded_array::_extend, other, m_depth, m_depth);
}
inline void shallow_append(const string_encoded_array &other) {
branch_execute(&string_encoded_array::_append, other, m_depth, m_depth);
}
inline void contains(const string_encoded_array &other) {
branch_execute(&string_encoded_array::_contains, other, 1, 1);
}
inline void filter(const string_encoded_array &other) {
branch_execute(&string_encoded_array::_filter, other, 1, 1);
}
inline void multi_len() {
branch_execute(&string_encoded_array::_multi_len);
}
inline void multi_longest(const string_encoded_array &other) {
branch_execute(&string_encoded_array::_multi_longest, other);
}
inline void multi_cut(const string_encoded_array &other) {
branch_execute(&string_encoded_array::_multi_cut, other);
}
inline void multi_left(const string_encoded_array &other) {
branch_execute(&string_encoded_array::_multi_left, other);
}
inline void multi_right(const string_encoded_array &other) {
branch_execute(&string_encoded_array::_multi_right, other);
}
inline void multi_greater(const string_encoded_array &other) {
branch_execute(&string_encoded_array::_multi_greater, other);
}
inline void multi_longer(const string_encoded_array &other) {
branch_execute(&string_encoded_array::_multi_longer, other);
}
inline void multi_add(const string_encoded_array &other) {
branch_execute(&string_encoded_array::_multi_add, other);
}
inline void multi_sub(const string_encoded_array &other) {
branch_execute(&string_encoded_array::_multi_sub, other);
}
inline void multi_mul(const string_encoded_array &other) {
branch_execute(&string_encoded_array::_multi_mul, other);
}
inline void multi_div(const string_encoded_array &other) {
branch_execute(&string_encoded_array::_multi_div, other);
}
inline void multi_divd(const string_encoded_array &other) {
branch_execute(&string_encoded_array::_multi_divd, other);
}
inline void multi_mod(const string_encoded_array &other) {
branch_execute(&string_encoded_array::_multi_mod, other);
}
inline void multi_min(const string_encoded_array &other) {
branch_execute(&string_encoded_array::_multi_min, other);
}
inline void multi_max(const string_encoded_array &other) {
branch_execute(&string_encoded_array::_multi_max, other);
}
inline void multi_round(const string_encoded_array &other) {
branch_execute(&string_encoded_array::_multi_round, other);
}
inline void sum() {
branch_execute(&string_encoded_array::_sum, 1);
}
pfc::string8 print() const;
pfc::string8 print_raw() const;
};