[Android] Use transliterator to slugify non-ASCII strings
Robert Stone
git at videolan.org
Fri Jan 19 07:58:02 UTC 2024
vlc-android | branch: master | Robert Stone <rhstone at gmail.com> | Wed Jan 17 21:16:55 2024 -0800| [23333ba47f6ef0d54bc5b959db38b730185ebef8] | committer: Robert Stone
Use transliterator to slugify non-ASCII strings
> https://code.videolan.org/videolan/vlc-android/commit/23333ba47f6ef0d54bc5b959db38b730185ebef8
---
.../src/org/videolan/vlc/util/Kextensions.kt | 19 +++++++++++++------
1 file changed, 13 insertions(+), 6 deletions(-)
diff --git a/application/vlc-android/src/org/videolan/vlc/util/Kextensions.kt b/application/vlc-android/src/org/videolan/vlc/util/Kextensions.kt
index 3709dc343d..9740023057 100644
--- a/application/vlc-android/src/org/videolan/vlc/util/Kextensions.kt
+++ b/application/vlc-android/src/org/videolan/vlc/util/Kextensions.kt
@@ -6,6 +6,7 @@ import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.content.res.Resources
+import android.icu.text.Transliterator
import android.net.Uri
import android.os.Build
import android.text.Spannable
@@ -286,16 +287,22 @@ fun CharSequence?.getFilesNumber():Int {
}
/**
- * Slugify a string
+ * Slugify a string. Use the Unicode Transliterator to convert
+ * non-ASCII characters into a latin representation.
*
* @param replacement the replacement char
* @return the slugified string
*/
-fun String.slugify(replacement: String = "-") = Normalizer
- .normalize(this, Normalizer.Form.NFD)
- .replace("[^\\p{ASCII}]".toRegex(), "")
- .replace("[^a-zA-Z0-9\\s]+".toRegex(), "").trim()
- .replace("\\s+".toRegex(), replacement)
+fun String.slugify(replacement: String = "-"): String {
+ val s = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
+ // NFD is included in Latin-ASCII
+ Transliterator.getInstance("Any-Latin; Lower; Latin-ASCII").transliterate(this)
+ } else {
+ Normalizer.normalize(this, Normalizer.Form.NFD)
+ }
+ return s.replace("[^a-zA-Z0-9\\s]+".toRegex(), "").trim()
+ .replace("\\s+".toRegex(), replacement)
+}
const val presentReplacementMarker = "§*§"
const val missingReplacementMarker = "*§*"
More information about the Android
mailing list