-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: unit test for normalizeForSearch
- extract method to extension file
- Loading branch information
1 parent
7dfb423
commit d12b39b
Showing
5 changed files
with
64 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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 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
44 changes: 44 additions & 0 deletions
44
AnkiDroid/src/main/java/com/ichi2/anki/utils/ext/String.kt
This file contains 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,44 @@ | ||
/* | ||
* Copyright (c) 2025 Ashish Yadav <[email protected]> | ||
* | ||
* This program is free software; you can redistribute it and/or modify it under | ||
* the terms of the GNU General Public License as published by the Free Software | ||
* Foundation; either version 3 of the License, or (at your option) any later | ||
* version. | ||
* | ||
* This program is distributed in the hope that it will be useful, but WITHOUT ANY | ||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | ||
* details. | ||
* | ||
* You should have received a copy of the GNU General Public License along with | ||
* this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package com.ichi2.anki.utils.ext | ||
|
||
import java.text.Normalizer | ||
import java.util.regex.Pattern | ||
|
||
private val DIACRITICAL_MARKS_PATTERN = Pattern.compile("\\p{InCombiningDiacriticalMarks}+") | ||
|
||
/** | ||
* Normalizes the given string by removing diacritical marks (accents) to enable accent-insensitive searches. | ||
* | ||
* This method uses Unicode normalization in **NFD (Normalization Form Decomposition)** mode, which separates | ||
* base characters from their diacritical marks. Then, it removes all combining diacritical marks using a regex. | ||
* | ||
* Example usage: | ||
* ``` | ||
* val input = "café naïve résumé" | ||
* val normalized = input.normalizeForSearch() | ||
* println(normalized) // Output: "cafe naive resume" | ||
* ``` | ||
* | ||
* @receiver The input string that may contain accented characters. | ||
* @return A new string with accents removed. | ||
*/ | ||
fun String.normalizeForSearch(): String { | ||
val normalized = Normalizer.normalize(this, Normalizer.Form.NFD) | ||
return DIACRITICAL_MARKS_PATTERN.matcher(normalized).replaceAll("") | ||
} |
This file contains 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 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