From e23f91bac1475ef6ef3faa8b71f19a64b9674087 Mon Sep 17 00:00:00 2001 From: Him188 Date: Sun, 12 Apr 2020 01:42:37 +0800 Subject: [PATCH] Estimate length of originalMessage --- .../kotlin/net/mamoe/mirai/qqandroid/utils/type.kt | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/utils/type.kt b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/utils/type.kt index 91bf04203..f1ce23a47 100644 --- a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/utils/type.kt +++ b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/utils/type.kt @@ -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)