Tests For GetPathRoot and Enabling NormalizeDirectorySeparatorTests (#27494)
authorAnirudh Agnihotry <anirudhagnihotry098@gmail.com>
Tue, 27 Feb 2018 20:04:53 +0000 (12:04 -0800)
committerJan Kotas <jkotas@microsoft.com>
Wed, 28 Feb 2018 02:51:42 +0000 (18:51 -0800)
Corrects its Implementation too

Signed-off-by: dotnet-bot-corefx-mirror <dotnet-bot@microsoft.com>
src/mscorlib/shared/System/IO/PathInternal.Windows.cs

index 433d6b6..f931380 100644 (file)
@@ -190,6 +190,7 @@ namespace System.IO
             int i = 0;
             int volumeSeparatorLength = 2;  // Length to the colon "C:"
             int uncRootLength = 2;          // Length to the start of the server name "\\"
+            int devicePrefixLength = PathInternal.ExtendedPathPrefix.Length;
 
             bool deviceSyntax = IsDevice(path);
             bool deviceUnc = deviceSyntax && IsDeviceUNC(path);
@@ -201,10 +202,10 @@ namespace System.IO
                     // "\\" -> "\\?\UNC\"
                     uncRootLength = UncExtendedPathPrefix.Length;
                 }
-                else
+                else if (devicePrefixLength + 1 < pathLength && path[devicePrefixLength + 1] == VolumeSeparatorChar && IsValidDriveChar(path[devicePrefixLength]))
                 {
                     // "C:" -> "\\?\C:"
-                    volumeSeparatorLength += ExtendedPathPrefix.Length;
+                    volumeSeparatorLength += devicePrefixLength;
                 }
             }
 
@@ -233,7 +234,18 @@ namespace System.IO
                 if (pathLength >= volumeSeparatorLength + 1 && IsDirectorySeparator(path[volumeSeparatorLength]))
                     i++;
             }
-            return i;
+            else if (deviceSyntax && ((devicePrefixLength + 1 >= pathLength) || !(path[devicePrefixLength + 1] == VolumeSeparatorChar)))
+            {
+                i = devicePrefixLength;
+                int n = 1; // Maximum separators to skip
+                while (i < pathLength && (!IsDirectorySeparator(path[i]) || --n > 0))
+                    i++;
+
+                if (i == devicePrefixLength)
+                    i--;
+            }
+
+            return (i < pathLength && IsDirectorySeparator(path[i])) ? i + 1 : i;
         }
 
         private static bool StartsWithOrdinal(ReadOnlySpan<char> source, string value)