diff options
author | Jeremy Kuhne <jeremy.kuhne@microsoft.com> | 2018-03-01 09:30:31 +0300 |
---|---|---|
committer | Stephen Toub <stoub@microsoft.com> | 2018-03-02 01:14:55 +0300 |
commit | 56566b79b5ec2517b4e9f2c5e6f8805d4ad47151 (patch) | |
tree | b1dc3db7e40c564be4f452c9985f1c62fdef173f | |
parent | 8b3392c4bf0efbd7d7051ea21cc93c74a072e9d7 (diff) |
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 <dotnet-bot@microsoft.com>
Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
3 files changed, 47 insertions, 6 deletions
diff --git a/src/System.Private.CoreLib/shared/System/IO/PathInternal.Unix.cs b/src/System.Private.CoreLib/shared/System/IO/PathInternal.Unix.cs index 0e31731a6..fae309be5 100644 --- a/src/System.Private.CoreLib/shared/System/IO/PathInternal.Unix.cs +++ b/src/System.Private.CoreLib/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; - /// <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 diff --git a/src/System.Private.CoreLib/shared/System/IO/PathInternal.Windows.cs b/src/System.Private.CoreLib/shared/System/IO/PathInternal.Windows.cs index 2ef666959..81d51ba4b 100644 --- a/src/System.Private.CoreLib/shared/System/IO/PathInternal.Windows.cs +++ b/src/System.Private.CoreLib/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 == '.'; + } + + /// <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> diff --git a/src/System.Private.CoreLib/shared/System/IO/PathInternal.cs b/src/System.Private.CoreLib/shared/System/IO/PathInternal.cs index f2f350ddd..eb06c2608 100644 --- a/src/System.Private.CoreLib/shared/System/IO/PathInternal.cs +++ b/src/System.Private.CoreLib/shared/System/IO/PathInternal.cs @@ -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> |