Skip to content
Merged
Changes from all commits
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
32 changes: 14 additions & 18 deletions src/libraries/Common/src/System/IO/PathInternal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,30 +50,26 @@ internal static int GetCommonPathLength(string first, string second, bool ignore
/// <summary>
/// Gets the count of common characters from the left optionally ignoring case
/// </summary>
internal static unsafe int EqualStartingCharacterCount(string? first, string? second, bool ignoreCase)
internal static int EqualStartingCharacterCount(string? first, string? second, bool ignoreCase)
{
if (string.IsNullOrEmpty(first) || string.IsNullOrEmpty(second)) return 0;

int commonChars = 0;

fixed (char* f = first)
fixed (char* s = second)
if (string.IsNullOrEmpty(first) || string.IsNullOrEmpty(second))
{
char* l = f;
char* r = s;
char* leftEnd = l + first.Length;
char* rightEnd = r + second.Length;
return 0;
}

while (l != leftEnd && r != rightEnd
&& (*l == *r || (ignoreCase && char.ToUpperInvariant(*l) == char.ToUpperInvariant(*r))))
int commonLength = first.AsSpan().CommonPrefixLength(second);
if (ignoreCase)
{
for (; (uint)commonLength < (uint)first.Length; commonLength++)
{
commonChars++;
l++;
r++;
if (commonLength >= second.Length ||
char.ToUpperInvariant(first[commonLength]) != char.ToUpperInvariant(second[commonLength]))
{
break;
}
}
}

return commonChars;
return commonLength;
}

/// <summary>
Expand Down
Loading