Skip to content

Commit

Permalink
feat: support integer range based bitmap-indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
Boy0000 committed Feb 12, 2025
1 parent 8fa07e3 commit 3a37a96
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
44 changes: 31 additions & 13 deletions core/src/main/kotlin/com/mineinabyss/emojy/EmojyHelpers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import java.util.*
val spaceRegex: Regex = "(?<!\\\\):space_(-?\\d+):".toRegex()
val escapedSpaceRegex: Regex = "\\\\(:space_(-?\\d+):)".toRegex()
val colorableRegex: Regex = "\\|(c|colorable)".toRegex()
val bitmapIndexRegex: Regex = "\\|([0-9]+)".toRegex()
val bitmapIndexRegex: Regex = "\\|([0-9]+)(?:\\.\\.([0-9]+))?:".toRegex()

private val randomKey = Key.key("random")
private val randomComponent = Component.text("random").font(randomKey)
Expand Down Expand Up @@ -57,15 +57,23 @@ fun String.transformEmotes(insert: Boolean = false): String {
for (emote in emojy.emotes) emote.baseRegex.findAll(this).forEach { match ->

val colorable = colorableRegex in match.value
val bitmapIndex = bitmapIndexRegex.find(match.value)?.groupValues?.get(1)?.toIntOrNull() ?: -1

content = content.replaceFirst(
emote.baseRegex, emote.formattedComponent(
insert = insert,
colorable = colorable,
bitmapIndex = bitmapIndex
).serialize()
)
val bitmapMatch = bitmapIndexRegex.find(match.value)
val startIndex = bitmapMatch?.groupValues?.get(1)?.toIntOrNull() ?: -1
val endIndex = bitmapMatch?.groupValues?.get(2)?.toIntOrNull()?.coerceAtLeast(startIndex) ?: startIndex

if (startIndex != -1) content = content.replace(match.value, (startIndex..endIndex).joinToString(":space_-1:") { index ->
":${emote.id}|$index${if (colorable) "|c" else ""}:"
})

for (bitmapIndex in startIndex..endIndex) {
content = content.replaceFirst(
emote.baseRegex, emote.formattedComponent(
insert = insert,
colorable = colorable,
bitmapIndex = bitmapIndex
).serialize()
)
}
}

for (gif in emojy.gifs) gif.baseRegex.findAll(this).forEach { _ ->
Expand All @@ -89,9 +97,19 @@ fun Component.transformEmotes(locale: Locale? = null, insert: Boolean = false):
for (emote in emojy.emotes) emote.baseRegex.findAll(serialized).forEach { match ->

val colorable = colorableRegex in match.value
val bitmapIndex = bitmapIndexRegex.find(match.value)?.groupValues?.get(1)?.toIntOrNull() ?: -1

component = component.replaceText(emote.replacementConfig(false, insert, colorable, bitmapIndex))
val bitmapMatch = bitmapIndexRegex.find(match.value)
val startIndex = bitmapMatch?.groupValues?.get(1)?.toIntOrNull() ?: -1
val endIndex = bitmapMatch?.groupValues?.get(2)?.toIntOrNull()?.coerceAtLeast(startIndex) ?: startIndex

if (startIndex != -1) component = component.replaceText {
it.matchLiteral(match.value).replacement((startIndex..endIndex).joinToString(":space_-1:") { index ->
":${emote.id}|$index${if (colorable) "|c" else ""}:"
})
}

for (bitmapIndex in startIndex..endIndex) {
component = component.replaceText(emote.replacementConfig(false, insert, colorable, bitmapIndex))
}
}

for (gif in emojy.gifs) gif.baseRegex.findAll(serialized).forEach { _ ->
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/kotlin/com/mineinabyss/emojy/config/Emote.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ data class Emote(
@Transient
val isMultiBitmap = bitmapWidth > 1 || bitmapHeight > 1
@Transient
val baseRegex = "(?<!\\\\):$id(\\|(c|colorable|\\d+))*:".toRegex()
val baseRegex = "(?<!\\\\):$id(\\|(c|colorable|\\d+(\\.\\.\\d+)?))*:".toRegex()
@Transient
val escapedRegex = "\\\\:$id(\\|(c|colorable|\\d+))*:".toRegex()
@Transient
Expand Down

0 comments on commit 3a37a96

Please # to comment.