From 95ba079c56f822c13f0211fda6f262cad66076eb Mon Sep 17 00:00:00 2001 From: EgorBo Date: Sun, 7 Aug 2022 19:36:35 +0200 Subject: [PATCH 1/2] Improve IndexOf(char, OrdinalIgnoreCase) --- .../src/System/String.Searching.cs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/String.Searching.cs b/src/libraries/System.Private.CoreLib/src/System/String.Searching.cs index 150018288ae096..9dc40bf7c3ddf9 100644 --- a/src/libraries/System.Private.CoreLib/src/System/String.Searching.cs +++ b/src/libraries/System.Private.CoreLib/src/System/String.Searching.cs @@ -60,13 +60,29 @@ public int IndexOf(char value, StringComparison comparisonType) return IndexOf(value); case StringComparison.OrdinalIgnoreCase: - return CompareInfo.Invariant.IndexOf(this, value, CompareOptions.OrdinalIgnoreCase); + return IndexOfCharOrdinalIgnoreCase(value); default: throw new ArgumentException(SR.NotSupported_StringComparison, nameof(comparisonType)); } } + private int IndexOfCharOrdinalIgnoreCase(char value) + { + if (!char.IsAscii(value)) + { + return CompareInfo.Invariant.IndexOf(this, value, CompareOptions.OrdinalIgnoreCase); + } + + if (char.IsAsciiLetter(value)) + { + char valueUc = (char)(value | 0x20); + char valueLc = (char)(value & ~0x20); + return SpanHelpers.IndexOfAny(ref _firstChar, valueLc, valueUc, Length); + } + return SpanHelpers.IndexOf(ref _firstChar, value, Length); + } + public unsafe int IndexOf(char value, int startIndex, int count) { if ((uint)startIndex > (uint)Length) From 22ef47c6dc0f245e1f21769fcaf28c07dafe746d Mon Sep 17 00:00:00 2001 From: Egor Bogatov Date: Sun, 7 Aug 2022 21:25:04 +0200 Subject: [PATCH 2/2] Update src/libraries/System.Private.CoreLib/src/System/String.Searching.cs Co-authored-by: Stephen Toub --- .../System.Private.CoreLib/src/System/String.Searching.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libraries/System.Private.CoreLib/src/System/String.Searching.cs b/src/libraries/System.Private.CoreLib/src/System/String.Searching.cs index 9dc40bf7c3ddf9..945959f104a7e3 100644 --- a/src/libraries/System.Private.CoreLib/src/System/String.Searching.cs +++ b/src/libraries/System.Private.CoreLib/src/System/String.Searching.cs @@ -80,6 +80,7 @@ private int IndexOfCharOrdinalIgnoreCase(char value) char valueLc = (char)(value & ~0x20); return SpanHelpers.IndexOfAny(ref _firstChar, valueLc, valueUc, Length); } + return SpanHelpers.IndexOf(ref _firstChar, value, Length); }