Просмотр исходного кода

Fix ambiguous contentEquals for different arguments String and Message: they now all compare contentToString directly

Him188 5 лет назад
Родитель
Сommit
3ad7bf3be8

+ 3 - 3
mirai-core-api/src/commonMain/kotlin/message/data/Message.kt

@@ -139,16 +139,16 @@ public interface Message { // must be interface. Don't consider any changes.
      *
      * 若本函数返回 `true`, 则表明:
      * - `this` 与 [another] 的 [contentToString] 相等
-     * - `this` 为 [another] 的所有 [MessageContent] 都 [相等][Message.equals] 且有同样的排列顺序.
      */
     public fun contentEquals(another: Message, ignoreCase: Boolean = false): Boolean =
-        contentEqualsImpl(another, ignoreCase)
+        this.contentToString().equals(another.contentToString(), ignoreCase = ignoreCase)
+    // contentEqualsImpl(another, ignoreCase)
 
     /**
      * 判断内容是否与 [another] 相等.
      *
      * 若本函数返回 `true`, 则表明:
-     * - [contentToString] 与 [another] 相等
+     * - `this` 与 [another] 的 [contentToString] 相等
      */
     public fun contentEquals(another: String, ignoreCase: Boolean = false): Boolean {
         return this.contentToString().equals(another, ignoreCase = ignoreCase)

+ 25 - 25
mirai-core-api/src/commonMain/kotlin/message/data/impl.kt

@@ -36,31 +36,31 @@ private fun Message.hasDuplicationOfConstrain(key: MessageKey<*>): Boolean {
     }
 }
 
-@JvmSynthetic
-internal fun Message.contentEqualsImpl(another: Message, ignoreCase: Boolean): Boolean {
-    if (!this.contentToString().equals(another.contentToString(), ignoreCase = ignoreCase)) return false
-    return when {
-        this is SingleMessage && another is SingleMessage -> true
-        this is SingleMessage && another is MessageChain -> another.all { it is MessageMetadata || it is PlainText }
-        this is MessageChain && another is SingleMessage -> this.all { it is MessageMetadata || it is PlainText }
-        this is MessageChain && another is MessageChain -> {
-            val anotherIterator = another.iterator()
-
-            /**
-             * 逐个判断非 [PlainText] 的 [Message] 是否 [equals]
-             */
-            this.forEachContent { thisElement ->
-                if (thisElement.isPlain()) return@forEachContent
-                for (it in anotherIterator) {
-                    if (it.isPlain() || it !is MessageContent) continue
-                    if (thisElement != it) return false
-                }
-            }
-            return true
-        }
-        else -> error("shouldn't be reached")
-    }
-}
+//@JvmSynthetic
+//internal fun Message.contentEqualsImpl(another: Message, ignoreCase: Boolean): Boolean {
+//    if (!this.contentToString().equals(another.contentToString(), ignoreCase = ignoreCase)) return false
+//    return when {
+//        this is SingleMessage && another is SingleMessage -> true
+//        this is SingleMessage && another is MessageChain -> another.all { it is MessageMetadata || it is PlainText }
+//        this is MessageChain && another is SingleMessage -> this.all { it is MessageMetadata || it is PlainText }
+//        this is MessageChain && another is MessageChain -> {
+//            val anotherIterator = another.iterator()
+//
+//            /**
+//             * 逐个判断非 [PlainText] 的 [Message] 是否 [equals]
+//             */
+//            this.forEachContent { thisElement ->
+//                if (thisElement.isPlain()) return@forEachContent
+//                for (it in anotherIterator) {
+//                    if (it.isPlain() || it !is MessageContent) continue
+//                    if (thisElement != it) return false
+//                }
+//            }
+//            return true
+//        }
+//        else -> error("shouldn't be reached")
+//    }
+//}
 
 @JvmSynthetic
 internal fun Message.followedByImpl(tail: Message): MessageChain {