|
1 | 1 | using System;
|
| 2 | +using System.Linq; |
| 3 | +using System.Text; |
2 | 4 |
|
3 | 5 | namespace Testably.Abstractions.Testing.Helpers;
|
4 | 6 |
|
@@ -63,17 +65,191 @@ public override bool IsPathRooted(string? path)
|
63 | 65 | (length >= 2 && IsValidDriveChar(path![0]) && path[1] == VolumeSeparatorChar);
|
64 | 66 | }
|
65 | 67 |
|
| 68 | + /// <summary> |
| 69 | + /// https://github.com/dotnet/runtime/blob/v8.0.3/src/libraries/Common/src/System/IO/PathInternal.Windows.cs#L181 |
| 70 | + /// </summary> |
| 71 | + protected override int GetRootLength(string path) |
| 72 | + { |
| 73 | + bool IsDeviceUNC(string p) |
| 74 | + => p.Length >= 8 |
| 75 | + && IsDevice(p) |
| 76 | + && IsDirectorySeparator(p[7]) |
| 77 | + && p[4] == 'U' |
| 78 | + && p[5] == 'N' |
| 79 | + && p[6] == 'C'; |
| 80 | + |
| 81 | + bool IsDevice(string p) |
| 82 | + => IsExtended(p) |
| 83 | + || |
| 84 | + ( |
| 85 | + p.Length >= 4 |
| 86 | + && IsDirectorySeparator(p[0]) |
| 87 | + && IsDirectorySeparator(p[1]) |
| 88 | + && (p[2] == '.' || p[2] == '?') |
| 89 | + && IsDirectorySeparator(p[3]) |
| 90 | + ); |
| 91 | + |
| 92 | + bool IsExtended(string p) |
| 93 | + => p.Length >= 4 |
| 94 | + && p[0] == '\\' |
| 95 | + && (p[1] == '\\' || p[1] == '?') |
| 96 | + && p[2] == '?' |
| 97 | + && p[3] == '\\'; |
| 98 | + |
| 99 | + int pathLength = path.Length; |
| 100 | + |
| 101 | + if (pathLength > 0 && IsDirectorySeparator(path[0])) |
| 102 | + { |
| 103 | + bool deviceSyntax = IsDevice(path); |
| 104 | + bool deviceUnc = deviceSyntax && IsDeviceUNC(path); |
| 105 | + |
| 106 | + if (deviceSyntax && !deviceUnc) |
| 107 | + { |
| 108 | + return GetRootLengthWithDeviceSyntax(path); |
| 109 | + } |
| 110 | + |
| 111 | + // UNC or simple rooted path (e.g. "\foo", NOT "\\?\C:\foo") |
| 112 | + if (deviceUnc || (path.Length > 1 && IsDirectorySeparator(path[1]))) |
| 113 | + { |
| 114 | + return GetRootLengthWithDeviceUncSyntax(path, deviceUnc); |
| 115 | + } |
| 116 | + |
| 117 | + // Current drive rooted (e.g. "\foo") |
| 118 | + return 1; |
| 119 | + } |
| 120 | + |
| 121 | + if (pathLength >= 2 |
| 122 | + && path[1] == ':' |
| 123 | + && IsValidDriveChar(path[0])) |
| 124 | + { |
| 125 | + // If the colon is followed by a directory separator, move past it (e.g "C:\") |
| 126 | + if (pathLength > 2 && IsDirectorySeparator(path[2])) |
| 127 | + { |
| 128 | + return 3; |
| 129 | + } |
| 130 | + |
| 131 | + // Valid drive specified path ("C:", "D:", etc.) |
| 132 | + return 2; |
| 133 | + } |
| 134 | + |
| 135 | + return 0; |
| 136 | + } |
| 137 | + |
| 138 | + private int GetRootLengthWithDeviceSyntax(string path) |
| 139 | + { |
| 140 | + // Device path (e.g. "\\?\.", "\\.\") |
| 141 | + // Skip any characters following the prefix that aren't a separator |
| 142 | + int i = 4; |
| 143 | + while (i < path.Length && !IsDirectorySeparator(path[i])) |
| 144 | + { |
| 145 | + i++; |
| 146 | + } |
| 147 | + |
| 148 | + // If there is another separator take it, as long as we have had at least one |
| 149 | + // non-separator after the prefix (e.g. don't take "\\?\\", but take "\\?\a\") |
| 150 | + if (i < path.Length && i > 4 && IsDirectorySeparator(path[i])) |
| 151 | + { |
| 152 | + i++; |
| 153 | + } |
| 154 | + |
| 155 | + return i; |
| 156 | + } |
| 157 | + |
| 158 | + private int GetRootLengthWithDeviceUncSyntax(string path, |
| 159 | + bool deviceUnc) |
| 160 | + { |
| 161 | + // Start past the prefix ("\\" or "\\?\UNC\") |
| 162 | + int i = deviceUnc ? 8 : 2; |
| 163 | + |
| 164 | + // Skip two separators at most |
| 165 | + int n = 2; |
| 166 | + while (i < path.Length && (!IsDirectorySeparator(path[i]) || --n > 0)) |
| 167 | + { |
| 168 | + i++; |
| 169 | + } |
| 170 | + |
| 171 | + return i; |
| 172 | + } |
| 173 | + |
66 | 174 | /// <summary>
|
67 | 175 | /// https://github.com/dotnet/runtime/blob/v8.0.4/src/libraries/Common/src/System/IO/PathInternal.Windows.cs#L280
|
68 | 176 | /// </summary>
|
69 | 177 | protected override bool IsDirectorySeparator(char c)
|
70 | 178 | => c == DirectorySeparatorChar || c == AltDirectorySeparatorChar;
|
71 | 179 |
|
| 180 | + /// <summary> |
| 181 | + /// https://github.com/dotnet/runtime/blob/v8.0.4/src/libraries/Common/src/System/IO/PathInternal.Windows.cs#L381 |
| 182 | + /// </summary> |
| 183 | + protected override bool IsEffectivelyEmpty(string path) |
| 184 | + { |
| 185 | + if (string.IsNullOrEmpty(path)) |
| 186 | + { |
| 187 | + return true; |
| 188 | + } |
| 189 | + |
| 190 | + return path.All(c => c == ' '); |
| 191 | + } |
| 192 | + |
72 | 193 | /// <summary>
|
73 | 194 | /// Returns true if the given character is a valid drive letter
|
74 | 195 | /// </summary>
|
75 | 196 | /// <remarks>https://github.com/dotnet/runtime/blob/v8.0.3/src/libraries/Common/src/System/IO/PathInternal.Windows.cs#L72</remarks>
|
76 | 197 | private static bool IsValidDriveChar(char value)
|
77 | 198 | => (uint)((value | 0x20) - 'a') <= 'z' - 'a';
|
| 199 | + |
| 200 | + /// <summary> |
| 201 | + /// https://github.com/dotnet/runtime/blob/v8.0.4/src/libraries/Common/src/System/IO/PathInternal.Windows.cs#L318 |
| 202 | + /// </summary> |
| 203 | + protected override string NormalizeDirectorySeparators(string path) |
| 204 | + { |
| 205 | + bool IsAlreadyNormalized() |
| 206 | + { |
| 207 | + for (int i = 1; i < path.Length; i++) |
| 208 | + { |
| 209 | + char current = path[i]; |
| 210 | + if (IsDirectorySeparator(current) |
| 211 | + && (current != DirectorySeparatorChar |
| 212 | + || (i + 1 < path.Length && IsDirectorySeparator(path[i + 1])))) |
| 213 | + { |
| 214 | + return false; |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + return true; |
| 219 | + } |
| 220 | + |
| 221 | + if (IsAlreadyNormalized()) |
| 222 | + { |
| 223 | + return path; |
| 224 | + } |
| 225 | + |
| 226 | + StringBuilder builder = new(); |
| 227 | + |
| 228 | + int start = 0; |
| 229 | + if (IsDirectorySeparator(path[start])) |
| 230 | + { |
| 231 | + start++; |
| 232 | + builder.Append(DirectorySeparatorChar); |
| 233 | + } |
| 234 | + |
| 235 | + for (int i = start; i < path.Length; i++) |
| 236 | + { |
| 237 | + char current = path[i]; |
| 238 | + |
| 239 | + if (IsDirectorySeparator(current)) |
| 240 | + { |
| 241 | + if (i + 1 < path.Length && IsDirectorySeparator(path[i + 1])) |
| 242 | + { |
| 243 | + continue; |
| 244 | + } |
| 245 | + |
| 246 | + current = DirectorySeparatorChar; |
| 247 | + } |
| 248 | + |
| 249 | + builder.Append(current); |
| 250 | + } |
| 251 | + |
| 252 | + return builder.ToString(); |
| 253 | + } |
78 | 254 | }
|
79 | 255 | }
|
0 commit comments