-
-
Notifications
You must be signed in to change notification settings - Fork 6
implement accent/diacritic ignore search #1724
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2927b3d
implement accent/diacritic ignore search
hahn-kev c236346
use contains helper to ensure search works for FwData
hahn-kev 21be9b9
use byte arrays instead of strings to avoid allocations
hahn-kev 914e0fe
update SqlHelper.SearchValue to execute matching the same as when tra…
hahn-kev c7d7c48
Revert adding unused imports
myieye b1bc48c
Document ultimate desired behaviour in tests
myieye 2e3de48
make ContainsDiacriticMatch which ignores diacritics when there's non…
hahn-kev 33f8ad1
write a test to validate the performance of entry search.
hahn-kev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
backend/FwLite/LcmCrdt/Data/CustomSqliteFunctionInterceptor.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| using System.Data.Common; | ||
| using System.Globalization; | ||
| using System.Runtime.CompilerServices; | ||
| using System.Text; | ||
| using Microsoft.Data.Sqlite; | ||
| using Microsoft.EntityFrameworkCore.Diagnostics; | ||
| using MiniLcm.Culture; | ||
|
|
||
| namespace LcmCrdt.Data; | ||
|
|
||
| public class CustomSqliteFunctionInterceptor : IDbConnectionInterceptor | ||
| { | ||
| public const string ContainsFunction = "contains"; | ||
|
|
||
| public void ConnectionOpened(DbConnection connection, ConnectionEndEventData eventData) | ||
| { | ||
| var sqliteConnection = (SqliteConnection)connection; | ||
| //creates a new function that can be used in queries | ||
| sqliteConnection.CreateFunction(ContainsFunction, | ||
| //in sqlite strings are byte arrays, so we can avoid allocating strings by using spans | ||
| (byte[]? str, byte[]? value) => | ||
| { | ||
| if (str is null || value is null) return false; | ||
|
|
||
| Span<char> source = stackalloc char[Encoding.UTF8.GetCharCount(str)]; | ||
| Span<char> search = stackalloc char[Encoding.UTF8.GetCharCount(value)]; | ||
| Encoding.UTF8.GetChars(str, source); | ||
| Encoding.UTF8.GetChars(value, search); | ||
| return CultureInfo.InvariantCulture.CompareInfo.IndexOf(source, | ||
| search, | ||
| ContainsDiacritic(search) | ||
| ? CompareOptions.IgnoreCase | ||
| : CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreCase | ||
| ) >= 0; | ||
| }); | ||
| } | ||
|
|
||
| private static bool ContainsDiacritic(in ReadOnlySpan<char> value) | ||
| { | ||
| bool hasAccent = false; | ||
| //todo we could maybe get rid of this normalization step if the text is already normalized | ||
| //that would mean we could just iterate the value here rather than creating a new string | ||
| foreach (var ch in new string(value).Normalize(NormalizationForm.FormD)) | ||
| { | ||
| if (CharUnicodeInfo.GetUnicodeCategory(ch) == UnicodeCategory.NonSpacingMark) | ||
| { | ||
| hasAccent = true; | ||
| break; | ||
| } | ||
| } | ||
| return hasAccent; | ||
| } | ||
|
|
||
| public Task ConnectionOpenedAsync(DbConnection connection, | ||
| ConnectionEndEventData eventData, | ||
| CancellationToken cancellationToken = new CancellationToken()) | ||
| { | ||
| ConnectionOpened(connection, eventData); | ||
| return Task.CompletedTask; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| using System.Globalization; | ||
| using System.Text; | ||
|
|
||
| namespace MiniLcm.Culture; | ||
|
|
||
| public static class StringExtensions | ||
| { | ||
| public static bool Contains(this string str, string value, CultureInfo cultureInfo, CompareOptions comparison = CompareOptions.None) | ||
| { | ||
| return cultureInfo.CompareInfo.IndexOf(str, value, comparison) >= 0; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// searches a string for a match ignoring diacritics, but only when the search string does not contain diacritics | ||
| /// </summary> | ||
| /// <param name="str">source of the search</param> | ||
| /// <param name="search">string to search for</param> | ||
| public static bool ContainsDiacriticMatch(this string str, string search) | ||
| { | ||
| if (ContainsDiacritic(search)) | ||
| { | ||
| return Contains(str, search, CultureInfo.InvariantCulture, CompareOptions.IgnoreCase); | ||
| } | ||
|
|
||
| return Contains(str, | ||
| search, | ||
| CultureInfo.InvariantCulture, | ||
| CompareOptions.IgnoreCase | CompareOptions.IgnoreNonSpace); | ||
| } | ||
|
|
||
| public static bool ContainsDiacritic(string value) | ||
| { | ||
| bool hasAccent = false; | ||
| foreach (var ch in value.Normalize(NormalizationForm.FormD)) | ||
| { | ||
| if (CharUnicodeInfo.GetUnicodeCategory(ch) == UnicodeCategory.NonSpacingMark) | ||
| { | ||
| hasAccent = true; | ||
| break; | ||
| } | ||
| } | ||
| return hasAccent; | ||
| } | ||
|
|
||
| public static bool Equals(this string str, | ||
| string value, | ||
| CultureInfo cultureInfo, | ||
| CompareOptions comparison = CompareOptions.None) | ||
| { | ||
| return cultureInfo.CompareInfo.Compare(str, value, comparison) == 0; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.