From 0e856d2add54479d2a82a3bc958dd3058862d2b9 Mon Sep 17 00:00:00 2001 From: noncenz <33376717+noncenz@users.noreply.github.com> Date: Thu, 5 Jan 2023 11:34:06 -0500 Subject: [PATCH] [PS-1352] Fix ignore diacritics in search (#2044) * 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. * Removed logs, added null or whitespace validation and improved formatting Co-authored-by: aj-rosado <109146700+aj-rosado@users.noreply.github.com> Co-authored-by: Andre Rosado --- src/Core/Services/SearchService.cs | 7 +++--- src/Core/Utilities/StringExtensions.cs | 34 ++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 src/Core/Utilities/StringExtensions.cs diff --git a/src/Core/Services/SearchService.cs b/src/Core/Services/SearchService.cs index 8a5b6ac7d..0c0bbfe17 100644 --- a/src/Core/Services/SearchService.cs +++ b/src/Core/Services/SearchService.cs @@ -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(); var lowPriorityMatchedCiphers = new List(); - 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); } diff --git a/src/Core/Utilities/StringExtensions.cs b/src/Core/Utilities/StringExtensions.cs new file mode 100644 index 000000000..a37dc9f49 --- /dev/null +++ b/src/Core/Utilities/StringExtensions.cs @@ -0,0 +1,34 @@ +using System; +using System.Globalization; +using System.Text; + +namespace Bit.Core.Utilities +{ + public static class StringExtensions + { + public static string RemoveDiacritics(this string text) + { + if (string.IsNullOrWhiteSpace(text)) + { + return 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); + } + } + + return stringBuilder + .ToString() + .Normalize(NormalizationForm.FormC); + } + } +}