-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathpath.cpp
More file actions
1301 lines (1119 loc) · 31.8 KB
/
path.cpp
File metadata and controls
1301 lines (1119 loc) · 31.8 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
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "common/path.h"
#include "common/hash-str.h"
#include "common/list.h"
#include "common/punycode.h"
namespace Common {
#ifndef RELEASE_BUILD
bool Path::_shownSeparatorCollisionWarning = false;
#endif
inline void Path::escape(String &dst, char srcSeparator, const char *begin, const char *end) {
if (!end) {
end = begin + strlen(begin);
}
const char *nextEsc = strchr(begin, ESCAPE);
const char *nextEscSep, *nextSrcSep;
if (srcSeparator == SEPARATOR) {
// The source directory separator is ours, no need to escape it
nextEscSep = nullptr;
} else {
nextEscSep = strchr(begin, SEPARATOR);
}
if (!srcSeparator || srcSeparator == SEPARATOR) {
// There is no source directory separator, every SEPARATOR will get escaped
// Source directory separator is already a SEPARATOR, nothing to do
nextSrcSep = nullptr;
} else {
nextSrcSep = strchr(begin, srcSeparator);
}
while (true) {
const char *next = nullptr;
if (nextEsc && nextEsc < end && (!next || nextEsc < next)) {
next = nextEsc;
}
if (nextEscSep && nextEscSep < end && (!next || nextEscSep < next)) {
next = nextEscSep;
}
if (nextSrcSep && nextSrcSep < end && (!next || nextSrcSep < next)) {
next = nextSrcSep;
}
if (!next) {
break;
}
// Append all content up to what we found first, excluded
dst.append(begin, next);
// We will look after the current found character
begin = next + 1;
// Do the escaping
if (next == nextEsc) {
// We found an ESCAPE
dst += ESCAPE;
dst += ESCAPED_ESCAPE;
nextEsc = strchr(begin, ESCAPE);
} else if (next == nextEscSep) {
// We found a SEPARATOR
dst += ESCAPE;
dst += ESCAPED_SEPARATOR;
nextEscSep = strchr(begin, SEPARATOR);
} else {
// We found a srcSeparator
dst += SEPARATOR;
nextSrcSep = strchr(begin, srcSeparator);
}
}
// Append anything after the last escape up to end
dst.append(begin, end);
}
inline String Path::unescape(char dstSeparator, const char *begin, const char *end) {
String dst;
if (!end) {
end = begin + strlen(begin);
}
#ifndef RELEASE_BUILD
if (!_shownSeparatorCollisionWarning &&
dstSeparator &&
dstSeparator != ESCAPE && dstSeparator != SEPARATOR) {
// ESCAPE and SEPARATOR cases are handled when unescaping
const char *dstSep = strchr(begin, dstSeparator);
if (dstSep && dstSep < end) {
warning("Collision in path \"%s\" with separator %c", begin, dstSeparator);
_shownSeparatorCollisionWarning = true;
}
}
#endif
const char *nextEsc = strchr(begin, ESCAPE);
const char *nextSep;
if (dstSeparator == SEPARATOR) {
// dstSeparator is SEPARATOR so there is no substitution to do
nextSep = nullptr;
} else {
nextSep = strchr(begin, SEPARATOR);
}
while (true) {
const char *next = nullptr;
if (nextEsc && nextEsc < end && (!next || nextEsc < next)) {
next = nextEsc;
}
if (nextSep && nextSep < end && (!next || nextSep < next)) {
next = nextSep;
}
if (!next) {
break;
}
// Append all content up to character excluded
dst.append(begin, next);
if (next == nextSep) {
// We found a SEPARATOR, replace with dstSeparator
// Make sure dstSeparator is not 0
assert(dstSeparator);
dst += dstSeparator;
begin = next + 1;
nextSep = strchr(begin, SEPARATOR);
continue;
}
// We found an ESCAPE
char follower = *(next + 1);
switch (follower) {
case ESCAPED_ESCAPE:
dst += ESCAPE;
#ifndef RELEASE_BUILD
if (!_shownSeparatorCollisionWarning && dstSeparator == ESCAPE) {
warning("Collision 1 while unescaping in path part \"%s\" with separator %c", begin, dstSeparator);
_shownSeparatorCollisionWarning = true;
}
#endif
break;
case ESCAPED_SEPARATOR:
dst += SEPARATOR;
#ifndef RELEASE_BUILD
if (!_shownSeparatorCollisionWarning && dstSeparator == SEPARATOR) {
warning("Collision 2 while unescaping in path part \"%s\" with separator %c", begin, dstSeparator);
_shownSeparatorCollisionWarning = true;
}
#endif
break;
default:
error("Invalid escape character '%c' in path part \"%s\"", follower, begin);
}
begin = next + 2;
// Look for the next one
nextEsc = strchr(begin, ESCAPE);
}
// Append anything after the last ESCAPE up to end
dst.append(begin, end);
return dst;
}
bool Path::canUnescape(bool willBeAtBegin, bool wasAtBegin,
const char *begin, const char *end) {
if (begin == end || (end == nullptr && *begin == '\x00')) {
// It's empty
return true;
}
if (*begin == ESCAPE) {
if (willBeAtBegin) {
// begin will be the start and contains an ESCAPE marker
// This means it escapes either a SEPARATOR or an ESCAPE
return false;
}
// We won't end up at begin of future path so we must check further what ESCAPE we have
} else {
if (wasAtBegin && end == nullptr) {
// begin is the start of an escaped path and doesn't begin with an ESCAPE
// and we go till the end of string
// This means we must have ESCAPED_SEPARATOR somewhere
return false;
}
}
const char *esc = strchr(begin, ESCAPE);
while (esc && (!end || esc < end)) {
if (*(esc + 1) == ESCAPED_SEPARATOR) {
return false;
}
esc = strchr(esc + 2, ESCAPE);
}
return true;
}
String Path::encode(const char *str, char separator) {
if (*str != ESCAPE && !strchr(str, SEPARATOR)) {
// If we start with ESCAPE we will need to escape
// No SEPARATOR, no clash
Common::String ret(str);
if (separator) {
// If separator is 0 there is only one path component
ret.replace(separator, SEPARATOR);
}
return ret;
}
// Prepend ESCAPE as we are escaping
Common::String ret(ESCAPE);
// Do the escape and replace separators by SEPARATOR
escape(ret, separator, str);
return ret;
}
Path Path::extract(const char *begin, const char *end) const {
if (_str.empty() || begin == end) {
// We are empty, we can't extract anything
return Path();
}
if (!end) {
end = _str.c_str() + _str.size();
}
if (!isEscaped()) {
Path ret;
if (*begin == ESCAPE) {
// We were not escaped but we will need to because we are creating a path starting with ESCAPE
ret._str += ESCAPE;
escape(ret._str, SEPARATOR, begin, end);
} else {
// Not escaped and no escape character at begin: just do the extraction
ret._str = String(begin, end);
}
return ret;
}
// We are escaped
// Make sure begin points on real start
if (begin == _str.c_str()) {
begin++;
}
if (canUnescape(true, false, begin, end)) {
// No escaped separator was found and no ESCAPE was at begin, unescape
Path ret;
ret._str = unescape(SEPARATOR, begin, end);
return ret;
}
// We can't unescape add escape marker and the escaped range
Path ret;
ret._str += ESCAPE;
ret._str.append(begin, end);
return ret;
}
String Path::toString(char separator) const {
// toString should never be called with \x00 separator
assert(separator != kNoSeparator);
if (_str.empty() ||
(separator == SEPARATOR && !isEscaped())) {
return _str;
}
if (!isEscaped()) {
// Path was not escaped, replace all SEPARATOR by the real separator
#ifndef RELEASE_BUILD
if (!_shownSeparatorCollisionWarning && strchr(_str.c_str(), separator)) {
warning("Collision 3 while unescaping path \"%s\" with separator %c", _str.c_str(), separator);
_shownSeparatorCollisionWarning = true;
}
#endif
Common::String ret(_str);
ret.replace(SEPARATOR, separator);
return ret;
}
// Remove leading escape indicator and do our stuff
const char *str = _str.c_str();
str++;
// Unescape and replace SEPARATORs by separator
return unescape(separator, str, _str.c_str() + _str.size());
}
size_t Path::findLastSeparator(size_t last) const {
if (last == String::npos || last > _str.size()) {
// Easy case
const char *sep = strrchr(_str.c_str(), SEPARATOR);
if (!sep) {
return String::npos;
} else {
return sep - _str.c_str();
}
}
// Let's jump from separator to separator
const char *begin = _str.c_str();
const char *end = begin + last;
const char *str = begin;
const char *sep = strchr(str, SEPARATOR);
while (sep && sep < end) {
str = sep + 1;
sep = strchr(str, SEPARATOR);
}
if (str == begin) {
// Nothing was found in the range specified
return String::npos;
} else {
// str was pointing just after the matching separator
return (str - 1) - begin;
}
}
Path Path::getParent() const {
if (_str.empty()) {
return Path();
}
// ignore potential trailing separator
size_t separatorPos = findLastSeparator(_str.size() - 1);
if (separatorPos == String::npos) {
return Path();
}
const char *begin = _str.c_str();
const char *end = begin + separatorPos + 1;
return extract(begin, end);
}
Path Path::getLastComponent() const {
if (_str.empty()) {
return Path();
}
// ignore potential trailing separator
size_t separatorPos = findLastSeparator(_str.size() - 1);
if (separatorPos == String::npos) {
return *this;
}
const char *begin = _str.c_str() + separatorPos + 1;
const char *end = _str.c_str() + _str.size();
return extract(begin, end);
}
String Path::baseName() const {
if (_str.empty()) {
return String();
}
bool escaped = isEscaped();
size_t last = _str.size();
if (isSeparatorTerminated()) {
last--;
}
const char *begin = _str.c_str();
const char *end = _str.c_str();
size_t separatorPos = findLastSeparator(last);
if (separatorPos != String::npos) {
begin += separatorPos + 1;
} else if (escaped) {
// unescape uses the real start, not the escape marker
begin++;
}
end += last;
if (!escaped) {
return String(begin, end);
}
return unescape(kNoSeparator, begin, end);
}
int Path::numComponents() const {
if (_str.empty())
return 0;
const char *str = _str.c_str();
if (isEscaped())
str++;
int num = 1;
const char *sep = strchr(str, SEPARATOR);
while (sep) {
str = sep + 1;
sep = strchr(str, SEPARATOR);
num++;
}
return num;
}
Path &Path::appendInPlace(const Path &x) {
if (x._str.empty()) {
return *this;
}
if (_str.empty()) {
_str = x._str;
return *this;
}
// From here both paths have data
if (isEscaped() == x.isEscaped()) {
if (isEscaped()) {
// Both are escaped: no chance we could unescape
// Append the other without its escape mark
_str.append(x._str.begin() + 1, x._str.end());
} else {
// No escape: append as we will don't need to escape
_str += x._str;
}
return *this;
}
// If we are here, one of us is escaped and the other one is not
if (isEscaped()) {
// This one is escaped not the other one
// There is no chance we could unescape this one
escape(_str, SEPARATOR, x._str.c_str(), x._str.c_str() + x._str.size());
return *this;
}
// This one is not escaped the other one is
// There is a small chance the other one is escaped because it started with ESCAPED
// In this case (and only this one) we could unescape
const char *other = x._str.c_str() + 1; // Remove leading ESCAPE
if (canUnescape(false, true, other)) {
// This one stays unescaped and the other one becomes unescaped
_str += unescape(SEPARATOR, other, x._str.c_str() + x._str.size());
} else {
// This one gets escaped and the other one stays escaped
String str(ESCAPE);
escape(str, SEPARATOR, _str.c_str(), _str.c_str() + _str.size());
str.append(other, x._str.c_str() + x._str.size());
// Replace ourselves
_str = str;
}
return *this;
}
Path &Path::appendInPlace(const char *str, char separator) {
if (!*str) {
return *this;
}
if (_str.empty()) {
set(str, separator);
return *this;
}
// From here both paths have data
if (isEscaped()) {
// This one is escaped
// There is no chance we could unescape it
escape(_str, separator, str);
return *this;
}
// We are not escaped, let's hope str won't need escaping
if (!needsEncoding(str, separator)) {
// No need to encode means it's perfect
_str += str;
return *this;
}
if (!strchr(str, SEPARATOR)) {
// No SEPARATOR, no clash
// Even if str starts with ESCAPE, no need to escape as we append
Common::String tmp(str);
if (separator) {
// If separator is 0 there is only one path component
tmp.replace(separator, SEPARATOR);
}
_str += tmp;
return *this;
}
// Too bad, we need to escape
String tmp(ESCAPE);
escape(tmp, SEPARATOR, _str.c_str(), _str.c_str() + _str.size());
escape(tmp, separator, str);
// Replace ourselves
_str = tmp;
return *this;
}
Path Path::appendComponent(const char *str) const {
if (!*str) {
return *this;
}
if (_str.empty()) {
return Path(str, kNoSeparator);
}
bool addSeparator = (*(_str.end() - 1) != SEPARATOR);
if (isEscaped()) {
// We are escaped, escape str as well
Path ret(*this);
if (addSeparator) {
ret._str += SEPARATOR;
}
// It's a component, there is no directory separator in source
escape(ret._str, kNoSeparator, str);
return ret;
} else if (strchr(str, SEPARATOR)) {
// We are not escaped but str will need escape: escape both parts
Path ret;
ret._str += ESCAPE;
escape(ret._str, SEPARATOR, _str.c_str(), _str.c_str() + _str.size());
if (addSeparator) {
ret._str += SEPARATOR;
}
// It's a component, there is no directory separator in source
escape(ret._str, kNoSeparator, str);
return ret;
} else {
// No need to escape anything
Path ret(*this);
if (addSeparator) {
ret._str += SEPARATOR;
}
ret._str += str;
return ret;
}
}
Path &Path::joinInPlace(const Path &x) {
if (x.empty()) {
return *this;
}
if (_str.empty()) {
_str = x._str;
return *this;
}
const char *str = x._str.c_str();
// Append SEPARATOR if:
// - we don't finish with SEPARATOR already
// - other Path is escaped and doesn't begin by SEPARATOR
// - or other Path isn't escaped and doesn't begin by SEPARATOR
if (*(_str.end() - 1) != SEPARATOR &&
((x.isEscaped() && str[1] != SEPARATOR) ||
*str != SEPARATOR)) {
_str += SEPARATOR;
}
return appendInPlace(x);
}
Path &Path::joinInPlace(const char *str, char separator) {
if (*str == '\0') {
return *this;
}
if (_str.empty()) {
set(str, separator);
return *this;
}
if (*(_str.end() - 1) != SEPARATOR && *str != separator) {
_str += SEPARATOR;
}
return appendInPlace(str, separator);
}
Path &Path::removeTrailingSeparators() {
while (_str.size() > 1 && _str.lastChar() == SEPARATOR) {
_str.deleteLastChar();
}
return *this;
}
Path &Path::removeExtension(const char *ext) {
if (_str.empty()) {
return *this;
}
// Find the last separator
size_t last = _str.size() - 1;
if (isSeparatorTerminated()) {
last--;
}
size_t sepPos = findLastSeparator(last);
const char *begin;
if (sepPos == String::npos) {
// No separator found, we are a single component
begin = _str.c_str();
} else {
// We have a separator, so we can find the last component
begin = _str.c_str() + sepPos + 1;
}
const char *end = _str.c_str() + _str.size();
String component(begin, end);
// If the last component is shorter than the extension,
// or it is punycode encoded, we do not change the path
if ((ext && component.size() < strlen(ext)) || punycode_hasprefix(component))
return *this;
if (!ext) {
// Remove the last extension, if any
size_t dotPos = component.findLastOf('.');
if (dotPos == String::npos) {
return *this; // No change
}
_str.chop(end - begin - dotPos);
return *this;
} else if (component.hasSuffix(ext)) {
// Remove the given extension, if it matches
_str.chop(strlen(ext));
}
return *this;
}
const char *Path::getSuffix(const Common::Path &other) const {
if (other.empty()) {
// Other is empty, return full string
const char *suffix = _str.c_str();
if (isEscaped()) {
suffix++;
}
return suffix;
}
if (isEscaped() == other.isEscaped()) {
// Easy one both have same escapism
if (_str.hasPrefix(other._str)) {
const char *suffix = _str.c_str() + other._str.size();
if (!other.isSeparatorTerminated()) {
if (*suffix == SEPARATOR) {
// Skip the separator
return suffix + 1;
} else if (*suffix == '\x00') {
// Both paths are equal: return end of string
return suffix;
} else {
// We are in the middle of some path component: this is not relative
return nullptr;
}
} else {
// Other already had a separator: this is relative and starts with next component
return suffix;
}
} else {
return nullptr;
}
}
// One is escaped and not the other
if (other.isEscaped()) {
// If the other is escaped it can't be a substring of this one
// as it must contain something which needed escape and we obviously didn't need it
return nullptr;
}
// We may be escaped because something needs escape after the end of the prefix
// We need to iterate over both paths and checking escape in ours
const char *str = _str.c_str() + 1;
const char *strOther = other._str.c_str();
while (*strOther) {
char c = *str;
if (c == ESCAPE) {
str++;
switch (*str) {
case ESCAPED_ESCAPE:
c = ESCAPE;
break;
case ESCAPED_SEPARATOR:
/* We are not supposed to have an escaped separator as
* the other one would need one too.
* We would then have the same escapism.
*/
c = SEPARATOR;
break;
default:
error("Invalid escape character '%c' in path \"%s\"", *str, _str.c_str());
}
}
if (*strOther != c) {
break;
}
++strOther;
++str;
}
// It's a prefix, if and only if all letters in other are 'used up' before
// we end.
if (*strOther != '\0') {
return nullptr;
}
if (!other.isSeparatorTerminated()) {
// Make sure we didn't end up in the middle of some path component
if (*str != SEPARATOR) {
return nullptr;
}
str++;
}
return str;
}
Path Path::relativeTo(const Common::Path &other) const {
const char *suffix = getSuffix(other);
if (!suffix) {
return *this;
}
// Remove all spurious separators
while (*suffix == SEPARATOR) {
suffix++;
}
return extract(suffix);
}
Path Path::normalize() const {
if (_str.empty()) {
return Path();
}
const char *cur = _str.c_str();
bool hasLeadingSeparator = false;
if (isEscaped()) {
// We are an escaped path
cur++;
}
// If there is a leading separator, preserve that:
if (*cur == SEPARATOR) {
hasLeadingSeparator = true;
cur++;
// Skip over multiple leading separators, so "//" equals "/"
while (*cur == SEPARATOR) {
cur++;
}
}
// Scan for path components till the end of the String
StringArray comps;
while (*cur) {
const char *start = cur;
// Scan till the next path separator resp. the end of the string
cur = strchr(cur, SEPARATOR);
if (!cur) {
cur = _str.c_str() + _str.size();
}
const String component(start, cur);
if (component.empty() || component == ".") {
// Skip empty components and dot components
} else if (!comps.empty() && component == ".." && comps.back() != "..") {
// If stack is non-empty and top is not "..", remove top
comps.pop_back();
} else {
// Add the component to the stack
comps.push_back(component);
}
// Skip over separator chars
while (*cur == SEPARATOR) {
cur++;
}
}
Common::Path result;
if (comps.empty()) {
// No components, add the leading separator if there was any
if (hasLeadingSeparator) {
result._str += SEPARATOR;
}
return result;
}
// Before making the new path, we need to determine if it will be escaped
bool needEscape = false;
bool needUnescape = false;
if (isEscaped()) {
// We are an escaped path
// Maybe we can get unescaped because problematic component got shifted
if (hasLeadingSeparator || *comps.front().c_str() != ESCAPE) {
// Well, we don't start with ESCAPE so there is still a chance we get unescaped
needUnescape = true;
for (const auto &comp : comps) {
if (!canUnescape(false, false, comp.c_str())) {
// Nope we can't get unescaped
needUnescape = false;
break;
}
}
}
// We will have an escaped result
if (!needUnescape) {
result._str += ESCAPE;
}
} else {
// We are not an escaped path
// The only reason we would need to escape is that we begin with an ESCAPE character
if (!hasLeadingSeparator && *comps.front().c_str() == ESCAPE) {
// Uh oh: we need to escape everything
needEscape = true;
result._str += ESCAPE;
}
}
// Finally, assemble all components back into a path
bool addSep = hasLeadingSeparator;
for (const auto &comp : comps) {
if (addSep) {
result._str += SEPARATOR;
}
addSep = true;
if (needEscape) {
escape(result._str, kNoSeparator, comp.c_str(), comp.c_str() + comp.size());
} else if (needUnescape) {
result._str += unescape(kNoSeparator, comp.c_str(), comp.c_str() + comp.size());
} else {
result._str += comp;
}
}
return result;
}
StringArray Path::splitComponents() const {
StringArray res;
if (_str.empty()) {
// We always return at least 1 component
res.push_back("");
return res;
}
const char *str = _str.c_str();
const char *end = str + _str.size();
bool escaped = isEscaped();
if (escaped) {
str++;
}
const char *sep = strchr(str, SEPARATOR);
while (sep) {
if (!escaped) {
res.push_back(String(str, sep));
} else {
res.push_back(unescape(kNoSeparator, str, sep));
}
str = sep + 1;
sep = strchr(str, SEPARATOR);
}
if (!escaped) {
res.push_back(String(str, end));
} else {
res.push_back(unescape(kNoSeparator, str, end));
}
return res;
}
Path Path::joinComponents(StringArray::const_iterator begin, StringArray::const_iterator end) {
if (begin == end) {
return Path();
}
Path ret(*begin, kNoSeparator);
for (StringArray::const_iterator it = begin + 1; it != end; it++) {
ret._str += SEPARATOR;
ret.appendInPlace(*it, kNoSeparator);
}
return ret;
}
template<typename T>
T Path::reduceComponents(T(*reducer)(T value, const String &element, bool last), T value) const {
if (_str.empty()) {
return value;
}
const char *str = _str.c_str();
const char *end = str + _str.size();
bool escaped = isEscaped();
if (escaped) {
str++;
}
const char *sep = strchr(str, SEPARATOR);
while (sep) {
String item;
if (escaped) {
item = unescape(kNoSeparator, str, sep);
} else {
item = String(str, sep);
}
value = reducer(value, item, false);
str = sep + 1;
sep = strchr(str, SEPARATOR);
}
String item;
if (escaped) {
item = unescape(kNoSeparator, str, end);
} else {
item = String(str, end);
}
value = reducer(value, item, true);
return value;
}
bool Path::compareComponents(bool (*comparator)(const String &x, const String &y),
const Path &other) const {
if (_str.empty() != other._str.empty()) {
// One is empty and the other is not
return false;
}
if (_str.empty()) {
// Both are empty
return true;
}
const char *str = _str.c_str();
const char *end = str + _str.size();
bool escaped = isEscaped();
if (escaped) {
str++;
}
const char *strOther = other._str.c_str();
const char *endOther = strOther + other._str.size();
bool escapedOther = other.isEscaped();
if (escapedOther) {
strOther++;
}
const char *sep = strchr(str, SEPARATOR);
const char *sepOther = strchr(strOther, SEPARATOR);