From 0de76149c82f98cfec37d88a2586e391c7aafa51 Mon Sep 17 00:00:00 2001 From: Him188 Date: Wed, 29 Apr 2020 15:21:00 +0800 Subject: [PATCH] Make `SingleMessage` not extending `CharSequence` for clearer semantics --- .../message/data/ForwardMessage.kt | 6 ------ .../message/data/HummerMessage.kt | 17 ----------------- .../net.mamoe.mirai/message/data/Image.kt | 8 ++------ .../net.mamoe.mirai/message/data/Message.kt | 11 +++-------- .../net.mamoe.mirai/message/data/RichMessage.kt | 5 ----- 5 files changed, 5 insertions(+), 42 deletions(-) diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/ForwardMessage.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/ForwardMessage.kt index 062e3c1e3..623d91c39 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/ForwardMessage.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/ForwardMessage.kt @@ -178,12 +178,6 @@ class ForwardMessage @JvmOverloads constructor( @MiraiExperimentalAPI override fun contentToString(): String = contentToString - override val length: Int get() = contentToString.length - override fun get(index: Int): Char = contentToString[length] - override fun subSequence(startIndex: Int, endIndex: Int): CharSequence = - contentToString.subSequence(startIndex, endIndex) - - override fun compareTo(other: String): Int = contentToString.compareTo(other) } diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/HummerMessage.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/HummerMessage.kt index 62382e848..186990a94 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/HummerMessage.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/HummerMessage.kt @@ -140,13 +140,7 @@ data class PokeMessage internal constructor( private val stringValue = "[mirai:poke:$type,$id]" override fun toString(): String = stringValue - override val length: Int get() = stringValue.length - override fun get(index: Int): Char = stringValue[index] - override fun subSequence(startIndex: Int, endIndex: Int): CharSequence = - stringValue.subSequence(startIndex, endIndex) - override fun contentToString(): String = "[戳一戳]" - override fun compareTo(other: String): Int = stringValue.compareTo(other) //businessType=0x00000001(1) //pbElem=08 01 18 00 20 FF FF FF FF 0F 2A 00 32 00 38 00 50 00 //serviceType=0x00000002(2) @@ -231,13 +225,7 @@ data class VipFace internal constructor( private val stringValue = "[mirai:vipface:$kind,$count]" override fun toString(): String = stringValue - override val length: Int get() = stringValue.length - override fun get(index: Int): Char = stringValue[index] - override fun subSequence(startIndex: Int, endIndex: Int): CharSequence = - stringValue.subSequence(startIndex, endIndex) - override fun contentToString(): String = "[${kind.name}]x$count" - override fun compareTo(other: String): Int = stringValue.compareTo(other) } @@ -299,11 +287,6 @@ sealed class FlashImage : MessageContent, HummerMessage() { } override fun toString(): String = stringValue!! - override val length: Int get() = stringValue!!.length - override fun get(index: Int) = stringValue!![index] - override fun subSequence(startIndex: Int, endIndex: Int) = stringValue!!.subSequence(startIndex, endIndex) - override fun compareTo(other: String) = other.compareTo(stringValue!!) - override fun contentToString(): String = "[闪照]" } diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/Image.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/Image.kt index e9089b4c4..afce9ce72 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/Image.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/Image.kt @@ -110,6 +110,7 @@ val FRIEND_IMAGE_ID_REGEX_2 = Regex("""/[0-9]*-[0-9]*-[0-9a-zA-Z]{32}""") * * `{01E9451B-70ED-EAE3-B37C-101F1EEBF5B5}.mirai` */ +@Suppress("RegExpRedundantEscape") // This is required on Android @SinceMirai("0.39.2") // Java: MessageUtils.GROUP_IMAGE_ID_REGEX val GROUP_IMAGE_ID_REGEX = Regex("""\{.{8}-(.{4}-){3}.{12}\}\.mirai""") @@ -117,6 +118,7 @@ val GROUP_IMAGE_ID_REGEX = Regex("""\{.{8}-(.{4}-){3}.{12}\}\.mirai""") /** * 在 `0.39.0` 前的图片的正则表示 */ +@Suppress("RegExpRedundantEscape") // This is required on Android @Deprecated("Only for temporal use", replaceWith = ReplaceWith("GROUP_IMAGE_ID_REGEX", "net.mamoe.mirai.message.data.GROUP_IMAGE_ID_REGEX")) @SinceMirai("0.39.2") @@ -323,12 +325,6 @@ sealed class AbstractImage : Image { field = "[mirai:image:$imageId]" field } - override val length: Int get() = _stringValue!!.length - override fun get(index: Int): Char = _stringValue!![index] - override fun subSequence(startIndex: Int, endIndex: Int): CharSequence = - _stringValue!!.subSequence(startIndex, endIndex) - - override fun compareTo(other: String): Int = _stringValue!!.compareTo(other) final override fun toString(): String = _stringValue!! final override fun contentToString(): String = "[图片]" } diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/Message.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/Message.kt index 26759c053..ca6059eec 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/Message.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/Message.kt @@ -279,7 +279,7 @@ interface Message { // must be interface. Don't consider any changes. @SinceMirai("0.39.3") fun Message.isContentEmpty(): Boolean = when (this) { is MessageMetadata -> true - is PlainText -> this.isEmpty() + is PlainText -> this.stringValue.isEmpty() is MessageChain -> this.all { it.isContentEmpty() } else -> false } @@ -312,7 +312,7 @@ inline fun Message.repeat(count: Int): MessageChain { inline operator fun Message.times(count: Int): MessageChain = this.repeat(count) @Suppress("OverridingDeprecatedMember") -interface SingleMessage : Message, CharSequence, Comparable { +interface SingleMessage : Message { @PlannedRemoval("1.0.0") @JvmSynthetic @Deprecated( @@ -352,12 +352,7 @@ interface SingleMessage : Message, CharSequence, Comparable { * * @see ConstrainSingle 约束一个 [MessageChain] 中只存在这一种类型的元素 */ -interface MessageMetadata : SingleMessage { - override val length: Int get() = 0 - override fun get(index: Int): Char = ""[index] // produce uniform exception - override fun subSequence(startIndex: Int, endIndex: Int): CharSequence = "".subSequence(startIndex, endIndex) - override fun compareTo(other: String): Int = "".compareTo(other) -} +interface MessageMetadata : SingleMessage /** * 约束一个 [MessageChain] 中只存在这一种类型的元素. 新元素将会替换旧元素, 保持原顺序. diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/RichMessage.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/RichMessage.kt index 69575649f..79161455a 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/RichMessage.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/RichMessage.kt @@ -86,11 +86,6 @@ interface RichMessage : MessageContent { } val content: String - - override val length: Int get() = content.length - override fun get(index: Int): Char = content[index] - override fun subSequence(startIndex: Int, endIndex: Int): CharSequence = content.subSequence(startIndex, endIndex) - override fun compareTo(other: String): Int = content.compareTo(other) } /**