From 02da2fd73eb6599955f3348a855affb0273d7801 Mon Sep 17 00:00:00 2001 From: Jeremy Kuhne Date: Wed, 28 Feb 2018 22:30:31 -0800 Subject: [PATCH] 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 --- .../shared/System/IO/PathInternal.Unix.cs | 5 ---- .../shared/System/IO/PathInternal.Windows.cs | 29 +++++++++++++++++++ src/mscorlib/shared/System/IO/PathInternal.cs | 19 +++++++++++- 3 files changed, 47 insertions(+), 6 deletions(-) diff --git a/src/mscorlib/shared/System/IO/PathInternal.Unix.cs b/src/mscorlib/shared/System/IO/PathInternal.Unix.cs index 0e31731a63c8..fae309be5651 100644 --- a/src/mscorlib/shared/System/IO/PathInternal.Unix.cs +++ b/src/mscorlib/shared/System/IO/PathInternal.Unix.cs @@ -84,11 +84,6 @@ internal static bool IsPartiallyQualified(ReadOnlySpan path) 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 2ef666959186..81d51ba4b4c2 100644 --- a/src/mscorlib/shared/System/IO/PathInternal.Windows.cs +++ b/src/mscorlib/shared/System/IO/PathInternal.Windows.cs @@ -70,7 +70,36 @@ internal static bool IsValidDriveChar(char value) 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 f2f350ddd015..eb06c2608cab 100644 --- a/src/mscorlib/shared/System/IO/PathInternal.cs +++ b/src/mscorlib/shared/System/IO/PathInternal.cs @@ -10,13 +10,30 @@ internal static partial class PathInternal /// /// 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. ///