-
Notifications
You must be signed in to change notification settings - Fork 504
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Accept letters with strokes or diacritics
- Loading branch information
1 parent
2fc863e
commit 55ce3e2
Showing
7 changed files
with
81 additions
and
90 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
26 changes: 7 additions & 19 deletions
26
...main/kotlin/com/pinterest/ktlint/ruleset/standard/internal/RemoveDiacriticsFromLetters.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 |
---|---|---|
@@ -1,23 +1,11 @@ | ||
package com.pinterest.ktlint.ruleset.standard.internal | ||
|
||
import java.text.Normalizer | ||
|
||
/** | ||
* Removes diacritics from letters. Note that ligatures æ (ae), œ (oe), Æ (AE), Œ (OE), and letters with strokes ł (l), | ||
* ø (o), ß (s), Ł (L), Ø (O) are not changed. | ||
* Transforms a string containing regular expression ranges like "A-Z" and "a-z" to a RegEx which checks whether a | ||
* unicode character has an uppercase versus a lowercase mapping to a letter. This function intents to keep the original | ||
* expression more readable | ||
*/ | ||
|
||
internal fun String.removeDiacriticsFromLetters() = | ||
map { originalChar -> | ||
Normalizer | ||
// Decompose characters having a diacritic into an ascii alphabetic character (a-zA-Z) followed by the diacritic(s) | ||
.normalize(originalChar.toString(), Normalizer.Form.NFD) | ||
.let { | ||
if (it.first().isLetterOrDigit()) { | ||
// Ignore all diacritics | ||
it.first() | ||
} else { | ||
it | ||
} | ||
} | ||
}.joinToString(separator = "") | ||
internal fun String.regExIgnoringDiacriticsAndStrokesOnLetters() = | ||
replace("A-Z", "\\p{Lu}") | ||
.replace("a-z", "\\p{Ll}") | ||
.toRegex() |
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
63 changes: 24 additions & 39 deletions
63
.../kotlin/com/pinterest/ktlint/ruleset/standard/internal/RemoveDiacriticsFromLettersTest.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 |
---|---|---|
@@ -1,53 +1,38 @@ | ||
package com.pinterest.ktlint.ruleset.standard.internal | ||
|
||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.params.ParameterizedTest | ||
import org.junit.jupiter.params.provider.CsvSource | ||
import org.junit.jupiter.params.provider.ValueSource | ||
|
||
class RemoveDiacriticsFromLettersTest { | ||
@ParameterizedTest(name = "Original character: {0}, expected result: {1}") | ||
@CsvSource( | ||
value = [ | ||
"àáâäãåā,aaaaaaa", | ||
"çćč,ccc", | ||
"èéêëēėę,eeeeeee", | ||
"îïíīįì,iiiiii", | ||
"ñń,nn", | ||
"ôöòóōõ,oooooo", | ||
"śš,ss", | ||
"ûüùúū,uuuuu", | ||
"ÿ,y", | ||
"žźż,zzz", | ||
"ÀÁÂÄÃÅĀ,AAAAAAA", | ||
"ÇĆČ,CCC", | ||
"ÈÉÊËĒĖĘ,EEEEEEE", | ||
"ÎÏÍĪĮÌ,IIIIII", | ||
"ÑŃ,NN", | ||
"ÔÖÒÓŌÕ,OOOOOO", | ||
"ŚŠ,SS", | ||
"ÛÜÙÚŪ,UUUUU", | ||
"Ÿ,Y", | ||
"ŽŹŻ,ZZZ" | ||
] | ||
) | ||
fun `Given a letter with a diacritic then remove it`(original: String, expected: String) { | ||
assertThat(original.removeDiacriticsFromLetters()).isEqualTo(expected) | ||
} | ||
|
||
@ParameterizedTest(name = "Character: {0}") | ||
@ValueSource( | ||
strings = [ | ||
"æ", "ł", "œ", "ø", "ß", "Æ", "Ł", "Œ", "Ø" | ||
"àáâäæãåā", | ||
"çćč", | ||
"èéêëēėę", | ||
"îïíīįì", | ||
"ł", | ||
"ñń", | ||
"ôöòóœøōõ", | ||
"ßśš", | ||
"ûüùúū", | ||
"ÿ", | ||
"žźż", | ||
"ÀÁÂÄÆÃÅĀ", | ||
"ÇĆČ", | ||
"ÈÉÊËĒĖĘ", | ||
"ÎÏÍĪĮÌ", | ||
"Ł", | ||
"ÑŃ", | ||
"ÔÖÒÓŒØŌÕ", | ||
"ŚŠ", | ||
"ÛÜÙÚŪ", | ||
"Ÿ", | ||
"ŽŹŻ" | ||
] | ||
) | ||
fun `Given a ligature or letter with stroke then keep it unchanged`(original: String) { | ||
assertThat(original.removeDiacriticsFromLetters()).isEqualTo(original) | ||
} | ||
|
||
@Test | ||
fun `Given a string containing`() { | ||
assertThat("ÅÄÖāăąēîïĩíĝġńñšŝśûůŷ".removeDiacriticsFromLetters()).isEqualTo("AAOaaaeiiiiggnnsssuuy") | ||
fun `Given a letter with a diacritic then remove it`(original: String) { | ||
assertThat(original.matches("[A-Za-z]*".regExIgnoringDiacriticsAndStrokesOnLetters())).isTrue | ||
} | ||
} |
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