This commit is contained in:
srkizer 2025-12-11 23:18:27 +01:00 committed by GitHub
commit c84e59af73
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 194 additions and 197 deletions

View file

@ -722,10 +722,10 @@ internal class ConsoleWindow : Window, IDisposable
.OrderBy(s => s) .OrderBy(s => s)
.Prepend("DalamudInternal") .Prepend("DalamudInternal")
.Where( .Where(
name => this.pluginFilter is "" || new FuzzyMatcher( name => string.IsNullOrWhiteSpace(this.pluginFilter) ||
this.pluginFilter.ToLowerInvariant(), this.pluginFilter.FuzzyMatches(
MatchMode.Fuzzy).Matches(name.ToLowerInvariant()) != name,
0) FuzzyMatcherMode.Fuzzy))
.ToList(); .ToList();
ImGui.PushItemWidth(ImGui.GetContentRegionAvail().X); ImGui.PushItemWidth(ImGui.GetContentRegionAvail().X);

View file

@ -1251,7 +1251,7 @@ internal class PluginInstallerWindow : Window, IDisposable
return; return;
} }
IEnumerable<IChangelogEntry> changelogs = null; IEnumerable<IChangelogEntry>? changelogs = null;
if (displayDalamud && displayPlugins && this.dalamudChangelogManager.Changelogs != null) if (displayDalamud && displayPlugins && this.dalamudChangelogManager.Changelogs != null)
{ {
changelogs = this.dalamudChangelogManager.Changelogs; changelogs = this.dalamudChangelogManager.Changelogs;
@ -1265,10 +1265,15 @@ internal class PluginInstallerWindow : Window, IDisposable
changelogs = this.dalamudChangelogManager.Changelogs.OfType<PluginChangelogEntry>(); changelogs = this.dalamudChangelogManager.Changelogs.OfType<PluginChangelogEntry>();
} }
var sortedChangelogs = changelogs?.Where(x => this.searchText.IsNullOrWhitespace() || new FuzzyMatcher(this.searchText.ToLowerInvariant(), MatchMode.FuzzyParts).Matches(x.Title.ToLowerInvariant()) > 0) changelogs ??= Array.Empty<IChangelogEntry>();
.OrderByDescending(x => x.Date).ToList(); var sortedChangelogs =
this.searchText.IsNullOrWhitespace()
? changelogs.ToList()
: changelogs.Where(x => x.Title.FuzzyMatches(this.searchText, FuzzyMatcherMode.FuzzyParts))
.OrderByDescending(x => x.Date)
.ToList();
if (sortedChangelogs == null || sortedChangelogs.Count == 0) if (sortedChangelogs.Count == 0)
{ {
ImGui.TextColored( ImGui.TextColored(
ImGuiColors.DalamudGrey2, ImGuiColors.DalamudGrey2,
@ -3790,22 +3795,20 @@ internal class PluginInstallerWindow : Window, IDisposable
private int GetManifestSearchScore(IPluginManifest manifest) private int GetManifestSearchScore(IPluginManifest manifest)
{ {
var searchString = this.searchText.ToLowerInvariant(); var maxScore = 0;
var matcher = new FuzzyMatcher(searchString, MatchMode.FuzzyParts);
var scores = new List<int> { 0 };
if (!manifest.Name.IsNullOrEmpty()) maxScore = Math.Max(maxScore, manifest.Name.FuzzyScore(this.searchText, FuzzyMatcherMode.FuzzyParts) * 110);
scores.Add(matcher.Matches(manifest.Name.ToLowerInvariant()) * 110); maxScore = Math.Max(
if (!manifest.InternalName.IsNullOrEmpty()) maxScore,
scores.Add(matcher.Matches(manifest.InternalName.ToLowerInvariant()) * 105); manifest.InternalName.FuzzyScore(this.searchText, FuzzyMatcherMode.FuzzyParts) * 105);
if (!manifest.Author.IsNullOrEmpty()) maxScore = Math.Max(maxScore, manifest.Author.FuzzyScore(this.searchText, FuzzyMatcherMode.FuzzyParts) * 100);
scores.Add(matcher.Matches(manifest.Author.ToLowerInvariant()) * 100); maxScore = Math.Max(
if (!manifest.Punchline.IsNullOrEmpty()) maxScore,
scores.Add(matcher.Matches(manifest.Punchline.ToLowerInvariant()) * 100); manifest.Punchline.FuzzyScore(this.searchText, FuzzyMatcherMode.FuzzyParts) * 100);
if (manifest.Tags != null) foreach (var tag in manifest.Tags ?? [])
scores.Add(matcher.MatchesAny(manifest.Tags.ToArray()) * 100); maxScore = Math.Max(maxScore, tag.FuzzyScore(this.searchText, FuzzyMatcherMode.FuzzyParts) * 100);
return scores.Max(); return maxScore;
} }
private (bool IsInstalled, LocalPlugin Plugin) IsManifestInstalled(IPluginManifest? manifest) private (bool IsInstalled, LocalPlugin Plugin) IsManifestInstalled(IPluginManifest? manifest)

View file

@ -1,184 +1,138 @@
#define BORDER_MATCHING #define BORDER_MATCHING
using System.Collections.Generic; using System.Globalization;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using Dalamud.Configuration.Internal;
namespace Dalamud.Utility; namespace Dalamud.Utility;
#pragma warning disable SA1600 /// <summary>
#pragma warning disable SA1602 /// Matches a string in a fuzzy way.
/// </summary>
internal enum MatchMode internal static class FuzzyMatcher
{ {
Simple, /// <summary>
Fuzzy, /// Scores how well <paramref name="needle"/> can be found in <paramref name="haystack"/> in a fuzzy way.
FuzzyParts, /// </summary>
} /// <param name="haystack">The string to search in.</param>
/// <param name="needle">The substring to search for.</param>
internal readonly ref struct FuzzyMatcher /// <param name="mode">Fuzzy match mode.</param>
{ /// <param name="cultureInfo">Culture info for case-insensitive matching. Defaults to the culture corresponding to Dalamud language.</param>
private static readonly (int, int)[] EmptySegArray = Array.Empty<(int, int)>(); /// <returns>The score. 0 means that the string did not match. The scores are meaningful only across matches using the same <paramref name="needle"/> value.</returns>
public static int FuzzyScore(
private readonly string needleString = string.Empty; this ReadOnlySpan<char> haystack,
private readonly ReadOnlySpan<char> needleSpan = ReadOnlySpan<char>.Empty; ReadOnlySpan<char> needle,
private readonly int needleFinalPosition = -1; FuzzyMatcherMode mode = FuzzyMatcherMode.Simple,
private readonly (int Start, int End)[] needleSegments = EmptySegArray; CultureInfo? cultureInfo = null)
private readonly MatchMode mode = MatchMode.Simple;
public FuzzyMatcher(string term, MatchMode matchMode)
{ {
this.needleString = term; cultureInfo ??=
this.needleSpan = this.needleString.AsSpan(); Service<DalamudConfiguration>.GetNullable().EffectiveLanguage is { } effectiveLanguage
this.needleFinalPosition = this.needleSpan.Length - 1; ? Localization.GetCultureInfoFromLangCode(effectiveLanguage)
this.mode = matchMode; : CultureInfo.CurrentCulture;
switch (matchMode) switch (mode)
{ {
case MatchMode.FuzzyParts: case var _ when needle.Length == 0:
this.needleSegments = FindNeedleSegments(this.needleSpan); return 0;
break;
case MatchMode.Fuzzy: case FuzzyMatcherMode.Simple:
case MatchMode.Simple: return cultureInfo.CompareInfo.IndexOf(haystack, needle, CompareOptions.IgnoreCase) != -1 ? 1 : 0;
this.needleSegments = EmptySegArray;
break; case FuzzyMatcherMode.Fuzzy:
return GetRawScore(haystack, needle, cultureInfo);
case FuzzyMatcherMode.FuzzyParts:
var score = 0;
foreach (var needleSegment in new WordEnumerator(needle))
{
var cur = GetRawScore(haystack, needleSegment, cultureInfo);
if (cur == 0)
return 0;
score += cur;
}
return score;
default: default:
throw new ArgumentOutOfRangeException(nameof(matchMode), matchMode, null); throw new ArgumentOutOfRangeException(nameof(mode), mode, null);
} }
} }
private static (int Start, int End)[] FindNeedleSegments(ReadOnlySpan<char> span) /// <inheritdoc cref="FuzzyScore(ReadOnlySpan{char},ReadOnlySpan{char},FuzzyMatcherMode,CultureInfo?)"/>
public static int FuzzyScore(
this string? haystack,
ReadOnlySpan<char> needle,
FuzzyMatcherMode mode = FuzzyMatcherMode.Simple,
CultureInfo? cultureInfo = null) => haystack.AsSpan().FuzzyScore(needle, mode, cultureInfo);
/// <summary>
/// Determines if <paramref name="needle"/> can be found in <paramref name="haystack"/> in a fuzzy way.
/// </summary>
/// <param name="haystack">The string to search from.</param>
/// <param name="needle">The substring to search for.</param>
/// <param name="mode">Fuzzy match mode.</param>
/// <param name="cultureInfo">Culture info for case-insensitive matching. Defaults to the culture corresponding to Dalamud language.</param>
/// <returns><c>true</c> if matches.</returns>
public static bool FuzzyMatches(
this ReadOnlySpan<char> haystack,
ReadOnlySpan<char> needle,
FuzzyMatcherMode mode = FuzzyMatcherMode.Simple,
CultureInfo? cultureInfo = null) => haystack.FuzzyScore(needle, mode, cultureInfo) > 0;
/// <summary>
/// Determines if <paramref name="needle"/> can be found in <paramref name="haystack"/> in a fuzzy way.
/// </summary>
/// <param name="haystack">The string to search from.</param>
/// <param name="needle">The substring to search for.</param>
/// <param name="mode">Fuzzy match mode.</param>
/// <param name="cultureInfo">Culture info for case-insensitive matching. Defaults to the culture corresponding to Dalamud language.</param>
/// <returns><c>true</c> if matches.</returns>
public static bool FuzzyMatches(
this string? haystack,
ReadOnlySpan<char> needle,
FuzzyMatcherMode mode = FuzzyMatcherMode.Simple,
CultureInfo? cultureInfo = null) => haystack.FuzzyScore(needle, mode, cultureInfo) > 0;
private static int GetRawScore(ReadOnlySpan<char> haystack, ReadOnlySpan<char> needle, CultureInfo cultureInfo)
{ {
var segments = new List<(int, int)>(); var (startPos, gaps, consecutive, borderMatches, endPos) = FindForward(haystack, needle, cultureInfo);
var wordStart = -1;
for (var i = 0; i < span.Length; i++)
{
if (span[i] is not ' ' and not '\u3000')
{
if (wordStart < 0)
{
wordStart = i;
}
}
else if (wordStart >= 0)
{
segments.Add((wordStart, i - 1));
wordStart = -1;
}
}
if (wordStart >= 0)
{
segments.Add((wordStart, span.Length - 1));
}
return segments.ToArray();
}
#pragma warning disable SA1202
public int Matches(string value)
#pragma warning restore SA1202
{
if (this.needleFinalPosition < 0)
{
return 0;
}
if (this.mode == MatchMode.Simple)
{
return value.Contains(this.needleString) ? 1 : 0;
}
var haystack = value.AsSpan();
if (this.mode == MatchMode.Fuzzy)
{
return this.GetRawScore(haystack, 0, this.needleFinalPosition);
}
if (this.mode == MatchMode.FuzzyParts)
{
if (this.needleSegments.Length < 2)
{
return this.GetRawScore(haystack, 0, this.needleFinalPosition);
}
var total = 0;
for (var i = 0; i < this.needleSegments.Length; i++)
{
var (start, end) = this.needleSegments[i];
var cur = this.GetRawScore(haystack, start, end);
if (cur == 0)
{
return 0;
}
total += cur;
}
return total;
}
return 8;
}
public int MatchesAny(params string[] values)
{
var max = 0;
for (var i = 0; i < values.Length; i++)
{
var cur = this.Matches(values[i]);
if (cur > max)
{
max = cur;
}
}
return max;
}
private int GetRawScore(ReadOnlySpan<char> haystack, int needleStart, int needleEnd)
{
var (startPos, gaps, consecutive, borderMatches, endPos) = this.FindForward(haystack, needleStart, needleEnd);
if (startPos < 0) if (startPos < 0)
{
return 0; return 0;
}
var needleSize = needleEnd - needleStart + 1; var score = CalculateRawScore(needle.Length, startPos, gaps, consecutive, borderMatches);
var score = CalculateRawScore(needleSize, startPos, gaps, consecutive, borderMatches);
// PluginLog.Debug( // PluginLog.Debug(
// $"['{needleString.Substring(needleStart, needleEnd - needleStart + 1)}' in '{haystack}'] fwd: needleSize={needleSize} startPos={startPos} gaps={gaps} consecutive={consecutive} borderMatches={borderMatches} score={score}"); // $"['{needle.Substring(needleStart, needleEnd - needleStart + 1)}' in '{haystack}'] fwd: needleSize={needleSize} startPos={startPos} gaps={gaps} consecutive={consecutive} borderMatches={borderMatches} score={score}");
(startPos, gaps, consecutive, borderMatches) = this.FindReverse(haystack, endPos, needleStart, needleEnd); (startPos, gaps, consecutive, borderMatches) = FindReverse(haystack[..(endPos + 1)], needle, cultureInfo);
var revScore = CalculateRawScore(needleSize, startPos, gaps, consecutive, borderMatches); var revScore = CalculateRawScore(needle.Length, startPos, gaps, consecutive, borderMatches);
// PluginLog.Debug( // PluginLog.Debug(
// $"['{needleString.Substring(needleStart, needleEnd - needleStart + 1)}' in '{haystack}'] rev: needleSize={needleSize} startPos={startPos} gaps={gaps} consecutive={consecutive} borderMatches={borderMatches} score={revScore}"); // $"['{needle.Substring(needleStart, needleEnd - needleStart + 1)}' in '{haystack}'] rev: needleSize={needleSize} startPos={startPos} gaps={gaps} consecutive={consecutive} borderMatches={borderMatches} score={revScore}");
return int.Max(score, revScore); return int.Max(score, revScore);
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
#pragma warning disable SA1204
private static int CalculateRawScore(int needleSize, int startPos, int gaps, int consecutive, int borderMatches) private static int CalculateRawScore(int needleSize, int startPos, int gaps, int consecutive, int borderMatches)
#pragma warning restore SA1204
{ {
var score = 100 var score = 100;
+ needleSize * 3 score += needleSize * 3;
+ borderMatches * 3 score += borderMatches * 3;
+ consecutive * 5 score += consecutive * 5;
- startPos score -= startPos;
- gaps * 2; score -= gaps * 2;
if (startPos == 0) if (startPos == 0)
score += 5; score += 5;
return score < 1 ? 1 : score; return score < 1 ? 1 : score;
} }
private (int StartPos, int Gaps, int Consecutive, int BorderMatches, int HaystackIndex) FindForward( private static (int StartPos, int Gaps, int Consecutive, int BorderMatches, int HaystackIndex) FindForward(
ReadOnlySpan<char> haystack, int needleStart, int needleEnd) ReadOnlySpan<char> haystack,
ReadOnlySpan<char> needle,
CultureInfo cultureInfo)
{ {
var needleIndex = needleStart; var needleIndex = 0;
var lastMatchIndex = -10; var lastMatchIndex = -10;
var startPos = 0; var startPos = 0;
@ -188,83 +142,69 @@ internal readonly ref struct FuzzyMatcher
for (var haystackIndex = 0; haystackIndex < haystack.Length; haystackIndex++) for (var haystackIndex = 0; haystackIndex < haystack.Length; haystackIndex++)
{ {
if (haystack[haystackIndex] == this.needleSpan[needleIndex]) if (char.ToLower(haystack[haystackIndex], cultureInfo) == char.ToLower(needle[needleIndex], cultureInfo))
{ {
#if BORDER_MATCHING #if BORDER_MATCHING
if (haystackIndex > 0) if (haystackIndex > 0)
{ {
if (!char.IsLetterOrDigit(haystack[haystackIndex - 1])) if (!char.IsLetterOrDigit(haystack[haystackIndex - 1]))
{
borderMatches++; borderMatches++;
}
} }
#endif #endif
needleIndex++; needleIndex++;
if (haystackIndex == lastMatchIndex + 1) if (haystackIndex == lastMatchIndex + 1)
{
consecutive++; consecutive++;
}
if (needleIndex > needleEnd) if (needleIndex >= needle.Length)
{
return (startPos, gaps, consecutive, borderMatches, haystackIndex); return (startPos, gaps, consecutive, borderMatches, haystackIndex);
}
lastMatchIndex = haystackIndex; lastMatchIndex = haystackIndex;
} }
else else
{ {
if (needleIndex > needleStart) if (needleIndex > 0)
{
gaps++; gaps++;
}
else else
{
startPos++; startPos++;
}
} }
} }
return (-1, 0, 0, 0, 0); return (-1, 0, 0, 0, 0);
} }
private (int StartPos, int Gaps, int Consecutive, int BorderMatches) FindReverse( private static (int StartPos, int Gaps, int Consecutive, int BorderMatches) FindReverse(
ReadOnlySpan<char> haystack, int haystackLastMatchIndex, int needleStart, int needleEnd) ReadOnlySpan<char> haystack,
ReadOnlySpan<char> needle,
CultureInfo cultureInfo)
{ {
var needleIndex = needleEnd; var needleIndex = needle.Length - 1;
var revLastMatchIndex = haystack.Length + 10; var revLastMatchIndex = haystack.Length + 10;
var gaps = 0; var gaps = 0;
var consecutive = 0; var consecutive = 0;
var borderMatches = 0; var borderMatches = 0;
for (var haystackIndex = haystackLastMatchIndex; haystackIndex >= 0; haystackIndex--) for (var haystackIndex = haystack.Length - 1; haystackIndex >= 0; haystackIndex--)
{ {
if (haystack[haystackIndex] == this.needleSpan[needleIndex]) if (char.ToLower(haystack[haystackIndex], cultureInfo) == char.ToLower(needle[needleIndex], cultureInfo))
{ {
#if BORDER_MATCHING #if BORDER_MATCHING
if (haystackIndex > 0) if (haystackIndex > 0)
{ {
if (!char.IsLetterOrDigit(haystack[haystackIndex - 1])) if (!char.IsLetterOrDigit(haystack[haystackIndex - 1]))
{
borderMatches++; borderMatches++;
}
} }
#endif #endif
needleIndex--; needleIndex--;
if (haystackIndex == revLastMatchIndex - 1) if (haystackIndex == revLastMatchIndex - 1)
{
consecutive++; consecutive++;
}
if (needleIndex < needleStart) if (needleIndex < 0)
{
return (haystackIndex, gaps, consecutive, borderMatches); return (haystackIndex, gaps, consecutive, borderMatches);
}
revLastMatchIndex = haystackIndex; revLastMatchIndex = haystackIndex;
} }
@ -276,7 +216,39 @@ internal readonly ref struct FuzzyMatcher
return (-1, 0, 0, 0); return (-1, 0, 0, 0);
} }
}
#pragma warning restore SA1600 private ref struct WordEnumerator(ReadOnlySpan<char> fullNeedle)
#pragma warning restore SA1602 {
private readonly ReadOnlySpan<char> fullNeedle = fullNeedle;
private int start = -1;
private int end = 0;
public ReadOnlySpan<char> Current
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => this.fullNeedle[this.start..this.end];
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool MoveNext()
{
if (this.start >= this.fullNeedle.Length - 1)
return false;
this.start = this.end;
// Skip the spaces
while (this.start < this.fullNeedle.Length && char.IsWhiteSpace(this.fullNeedle[this.start]))
this.start++;
this.end = this.start;
while (this.end < this.fullNeedle.Length && !char.IsWhiteSpace(this.fullNeedle[this.end]))
this.end++;
return this.start != this.end;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public WordEnumerator GetEnumerator() => this;
}
}

View file

@ -0,0 +1,22 @@
namespace Dalamud.Utility;
/// <summary>
/// Specify fuzzy match mode.
/// </summary>
internal enum FuzzyMatcherMode
{
/// <summary>
/// The matcher only considers whether the haystack contains the needle (case-insensitive.)
/// </summary>
Simple,
/// <summary>
/// The string is considered for fuzzy matching as a whole.
/// </summary>
Fuzzy,
/// <summary>
/// Each part of the string, separated by whitespace, is considered for fuzzy matching; each part must match in a fuzzy way.
/// </summary>
FuzzyParts,
}