Skip to content

Commit c2df52e

Browse files
committed
feat: optimize and stabilize string normalization in StringMatcher
Enhance search reliability and performance by refining the Normalize method. - Added null/empty string handling to prevent NullReferenceExceptions during search.
1 parent 839b978 commit c2df52e

1 file changed

Lines changed: 19 additions & 9 deletions

File tree

Flow.Launcher.Infrastructure/StringMatcher.cs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
using CommunityToolkit.Mvvm.DependencyInjection;
2-
using Flow.Launcher.Plugin.SharedModels;
3-
using System;
1+
using System;
2+
using System.Buffers;
43
using System.Collections.Generic;
54
using System.Globalization;
65
using System.Linq;
76
using System.Text;
7+
using CommunityToolkit.Mvvm.DependencyInjection;
88
using Flow.Launcher.Infrastructure.UserSettings;
9+
using Flow.Launcher.Plugin.SharedModels;
910

1011
namespace Flow.Launcher.Infrastructure
1112
{
@@ -256,14 +257,23 @@ public MatchResult FuzzyMatch(string query, string stringToCompare, MatchOption
256257
};
257258
public static string Normalize(string value)
258259
{
259-
Span<char> buffer = stackalloc char[value.Length];
260-
for (int i = 0; i < value.Length; i++)
260+
if (string.IsNullOrEmpty(value)) return value;
261+
char[] arrayFromPool = null;
262+
Span<char> buffer = value.Length <= 512 ? stackalloc char[value.Length] : (arrayFromPool = ArrayPool<char>.Shared.Rent(value.Length));
263+
try
261264
{
262-
var c = char.ToLowerInvariant(value[i]);
263-
buffer[i] = AccentMap.TryGetValue(c, out var mapped) ? mapped : c;
265+
for (int i = 0; i < value.Length; i++)
266+
{
267+
var c = char.ToLowerInvariant(value[i]);
268+
buffer[i] = AccentMap.TryGetValue(c, out var mapped) ? mapped : c;
269+
}
270+
return new string(buffer.Slice(0, value.Length));
271+
}
272+
finally
273+
{
274+
if (arrayFromPool != null)
275+
ArrayPool<char>.Shared.Return(arrayFromPool);
264276
}
265-
266-
return new string(buffer);
267277
}
268278
private static bool IsAcronym(string stringToCompare, int compareStringIndex)
269279
{

0 commit comments

Comments
 (0)