Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use SequenceEqual in win32 GetFullPath #10706

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 1 addition & 26 deletions src/Framework/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1526,7 +1526,7 @@ internal static unsafe string GetFullPath(string path)
char* buffer = stackalloc char[bufferSize];
int fullPathLength = GetFullPathWin32(path, bufferSize, buffer, IntPtr.Zero);
// Avoid creating new strings unnecessarily
return AreStringsEqual(buffer, fullPathLength, path) ? path : new string(buffer, startIndex: 0, length: fullPathLength);
return MemoryExtensions.SequenceEqual(path.AsSpan(), new ReadOnlySpan<char>(buffer, fullPathLength)) ? path : new string(buffer, startIndex: 0, length: fullPathLength);
}

[SupportedOSPlatform("windows")]
Expand All @@ -1537,31 +1537,6 @@ private static unsafe int GetFullPathWin32(string target, int bufferLength, char
return pathLength;
}

/// <summary>
/// Compare an unsafe char buffer with a <see cref="System.String"/> to see if their contents are identical.
/// </summary>
/// <param name="buffer">The beginning of the char buffer.</param>
/// <param name="len">The length of the buffer.</param>
/// <param name="s">The string.</param>
/// <returns>True only if the contents of <paramref name="s"/> and the first <paramref name="len"/> characters in <paramref name="buffer"/> are identical.</returns>
private static unsafe bool AreStringsEqual(char* buffer, int len, string s)
{
if (len != s.Length)
{
return false;
}

foreach (char ch in s)
{
if (ch != *buffer++)
{
return false;
}
}

return true;
}

internal static void VerifyThrowWin32Result(int result)
{
bool isError = result == 0;
Expand Down
Loading