Skip to content

Commit

Permalink
Add MaybeAdjustFilePath(ReadOnlyMemory<char>)
Browse files Browse the repository at this point in the history
  • Loading branch information
ladipro committed Feb 7, 2021
1 parent 3d351bd commit a6ec133
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/Shared/FileUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,42 @@ internal static string MaybeAdjustFilePath(string value, string baseDirectory =
return shouldAdjust ? newValue.ToString() : value;
}

#if !CLR2COMPATIBILITY
/// <summary>
/// If on Unix, convert backslashes to slashes for strings that resemble paths.
/// This overload takes and returns ReadOnlyMemory of characters.
/// </summary>
internal static ReadOnlyMemory<char> MaybeAdjustFilePath(ReadOnlyMemory<char> value, string baseDirectory = "")
{
if (NativeMethodsShared.IsWindows || value.IsEmpty)
{
return value;
}

// Don't bother with arrays or properties or network paths.
if (value.Length >= 2)
{
var span = value.Span;
if ((span[1] == '(' && (span[0] == '$' || span[0] == '@')) ||
(span[1] == '\\' && span[0] == '\\'))
{
return value;
}
}

// For Unix-like systems, we may want to convert backslashes to slashes
#if FEATURE_SPAN
Span<char> newValue = ConvertToUnixSlashes(value.ToArray());
#else
string newValue = ConvertToUnixSlashes(value.ToString());
#endif

// Find the part of the name we want to check, that is remove quotes, if present
bool shouldAdjust = newValue.IndexOf('/') != -1 && LooksLikeUnixFilePath(RemoveQuotes(newValue), baseDirectory);
return shouldAdjust ? newValue.ToString().AsMemory() : value;
}
#endif

#if !FEATURE_SPAN
private static string ConvertToUnixSlashes(string path)
{
Expand Down

0 comments on commit a6ec133

Please sign in to comment.