Remove span helpers, use mirrored PathInternal helper (#27585)
authorJeremy Kuhne <jeremy.kuhne@microsoft.com>
Thu, 1 Mar 2018 06:30:31 +0000 (22:30 -0800)
committerAnirudh Agnihotry <anirudhagnihotry098@gmail.com>
Thu, 1 Mar 2018 11:10:08 +0000 (03:10 -0800)
* Remove span helpers, use mirrored PathInternal helper

Remove other unnecessary helpers (notably with Path.Join available)

* Fix Unix

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

index 0e31731..fae309b 100644 (file)
@@ -84,11 +84,6 @@ namespace System.IO
             return !Path.IsPathRooted(path);
         }
 
-        internal static string TrimEndingDirectorySeparator(string path) =>
-            path.Length > 1 && IsDirectorySeparator(path[path.Length - 1]) ? // exclude root "/"
-            path.Substring(0, path.Length - 1) :
-            path;
-
         /// <summary>
         /// Returns true if the path is effectively empty for the current OS.
         /// For unix, this is empty or null. For Windows, this is empty, null, or 
index 2ef6669..81d51ba 100644 (file)
@@ -70,7 +70,36 @@ namespace System.IO
             return ((value >= 'A' && value <= 'Z') || (value >= 'a' && value <= 'z'));
         }
 
+        private static bool EndsWithPeriodOrSpace(string path)
+        {
+            if (string.IsNullOrEmpty(path))
+                return false;
+
+            char c = path[path.Length - 1];
+            return c == ' ' || c == '.';
+        }
+
+        /// <summary>
+        /// Adds the extended path prefix (\\?\) if not already a device path, IF the path is not relative,
+        /// AND the path is more than 259 characters. (> MAX_PATH + null). This will also insert the extended
+        /// prefix if the path ends with a period or a space. Trailing periods and spaces are normally eaten
+        /// away from paths during normalization, but if we see such a path at this point it should be
+        /// normalized and has retained the final characters. (Typically from one of the *Info classes)
+        /// </summary>
+        internal static string EnsureExtendedPrefixIfNeeded(string path)
+        {
+            if (path != null && (path.Length >= MaxShortPath || EndsWithPeriodOrSpace(path)))
+            {
+                return EnsureExtendedPrefix(path);
+            }
+            else
+            {
+                return path;
+            }
+        }
+
         /// <summary>
+        /// DO NOT USE- Use EnsureExtendedPrefixIfNeeded. This will be removed shortly.
         /// Adds the extended path prefix (\\?\) if not already a device path, IF the path is not relative,
         /// AND the path is more than 259 characters. (> MAX_PATH + null)
         /// </summary>
index f2f350d..eb06c26 100644 (file)
@@ -10,13 +10,30 @@ namespace System.IO
         /// <summary>
         /// Returns true if the path ends in a directory separator.
         /// </summary>
-        internal static bool EndsInDirectorySeparator(ReadOnlySpan<char> path) => path.Length > 0 && IsDirectorySeparator(path[path.Length - 1]);
+        internal static bool EndsInDirectorySeparator(ReadOnlySpan<char> path)
+            => path.Length > 0 && IsDirectorySeparator(path[path.Length - 1]);
 
         /// <summary>
         /// Returns true if the path starts in a directory separator.
         /// </summary>
         internal static bool StartsWithDirectorySeparator(ReadOnlySpan<char> path) => path.Length > 0 && IsDirectorySeparator(path[0]);
 
+        internal static string EnsureTrailingSeparator(string path)
+            => EndsInDirectorySeparator(path) ? path : path + DirectorySeparatorCharAsString;
+
+        internal static string TrimEndingDirectorySeparator(string path) =>
+            EndsInDirectorySeparator(path) && !IsRoot(path) ?
+                path.Substring(0, path.Length - 1) :
+                path;
+
+        internal static ReadOnlySpan<char> TrimEndingDirectorySeparator(ReadOnlySpan<char> path) =>
+            EndsInDirectorySeparator(path) && !IsRoot(path) ?
+                path.Slice(0, path.Length - 1) :
+                path;
+
+        internal static bool IsRoot(ReadOnlySpan<char> path)
+            => path.Length == GetRootLength(path);
+
         /// <summary>
         /// Get the common path length from the start of the string.
         /// </summary>