-
-
Notifications
You must be signed in to change notification settings - Fork 61
/
UPath.cs
564 lines (479 loc) · 17.7 KB
/
UPath.cs
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
// Copyright (c) Alexandre Mutel. All rights reserved.
// This file is licensed under the BSD-Clause 2 license.
// See the license.txt file in the project root for more information.
using System.Text;
namespace Zio;
/// <summary>
/// A uniform unix like path.
/// </summary>
/// <seealso cref="UPath" />
public readonly struct UPath : IEquatable<UPath>, IComparable<UPath>
{
[ThreadStatic] private static InternalHelper? _internalHelperTls;
/// <summary>
/// An empty path.
/// </summary>
public static readonly UPath Empty = new UPath(string.Empty, true);
/// <summary>
/// The root path `/`
/// </summary>
public static readonly UPath Root = new UPath("/", true);
internal static readonly UPath Null = new UPath(null!);
/// <summary>
/// The directory separator `/`
/// </summary>
public const char DirectorySeparator = '/';
private static InternalHelper InternalHelperTls => _internalHelperTls ??= new InternalHelper();
/// <summary>
/// The default comparer for a <see cref="UPath"/> that is case sensitive.
/// </summary>
public static readonly IComparer<UPath> DefaultComparer = UPathComparer.Ordinal;
/// <summary>
/// The default comparer for a <see cref="UPath"/> that is case insensitive.
/// </summary>
public static readonly IComparer<UPath> DefaultComparerIgnoreCase = UPathComparer.OrdinalIgnoreCase;
/// <summary>
/// Initializes a new instance of the <see cref="UPath"/> struct.
/// </summary>
/// <param name="path">The path that will be normalized.</param>
public UPath(string path) : this(path, false)
{
}
internal UPath(string path, bool safe)
{
if (safe)
{
FullName = path;
}
else
{
string? errorMessage;
FullName = ValidateAndNormalize(path, out errorMessage)!;
if (errorMessage != null)
throw new ArgumentException(errorMessage, nameof(path));
}
}
/// <summary>
/// Gets the full name of this path (Note that it may be null).
/// </summary>
/// <value>The full name of this path.</value>
public string FullName { get; }
/// <summary>
/// Gets a value indicating whether this path is null.
/// </summary>
/// <value><c>true</c> if this instance is null; otherwise, <c>false</c>.</value>
public bool IsNull => FullName is null;
/// <summary>
/// Gets a value indicating whether this path is empty (<see cref="FullName"/> equals to the empty string)
/// </summary>
/// <value><c>true</c> if this instance is empty; otherwise, <c>false</c>.</value>
public bool IsEmpty => this.FullName?.Length == 0;
/// <summary>
/// Gets a value indicating whether this path is absolute by starting with a leading `/`.
/// </summary>
/// <value><c>true</c> if this path is absolute; otherwise, <c>false</c>.</value>
public bool IsAbsolute => FullName is not null && FullName.Length > 0 && FullName[0] == '/';
/// <summary>
/// Gets a value indicating whether this path is relative by **not** starting with a leading `/`.
/// </summary>
/// <value><c>true</c> if this instance is relative; otherwise, <c>false</c>.</value>
public bool IsRelative => !IsAbsolute;
/// <summary>
/// Performs an implicit conversion from <see cref="System.String"/> to <see cref="UPath"/>.
/// </summary>
/// <param name="path">The path as a string.</param>
/// <returns>The result of the conversion.</returns>
public static implicit operator UPath(string path)
{
return new UPath(path);
}
/// <summary>
/// Performs an explicit conversion from <see cref="UPath"/> to <see cref="System.String"/>.
/// </summary>
/// <param name="path">The path.</param>
/// <returns>The result as a string of the conversion.</returns>
public static explicit operator string(UPath path)
{
return path.FullName;
}
/// <summary>
/// Combines two paths into a new path.
/// </summary>
/// <param name="path1">The first path to combine.</param>
/// <param name="path2">The second path to combine.</param>
/// <returns>The combined paths. If one of the specified paths is a zero-length string, this method returns the other path. If path2 contains an absolute path, this method returns path2.</returns>
/// <exception cref="System.ArgumentNullException">
/// path1
/// or
/// path2
/// </exception>
/// <exception cref="System.ArgumentException">If an error occurs while trying to combine paths.</exception>
public static UPath Combine(UPath path1, UPath path2)
{
if (TryGetAbsolute(path1, path2, out var result))
{
return result;
}
try
{
#if NET7_0_OR_GREATER
return string.Create(path1.FullName.Length + path2.FullName.Length + 1, new KeyValuePair<UPath, UPath>(path1, path2), (span, state) =>
{
var (left, right) = state;
left.FullName.AsSpan().CopyTo(span);
span[left.FullName.Length] = '/';
right.FullName.AsSpan().CopyTo(span.Slice(left.FullName.Length + 1));
});
#else
return new UPath($"{path1.FullName}/{path2.FullName}");
#endif
}
catch (ArgumentException ex)
{
throw new ArgumentException($"Unable to combine path `{path1}` with `{path2}`", ex);
}
}
private static bool TryGetAbsolute(UPath path1, UPath path2, out UPath result)
{
if (path1.FullName is null)
throw new ArgumentNullException(nameof(path1));
if (path2.FullName is null)
throw new ArgumentNullException(nameof(path2));
// If the right path is absolute, it takes priority over path1
if (path1.IsEmpty || path2.IsAbsolute)
{
result = path2;
return true;
}
result = default;
return false;
}
public static UPath Combine(UPath path1, UPath path2, UPath path3)
{
if (TryGetAbsolute(path1, path2, out var result))
{
return Combine(result, path3);
}
if (TryGetAbsolute(path2, path3, out result))
{
return Combine(path1, result);
}
#if NET7_0_OR_GREATER
return string.Create(path1.FullName.Length + path2.FullName.Length + path3.FullName.Length + 2, (path1, path2, path3), (span, state) =>
{
var (p1, p2, p3) = state;
var remaining = span;
p1.FullName.AsSpan().CopyTo(remaining);
remaining[p1.FullName.Length] = '/';
remaining = remaining.Slice(p1.FullName.Length + 1);
p2.FullName.AsSpan().CopyTo(remaining);
remaining[p2.FullName.Length] = '/';
remaining = remaining.Slice(p2.FullName.Length + 1);
p3.FullName.AsSpan().CopyTo(remaining);
});
#else
return UPath.Combine(UPath.Combine(path1, path2), path3);
#endif
}
public static UPath Combine(UPath path1, UPath path2, UPath path3, UPath path4)
{
if (TryGetAbsolute(path1, path2, out var result))
{
return Combine(result, path3, path4);
}
if (TryGetAbsolute(path2, path3, out result))
{
return Combine(path1, result, path4);
}
if (TryGetAbsolute(path3, path4, out result))
{
return Combine(path1, path2, result);
}
#if NET7_0_OR_GREATER
return string.Create(path1.FullName.Length + path2.FullName.Length + path3.FullName.Length + path4.FullName.Length + 3, (path1, path2, path3, path4), (span, state) =>
{
var (p1, p2, p3, p4) = state;
var remaining = span;
p1.FullName.AsSpan().CopyTo(remaining);
remaining[p1.FullName.Length] = '/';
remaining = remaining.Slice(p1.FullName.Length + 1);
p2.FullName.AsSpan().CopyTo(remaining);
remaining[p2.FullName.Length] = '/';
remaining = remaining.Slice(p2.FullName.Length + 1);
p3.FullName.AsSpan().CopyTo(remaining);
remaining[p3.FullName.Length] = '/';
remaining = remaining.Slice(p3.FullName.Length + 1);
p4.FullName.AsSpan().CopyTo(remaining);
});
#else
return UPath.Combine(UPath.Combine(path1, path2), UPath.Combine(path3, path4));
#endif
}
public static UPath Combine(params UPath[] paths)
{
var path = paths[0];
for (var i = 1; i < paths.Length; i++)
path = Combine(path, paths[i]);
return path;
}
/// <summary>
/// Implements the / operator equivalent of <see cref="Combine"/>
/// </summary>
/// <param name="path1">The first path to combine.</param>
/// <param name="path2">The second path to combine.</param>
/// <returns>The combined paths. If one of the specified paths is a zero-length string, this method returns the other path. If path2 contains an absolute path, this method returns path2.</returns>
/// <exception cref="System.ArgumentNullException">
/// path1
/// or
/// path2
/// </exception>
/// <exception cref="System.ArgumentException">If an error occurs while trying to combine paths.</exception>
public static UPath operator /(UPath path1, UPath path2)
{
return Combine(path1, path2);
}
/// <inheritdoc />
public bool Equals(UPath other)
{
return string.Equals(FullName, other.FullName);
}
/// <inheritdoc />
public override bool Equals(object? obj)
{
return obj is UPath path && Equals(path);
}
/// <inheritdoc />
public override int GetHashCode()
{
return FullName?.GetHashCode() ?? 0;
}
/// <summary>
/// Implements the == operator.
/// </summary>
/// <param name="left">The left.</param>
/// <param name="right">The right.</param>
/// <returns>The result of the operator.</returns>
public static bool operator ==(UPath left, UPath right)
{
return left.Equals(right);
}
/// <summary>
/// Implements the != operator.
/// </summary>
/// <param name="left">The left.</param>
/// <param name="right">The right.</param>
/// <returns>The result of the operator.</returns>
public static bool operator !=(UPath left, UPath right)
{
return !left.Equals(right);
}
/// <inheritdoc />
public override string ToString()
{
return FullName;
}
/// <summary>
/// Tries to parse the specified string into a <see cref="UPath"/>
/// </summary>
/// <param name="path">The path as a string.</param>
/// <param name="pathInfo">The path parsed if successful.</param>
/// <returns><c>true</c> if path was parsed successfully, <c>false</c> otherwise.</returns>
public static bool TryParse(string path, out UPath pathInfo)
{
string? errorMessage;
path = ValidateAndNormalize(path, out errorMessage)!;
pathInfo = errorMessage is null ? new UPath(path!, true) : new UPath();
return errorMessage is null;
}
internal static StringBuilder GetSharedStringBuilder()
{
var builder = InternalHelperTls.Builder;
builder.Length = 0;
return builder;
}
private static string? ValidateAndNormalize(string path, out string? errorMessage)
{
errorMessage = null;
// Early exit
switch (path)
{
case null:
return null;
case "/":
case "..":
case ".":
return path;
case "\\":
return "/";
}
// Optimized path
var internalHelper = InternalHelperTls;
var parts = internalHelper.Slices;
parts.Clear();
var lastIndex = 0;
var i = 0;
var processParts = false;
var dotCount = 0;
for (; i < path.Length; i++)
{
var c = path[i];
// We don't disallow characters, as we let the IFileSystem implementations decided for them
// depending on the platform
//if (c < ' ' || c == ':' || c == '<' || c == '>' || c == '"' || c == '|')
//{
// throw new InvalidUPathException($"The path `{path}` contains invalid characters `{c}`");
//}
if (c == '.')
{
dotCount++;
}
else if (c == DirectorySeparator || c == '\\')
{
// optimization: If we don't expect to process the path
// and we only have a trailing / or \\, then just perform
// a substring on the path
if (!processParts && i + 1 == path.Length)
return path.Substring(0, path.Length - 1);
if (c == '\\')
processParts = true;
var endIndex = i - 1;
for (i++; i < path.Length; i++)
{
c = path[i];
if (c == DirectorySeparator || c == '\\')
{
// If we have consecutive / or \\, we need to process parts
processParts = true;
continue;
}
break;
}
if (endIndex >= lastIndex || endIndex == -1)
{
var part = new TextSlice(lastIndex, endIndex);
parts.Add(part);
if (dotCount > 0 && // has dots
dotCount == part.Length && // only dots
(dotCount != 2 || parts.Count > 1)) // Skip ".." if it's the first part
processParts = true;
}
dotCount = c == '.' ? 1 : 0;
lastIndex = i;
}
}
if (lastIndex < path.Length)
{
var part = new TextSlice(lastIndex, path.Length - 1);
parts.Add(part);
// If the previous part had only dots, we need to process it
if (part.Length == dotCount)
processParts = true;
}
// Optimized path if we don't need to compact the path
if (!processParts)
return path;
// Slow path, we need to process the parts
for (i = 0; i < parts.Count; i++)
{
var part = parts[i];
var partLength = part.Length;
if (partLength < 1)
continue;
if (path[part.Start] != '.')
continue;
if (partLength == 1)
{
// We have a '.'
if (parts.Count > 1)
parts.RemoveAt(i--);
}
else
{
if (path[part.Start + 1] != '.')
continue;
// Throws an exception if our slice parth contains only `.` and is longer than 2 characters
if (partLength > 2)
{
var isValid = false;
for (var j = part.Start + 2; j <= part.End; j++)
{
if (path[j] != '.')
{
isValid = true;
break;
}
}
if (!isValid)
{
errorMessage = $"The path `{path}` contains invalid dots `{path.Substring(part.Start, part.Length)}` while only `.` or `..` are supported";
return string.Empty;
}
// Otherwise, it is a valid path part
continue;
}
if (i - 1 >= 0)
{
var previousSlice = parts[i - 1];
if (!IsDotDot(previousSlice, path))
{
if (previousSlice.Length == 0)
{
errorMessage = $"The path `{path}` cannot go to the parent (..) of a root path /";
return string.Empty;
}
parts.RemoveAt(i--);
parts.RemoveAt(i--);
}
}
}
}
// If we have a single part and it is empty, it is a root
if (parts.Count == 1 && parts[0].Start == 0 && parts[0].End < 0)
{
return "/";
}
var builder = internalHelper.Builder;
builder.Length = 0;
for (i = 0; i < parts.Count; i++)
{
var slice = parts[i];
if (slice.Length > 0)
builder.Append(path, slice.Start, slice.Length);
if (i + 1 < parts.Count)
builder.Append('/');
}
return builder.ToString();
}
private static bool IsDotDot(TextSlice slice, string path)
{
if (slice.Length != 2)
return false;
return path[slice.Start] == '.' && path[slice.End] == '.';
}
private class InternalHelper
{
public readonly StringBuilder Builder;
public readonly List<TextSlice> Slices;
public InternalHelper()
{
Builder = new StringBuilder();
Slices = new List<TextSlice>();
}
}
private readonly struct TextSlice
{
public TextSlice(int start, int end)
{
Start = start;
End = end;
}
public readonly int Start;
public readonly int End;
public int Length => End - Start + 1;
}
/// <inheritdoc />
public int CompareTo(UPath other)
{
return string.Compare(FullName, other.FullName, StringComparison.Ordinal);
}
}