Repository navigation
Expand file tree
/
Copy pathtagged_string.cpp
More file actions
655 lines (605 loc) · 19.6 KB
/
Copy pathtagged_string.cpp
File metadata and controls
655 lines (605 loc) · 19.6 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
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
//+----------------------------------------------------------------------------+
//| 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/tagged_string.hpp>
#include <stack>
using std::max;
using std::min;
using std::stack;
// ----------------------------------------------------------------------------- : Conversion to/from normal string
Char untag_char(Char c) {
if (c == _('\1')) return _('<');
else if (c == CONNECTION_SPACE) return _(' ');
else return c;
}
Char tag_char(Char c) {
if (c == _('<')) return _('\1');
else return c;
}
String untag(const String& str) {
bool intag = false;
String ret; ret.reserve(str.size());
for(const auto& c : str) {
if (c==_('<')) intag = true;
if (!intag) ret += untag_char(c);
if (c==_('>')) intag = false;
}
return ret;
}
String untag_no_escape(const String& str) {
bool intag = false;
String ret; ret.reserve(str.size());
for(const auto& c : str) {
if (c==_('<')) intag = true;
if (!intag) ret += c;
if (c==_('>')) intag = false;
}
return ret;
}
String untag_hide_sep(const String& str) {
return untag(remove_tag_contents(str,_("<sep-soft")));
}
String escape(const String& str) {
String ret; ret.reserve(str.size());
for(const auto& c : str) {
ret += tag_char(c);
}
return ret;
}
String fix_old_tags(const String& str) {
String ret; ret.reserve(str.size());
stack<String> tags;
bool intag = false;
// invariant : intag => !tags.empty()
for (size_t i = 0 ; i < str.size() ; ++i) {
Char c = str.GetChar(i);
if (is_substr(str, i, _("</>"))) {
i += 2;
// old style close tag, replace by the correct tag type
if (!tags.empty()) {
// need a close tag?
if (!tags.top().empty()) {
ret += _("</") + tags.top() + _(">");
}
tags.pop();
}
intag = false;
} else {
ret += c;
if (c==_('<')) {
intag = true;
tags.push(wxEmptyString);
} else if (c==_('>') && intag) {
intag = false;
if (!starts_with(tags.top(), _("kw")) && !starts_with(tags.top(), _("atom"))) {
// only keep keyword related stuff
ret.resize(ret.size() - tags.top().size() - 2); // remove from output
tags.top() = wxEmptyString;
}
} else if (intag) {
tags.top() += c;
}
}
}
return ret;
}
// ----------------------------------------------------------------------------- : Finding tags
size_t tag_start(const String& str, size_t pos) {
size_t start = str.find_last_of(_('<'), pos);
if (start == String::npos) return String::npos;
size_t end = skip_tag(str, start);
if (end <= pos) return String::npos;
return start;
}
size_t skip_tag(const String& str, size_t start) {
if (start >= str.size()) return String::npos;
size_t end = str.find_first_of(_('>'), start);
return end == String::npos ? String::npos : end + 1;
}
size_t match_close_tag(const String& str, size_t start) {
String tag = tag_type_at(str, start);
String ctag = _("/") + tag;
size_t size = str.size();
int taglevel = 1;
for (size_t pos = start + tag.size() + 2 ; pos < size ; ++pos) {
Char c = str.GetChar(pos);
if (c == _('<')) {
if (is_substr(str, pos + 1, tag)) {
++taglevel;
pos += tag.size() + 1;
} else if (is_substr(str, pos + 1, ctag)) {
--taglevel; // close tag
if (taglevel == 0) return pos;
pos += ctag.size() + 1;
}
}
}
return String::npos;
}
size_t match_close_tag_end(const String& str, size_t start) {
return skip_tag(str, match_close_tag(str, start));
}
size_t last_start_tag_before(const String& str, const String& tag, size_t start) {
start = min(str.size(), start);
for (size_t pos = start ; pos > 0 ; --pos) {
if (is_substr(str, pos - 1, tag)) {
return pos - 1;
}
}
return String::npos;
}
size_t in_tag(const String& str, const String& tag, size_t start, size_t end) {
size_t last_start = String::npos;
size_t size = str.size();
int taglevel = 0;
end = min(end,size);
for (size_t pos = 0 ; pos < end ; ) {
Char c = str.GetChar(pos);
if (c == _('<')) {
if (is_substr(str, pos + 1, static_cast<const Char*>(tag.c_str())+1)) {
if (pos < start) last_start = pos;
++taglevel;
} else if (pos + 2 < size && str.GetChar(pos+1) == _('/') && is_substr(str, pos + 2, static_cast<const Char*>(tag.c_str())+1)) {
--taglevel; // close tag
}
pos = skip_tag(str,pos);
} else {
pos++;
}
if (pos >= start && taglevel < 1) {
// not inside tag anymore
return String::npos;
}
}
return taglevel < 1 ? String::npos : last_start;
}
bool is_in_tag(const String& str, const String& tag, size_t start, size_t end) {
return in_tag(str,tag,start,end) != String::npos;
}
String tag_at(const String& str, size_t pos) {
size_t end = str.find_first_of(_(">"), pos);
if (end == String::npos) return wxEmptyString;
return str.substr(pos + 1, end - pos - 1);
}
String tag_type_at(const String& str, size_t pos) {
size_t end = str.find_first_of(_(">-"), pos);
if (end == String::npos) return wxEmptyString;
return str.substr(pos + 1, end - pos - 1);
}
String close_tag(const String& tag) {
if (tag.size() < 1) return _("</>");
else return _("</") + tag.substr(1);
}
String anti_tag(const String& tag) {
if (!tag.empty() && tag.GetChar(0) == _('/')) return _("<") + tag.substr(1) + _(">");
else return _("</") + tag + _(">");
}
// ----------------------------------------------------------------------------- : Cursor position
size_t index_to_cursor(const String& str, size_t index, Movement dir) {
size_t cursor = 0;
index = min(index, str.size());
// find the range [start...end) with the same cursor value, that contains index
// after the loop, 'cursor' corresponds to the index i/end
for (size_t i = 0 ; i < str.size() ;) {
Char c = str.GetChar(i);
bool has_width = true;
if (c == _('<')) {
// a tag
if (is_substr(str, i, _("<atom")) || is_substr(str, i, _("<sep"))) {
// skip tag contents, tag counts as a single 'character'
size_t before = i;
size_t close = match_close_tag(str, i);
size_t after = skip_tag(str, close);
if (index > before && index < after) {
// Index is inside an atom, determine on which side we want the cursor
// This is the only place where MOVE_LEFT/RIGHT and MOVE_*_OPT differ
// for the OPT version we must check if we are actually past any real characters
// but, if the atom is empty, it still counts as a single character!
if (dir == MOVE_LEFT) {
return cursor;
} else if (dir == MOVE_RIGHT) {
return cursor + 1;
} else if (dir == MOVE_LEFT_OPT) {
// is there any non-tag after index?
bool empty = true;
while (i < close) {
c = str.GetChar(i);
if (c == _('<')) {
i = skip_tag(str, i);
} else if (i >= index) {
return cursor; // this is a non-tag character after index
} else {
empty = false;
++i;
}
}
return empty ? cursor : cursor + 1; // still didn't pass any
} else if (dir == MOVE_RIGHT_OPT) {
// is index actually past any non-tag?
while (i < close) {
if (i >= index) {
return cursor; // we didn't pass any non-tag stuff
}
c = str.GetChar(i);
if (c != _('<')) break;
i = skip_tag(str, i);
}
return cursor + 1; // yes it is
} else if (dir == MOVE_MID) {
// count number of actual characters before/after
int before_c = 0;
int after_c = 0;
while (i < close) {
c = str.GetChar(i);
if (c == _('<')) {
i = skip_tag(str, i);
} else {
if (i < index) before_c++;
else after_c++;
++i;
}
}
// take the closest side
return before_c <= after_c ? cursor : cursor + 1;
}
}
i = after;
} else if (i == 0 && is_substr(str, i, _("<prefix"))) {
// prefix at start of string, skip contents
i = match_close_tag_end(str, i);
has_width = false;
} else if (is_substr(str, i, _("<suffix")) && match_close_tag_end(str,i) >= str.size()) {
// suffix at end of string
break;
} else {
i = skip_tag(str, i);
has_width = false;
}
} else {
i++;
}
if (i > index) break;
if (has_width) {
cursor++;
}
}
return cursor;
}
void cursor_to_index_range(const String& str, size_t cursor, size_t& start, size_t& end) {
start = end = 0;
size_t cur = 0;
size_t i = 0;
size_t size = str.size(); // can be changed by <suffix> tags
while (cur <= cursor && i < size) {
Char c = str.GetChar(i);
bool has_width = true;
if (c == _('<')) {
// a tag
if (is_substr(str, i, _("<atom")) || is_substr(str, i, _("<sep"))) {
// never move the end over an atom/sep
if (cur >= cursor) { ++i; break; }
// skip tag contents, tag counts as a single 'character'
i = match_close_tag_end(str, i);
} else if (i == 0 && is_substr(str, i, _("<prefix"))) {
// prefix at start of string, skip contents, index never before
start = i = match_close_tag_end(str,i);
has_width = false;
} else if (is_substr(str, i, _("<suffix")) && match_close_tag_end(str,i) >= str.size()) {
// suffix at start of string, skip contents
size = i;
has_width = false;
} else {
i = skip_tag(str, i);
has_width = false;
}
} else {
i++;
}
if (has_width) {
cur++;
if (cur == cursor) start = i;
}
}
if (cur < cursor) {
start = end = size;
} else {
end = min(i, size);
}
end = max(end, start + 1); // always start < end, since there are always valid cursor positions
}
size_t cursor_to_index(const String& str, size_t cursor, Movement dir) {
size_t start, end;
cursor_to_index_range(str, cursor, start, end);
if (dir == MOVE_MID) {
// find the middle between start and end
// if the string in between contains a pair "<tag></tag>" or "</tag><tag>" returns the middle
// otherwise returns start
for (size_t i = start ; i < end ; ) {
if (str.GetChar(i) == _('<')) {
String tag1 = tag_at(str, i);
i = skip_tag(str, i);
if (str.GetChar(i) == _('<')) {
String tag2 = tag_at(str, i);
if (_("<") + tag2 + _(">") == anti_tag(tag1)) {
return i;
}
}
if (starts_with(tag1, _("/sym"))) {
// we like to be inside <b> and <i> tags, but outside <sym> tags
start = i;
}
} else {
i++;
}
}
}
// This allows formating to be enabled without a selection
return dir <= 0 /*MOVE_LEFT*/ ? start : end - 1;
}
String untag_for_cursor(const String& str) {
String ret; ret.reserve(str.size());
for (size_t i = 0 ; i < str.size() ; ) {
Char c = str.GetChar(i);
if (c == _('<')) {
if (is_substr(str, i, _("<atom-kwpph"))) {
i = match_close_tag_end(str, i);
ret += UNTAG_ATOM_KWPPH;
} else if (is_substr(str, i, _("<atom"))) {
i = match_close_tag_end(str, i);
ret += UNTAG_ATOM;
} else if (is_substr(str, i, _("<sep"))) {
i = match_close_tag_end(str, i);
ret += UNTAG_SEP;
} else if (i == 0 && is_substr(str, i, _("<prefix"))) {
// prefix at start of string, skip contents, index never before
i = match_close_tag_end(str,i);
} else if (is_substr(str, i, _("<suffix")) && match_close_tag_end(str,i) >= str.size()) {
// suffix at start of string, skip contents
i = str.size();
} else {
i = skip_tag(str, i);
}
} else {
ret += c;
++i;
}
}
return ret;
}
// ----------------------------------------------------------------------------- : Untagged position
size_t untagged_to_index(const String& str, size_t pos, bool inside, size_t start_index) {
size_t i = start_index, p = 0;
while (i < str.size()) {
Char c = str.GetChar(i);
if (c == _('<')) {
bool is_close = is_substr(str, i, _("</"));
if (p == pos && is_close == inside) break;
i = skip_tag(str, i);
} else {
if (p == pos) break;
i++;
p++;
}
}
return i;
}
size_t index_to_untagged(const String& str, size_t index) {
size_t i = 0, p = 0;
index = min(str.size(), index);
while (i < index) {
Char c = str.GetChar(i);
if (c == _('<')) {
i = skip_tag(str, i);
} else {
i++;
p++;
}
}
return p;
}
// ----------------------------------------------------------------------------- : Global operations
String remove_tag(const String& str, const String& tag) {
if (tag.size() < 1) return str;
String ctag = close_tag(tag);
return remove_tag_exact(remove_tag_exact(str, tag), ctag);
}
String remove_tag_exact(const String& str, const String& tag) {
size_t start = 0, pos = str.find(tag);
if (pos == String::npos) return str; // no need to copy
String ret; ret.reserve(str.size());
while (pos != String::npos) {
ret += str.substr(start, pos - start); // before
// next
start = skip_tag(str, pos);
if (start > str.size()) break;
pos = str.find(tag, start);
}
ret += str.substr(start);
return ret;
}
String remove_tag_contents(const String& str, const String& tag) {
size_t start = 0, pos = str.find(tag);
if (pos == String::npos) return str; // no need to copy
String ret; ret.reserve(str.size());
while (pos != String::npos) {
size_t end = match_close_tag(str, pos);
if (end == String::npos) return ret; // missing close tag
ret += str.substr(start, pos - start);
// next
start = skip_tag(str, end);
if (start > str.size()) break;
pos = str.find(tag, start);
}
ret += str.substr(start);
return ret;
}
// ----------------------------------------------------------------------------- : Updates
String get_tags(const String& str, size_t start, size_t end, bool open_tags, bool close_tags) {
String ret;
bool intag = false;
bool keeptag = false;
for (size_t i = start ; i < end ; ++i) {
Char c = str.GetChar(i);
if (c == _('<') && !intag) {
intag = true;
// is this tag an open tag?
if (i + 1 < end && (str.GetChar(i + 1) == _('/') ? close_tags : open_tags)) {
keeptag = true;
}
}
if (intag && keeptag) ret += c;
if (c == _('>')) {
intag = false;
keeptag = false;
}
}
return ret;
}
String tagged_substr_replace(const String& input, size_t start, size_t end, const String& replacement) {
assert(start <= end);
size_t size = input.size();
String ret; ret.reserve(size + replacement.size() - (end - start)); // estimated size
String collect_tags = simplify_tagged_merge(get_tags(input, start, end, true, true),true);
return simplify_tagged(
substr_replace(input, start, end,
get_tags(collect_tags, 0, collect_tags.size(), false, true) + // close tags
replacement +
get_tags(collect_tags, 0, collect_tags.size(), true, false) // open tags
));
}
// ----------------------------------------------------------------------------- : Simplification
String simplify_tagged(const String& str) {
return simplify_tagged_overlap(simplify_tagged_merge(str));
}
// Add a tag to a stack of tags, try to cancel it out
// If </tag> is in stack remove it and returns true
// otherwise appends <tag> and returns fales
// (where </tag> is the negation of tag)
bool add_or_cancel_tag(const String& tag, String& stack, bool all = false) {
if (all || starts_with(tag, _("/")) ||
starts_with(tag, _("b")) || starts_with(tag, _("i")) || starts_with(tag, _("sym"))) {
// cancel out all close tags, but not all open tags,
// so <xx></xx> is always removed
// but </xx><xx> is not
String anti = anti_tag(tag);
size_t pos = stack.find(anti);
if (pos == String::npos) {
stack += _("<") + tag + _(">");
return false;
} else {
// cancel out with anti tag
stack = stack.substr(0, pos) + stack.substr(pos + anti.size());
return true;
}
} else {
stack += _("<") + tag + _(">");
return false;
}
}
String simplify_tagged_merge(const String& str, bool all) {
String ret; ret.reserve(str.size());
String waiting_tags; // tags that are waiting to be written to the output
size_t size = str.size();
for (size_t i = 0 ; i < size ; ++i) {
Char c = str.GetChar(i);
if (c == _('<')) {
String tag = tag_at(str, i);
add_or_cancel_tag(tag, waiting_tags, all);
i += tag.size() + 1;
} else {
ret += waiting_tags;
waiting_tags.clear();
ret += c;
}
}
return ret + waiting_tags;
}
String simplify_tagged_overlap(const String& str) {
String ret; ret.reserve(str.size());
String open_tags; // tags we are in
size_t size = str.size();
for (size_t i = 0 ; i < size ; ++i) {
Char c = str.GetChar(i);
if (c == _('<')) {
String tag = tag_at(str, i);
if (starts_with(tag, _("b")) || starts_with(tag, _("i")) || starts_with(tag, _("sym")) ||
starts_with(tag, _("/b")) || starts_with(tag, _("/i")) || starts_with(tag, _("/sym"))) {
// optimize this tag
if (open_tags.find(_("<") + tag + _(">")) == String::npos) {
// we are not already inside this tag
add_or_cancel_tag(tag, open_tags, true);
if (open_tags.find(anti_tag(tag)) != String::npos) {
// still not canceled out
i += tag.size() + 1;
continue;
}
} else {
// skip this tag, doubling it has no effect
i += tag.size() + 1;
add_or_cancel_tag(tag, open_tags, true);
continue;
}
}
}
ret += c;
}
return ret;
}
// ----------------------------------------------------------------------------- : Verification
void check_tagged(const String& str, bool check_balance) {
for (size_t i = 0 ; i < str.size() ; ) {
if (str.GetChar(i) == _('<')) {
size_t end = skip_tag(str,i);
if (end == String::npos) {
queue_message(MESSAGE_WARNING, _("Invalid tagged string: missing '>'"));
}
for (size_t j = i + 1 ; j + 1 < end ; ++j) {
Char c = str.GetChar(j);
if (c == _(' ') || c == _('<')) {
queue_message(MESSAGE_WARNING, _("Invalid character in tag"));
}
}
if (check_balance) {
if (str.GetChar(i+1) == _('/')) {
// close tag, don't check
} else if (is_substr(str,i,_("<hint"))) {
// no close tag for <hint> tags
} else {
size_t close = match_close_tag(str,i);
if (close == String::npos) {
queue_message(MESSAGE_WARNING, _("Invalid tagged string: missing close tag for <") + tag_at(str,i) + _(">"));
}
}
}
i = end;
} else {
++i;
}
}
}
// ----------------------------------------------------------------------------- : Other utilities
bool is_space_like(Char c) {
return isSpace(c) || c == _('(') || c == _('[') || c == _('{') || c == EN_DASH || c == EM_DASH;
}
String curly_quotes(String str, bool curl) {
bool open = true, in_tag = false;
for(auto& c : str) {
if (c == _('\'') || c == LEFT_SINGLE_QUOTE || c == RIGHT_SINGLE_QUOTE) {
c = curl ? (open ? LEFT_SINGLE_QUOTE : RIGHT_SINGLE_QUOTE) : _('\'');
} else if (c == _('\"') || c == LEFT_DOUBLE_QUOTE || c == RIGHT_DOUBLE_QUOTE) {
c = curl ? (open ? LEFT_DOUBLE_QUOTE : RIGHT_DOUBLE_QUOTE) : _('\"');
} else if (c == _('<')) {
in_tag = true;
} else if (c == _('>')) {
in_tag = false;
} else if (!in_tag) {
// Also allow double-nesting of quotes
open = is_space_like(c);
}
}
return str;
}