Skip to content

Commit 174c5d4

Browse files
Merge pull request #9405 from h3xds1nz/remove-case-insensitive-comparers
Replace CaseInsensitiveComparer with generics in Speller, remove allocations
2 parents f0f777d + 701b219 commit 174c5d4

File tree

1 file changed

+9
-21
lines changed
  • src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents

1 file changed

+9
-21
lines changed

src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/Speller.cs

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ internal Speller(TextEditor textEditor)
5353

5454
_defaultCulture = InputLanguageManager.Current != null ? InputLanguageManager.Current.CurrentInputLanguage :
5555
Thread.CurrentThread.CurrentCulture;
56+
_defaultComparer = StringComparer.Create(_defaultCulture, true);
5657
}
5758

5859
#endregion Constructors
@@ -224,17 +225,15 @@ internal IList GetSuggestionsForError(SpellingError error)
224225
// implement this as a process-wide list.
225226
internal void IgnoreAll(string word)
226227
{
227-
if (_ignoredWordsList == null)
228-
{
229-
_ignoredWordsList = new ArrayList(1);
230-
}
228+
if (_ignoredWordsList is null)
229+
_ignoredWordsList = new List<string>(1);
231230

232-
int index = _ignoredWordsList.BinarySearch(word, new CaseInsensitiveComparer(_defaultCulture));
231+
int index = _ignoredWordsList.BinarySearch(word, _defaultComparer);
233232

233+
// If we didn't find the word, we're gonna add it to ignore list
234234
if (index < 0)
235235
{
236236
// This is a new word to ignore.
237-
238237
// Add it the list so we don't flag it later.
239238
_ignoredWordsList.Insert(~index, word);
240239

@@ -944,7 +943,6 @@ private bool ScanTextSegment(SpellerInteropBase.ISpellerSegment textSegment, obj
944943
{
945944
TextMapCallbackData data = (TextMapCallbackData)o;
946945
SpellerInteropBase.ITextRange sTextRange = textSegment.TextRange;
947-
char[] word;
948946

949947
// Check if this segment falls outside the content range.
950948
// The region before/after the content is only for context --
@@ -963,8 +961,7 @@ private bool ScanTextSegment(SpellerInteropBase.ISpellerSegment textSegment, obj
963961
if (sTextRange.Length > 1) // Ignore single letter errors.
964962
{
965963
// Check if the segment has been marked "ignore" by the user.
966-
word = new char[sTextRange.Length];
967-
Array.Copy(data.TextMap.Text, sTextRange.Start, word, 0, sTextRange.Length);
964+
string word = new(data.TextMap.Text, sTextRange.Start, sTextRange.Length);
968965

969966
if (!IsIgnoredWord(word))
970967
{
@@ -1438,17 +1435,7 @@ private bool ExpandToWordBreakCallback(SpellerInteropBase.ISpellerSegment textSe
14381435
}
14391436

14401437
// Returns true if a user has tagged the specified word with "Ignore All".
1441-
private bool IsIgnoredWord(char[] word)
1442-
{
1443-
bool isIgnoredWord = false;
1444-
1445-
if (_ignoredWordsList != null)
1446-
{
1447-
isIgnoredWord = _ignoredWordsList.BinarySearch(new string(word), new CaseInsensitiveComparer(_defaultCulture)) >= 0;
1448-
}
1449-
1450-
return isIgnoredWord;
1451-
}
1438+
private bool IsIgnoredWord(string word) => _ignoredWordsList?.BinarySearch(word, _defaultComparer) >= 0;
14521439

14531440
// Returns true if we have an engine capable of proofing the specified
14541441
// language.
@@ -2052,11 +2039,12 @@ internal object Lexicon
20522039
private bool _pendingCaretMovedCallback;
20532040

20542041
// List of words tagged by the user as non-errors.
2055-
private ArrayList _ignoredWordsList;
2042+
private List<string> _ignoredWordsList;
20562043

20572044
// The CultureInfo associated with this speller.
20582045
// Used for ignored words comparison, and plain text controls (TextBox).
20592046
private readonly CultureInfo _defaultCulture;
2047+
private readonly IComparer<string> _defaultComparer;
20602048

20612049
// Set true if the nl6 library is unavailable.
20622050
private bool _failedToInit;

0 commit comments

Comments
 (0)