Jérémy CROS
08/22/2023, 3:15 PMinterface StringNormalizer {
fun normalizeString(stringWithAccents: String): String
}
expect fun getStringNormalizer(): StringNormalizer
and an easy Android implementation:
class AndroidStringNormalizer : StringNormalizer {
override fun normalizeString(stringWithAccents: String): String {
val normalized = Normalizer.normalize(stringWithAccents, Normalizer.Form.NFD)
return normalized.replace("\\p{Mn}+".toRegex(), "")
}
}
actual fun getStringNormalizer(): StringNormalizer = AndroidStringNormalizer()
But I have no idea how to write the ios part:
class IOSStringNormalizer : StringNormalizer {
override fun normalizeString(stringWithAccents: String): String {
TODO()
}
}
actual fun getStringNormalizer(): StringNormalizer = IOSStringNormalizer()
I know the iOS team is currently using this string extension: .folding(options: .diacriticInsensitive, locale: .current)
in Swift
How can I have something equivalent in my IOSStringNormalizer
?
Thanks! 🙏Pablichjenkov
08/22/2023, 3:24 PMmkrussel
08/22/2023, 3:31 PMfolding
is an instance function of NSString
I think you need to cast the String to NSString, and then you can call the function. stringByFoldingWithOptions
https://developer.apple.com/documentation/foundation/nsstring/1413779-stringbyfoldingwithoptions?language=objcLandry Norris
08/22/2023, 7:54 PMJérémy CROS
08/23/2023, 7:37 AMfolding(options:locale:)
or objc stringByFoldingWithOptions:locale:
I just don’t get how to use that
I tried casting to NSString from kotlin but the IDE says the cast can never succeed
Is there a way to make it work or is it not possible as @Pablichjenkov suggested?Jérémy CROS
08/23/2023, 8:22 AM@Suppress("CAST_NEVER_SUCCEEDS")
override fun normalizeString(stringWithAccents: String): String {
return (stringWithAccents as NSString)
.stringByFoldingWithOptions(NSDiacriticInsensitiveSearch, NSLocale.currentLocale())
}
mkrussel
08/23/2023, 12:35 PMJérémy CROS
08/23/2023, 1:50 PM