Hi there, Is there any kotlin native function or w...
# android
u
Hi there, Is there any kotlin native function or workaround I can use to count number of emojis in text And also check if text has only emoji I am using Unicode range to check if text contains surrogate but this feels not good I also written code to use Character class but not confident if it is good to do loops for every text in list
e
checking for surrogates is definitely the wrong thing to do, as it'll catch all non-BMP unicode characters, of which only a tiny fraction are emoji
the only way to do this is to reference the Unicode standard, which changes approximately yearly; see https://unicode.org/reports/tr51/#Emoji_Properties_and_Data_Files for example
you almost certainly will want to use a library like ICU to handle that for you
u
e
if you're on Android, then 1. the platform has ICU built-in and 2. that answer uses an unbundled emoji library which gets updated, https://developer.android.com/jetpack/androidx/releases/emoji
but that's not usable from Kotlin Native
u
Ok That means I can't make library that can be shared to both iOS and Android Swift has built in support to give Boolean if it is emoji https://developer.apple.com/documentation/swift/unicode/scalar/properties/3081577-isemoji
I am looking for same in kotlin
e
Swift does it by depending on ICU in its runtime, which is a significant pain to distribute for non-Apple targets. Kotlin doesn't, so…
u
I am not able to find emoji sample for icu, I can see the docs, can you help me find one My requirement is simple I will get text that may have emoji or text or both I need to know if text contains only emoji or both text and emoji, and if only emoji then I need the count
this function returns true if text contains emoji
Copy code
private fun containsEmoji(displayName: String): Boolean {
    val nameLength = displayName.length
    for (i in 0 until nameLength) {
        val hs = displayName[i]
        if (hs.code in 0xd800..0xdbff) {
            val ls = displayName[i + 1]
            val uc = (hs.code - 0xd800) * 0x400 + (ls.code - 0xdc00) + 0x10000
            if (uc in 0x1d000..0x1f77f) {
                return true
            }
        } else if (Character.isHighSurrogate(hs)) {
            val ls = displayName[i + 1]
            if (ls.code == 0x20e3) {
                return true
            }
        } else {
            // non surrogate
            if (hs.code in 0x2100..0x27ff) {
                return true
            } else if (hs.code in 0x2B05..0x2b07) {
                return true
            } else if (hs.code in 0x2934..0x2935) {
                return true
            } else if (hs.code in 0x3297..0x3299) {
                return true
            } else if (hs.code == 0xa9 || hs.code == 0xae || hs.code == 0x303d || hs.code == 0x3030 ||
                hs.code == 0x2b55 || hs.code == 0x2b1c || hs.code == 0x2b1b || hs.code == 0x2b50
            ) {
                return true
            }
        }
    }
    return false
}
e
no, because it's missing emoji modifiers which can make other characters either become emoji or not
295 Views