-
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathExecute.SimulatedPath.cs
More file actions
842 lines (703 loc) · 21.8 KB
/
Copy pathExecute.SimulatedPath.cs
File metadata and controls
842 lines (703 loc) · 21.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
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Text;
#if FEATURE_FILESYSTEM_NET_7_OR_GREATER
using Testably.Abstractions.Testing.Storage;
#endif
namespace Testably.Abstractions.Testing.Helpers;
internal sealed partial class Execute
{
private abstract class SimulatedPath(MockFileSystem fileSystem) : IPath
{
#region IPath Members
/// <inheritdoc cref="IPath.AltDirectorySeparatorChar" />
public abstract char AltDirectorySeparatorChar { get; }
/// <inheritdoc cref="IPath.DirectorySeparatorChar" />
public abstract char DirectorySeparatorChar { get; }
/// <inheritdoc cref="IFileSystemEntity.FileSystem" />
public IFileSystem FileSystem => fileSystem;
/// <inheritdoc cref="IPath.PathSeparator" />
public abstract char PathSeparator { get; }
/// <inheritdoc cref="IPath.VolumeSeparatorChar" />
public abstract char VolumeSeparatorChar { get; }
/// <inheritdoc cref="IPath.ChangeExtension(string, string)" />
[return: NotNullIfNotNull("path")]
public string? ChangeExtension(string? path, string? extension)
{
if (path == null)
{
return null;
}
if (path == string.Empty)
{
return string.Empty;
}
if (extension == null)
{
extension = "";
}
else if (!extension.StartsWith('.'))
{
extension = "." + extension;
}
if (!TryGetExtensionIndex(path, out int? dotIndex))
{
return path + extension;
}
#pragma warning disable CA1845 // Use span-based 'string.Concat' and 'AsSpan' instead of 'Substring
return path.Substring(0, dotIndex.Value) + extension;
#pragma warning restore CA1845
}
/// <inheritdoc cref="IPath.Combine(string, string)" />
public string Combine(string path1, string path2)
{
if (path1 == null)
{
throw new ArgumentNullException(nameof(path1));
}
if (path2 == null)
{
throw new ArgumentNullException(nameof(path2));
}
return CombineInternal([path1, path2]);
}
/// <inheritdoc cref="IPath.Combine(string, string, string)" />
public string Combine(string path1, string path2, string path3)
{
if (path1 == null)
{
throw new ArgumentNullException(nameof(path1));
}
if (path2 == null)
{
throw new ArgumentNullException(nameof(path2));
}
if (path3 == null)
{
throw new ArgumentNullException(nameof(path3));
}
return CombineInternal([path1, path2, path3]);
}
/// <inheritdoc cref="IPath.Combine(string, string, string, string)" />
public string Combine(string path1, string path2, string path3, string path4)
{
if (path1 == null)
{
throw new ArgumentNullException(nameof(path1));
}
if (path2 == null)
{
throw new ArgumentNullException(nameof(path2));
}
if (path3 == null)
{
throw new ArgumentNullException(nameof(path3));
}
if (path4 == null)
{
throw new ArgumentNullException(nameof(path4));
}
return CombineInternal([path1, path2, path3, path4]);
}
/// <inheritdoc cref="IPath.Combine(string[])" />
public string Combine(params string[] paths)
=> CombineInternal(paths);
#if FEATURE_PATH_SPAN
/// <inheritdoc cref="IPath.Combine(ReadOnlySpan{string})" />
public string Combine(params ReadOnlySpan<string> paths)
=> CombineInternal(paths.ToArray());
#endif
#if FEATURE_PATH_ADVANCED
/// <inheritdoc cref="IPath.EndsInDirectorySeparator(ReadOnlySpan{char})" />
public bool EndsInDirectorySeparator(ReadOnlySpan<char> path)
=> EndsInDirectorySeparator(path.ToString());
#endif
#if FEATURE_PATH_ADVANCED
/// <inheritdoc cref="IPath.EndsInDirectorySeparator(string)" />
public bool EndsInDirectorySeparator(string path)
=> !string.IsNullOrEmpty(path) && IsDirectorySeparator(path[path.Length - 1]);
#endif
#if FEATURE_FILESYSTEM_NET_7_OR_GREATER
/// <inheritdoc cref="IPath.Exists(string)" />
public bool Exists([NotNullWhen(true)] string? path)
{
if (string.IsNullOrEmpty(path))
{
return false;
}
return fileSystem.Storage.GetContainer(fileSystem.Storage.GetLocation(path))
is not NullContainer;
}
#endif
#if FEATURE_SPAN
/// <inheritdoc cref="IPath.GetDirectoryName(ReadOnlySpan{char})" />
public ReadOnlySpan<char> GetDirectoryName(ReadOnlySpan<char> path)
=> GetDirectoryName(path.ToString());
#endif
/// <inheritdoc cref="IPath.GetDirectoryName(string)" />
public string? GetDirectoryName(string? path)
{
if (path == null || IsEffectivelyEmpty(path))
{
return null;
}
int rootLength = GetRootLength(path);
if (path.Length <= rootLength)
{
return null;
}
int end = path.Length;
while (end > rootLength && !IsDirectorySeparator(path[end - 1]))
{
end--;
}
while (end > rootLength && IsDirectorySeparator(path[end - 1]))
{
end--;
}
return NormalizeDirectorySeparators(path.Substring(0, end));
}
#if FEATURE_SPAN
/// <inheritdoc cref="IPath.GetExtension(ReadOnlySpan{char})" />
public ReadOnlySpan<char> GetExtension(ReadOnlySpan<char> path)
=> GetExtension(path.ToString());
#endif
/// <inheritdoc cref="IPath.GetExtension(string)" />
[return: NotNullIfNotNull("path")]
public string? GetExtension(string? path)
{
if (path == null)
{
return null;
}
if (TryGetExtensionIndex(path, out int? dotIndex))
{
return dotIndex != path.Length - 1
? path.Substring(dotIndex.Value)
: string.Empty;
}
return string.Empty;
}
#if FEATURE_SPAN
/// <inheritdoc cref="IPath.GetFileName(ReadOnlySpan{char})" />
public ReadOnlySpan<char> GetFileName(ReadOnlySpan<char> path)
=> GetFileName(path.ToString());
#endif
/// <inheritdoc cref="IPath.GetFileName(string)" />
[return: NotNullIfNotNull("path")]
public string? GetFileName(string? path)
{
if (path == null)
{
return null;
}
for (int i = path.Length - 1; i >= 0; i--)
{
if (IsDirectorySeparator(path[i]))
{
return path.Substring(i + 1);
}
}
return path;
}
#if FEATURE_SPAN
/// <inheritdoc cref="IPath.GetFileNameWithoutExtension(ReadOnlySpan{char})" />
public ReadOnlySpan<char> GetFileNameWithoutExtension(ReadOnlySpan<char> path)
=> GetFileNameWithoutExtension(path.ToString());
#endif
/// <inheritdoc cref="IPath.GetFileNameWithoutExtension(string)" />
[return: NotNullIfNotNull("path")]
public string? GetFileNameWithoutExtension(string? path)
{
if (path == null)
{
return null;
}
string fileName = GetFileName(path);
int lastPeriod = fileName.LastIndexOf('.');
return lastPeriod < 0 ? fileName : fileName.Substring(0, lastPeriod);
}
/// <inheritdoc cref="IPath.GetFullPath(string)" />
public abstract string GetFullPath(string path);
#if FEATURE_PATH_RELATIVE
/// <inheritdoc cref="IPath.GetFullPath(string, string)" />
public abstract string GetFullPath(string path, string basePath);
#endif
/// <inheritdoc cref="IPath.GetInvalidFileNameChars()" />
public abstract char[] GetInvalidFileNameChars();
/// <inheritdoc cref="IPath.GetInvalidPathChars()" />
public abstract char[] GetInvalidPathChars();
#if FEATURE_SPAN
/// <inheritdoc cref="IPath.GetPathRoot(ReadOnlySpan{char})" />
public ReadOnlySpan<char> GetPathRoot(ReadOnlySpan<char> path)
=> GetPathRoot(path.ToString());
#endif
/// <inheritdoc cref="IPath.GetPathRoot(string?)" />
public abstract string? GetPathRoot(string? path);
/// <inheritdoc cref="IPath.GetRandomFileName()" />
public string GetRandomFileName()
=> $"{RandomString(fileSystem, 8)}.{RandomString(fileSystem, 3)}";
#if FEATURE_PATH_RELATIVE
/// <inheritdoc cref="IPath.GetRelativePath(string, string)" />
public string GetRelativePath(string relativeTo, string path)
{
relativeTo.EnsureValidArgument(fileSystem, nameof(relativeTo));
relativeTo = fileSystem.Execute.Path.GetFullPath(relativeTo);
path = fileSystem.Execute.Path.GetFullPath(path);
Func<char, char, bool> charComparer = (c1, c2) => c1 == c2;
if (fileSystem.Execute.StringComparisonMode == StringComparison.OrdinalIgnoreCase)
{
charComparer = (c1, c2) => char.ToUpperInvariant(c1) == char.ToUpperInvariant(c2);
}
int commonLength = GetCommonPathLength(relativeTo, path, charComparer);
// If there is nothing in common they can't share the same root, return the "to" path as is.
if (commonLength == 0)
{
return path;
}
// Trailing separators aren't significant for comparison
int relativeToLength = relativeTo.Length;
if (IsDirectorySeparator(relativeTo[relativeToLength - 1]))
{
relativeToLength--;
}
int pathLength = path.Length;
bool pathEndsInSeparator = IsDirectorySeparator(path[pathLength - 1]);
if (pathEndsInSeparator)
{
pathLength--;
}
// If we have effectively the same path, return "."
if (relativeToLength == pathLength && commonLength >= relativeToLength)
{
return ".";
}
return CreateRelativePath(relativeTo, path,
commonLength, relativeToLength, pathLength,
pathEndsInSeparator);
}
#endif
/// <inheritdoc cref="IPath.GetTempFileName()" />
#if !NETSTANDARD2_0
[Obsolete(
"Insecure temporary file creation methods should not be used. Use `Path.Combine(Path.GetTempPath(), Path.GetRandomFileName())` instead.")]
#endif
public string GetTempFileName()
=> CreateTempFileName(fileSystem);
/// <inheritdoc cref="IPath.GetTempPath()" />
public abstract string GetTempPath();
#if FEATURE_SPAN
/// <inheritdoc cref="IPath.HasExtension(ReadOnlySpan{char})" />
public bool HasExtension(ReadOnlySpan<char> path)
=> HasExtension(path.ToString());
#endif
/// <inheritdoc cref="IPath.HasExtension(string)" />
public bool HasExtension([NotNullWhen(true)] string? path)
{
if (path == null)
{
return false;
}
return TryGetExtensionIndex(path, out int? dotIndex)
&& dotIndex < path.Length - 1;
}
#if FEATURE_SPAN
/// <inheritdoc cref="IPath.IsPathFullyQualified(ReadOnlySpan{char})" />
public bool IsPathFullyQualified(ReadOnlySpan<char> path)
=> IsPathFullyQualified(path.ToString());
#endif
#if FEATURE_PATH_RELATIVE
/// <inheritdoc cref="IPath.IsPathFullyQualified(string)" />
public bool IsPathFullyQualified(string path)
{
if (path == null)
{
throw new ArgumentNullException(nameof(path));
}
return !IsPartiallyQualified(path);
}
#endif
#if FEATURE_SPAN
/// <inheritdoc cref="IPath.IsPathRooted(ReadOnlySpan{char})" />
public bool IsPathRooted(ReadOnlySpan<char> path)
=> IsPathRooted(path.ToString());
#endif
/// <inheritdoc cref="IPath.IsPathRooted(string)" />
public abstract bool IsPathRooted(string? path);
#if FEATURE_PATH_JOIN
/// <inheritdoc cref="IPath.Join(ReadOnlySpan{char}, ReadOnlySpan{char})" />
public string Join(ReadOnlySpan<char> path1, ReadOnlySpan<char> path2)
=> JoinInternal([path1.ToString(), path2.ToString()]);
#endif
#if FEATURE_PATH_JOIN
/// <inheritdoc cref="IPath.Join(ReadOnlySpan{char}, ReadOnlySpan{char}, ReadOnlySpan{char})" />
public string Join(ReadOnlySpan<char> path1,
ReadOnlySpan<char> path2,
ReadOnlySpan<char> path3)
=> JoinInternal([path1.ToString(), path2.ToString(), path3.ToString()]);
#endif
#if FEATURE_PATH_ADVANCED
/// <inheritdoc cref="IPath.Join(ReadOnlySpan{char}, ReadOnlySpan{char}, ReadOnlySpan{char}, ReadOnlySpan{char})" />
public string Join(ReadOnlySpan<char> path1,
ReadOnlySpan<char> path2,
ReadOnlySpan<char> path3,
ReadOnlySpan<char> path4)
=> JoinInternal(
[path1.ToString(), path2.ToString(), path3.ToString(), path4.ToString()]);
#endif
#if FEATURE_PATH_ADVANCED
/// <inheritdoc cref="IPath.Join(string, string)" />
public string Join(string? path1, string? path2)
=> JoinInternal([path1, path2]);
#endif
#if FEATURE_PATH_ADVANCED
/// <inheritdoc cref="IPath.Join(string, string, string)" />
public string Join(string? path1, string? path2, string? path3)
=> JoinInternal([path1, path2, path3]);
#endif
#if FEATURE_PATH_ADVANCED
/// <inheritdoc cref="IPath.Join(string, string, string, string)" />
public string Join(string? path1, string? path2, string? path3, string? path4)
=> JoinInternal([path1, path2, path3, path4]);
#endif
#if FEATURE_PATH_ADVANCED
/// <inheritdoc cref="IPath.Join(string[])" />
public string Join(params string?[] paths)
=> JoinInternal(paths);
#endif
#if FEATURE_PATH_SPAN
/// <inheritdoc cref="IPath.Join(ReadOnlySpan{string})" />
public string Join(params ReadOnlySpan<string?> paths)
=> JoinInternal(paths.ToArray());
#endif
#if FEATURE_PATH_ADVANCED
/// <inheritdoc cref="IPath.TrimEndingDirectorySeparator(ReadOnlySpan{char})" />
public ReadOnlySpan<char> TrimEndingDirectorySeparator(ReadOnlySpan<char> path)
=> TrimEndingDirectorySeparator(path.ToString());
#endif
#if FEATURE_PATH_ADVANCED
/// <inheritdoc cref="IPath.TrimEndingDirectorySeparator(string)" />
public string TrimEndingDirectorySeparator(string path)
{
return path.Length != GetRootLength(path) && EndsInDirectorySeparator(path)
? path.Substring(0, path.Length - 1)
: path;
}
#endif
#if FEATURE_PATH_JOIN
/// <inheritdoc cref="IPath.TryJoin(ReadOnlySpan{char}, ReadOnlySpan{char}, Span{char}, out int)" />
public bool TryJoin(ReadOnlySpan<char> path1,
ReadOnlySpan<char> path2,
Span<char> destination,
out int charsWritten)
{
string result = Join(path1, path2);
if (destination.Length < result.Length)
{
charsWritten = 0;
return false;
}
result.AsSpan().CopyTo(destination);
charsWritten = result.Length;
return true;
}
#endif
#if FEATURE_PATH_JOIN
/// <inheritdoc cref="IPath.TryJoin(ReadOnlySpan{char}, ReadOnlySpan{char}, ReadOnlySpan{char}, Span{char}, out int)" />
public bool TryJoin(ReadOnlySpan<char> path1,
ReadOnlySpan<char> path2,
ReadOnlySpan<char> path3,
Span<char> destination,
out int charsWritten)
{
string result = Join(path1, path2, path3);
if (destination.Length < result.Length)
{
charsWritten = 0;
return false;
}
result.AsSpan().CopyTo(destination);
charsWritten = result.Length;
return true;
}
#endif
#endregion
private string CombineInternal(string[] paths)
{
string NormalizePath(string path)
{
if (path[path.Length - 1] == DirectorySeparatorChar ||
path[path.Length - 1] == AltDirectorySeparatorChar)
{
path = path.Substring(0, path.Length - 1);
}
return NormalizeDirectorySeparators(path);
}
if (paths == null)
{
throw new ArgumentNullException(nameof(paths));
}
StringBuilder sb = new();
bool endsWithDirectorySeparator = true;
foreach (string path in paths)
{
if (path == null)
{
throw new ArgumentNullException(nameof(paths));
}
if (string.IsNullOrEmpty(path))
{
continue;
}
if (IsPathRooted(path))
{
sb.Clear();
}
sb.Append(NormalizePath(path));
sb.Append(DirectorySeparatorChar);
endsWithDirectorySeparator = path.EndsWith(DirectorySeparatorChar) ||
path.EndsWith(AltDirectorySeparatorChar);
}
if (!endsWithDirectorySeparator)
{
return sb.ToString(0, sb.Length - 1);
}
return sb.ToString();
}
#if FEATURE_PATH_RELATIVE
/// <summary>
/// We have the same root, we need to calculate the difference now using the
/// common Length and Segment count past the length.
/// </summary>
/// <remarks>
/// Some examples:
/// <para />
/// C:\Foo C:\Bar L3, S1 -> ..\Bar<br />
/// C:\Foo C:\Foo\Bar L6, S0 -> Bar<br />
/// C:\Foo\Bar C:\Bar\Bar L3, S2 -> ..\..\Bar\Bar<br />
/// C:\Foo\Foo C:\Foo\Bar L7, S1 -> ..\Bar<br />
/// </remarks>
private string CreateRelativePath(string relativeTo, string path, int commonLength,
int relativeToLength, int pathLength, bool pathEndsInSeparator)
{
StringBuilder sb = new();
// Add parent segments for segments past the common on the "from" path
if (commonLength < relativeToLength)
{
sb.Append("..");
for (int i = commonLength + 1; i < relativeToLength; i++)
{
if (IsDirectorySeparator(relativeTo[i]))
{
sb.Append(DirectorySeparatorChar);
sb.Append("..");
}
}
}
else if (IsDirectorySeparator(path[commonLength]))
{
// No parent segments, and we need to eat the initial separator
commonLength++;
}
// Now add the rest of the "to" path, adding back the trailing separator
int differenceLength = pathLength - commonLength;
if (pathEndsInSeparator)
{
differenceLength++;
}
if (differenceLength > 0)
{
if (sb.Length > 0)
{
sb.Append(DirectorySeparatorChar);
}
sb.Append(path, commonLength, differenceLength);
}
return sb.ToString();
}
#endif
#if FEATURE_PATH_RELATIVE
/// <summary>
/// Get the common path length from the start of the string.
/// </summary>
private int GetCommonPathLength(string first, string second,
Func<char, char, bool> charComparer)
{
int commonChars = 0;
for (; commonChars < first.Length; commonChars++)
{
if (second.Length <= commonChars)
{
break;
}
if (!charComparer(first[commonChars], second[commonChars]))
{
break;
}
}
// If nothing matches
if (commonChars == 0)
{
return commonChars;
}
// Or we're a full string and equal length or match to a separator
if (commonChars == first.Length && commonChars == second.Length)
{
return commonChars;
}
if ((second.Length > commonChars && IsDirectorySeparator(second[commonChars])) ||
IsDirectorySeparator(first[commonChars - 1]))
{
return commonChars;
}
// It's possible we matched somewhere in the middle of a segment e.g. C:\Foodie and C:\Foobar.
while (commonChars > 0 && !IsDirectorySeparator(first[commonChars - 1]))
{
commonChars--;
}
return commonChars;
}
#endif
protected abstract int GetRootLength(string path);
protected abstract bool IsDirectorySeparator(char c);
protected abstract bool IsEffectivelyEmpty(string path);
#if FEATURE_PATH_RELATIVE
protected abstract bool IsPartiallyQualified(string path);
#endif
#if FEATURE_PATH_JOIN || FEATURE_PATH_ADVANCED
private string JoinInternal(string?[] paths)
{
if (paths == null)
{
throw new ArgumentNullException(nameof(paths));
}
if (paths.Length == 0)
{
return string.Empty;
}
StringBuilder sb = new();
foreach (string? path in paths)
{
if (string.IsNullOrEmpty(path))
{
continue;
}
if (sb.Length == 0)
{
sb.Append(path);
}
else
{
if (!IsDirectorySeparator(sb[sb.Length - 1]) && !IsDirectorySeparator(path[0]))
{
sb.Append(DirectorySeparatorChar);
}
sb.Append(path);
}
}
return sb.ToString();
}
#endif
protected abstract string NormalizeDirectorySeparators(string path);
/// <summary>
/// Remove relative segments from the given path (without combining with a root).
/// </summary>
protected string RemoveRelativeSegments(string path, int rootLength)
{
Debug.Assert(rootLength > 0);
bool flippedSeparator = false;
StringBuilder sb = new();
int skip = rootLength;
// We treat "\.." , "\." and "\\" as a relative segment. We want to collapse the first separator past the root presuming
// the root actually ends in a separator. Otherwise the first segment for RemoveRelativeSegments
// in cases like "\\?\C:\.\" and "\\?\C:\..\", the first segment after the root will be ".\" and "..\" which is not considered as a relative segment and hence not be removed.
if (IsDirectorySeparator(path[skip - 1]))
{
skip--;
}
sb.Append(path, 0, skip);
// Remove "//", "/./", and "/../" from the path by copying each character to the output,
// except the ones we're removing, such that the builder contains the normalized path
// at the end.
for (int i = skip; i < path.Length; i++)
{
char c = path[i];
if (IsDirectorySeparator(c) && i + 1 < path.Length)
{
// Skip this character if it's a directory separator and if the next character is, too,
// e.g. "parent//child" => "parent/child"
if (IsDirectorySeparator(path[i + 1]))
{
continue;
}
// Skip this character and the next if it's referring to the current directory,
// e.g. "parent/./child" => "parent/child"
if ((i + 2 == path.Length || IsDirectorySeparator(path[i + 2])) &&
path[i + 1] == '.')
{
i++;
continue;
}
// Skip this character and the next two if it's referring to the parent directory,
// e.g. "parent/child/../grandchild" => "parent/grandchild"
if (i + 2 < path.Length &&
(i + 3 == path.Length || IsDirectorySeparator(path[i + 3])) &&
path[i + 1] == '.' && path[i + 2] == '.')
{
// Unwind back to the last slash (and if there isn't one, clear out everything).
for (int s = sb.Length - 1; s >= skip; s--)
{
if (IsDirectorySeparator(sb[s]))
{
sb.Length =
i + 3 >= path.Length && s == skip
? s + 1
: s; // to avoid removing the complete "\tmp\" segment in cases like \\?\C:\tmp\..\, C:\tmp\..
break;
}
}
i += 2;
continue;
}
}
// Normalize the directory separator if needed
if (c != DirectorySeparatorChar && c == AltDirectorySeparatorChar)
{
c = DirectorySeparatorChar;
flippedSeparator = true;
}
sb.Append(c);
}
// If we haven't changed the source path, return the original
if (!flippedSeparator && sb.Length == path.Length)
{
return path;
}
// We may have eaten the trailing separator from the root when we started and not replaced it
if (skip != rootLength && sb.Length < rootLength)
{
sb.Append(path[rootLength - 1]);
}
return sb.ToString();
}
private bool TryGetExtensionIndex(string path, [NotNullWhen(true)] out int? dotIndex)
{
for (int i = path.Length - 1; i >= 0; i--)
{
char ch = path[i];
if (ch == '.')
{
dotIndex = i;
return true;
}
if (IsDirectorySeparator(ch))
{
break;
}
}
dotIndex = null;
return false;
}
}
}