mirror of
https://github.com/Novaloop-AG/element-x-android.git
synced 2026-11-02 12:07:28 +00:00
Normalize the applicationName used in user agent, as HTTP spec does not allow UTF-8 chars in headers.
This commit is contained in:
+12
@@ -8,6 +8,7 @@
|
||||
|
||||
package io.element.android.libraries.core.extensions
|
||||
|
||||
import java.text.Normalizer
|
||||
import java.util.Locale
|
||||
|
||||
fun Boolean.toOnOff() = if (this) "ON" else "OFF"
|
||||
@@ -114,3 +115,14 @@ fun String.toSafeLength(
|
||||
this
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove accents and replace special characters by ASCII from the string.
|
||||
* For instance, "é" will be replaced by "e", "ç" by "c", etc.
|
||||
*/
|
||||
fun String.normalized(): String {
|
||||
// Normalize the string.
|
||||
return Normalizer.normalize(this, Normalizer.Form.NFD)
|
||||
// then keep only ASCII characters and replace non-ASCII characters with an empty string.
|
||||
.replace("[^\\p{ASCII}]".toRegex(), "")
|
||||
}
|
||||
|
||||
+6
@@ -74,4 +74,10 @@ class BasicExtensionsTest {
|
||||
val result = textWithDirectionOverrides.filterDirectionOverrides()
|
||||
assertEquals("123456789", result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `normalized removes accents and replace special characters from text`() {
|
||||
assertEquals("Stefan", "Ștefan".normalized())
|
||||
assertEquals("ca enleve etre", "ça enlève être".normalized())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user