-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathHelper.cs
More file actions
234 lines (193 loc) · 7.52 KB
/
Copy pathPathHelper.cs
File metadata and controls
234 lines (193 loc) · 7.52 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
using System;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;
namespace NETMetaCoder
{
/// <summary>
/// A helper class for manipulating file paths.
/// </summary>
public static class PathHelper
{
/// <summary>
/// Get the path to a file, relative to another path.
/// </summary>
/// <param name="relativeTo"></param>
/// <param name="path"></param>
/// <returns></returns>
public static string GetRelativePath(string relativeTo, string path)
{
return GetRelativePath(relativeTo, path, StringComparison);
}
private static string GetRelativePath(string relativeTo, string path, StringComparison comparisonType)
{
if (relativeTo == null)
{
throw new ArgumentNullException(nameof(relativeTo));
}
if (relativeTo.AsSpan().IsEmpty)
{
throw new ArgumentException("The relative path must not be empty.", nameof(relativeTo));
}
if (path == null)
{
throw new ArgumentNullException(nameof(path));
}
if (path.AsSpan().IsEmpty)
{
throw new ArgumentException("The relative path must not be empty.", nameof(path));
}
System.Diagnostics.Debug.Assert(comparisonType == StringComparison.Ordinal ||
comparisonType == StringComparison.OrdinalIgnoreCase);
relativeTo = Path.GetFullPath(relativeTo);
path = Path.GetFullPath(path);
// Need to check if the roots are different- if they are we need to return the "to" path.
if (!AreRootsEqual(relativeTo, path, comparisonType))
{
return path;
}
var commonLength =
GetCommonPathLength(relativeTo, path, comparisonType == StringComparison.OrdinalIgnoreCase);
// 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
var relativeToLength = relativeTo.Length;
if (EndsInDirectorySeparator(relativeTo.AsSpan()))
{
relativeToLength--;
}
var pathEndsInSeparator = EndsInDirectorySeparator(path.AsSpan());
var pathLength = path.Length;
if (pathEndsInSeparator)
{
pathLength--;
}
// If we have effectively the same path, return "."
if (relativeToLength == pathLength && commonLength >= relativeToLength)
{
return ".";
}
var sb = new StringBuilder(Math.Max(relativeTo.Length, path.Length));
// Add parent segments for segments past the common on the "from" path
if (commonLength < relativeToLength)
{
sb.Append("..");
for (var i = commonLength + 1; i < relativeToLength; i++)
{
if (IsDirectorySeparatorChar(relativeTo[i]))
{
sb.Append(Path.DirectorySeparatorChar);
sb.Append("..");
}
}
}
else if (IsDirectorySeparatorChar(path[commonLength]))
{
// No parent segments and we need to eat the initial separator
// (C:\Foo C:\Foo\Bar case)
commonLength++;
}
// Now add the rest of the "to" path, adding back the trailing separator
var differenceLength = pathLength - commonLength;
if (pathEndsInSeparator)
{
differenceLength++;
}
if (differenceLength > 0)
{
if (sb.Length > 0)
{
sb.Append(Path.DirectorySeparatorChar);
}
sb.Append(path, commonLength, differenceLength);
}
return sb.ToString();
}
private static bool EndsInDirectorySeparator(ReadOnlySpan<char> path)
=> path.Length > 0 && IsDirectorySeparatorChar(path[path.Length - 1]);
private static StringComparison StringComparison =>
IsCaseSensitive ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase;
private static bool IsCaseSensitive
{
get
{
#if PLATFORM_WINDOWS || PLATFORM_OSX
return false;
#else
return true;
#endif
}
}
private static bool AreRootsEqual(string first, string second, StringComparison comparisonType)
{
var firstRootLength = GetRootLength(first.AsSpan());
var secondRootLength = GetRootLength(second.AsSpan());
return firstRootLength == secondRootLength &&
string.Compare(
strA: first,
indexA: 0,
strB: second,
indexB: 0,
length: firstRootLength,
comparisonType: comparisonType) ==
0;
}
private static int GetRootLength(ReadOnlySpan<char> path) =>
path.Length > 0 && IsDirectorySeparatorChar(path[0]) ? 1 : 0;
private static int GetCommonPathLength(string first, string second, bool ignoreCase)
{
var commonChars = EqualStartingCharacterCount(first, second, ignoreCase: ignoreCase);
// 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 || IsDirectorySeparatorChar(second[commonChars])))
{
return commonChars;
}
if (commonChars == second.Length && IsDirectorySeparatorChar(first[commonChars]))
{
return commonChars;
}
// It's possible we matched somewhere in the middle of a segment e.g. C:\Foodie and C:\Foobar.
while (commonChars > 0 && first[commonChars - 1] != Path.DirectorySeparatorChar)
{
commonChars--;
}
return commonChars;
}
private static unsafe int EqualStartingCharacterCount(string first, string second, bool ignoreCase)
{
if (string.IsNullOrEmpty(first) || string.IsNullOrEmpty(second))
{
return 0;
}
var commonChars = 0;
fixed (char* f = first)
fixed (char* s = second)
{
char* l = f;
char* r = s;
char* leftEnd = l + first.Length;
char* rightEnd = r + second.Length;
while (l != leftEnd &&
r != rightEnd &&
(*l == *r || (ignoreCase && char.ToUpperInvariant((*l)) == char.ToUpperInvariant((*r)))))
{
commonChars++;
l++;
r++;
}
}
return commonChars;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool IsDirectorySeparatorChar(char c) => c == Path.DirectorySeparatorChar;
}
}