mirror of
https://github.com/mamoe/mirai.git
synced 2025-03-29 09:10:11 +08:00
Add Message.contentEquals parameter strict
for checking element properties.
This commit is contained in:
parent
4a8aded303
commit
cce37faae9
@ -30,6 +30,7 @@ import net.mamoe.mirai.utils.MiraiExperimentalApi
|
|||||||
import net.mamoe.mirai.utils.PlannedRemoval
|
import net.mamoe.mirai.utils.PlannedRemoval
|
||||||
import net.mamoe.mirai.utils.safeCast
|
import net.mamoe.mirai.utils.safeCast
|
||||||
import kotlin.contracts.contract
|
import kotlin.contracts.contract
|
||||||
|
import kotlin.internal.LowPriorityInOverloadResolution
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 可发送的或从服务器接收的消息.
|
* 可发送的或从服务器接收的消息.
|
||||||
@ -132,14 +133,31 @@ public interface Message { // must be interface. Don't consider any changes.
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 判断内容是否与 [another] 相等.
|
* 判断内容是否与 [another] 相等即 `this` 与 [another] 的 [contentToString] 相等.
|
||||||
|
* [strict] 为 `true` 时, 还会额外判断每个消息元素的类型, 顺序和属性. 如 [Image] 会判断 [Image.imageId]
|
||||||
*
|
*
|
||||||
* 若本函数返回 `true`, 则表明:
|
* **有关 [strict]:** 每个 [Image] 的 [contentToString] 都是 `"[图片]"`,
|
||||||
* - `this` 与 [another] 的 [contentToString] 相等
|
* 在 [strict] 为 `false` 时 [contentEquals] 会得到 `true`,
|
||||||
|
* 而为 `true` 时由于 [Image.imageId] 会被比较, 两张不同的图片的 [contentEquals] 会是 `false`.
|
||||||
|
*
|
||||||
|
* @param ignoreCase 为 `true` 时忽略大小写
|
||||||
*/
|
*/
|
||||||
|
public fun contentEquals(another: Message, ignoreCase: Boolean = false, strict: Boolean = false): Boolean {
|
||||||
|
return if (strict) this.contentEqualsStrictImpl(another, ignoreCase)
|
||||||
|
else this.contentToString().equals(another.contentToString(), ignoreCase = ignoreCase)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判断内容是否与 [another] 相等即 `this` 与 [another] 的 [contentToString] 相等.
|
||||||
|
*
|
||||||
|
* 单个消息的顺序和内容不会被检查, 即只要比较两个 [Image], 总是会得到 `true`, 因为 [Image] 的 [contentToString] 都是 `"[图片]"`.
|
||||||
|
*
|
||||||
|
* @param ignoreCase 为 `true` 时忽略大小写
|
||||||
|
*/
|
||||||
|
@LowPriorityInOverloadResolution
|
||||||
public fun contentEquals(another: Message, ignoreCase: Boolean = false): Boolean =
|
public fun contentEquals(another: Message, ignoreCase: Boolean = false): Boolean =
|
||||||
this.contentToString().equals(another.contentToString(), ignoreCase = ignoreCase)
|
contentEquals(another, ignoreCase, false)
|
||||||
// contentEqualsImpl(another, ignoreCase)
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 判断内容是否与 [another] 相等.
|
* 判断内容是否与 [another] 相等.
|
||||||
|
@ -38,31 +38,31 @@ private fun Message.hasDuplicationOfConstrain(key: MessageKey<*>): Boolean {
|
|||||||
}*/
|
}*/
|
||||||
}
|
}
|
||||||
|
|
||||||
//@JvmSynthetic
|
@JvmSynthetic
|
||||||
//internal fun Message.contentEqualsImpl(another: Message, ignoreCase: Boolean): Boolean {
|
internal fun Message.contentEqualsStrictImpl(another: Message, ignoreCase: Boolean): Boolean {
|
||||||
// if (!this.contentToString().equals(another.contentToString(), ignoreCase = ignoreCase)) return false
|
if (!this.contentToString().equals(another.contentToString(), ignoreCase = ignoreCase)) return false
|
||||||
// return when {
|
return when {
|
||||||
// this is SingleMessage && another is SingleMessage -> true
|
this is SingleMessage && another is SingleMessage -> true
|
||||||
// this is SingleMessage && another is MessageChain -> another.all { it is MessageMetadata || it is PlainText }
|
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 SingleMessage -> this.all { it is MessageMetadata || it is PlainText }
|
||||||
// this is MessageChain && another is MessageChain -> {
|
this is MessageChain && another is MessageChain -> {
|
||||||
// val anotherIterator = another.iterator()
|
val anotherIterator = another.iterator()
|
||||||
//
|
|
||||||
// /**
|
/**
|
||||||
// * 逐个判断非 [PlainText] 的 [Message] 是否 [equals]
|
* 逐个判断非 [PlainText] 的 [Message] 是否 [equals]
|
||||||
// */
|
*/
|
||||||
// this.forEachContent { thisElement ->
|
this.forEachContent { thisElement ->
|
||||||
// if (thisElement.isPlain()) return@forEachContent
|
if (thisElement.isPlain()) return@forEachContent
|
||||||
// for (it in anotherIterator) {
|
for (it in anotherIterator) {
|
||||||
// if (it.isPlain() || it !is MessageContent) continue
|
if (it.isPlain() || it !is MessageContent) continue
|
||||||
// if (thisElement != it) return false
|
if (thisElement != it) return false
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
// return true
|
return true
|
||||||
// }
|
}
|
||||||
// else -> error("shouldn't be reached")
|
else -> error("shouldn't be reached")
|
||||||
// }
|
}
|
||||||
//}
|
}
|
||||||
|
|
||||||
@JvmSynthetic
|
@JvmSynthetic
|
||||||
internal fun Message.followedByImpl(tail: Message): MessageChain {
|
internal fun Message.followedByImpl(tail: Message): MessageChain {
|
||||||
|
Loading…
Reference in New Issue
Block a user