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 IndexOfAnyInRange in StripBidiControlCharacters #78658

Merged
merged 2 commits into from Nov 22, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 14 additions & 20 deletions src/libraries/System.Private.Uri/src/System/UriHelper.cs
Expand Up @@ -570,51 +570,45 @@ internal static bool IsLWS(char ch)
return (ch <= ' ') && (ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t');
}

//
// Is this a Bidirectional control char.. These get stripped
//
internal static bool IsBidiControlCharacter(char ch)
{
return (ch == '\u200E' /*LRM*/ || ch == '\u200F' /*RLM*/ || ch == '\u202A' /*LRE*/ ||
ch == '\u202B' /*RLE*/ || ch == '\u202C' /*PDF*/ || ch == '\u202D' /*LRO*/ ||
ch == '\u202E' /*RLO*/);
}
internal static bool IsBidiControlCharacter(char ch) =>
char.IsBetween(ch, '\u200E', '\u202E') && !char.IsBetween(ch, '\u2010', '\u2029');

//
// Strip Bidirectional control characters from this string
//
internal static unsafe string StripBidiControlCharacters(ReadOnlySpan<char> strToClean, string? backingString = null)
{
Debug.Assert(backingString is null || strToClean.Length == backingString.Length);

int charsToRemove = 0;
foreach (char c in strToClean)

int indexOfPossibleCharToRemove = strToClean.IndexOfAnyInRange('\u200E', '\u202E');
if (indexOfPossibleCharToRemove >= 0)
{
if ((uint)(c - '\u200E') <= ('\u202E' - '\u200E') && IsBidiControlCharacter(c))
// Slow path: Contains chars that fall in the [u200E, u202E] range (so likely Bidi)
foreach (char c in strToClean.Slice(indexOfPossibleCharToRemove))
{
charsToRemove++;
if (IsBidiControlCharacter(c))
{
charsToRemove++;
}
}
}

if (charsToRemove == 0)
{
// Hot path
return backingString ?? new string(strToClean);
}

if (charsToRemove == strToClean.Length)
{
return string.Empty;
}
stephentoub marked this conversation as resolved.
Show resolved Hide resolved

fixed (char* pStrToClean = &MemoryMarshal.GetReference(strToClean))
{
return string.Create(strToClean.Length - charsToRemove, (StrToClean: (IntPtr)pStrToClean, strToClean.Length), (buffer, state) =>
return string.Create(strToClean.Length - charsToRemove, (StrToClean: (IntPtr)pStrToClean, strToClean.Length), static (buffer, state) =>
{
var strToClean = new ReadOnlySpan<char>((char*)state.StrToClean, state.Length);
int destIndex = 0;
foreach (char c in strToClean)
{
if ((uint)(c - '\u200E') > ('\u202E' - '\u200E') || !IsBidiControlCharacter(c))
if (!IsBidiControlCharacter(c))
{
buffer[destIndex++] = c;
}
Expand Down