* 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
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>
}
// 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
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);
// "\\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));
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);
/// 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)
{
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 "..").
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);
+ }
}
}
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;
+ }
}
}
/// <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>
{
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)