Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: fix spotless violations #163

Merged
merged 1 commit into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions emojify/src/main/kotlin/io/wax911/emojify/EmojiManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import io.wax911.emojify.util.trie.Matches
class EmojiManager(
override val emojiList: Collection<Emoji>,
) : IEmojiManager {

private val emojiByAlias by lazy {
val aliasMap = HashMap<String, Emoji>()
emojiList.forEach { emoji ->
Expand Down Expand Up @@ -69,8 +68,7 @@ class EmojiManager(
*
* @return the associated [Emoji]s, null if the tag is unknown
*/
override fun getForTag(tag: String?): Collection<Emoji>? =
tag?.let { emojiByTag[it] }
override fun getForTag(tag: String?): Collection<Emoji>? = tag?.let { emojiByTag[it] }

/**
* Returns the [Emoji] for a given alias.
Expand All @@ -79,8 +77,7 @@ class EmojiManager(
*
* @return the associated [Emoji], null if the alias is unknown
*/
override fun getForAlias(alias: String?): Emoji? =
alias?.let { emojiByAlias[trimAlias(it)] }
override fun getForAlias(alias: String?): Emoji? = alias?.let { emojiByAlias[trimAlias(it)] }

@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
fun trimAlias(alias: String): String {
Expand All @@ -98,8 +95,7 @@ class EmojiManager(
*
* @return the associated [Emoji], null if the unicode is unknown
*/
override fun getByUnicode(unicode: String?): Emoji? =
unicode?.let { emojiTrie.getEmoji(it.toCharArray()) }
override fun getByUnicode(unicode: String?): Emoji? = unicode?.let { emojiTrie.getEmoji(it.toCharArray()) }

/**
* Tests if a given String is an emoji.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import kotlinx.serialization.json.decodeFromStream
import java.io.IOException

class EmojiInitializer : Initializer<EmojiManager> {

/**
* Initializes emoji objects from an asset file in the library directory
*
Expand All @@ -39,7 +38,10 @@ class EmojiInitializer : Initializer<EmojiManager> {
* @throws SerializationException when an error occurs during deserialization
*/
@Throws(IOException::class, SerializationException::class)
fun initEmojiData(assetManager: AssetManager, path: String = DEFAULT_PATH): List<Emoji> {
fun initEmojiData(
assetManager: AssetManager,
path: String = DEFAULT_PATH,
): List<Emoji> {
assetManager.open(path).use { inputStream ->
val json = Json { isLenient = true }
val deserializer = ListSerializer(Emoji.serializer())
Expand All @@ -54,10 +56,11 @@ class EmojiInitializer : Initializer<EmojiManager> {
*/
override fun create(context: Context): EmojiManager {
val emojiManagerDefault = EmojiManager(emptyList())
val result = runCatching {
val emojis = initEmojiData(context.assets)
EmojiManager(emojis)
}.onFailure { it.printStackTrace() }
val result =
runCatching {
val emojis = initEmojiData(context.assets)
EmojiManager(emojis)
}.onFailure { it.printStackTrace() }
return result.getOrNull() ?: emojiManagerDefault
}

Expand Down
13 changes: 7 additions & 6 deletions emojify/src/main/kotlin/io/wax911/emojify/model/Emoji.kt
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ data class Emoji(
if (!supportsFitzpatrick) {
throw UnsupportedOperationException(
"""
Cannot get the unicode with a fitzpatrick modifier,
the emoji doesn't support fitzpatrick.
Cannot get the unicode with a fitzpatrick modifier,
the emoji doesn't support fitzpatrick.
""".trimIndent(),
)
} else if (fitzpatrick == null) {
Expand All @@ -110,10 +110,11 @@ data class Emoji(
return unicode + fitzpatrick.unicode
}

override fun equals(other: Any?) = when (other) {
is Emoji -> other.unicode == unicode
else -> super.equals(other)
}
override fun equals(other: Any?) =
when (other) {
is Emoji -> other.unicode == unicode
else -> super.equals(other)
}

override fun hashCode(): Int {
var result = description?.hashCode() ?: 0
Expand Down
186 changes: 108 additions & 78 deletions emojify/src/main/kotlin/io/wax911/emojify/parser/EmojiParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -59,32 +59,33 @@ fun EmojiManager.parseToAliases(
input: String,
fitzpatrickAction: FitzpatrickAction = FitzpatrickAction.PARSE,
): String {
val emojiTransformer = object : EmojiTransformer {
override fun invoke(unicodeCandidate: UnicodeCandidate): String {
val alias = unicodeCandidate.emoji?.aliases?.get(0)
val fitzpatrickType = unicodeCandidate.fitzpatrickType
val fitzpatrickUnicode = unicodeCandidate.fitzpatrickUnicode
when (fitzpatrickAction) {
FitzpatrickAction.PARSE -> {
return if (unicodeCandidate.hasFitzpatrick()) {
":$alias|$fitzpatrickType:"
} else {
":$alias:"
val emojiTransformer =
object : EmojiTransformer {
override fun invoke(unicodeCandidate: UnicodeCandidate): String {
val alias = unicodeCandidate.emoji?.aliases?.get(0)
val fitzpatrickType = unicodeCandidate.fitzpatrickType
val fitzpatrickUnicode = unicodeCandidate.fitzpatrickUnicode
when (fitzpatrickAction) {
FitzpatrickAction.PARSE -> {
return if (unicodeCandidate.hasFitzpatrick()) {
":$alias|$fitzpatrickType:"
} else {
":$alias:"
}
}
}

FitzpatrickAction.REMOVE -> return ":$alias:"
FitzpatrickAction.IGNORE -> return ":$alias:$fitzpatrickUnicode"
else -> {
return if (unicodeCandidate.hasFitzpatrick()) {
":$alias|$fitzpatrickType:"
} else {
":$alias:"
FitzpatrickAction.REMOVE -> return ":$alias:"
FitzpatrickAction.IGNORE -> return ":$alias:$fitzpatrickUnicode"
else -> {
return if (unicodeCandidate.hasFitzpatrick()) {
":$alias|$fitzpatrickType:"
} else {
":$alias:"
}
}
}
}
}
}

return parseFromUnicode(input, emojiTransformer)
}
Expand All @@ -96,10 +97,14 @@ fun EmojiManager.parseToAliases(
* @param replacementString replacement the string that will replace all the emojis
* @return the string with replaced character
*/
fun EmojiManager.replaceAllEmojis(str: String, replacementString: String): String {
val emojiTransformer = object : EmojiTransformer {
override fun invoke(unicodeCandidate: UnicodeCandidate) = replacementString
}
fun EmojiManager.replaceAllEmojis(
str: String,
replacementString: String,
): String {
val emojiTransformer =
object : EmojiTransformer {
override fun invoke(unicodeCandidate: UnicodeCandidate) = replacementString
}

return parseFromUnicode(str, emojiTransformer)
}
Expand Down Expand Up @@ -145,7 +150,10 @@ fun EmojiManager.parseToUnicode(input: String): String {
return sb.toString()
}

internal fun EmojiManager.aliasCandidateAt(input: String, start: Int): AliasCandidate? {
internal fun EmojiManager.aliasCandidateAt(
input: String,
start: Int,
): AliasCandidate? {
if (input.length < start + 2 || input[start] != ':') return null; // Aliases start with :
val aliasEnd: Int = input.indexOf(':', start + 2) // Alias must be at least 1 char in length
if (aliasEnd == -1) return null // No alias end found
Expand All @@ -169,7 +177,10 @@ internal fun EmojiManager.aliasCandidateAt(input: String, start: Int): AliasCand
)
}

internal fun EmojiManager.htmlEncodedEmojiAt(input: String, start: Int): AliasCandidate? {
internal fun EmojiManager.htmlEncodedEmojiAt(
input: String,
start: Int,
): AliasCandidate? {
if (input.length < start + 4 || input[start] != '&' || input[start + 1] != '#') return null

var longestEmoji: Emoji? = null
Expand Down Expand Up @@ -244,19 +255,20 @@ fun EmojiManager.parseToHtmlDecimal(
input: String,
fitzpatrickAction: FitzpatrickAction = FitzpatrickAction.PARSE,
): String {
val emojiTransformer = object : EmojiTransformer {
override fun invoke(unicodeCandidate: UnicodeCandidate): String? {
return when (fitzpatrickAction) {
FitzpatrickAction.PARSE,
FitzpatrickAction.REMOVE,
->
unicodeCandidate.emoji?.htmlDec

FitzpatrickAction.IGNORE ->
unicodeCandidate.emoji?.htmlDec + unicodeCandidate.fitzpatrickUnicode
val emojiTransformer =
object : EmojiTransformer {
override fun invoke(unicodeCandidate: UnicodeCandidate): String? {
return when (fitzpatrickAction) {
FitzpatrickAction.PARSE,
FitzpatrickAction.REMOVE,
->
unicodeCandidate.emoji?.htmlDec

FitzpatrickAction.IGNORE ->
unicodeCandidate.emoji?.htmlDec + unicodeCandidate.fitzpatrickUnicode
}
}
}
}

return parseFromUnicode(input, emojiTransformer)
}
Expand Down Expand Up @@ -288,19 +300,20 @@ fun EmojiManager.parseToHtmlHexadecimal(
input: String,
fitzpatrickAction: FitzpatrickAction = FitzpatrickAction.PARSE,
): String {
val emojiTransformer = object : EmojiTransformer {
override fun invoke(unicodeCandidate: UnicodeCandidate): String? {
return when (fitzpatrickAction) {
FitzpatrickAction.PARSE,
FitzpatrickAction.REMOVE,
->
unicodeCandidate.emoji?.htmlHex

FitzpatrickAction.IGNORE ->
unicodeCandidate.emoji?.htmlHex + unicodeCandidate.fitzpatrickUnicode
val emojiTransformer =
object : EmojiTransformer {
override fun invoke(unicodeCandidate: UnicodeCandidate): String? {
return when (fitzpatrickAction) {
FitzpatrickAction.PARSE,
FitzpatrickAction.REMOVE,
->
unicodeCandidate.emoji?.htmlHex

FitzpatrickAction.IGNORE ->
unicodeCandidate.emoji?.htmlHex + unicodeCandidate.fitzpatrickUnicode
}
}
}
}

return parseFromUnicode(input, emojiTransformer)
}
Expand All @@ -313,9 +326,10 @@ fun EmojiManager.parseToHtmlHexadecimal(
* @return the string without any emoji
*/
fun EmojiManager.removeAllEmojis(str: String): String {
val emojiTransformer = object : EmojiTransformer {
override fun invoke(unicodeCandidate: UnicodeCandidate) = ""
}
val emojiTransformer =
object : EmojiTransformer {
override fun invoke(unicodeCandidate: UnicodeCandidate) = ""
}

return parseFromUnicode(str, emojiTransformer)
}
Expand All @@ -328,16 +342,20 @@ fun EmojiManager.removeAllEmojis(str: String): String {
*
* @return the string without the emojis that were removed
*/
fun EmojiManager.removeEmojis(str: String, emojisToRemove: Collection<Emoji>): String {
val emojiTransformer = object : EmojiTransformer {
override fun invoke(unicodeCandidate: UnicodeCandidate): String {
return if (!emojisToRemove.contains(unicodeCandidate.emoji)) {
unicodeCandidate.emoji?.unicode + unicodeCandidate.fitzpatrickUnicode
} else {
""
fun EmojiManager.removeEmojis(
str: String,
emojisToRemove: Collection<Emoji>,
): String {
val emojiTransformer =
object : EmojiTransformer {
override fun invoke(unicodeCandidate: UnicodeCandidate): String {
return if (!emojisToRemove.contains(unicodeCandidate.emoji)) {
unicodeCandidate.emoji?.unicode + unicodeCandidate.fitzpatrickUnicode
} else {
""
}
}
}
}

return parseFromUnicode(str, emojiTransformer)
}
Expand All @@ -350,16 +368,20 @@ fun EmojiManager.removeEmojis(str: String, emojisToRemove: Collection<Emoji>): S
*
* @return the string without the emojis that were removed
*/
fun EmojiManager.removeAllEmojisExcept(str: String, emojisToKeep: Collection<Emoji>): String {
val emojiTransformer = object : EmojiTransformer {
override fun invoke(unicodeCandidate: UnicodeCandidate): String {
return if (emojisToKeep.contains(unicodeCandidate.emoji)) {
unicodeCandidate.emoji?.unicode + unicodeCandidate.fitzpatrickUnicode
} else {
""
fun EmojiManager.removeAllEmojisExcept(
str: String,
emojisToKeep: Collection<Emoji>,
): String {
val emojiTransformer =
object : EmojiTransformer {
override fun invoke(unicodeCandidate: UnicodeCandidate): String {
return if (emojisToKeep.contains(unicodeCandidate.emoji)) {
unicodeCandidate.emoji?.unicode + unicodeCandidate.fitzpatrickUnicode
} else {
""
}
}
}
}

return parseFromUnicode(str, emojiTransformer)
}
Expand All @@ -373,7 +395,10 @@ fun EmojiManager.removeAllEmojisExcept(str: String, emojisToKeep: Collection<Emo
*
* @return input string with all emojis transformed
*/
fun EmojiManager.parseFromUnicode(input: String, transformer: EmojiTransformer): String {
fun EmojiManager.parseFromUnicode(
input: String,
transformer: EmojiTransformer,
): String {
var prev = 0
val sb = StringBuilder(input.length)
val replacements = unicodeCandidates(input)
Expand Down Expand Up @@ -416,10 +441,11 @@ internal fun EmojiManager.unicodeCandidates(input: String): List<UnicodeCandidat
var next: UnicodeCandidate?
var i = 0
do {
next = nextUnicodeCandidate(inputCharArray, i)?.apply {
candidates.add(this)
i = fitzpatrickEndIndex
}
next =
nextUnicodeCandidate(inputCharArray, i)?.apply {
candidates.add(this)
i = fitzpatrickEndIndex
}
} while (next != null)

return candidates
Expand All @@ -441,11 +467,12 @@ internal fun EmojiManager.nextUnicodeCandidate(

if (emojiEnd != -1) {
val emoji = getByUnicode(String(chars, index, emojiEnd - index))
val fitzpatrickString = if (emojiEnd + 2 <= chars.size) {
String(chars, emojiEnd, 2)
} else {
null
}
val fitzpatrickString =
if (emojiEnd + 2 <= chars.size) {
String(chars, emojiEnd, 2)
} else {
null
}
return UnicodeCandidate(emoji, fitzpatrickString, index)
}
}
Expand All @@ -466,7 +493,10 @@ internal fun EmojiManager.nextUnicodeCandidate(
*
* @return the end index of the unicode emoji starting at startPos. -1 if not found
*/
internal fun EmojiManager.emojiEndPosition(text: CharArray, startPos: Int): Int {
internal fun EmojiManager.emojiEndPosition(
text: CharArray,
startPos: Int,
): Int {
var best = -1
for (j in startPos + 1..text.size) {
val status = isEmoji(text.copyOfRange(startPos, j))
Expand Down
Loading
Loading