Repository navigation
Expand file tree
/
Copy pathspec_sort.cpp
More file actions
346 lines (316 loc) · 9.76 KB
/
Copy pathspec_sort.cpp
File metadata and controls
346 lines (316 loc) · 9.76 KB
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
//+----------------------------------------------------------------------------+
//| Description: Magic Set Editor - Program to make Magic (tm) cards |
//| Copyright: (C) 2001 - 2012 Twan van Laarhoven and Sean Hunt |
//| License: GNU General Public License 2 or later (see file COPYING) |
//+----------------------------------------------------------------------------+
// ----------------------------------------------------------------------------- : Includes
#include <util/prec.hpp>
#include <util/spec_sort.hpp>
#include <util/error.hpp>
using std::reverse;
const Char REMOVED = _('\2');
const Char PLACEHOLDER = _('\3');
String spec_sort(const String& spec, String& input, String& ret);
// ----------------------------------------------------------------------------- : Iterator for reading specs
/// Iterator over a sort specification (for spec_sort)
class SpecIterator {
public:
SpecIterator(const String& spec, size_t pos = 0)
: spec(spec), pos(pos)
{}
Char value; ///< Current character
bool escaped; ///< Was the current character escaped?
bool preceded_by_space; ///< Was there a ' ' before this character?
/// Move to the next item in the specification.
/** returns false if we are at the end or encounter close.
*/
bool nextUntil(Char close, bool skip_space = true) {
if (pos >= spec.size()) {
value = 0;
if (close == 0) {
return false;
} else {
throw ParseError(String::Format(_("Expected '%c' in sort_rule specification"),close));
}
}
value = spec.GetChar(pos++);
preceded_by_space = false;
// skip whitespace
if (skip_space) {
while (value == _(' ')) {
if (pos >= spec.size()) {
if (close == 0) {
return false;
} else {
throw ParseError(String::Format(_("Expected '%c' in sort_rule specification"),close));
}
}
value = spec.GetChar(pos++);
preceded_by_space = true;
}
}
// escape?
if (value == _('\\')) {
escaped = true;
if (pos >= spec.size()) {
throw ParseError(String::Format(_("Expected '%c' in sort_rule specification"),close));
}
value = spec.GetChar(pos++);
} else {
escaped = false;
if (value == close) return false;
}
return true;
}
/// Read a whole parameter, terminated by close
String readParam(Char close, bool skip_space = true) {
String ret;
while(nextUntil(close)) ret += value;
return ret;
}
/// Read a parameter, matches nested parentheses, keeps escape sequences
String readRawParam(Char close1, Char close2 = 0) {
String ret;
int parens = 0;
while (nextUntil(0, false)) {
if (escaped) {
ret += _('\\');
} else {
if (parens == 0 && (value == close1 || value == close2)) break;
if (value == _('(')) parens++;
else if (value == _(')')) parens--;
}
ret += value;
}
return ret;
}
/// Does the current position match a keyword? If so, skip it
bool keyword(const Char* kw) {
if (value == kw[0]) {
if (is_substr(spec, pos, kw + 1)) {
pos += wxStrlen(kw + 1);
return true;
}
}
return false;
}
private:
const String& spec;
size_t pos;
};
// ----------------------------------------------------------------------------- : Sort functions
/// Sort a string using a specification using the shortest cycle method, see spec_sort
/** Removed used characters from input! */
void cycle_sort(const String& spec, String& input, String& ret) {
size_t size = spec.size();
vector<UInt> counts;
// count occurences of each char in spec
for(const auto& s : spec) {
UInt c = 0;
for(auto& i : input) {
if (s == i) {
i = REMOVED; // remove
c++;
}
}
counts.push_back(c);
}
// determine best start point
size_t best_start = 0;
UInt best_start_score = 0xffffffff;
for (size_t start = 0 ; start < size ; ++start) {
// score of a start position, can be considered as:
// - count saturated to binary
// - rotated left by start
// - interpreted as a binary number, but without trailing 0s
UInt score = 0, mul = 1;
for (size_t i = 0 ; i < size ; ++i) {
mul *= 2;
if (counts[(start + i) % size]) {
score = score * mul + 1;
mul = 1;
}
}
if (score < best_start_score) {
best_start_score = score;
best_start = start;
}
}
// return string
for (size_t i = 0 ; i < size ; ++i) {
size_t pos = (best_start + i) % size;
ret.append(counts[pos], spec[pos]);
}
}
/// Sort a string, keeping the characters in the original order
/** Removed used characters from input! */
void mixed_sort(const String& spec, String& input, String& ret) {
for(auto& c : input) {
if (spec.find(c) != String::npos) {
ret += c;
c = REMOVED;
}
}
}
/// Sort a string, find a compound item
/** Removed used characters from input! */
void compound_sort(const String& spec, String& input, String& ret) {
size_t pos = input.find(spec);
while (pos != String::npos) {
ret += spec;
for (size_t j = 0 ; j < spec.size() ; ++j) input.SetChar(pos + j, REMOVED);
pos = input.find(spec, pos + 1);
}
}
/// Sort things matching a pattern
void pattern_sort(const String& pattern, const String& spec, String& input, String& ret) {
if (pattern.size() > input.size()) return;
size_t end = input.size() - pattern.size() + 1;
for (size_t pos = 0 ; pos < end ; ++pos) {
// does the pattern match here?
String placeholders;
bool match = true;
for (size_t j = 0 ; j < pattern.size() ; ++j) {
Char c = input.GetChar(pos + j);
Char p = pattern.GetChar(j);
if (c == REMOVED) { match = false; break; }
else if (p == PLACEHOLDER) {
placeholders += c;
} else if (c != p) { match = false; break; }
}
// do we have a match?
if (match) {
// sort placeholders
String new_placeholders = spec_sort(spec, placeholders);
if (new_placeholders.size() == placeholders.size()) {
// add to output, erase from input
size_t ph = 0;
for (size_t j = 0 ; j < pattern.size() ; ++j) {
Char p = pattern.GetChar(j);
if (p == PLACEHOLDER) {
ret += new_placeholders.GetChar(ph++);
} else {
ret += p;
}
input.SetChar(pos + j, REMOVED);
}
// erase from input
pos += pattern.size() - 1;
}
}
}
}
/// Sort things in place, keep the rest of the input
void in_place_sort(const String& spec, String& input, String& ret) {
String result;
spec_sort(spec, input, result);
// restore into the same order as in 'input'
size_t pos_r = 0;
for(auto& c : input) {
if (c == REMOVED) {
if (pos_r < result.size()) {
ret += result.GetChar(pos_r++);
}
} else {
ret += c;
}
}
input.clear(); // we ate all the input
}
// ----------------------------------------------------------------------------- : spec_sort
String spec_sort(const String& spec, String& input, String& ret) {
SpecIterator it(spec);
while(it.nextUntil(0)) {
if (it.escaped) { // single character, escaped
for(auto& d : input) {
if (d == it.value) {
ret += d;
d = REMOVED;
}
}
} else if (it.value == _('<')) { // keep only a single copy
while (it.nextUntil(_('>'))) {
size_t pos = input.find_first_of(it.value);
if (pos != String::npos) {
input.SetChar(pos, REMOVED);
ret += it.value; // input contains it.value
}
}
} else if (it.keyword(_("once("))) {
while (it.nextUntil(_(')'))) {
size_t pos = input.find_first_of(it.value);
if (pos != String::npos) {
input.SetChar(pos, REMOVED);
ret += it.value; // input contains it.value
}
}
} else if (it.value == _('[')) { // in input order
mixed_sort(it.readParam(_(']')), input, ret);
} else if (it.keyword(_("mixed("))) {
mixed_sort(it.readParam(_(')')), input, ret);
} else if (it.keyword(_("cycle("))) {
cycle_sort(it.readParam(_(')')), input, ret);
} else if (it.value == _('(')) {
cycle_sort(it.readParam(_(')')), input, ret);
} else if (it.keyword(_("compound("))) { // compound item
compound_sort(it.readParam(_(')')), input, ret);
} else if (it.keyword(_("pattern("))) { // recurse with pattern
String pattern;
// read pattern
while (it.nextUntil(_(' '), false)) {
if (it.value == _('.') && !it.escaped) {
it.value = PLACEHOLDER;
}
pattern += it.value;
}
// read spec to apply to pattern
String sub_spec = it.readRawParam(_(')'));
// sort
pattern_sort(pattern, sub_spec, input, ret);
} else if (it.keyword(_("in_place("))) { // recurse without pattern
// read spec to apply to pattern
String sub_spec = it.readRawParam(_(')'));
in_place_sort(sub_spec, input, ret);
} else if (it.keyword(_("any()"))) { // remaining input
for(auto& d : input) {
if (d != REMOVED) {
ret += d;
d = REMOVED;
}
}
} else if (it.keyword(_("reverse_order("))) { // reverse order of preference
size_t old_ret_size = ret.size();
while (it.value != _(')')) {
size_t before_ret_size = ret.size();
String sub_spec = it.readRawParam(_(')'),_(' '));
spec_sort(sub_spec, input, ret);
// reverse this item
reverse(ret.begin() + before_ret_size, ret.end());
}
// re-reverse all items
reverse(ret.begin() + old_ret_size, ret.end());
} else if (it.keyword(_("ordered("))) { // in spec order
while (it.nextUntil(_(')'))) {
for(auto& d : input) {
if (d == it.value) {
ret += d;
d = REMOVED;
}
}
}
} else { // single char
for(auto& d : input) {
if (d == it.value) {
ret += d;
d = REMOVED;
}
}
}
}
return ret;
}
String spec_sort(const String& spec, String input) {
String ret;
spec_sort(spec, input, ret);
return ret;
}