1
0
mirror of https://github.com/bitwarden/mobile synced 2026-02-25 17:03:19 +00:00

Fix ignore diacritics in search

This change updates the search function to ignore diacritical marks in search results. Marks are stripped from both the search input and results.
This commit is contained in:
noncenz
2022-08-22 16:26:45 -04:00
parent e829279e29
commit 733464e05d
2 changed files with 37 additions and 3 deletions

View File

@@ -5,6 +5,7 @@ using System.Threading;
using System.Threading.Tasks;
using Bit.Core.Abstractions;
using Bit.Core.Models.View;
using Bit.Core.Utilities;
namespace Bit.Core.Services
{
@@ -76,12 +77,12 @@ namespace Bit.Core.Services
ct.ThrowIfCancellationRequested();
var matchedCiphers = new List<CipherView>();
var lowPriorityMatchedCiphers = new List<CipherView>();
query = query.Trim().ToLower();
query = (query.Trim().ToLower().RemoveDiacritics());
foreach (var c in ciphers)
{
ct.ThrowIfCancellationRequested();
if (c.Name?.ToLower().Contains(query) ?? false)
if (c.Name?.ToLower().RemoveDiacritics().Contains(query) ?? false)
{
matchedCiphers.Add(c);
}
@@ -89,7 +90,7 @@ namespace Bit.Core.Services
{
lowPriorityMatchedCiphers.Add(c);
}
else if (c.SubTitle?.ToLower().Contains(query) ?? false)
else if (c.SubTitle?.ToLower().RemoveDiacritics().Contains(query) ?? false)
{
lowPriorityMatchedCiphers.Add(c);
}

View File

@@ -0,0 +1,33 @@
using System;
using System.Globalization;
using System.Text;
namespace Bit.Core.Utilities
{
public static class StringExtensions
{
public static string RemoveDiacritics(this String text)
{
var normalizedString = text.Normalize(NormalizationForm.FormD);
var stringBuilder = new StringBuilder(capacity: normalizedString.Length);
for (int i = 0; i < normalizedString.Length; i++)
{
char c = normalizedString[i];
var unicodeCategory = CharUnicodeInfo.GetUnicodeCategory(c);
if (unicodeCategory != UnicodeCategory.NonSpacingMark)
{
stringBuilder.Append(c);
}
}
Console.Write(stringBuilder.Length);
Console.WriteLine(stringBuilder.ToString().Normalize(NormalizationForm.FormC));
return stringBuilder
.ToString()
.Normalize(NormalizationForm.FormC);
}
}
}