Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Commit

Permalink
Remove span helpers, use mirrored PathInternal helper (#27585)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
JeremyKuhne authored and Anipik committed Mar 1, 2018
1 parent 3a95c15 commit 02da2fd
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
5 changes: 0 additions & 5 deletions src/mscorlib/shared/System/IO/PathInternal.Unix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,6 @@ internal static bool IsPartiallyQualified(ReadOnlySpan<char> 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;

/// <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
Expand Down
29 changes: 29 additions & 0 deletions src/mscorlib/shared/System/IO/PathInternal.Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 == '.';
}

/// <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>
Expand Down
19 changes: 18 additions & 1 deletion src/mscorlib/shared/System/IO/PathInternal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,30 @@ internal static partial class PathInternal
/// <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>
Expand Down

0 comments on commit 02da2fd

Please sign in to comment.