From: Jeremy Kuhne Date: Thu, 1 Mar 2018 06:30:31 +0000 (-0800) Subject: Remove span helpers, use mirrored PathInternal helper (#27585) X-Git-Tag: accepted/tizen/unified/20190422.045933~2818 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=02da2fd73eb6599955f3348a855affb0273d7801;p=platform%2Fupstream%2Fcoreclr.git Remove span helpers, use mirrored PathInternal helper (#27585) * 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 --- diff --git a/src/mscorlib/shared/System/IO/PathInternal.Unix.cs b/src/mscorlib/shared/System/IO/PathInternal.Unix.cs index 0e31731..fae309b 100644 --- a/src/mscorlib/shared/System/IO/PathInternal.Unix.cs +++ b/src/mscorlib/shared/System/IO/PathInternal.Unix.cs @@ -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; - /// /// 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 diff --git a/src/mscorlib/shared/System/IO/PathInternal.Windows.cs b/src/mscorlib/shared/System/IO/PathInternal.Windows.cs index 2ef6669..81d51ba 100644 --- a/src/mscorlib/shared/System/IO/PathInternal.Windows.cs +++ b/src/mscorlib/shared/System/IO/PathInternal.Windows.cs @@ -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 == '.'; + } + + /// + /// 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) + /// + internal static string EnsureExtendedPrefixIfNeeded(string path) + { + if (path != null && (path.Length >= MaxShortPath || EndsWithPeriodOrSpace(path))) + { + return EnsureExtendedPrefix(path); + } + else + { + return path; + } + } + /// + /// 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) /// diff --git a/src/mscorlib/shared/System/IO/PathInternal.cs b/src/mscorlib/shared/System/IO/PathInternal.cs index f2f350d..eb06c26 100644 --- a/src/mscorlib/shared/System/IO/PathInternal.cs +++ b/src/mscorlib/shared/System/IO/PathInternal.cs @@ -10,13 +10,30 @@ namespace System.IO /// /// Returns true if the path ends in a directory separator. /// - internal static bool EndsInDirectorySeparator(ReadOnlySpan path) => path.Length > 0 && IsDirectorySeparator(path[path.Length - 1]); + internal static bool EndsInDirectorySeparator(ReadOnlySpan path) + => path.Length > 0 && IsDirectorySeparator(path[path.Length - 1]); /// /// Returns true if the path starts in a directory separator. /// internal static bool StartsWithDirectorySeparator(ReadOnlySpan 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 TrimEndingDirectorySeparator(ReadOnlySpan path) => + EndsInDirectorySeparator(path) && !IsRoot(path) ? + path.Slice(0, path.Length - 1) : + path; + + internal static bool IsRoot(ReadOnlySpan path) + => path.Length == GetRootLength(path); + /// /// Get the common path length from the start of the string. ///