Estimate length of originalMessage

This commit is contained in:
Him188 2020-04-12 01:42:37 +08:00
parent 6f5441bf1a
commit e23f91bac1

View File

@ -13,6 +13,7 @@
package net.mamoe.mirai.qqandroid.utils
import net.mamoe.mirai.message.data.*
import net.mamoe.mirai.message.data.AtAll.display
import kotlin.jvm.JvmMultifileClass
import kotlin.jvm.JvmName
@ -33,7 +34,7 @@ internal fun Int.toIpV4AddressString(): String {
internal fun String.chineseLength(upTo: Int): Int {
return this.sumUpTo(upTo) {
when(it) {
when (it) {
in '\u0000'..'\u007F' -> 1
in '\u0080'..'\u07FF' -> 2
in '\u0800'..'\uFFFF' -> 3
@ -42,14 +43,14 @@ internal fun String.chineseLength(upTo: Int): Int {
}
}
internal fun MessageChain.estimateLength(upTo: Int = Int.MAX_VALUE): Int =
internal fun MessageChain.estimateLength(upTo: Int): Int =
sumUpTo(upTo) { it, up ->
it.estimateLength(up)
}
internal fun SingleMessage.estimateLength(upTo: Int = Int.MAX_VALUE): Int {
internal fun SingleMessage.estimateLength(upTo: Int): Int {
return when (this) {
is QuoteReply -> 444 // Magic number
is QuoteReply -> 444 + this.source.originalMessage.estimateLength(upTo) // Magic number
is Image -> 260 // Magic number
is PlainText -> stringValue.chineseLength(upTo)
is At -> display.chineseLength(upTo)