Path whitespace changes. (#12862)
authorNina Chikanov <nchikanov@gmail.com>
Tue, 18 Jul 2017 04:49:06 +0000 (21:49 -0700)
committerJeremy Kuhne <jeremy.kuhne@microsoft.com>
Tue, 18 Jul 2017 04:49:06 +0000 (21:49 -0700)
* Path whitespace changes.

Change IsNullOrWhitespace to IsNullOrEmpty to be more consistent with legacy desktop .NET code. Path.GetFullPath no longer trims trailing whitespace. (Windows will still eat trailing spaces)

* Fix compilation errors with searchPattern

src/mscorlib/shared/System/IO/Path.Unix.cs
src/mscorlib/shared/System/IO/Path.Windows.cs
src/mscorlib/shared/System/IO/Path.cs
src/mscorlib/shared/System/IO/PathHelper.Windows.cs
src/mscorlib/shared/System/IO/PathInternal.Unix.cs
src/mscorlib/shared/System/IO/PathInternal.Windows.cs
src/mscorlib/shared/System/IO/PathInternal.cs
src/mscorlib/src/System/IO/FileSystemEnumerable.cs

index c52cffc..613bb1f 100644 (file)
@@ -188,10 +188,10 @@ namespace System.IO
         public static string GetPathRoot(string path)
         {
             if (path == null) return null;
-                       if (string.IsNullOrWhiteSpace(path))
+            if (PathInternal.IsEffectivelyEmpty(path))
                 throw new ArgumentException(SR.Arg_PathEmpty, nameof(path));
 
-                       return IsPathRooted(path) ? PathInternal.DirectorySeparatorCharAsString : String.Empty;
+            return IsPathRooted(path) ? PathInternal.DirectorySeparatorCharAsString : String.Empty;
         }
 
         /// <summary>Gets whether the system is case-sensitive.</summary>
index 6dd4d71..4397171 100644 (file)
@@ -75,7 +75,7 @@ namespace System.IO
             }
 
             // Technically this doesn't matter but we used to throw for this case
-            if (string.IsNullOrWhiteSpace(path))
+            if (PathInternal.IsEffectivelyEmpty(path))
                 throw new ArgumentException(SR.Arg_PathEmpty, nameof(path));
 
             // We don't want to check invalid characters for device format- see comments for extended above
@@ -141,7 +141,7 @@ namespace System.IO
         public static string GetPathRoot(string path)
         {
             if (path == null) return null;
-            if (string.IsNullOrWhiteSpace(path)) 
+            if (string.IsNullOrEmpty(path))
                 throw new ArgumentException(SR.Arg_PathEmpty, nameof(path));
 
             PathInternal.CheckInvalidPathChars(path);
index 6db635d..aa7c5cd 100644 (file)
@@ -76,7 +76,7 @@ namespace System.IO
         // "\\server\share").
         public static string GetDirectoryName(string path)
         {
-            if (string.IsNullOrWhiteSpace(path))
+            if (PathInternal.IsEffectivelyEmpty(path))
             {
                 if (path == null) return null;
                 throw new ArgumentException(SR.Arg_PathEmpty, nameof(path));
@@ -491,7 +491,7 @@ namespace System.IO
         private static string GetRelativePath(string relativeTo, string path, StringComparison comparisonType)
         {
             if (string.IsNullOrEmpty(relativeTo)) throw new ArgumentNullException(nameof(relativeTo));
-            if (string.IsNullOrWhiteSpace(path)) throw new ArgumentNullException(nameof(path));
+            if (PathInternal.IsEffectivelyEmpty(path)) throw new ArgumentNullException(nameof(path));
             Debug.Assert(comparisonType == StringComparison.Ordinal || comparisonType == StringComparison.OrdinalIgnoreCase);
 
             relativeTo = GetFullPath(relativeTo);
index 78b62c2..a0dba66 100644 (file)
@@ -23,7 +23,7 @@ namespace System.IO
         /// Normalize the given path.
         /// </summary>
         /// <remarks>
-        /// Normalizes via Win32 GetFullPathName(). It will also trim all "typical" whitespace at the end of the path (see s_trimEndChars). Will also trim initial
+        /// Normalizes via Win32 GetFullPathName(). Will also trim initial
         /// spaces if the path is determined to be rooted.
         /// 
         /// Note that invalid characters will be checked after the path is normalized, which could remove bad characters. (C:\|\..\a.txt -- C:\a.txt)
@@ -47,9 +47,6 @@ namespace System.IO
             {
                 GetFullPathName(path, ref fullPath);
 
-                // Trim whitespace off the end of the string. Win32 normalization trims only U+0020.
-                fullPath.TrimEnd(PathInternal.s_trimEndChars);
-
                 // Checking path validity used to happen before getting the full path name. To avoid additional input allocation
                 // (to trim trailing whitespace) we now do it after the Win32 call. This will allow legitimate paths through that
                 // used to get kicked back (notably segments with invalid characters might get removed via "..").
index ac9c4e7..1113201 100644 (file)
@@ -105,5 +105,15 @@ namespace System.IO
             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 
+        /// just spaces ((char)32).
+        /// </summary>
+        internal static bool IsEffectivelyEmpty(string path)
+        {
+            return string.IsNullOrEmpty(path);
+        }
     }
 }
index a82698d..01069e7 100644 (file)
@@ -441,5 +441,23 @@ namespace System.IO
             EndsInDirectorySeparator(path) ?
             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 
+        /// just spaces ((char)32).
+        /// </summary>
+        internal static bool IsEffectivelyEmpty(string path)
+        {
+            if (string.IsNullOrEmpty(path))
+                return true;
+
+            foreach (char c in path)
+            {
+                if (c != ' ')
+                    return false;
+            }
+            return true;
+        }
     }
 }
index 63753e9..9362636 100644 (file)
@@ -10,22 +10,6 @@ namespace System.IO
     /// <summary>Contains internal path helpers that are shared between many projects.</summary>
     internal static partial class PathInternal
     {
-        // Trim trailing white spaces, tabs etc but don't be aggressive in removing everything that has UnicodeCategory of trailing space.
-        // string.WhitespaceChars will trim more aggressively than what the underlying FS does (for ex, NTFS, FAT).
-        //
-        // (This is for compatibility with old behavior.)
-        internal static readonly char[] s_trimEndChars =
-        {
-            (char)0x9,          // Horizontal tab
-            (char)0xA,          // Line feed
-            (char)0xB,          // Vertical tab
-            (char)0xC,          // Form feed
-            (char)0xD,          // Carriage return
-            (char)0x20,         // Space
-            (char)0x85,         // Next line
-            (char)0xA0          // Non breaking space
-        };
-
         /// <summary>
         /// Checks for invalid path characters in the given path.
         /// </summary>
index 5e19fbe..ed482c7 100644 (file)
@@ -496,17 +496,14 @@ namespace System.IO
         {
             Contract.Requires(searchPattern != null);
 
-            // Win32 normalization trims only U+0020.
-            String tempSearchPattern = searchPattern.TrimEnd(PathInternal.s_trimEndChars);
-
             // Make this corner case more useful, like dir
-            if (tempSearchPattern.Equals("."))
+            if (searchPattern.Equals("."))
             {
-                tempSearchPattern = "*";
+                return "*";
             }
 
-            PathInternal.CheckSearchPattern(tempSearchPattern);
-            return tempSearchPattern;
+            PathInternal.CheckSearchPattern(searchPattern);
+            return searchPattern;
         }
 
         private static String GetNormalizedSearchCriteria(String fullSearchString, String fullPathMod)