Skip to content

Instantly share code, notes, and snippets.

@psinetron
Created November 24, 2025 15:38
Show Gist options
  • Select an option

  • Save psinetron/3c9adc35fb086003fb5c4f315a5cf1b8 to your computer and use it in GitHub Desktop.

Select an option

Save psinetron/3c9adc35fb086003fb5c4f315a5cf1b8 to your computer and use it in GitHub Desktop.
Main Emoji EN
/**
* An object containing information about the emoji being processed.
*/
data class Emoji(val emoji: String, @DrawableRes val resource: Int)
/**
* A list of emojis we can customize
*/
val EMOJIS: List<Emoji> = listOf(
Emoji("\ud83d\ude42", R.drawable.simple_smile), // πŸ™‚
Emoji("\ud83d\udc69\u200d\ud83d\udcbb", R.drawable.female_engineer), // πŸ‘©β€πŸ’»
Emoji("\ud83d\ude0e", R.drawable.smiling_with_sunglasses) // 😎
)
/**
* Generates a regular expression to find all emoji from a list.
*/
fun List<Emoji>.toRegex(): Regex {
val pattern = joinToString(separator = "|") { emoji ->
Regex.escape(emoji.emoji)
}
return Regex(pattern)
}
/**
* Gets an Emoji object based on the found emoji text
*/
fun List<Emoji>.getEmojiByText(emojiText: String): Emoji? {
return firstOrNull { it.emoji == emojiText }
}
/**
* Finds all occurrences of an emoji from a list in text.
*/
fun List<Emoji>.findEmojisInText(text: String): List<EmojiRange> {
return toRegex().findAll(text).mapNotNull { match ->
getEmojiByText(match.value)?.let { emoji ->
EmojiRange(emoji, match.range)
}
}.toList()
}
data class EmojiRange(
val emoji: Emoji,
val range: IntRange,
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment