From 99ca0dec1f993957e16ad2677ee23c0346b2bcff Mon Sep 17 00:00:00 2001 From: Him188 Date: Wed, 4 Mar 2020 12:17:05 +0800 Subject: [PATCH] Introduce cache for MessageChainBuilder --- .../net.mamoe.mirai/message/data/builder.kt | 61 ++++++++++++------- 1 file changed, 40 insertions(+), 21 deletions(-) diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/builder.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/builder.kt index 2214a0a05..9693b5295 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/builder.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/builder.kt @@ -53,47 +53,66 @@ class MessageChainBuilder ) : MutableCollection by container, Appendable { constructor(initialSize: Int) : this(ArrayList(initialSize)) + override fun add(element: Message): Boolean { + flushCache() + return container.add(element) + } + + override fun addAll(elements: Collection): Boolean { + flushCache() + return container.addAll(elements) + } + operator fun Message.unaryPlus() { + flushCache() add(this) } operator fun String.unaryPlus() { - add(this.toMessage()) + add(this) } operator fun plusAssign(plain: String) { - this.add(plain.toMessage()) + withCache { append(plain) } } operator fun plusAssign(message: Message) { + flushCache() this.add(message) } fun add(plain: String) { - this.add(plain.toMessage()) + withCache { append(plain) } + } + + var cache: StringBuilder? = null + private fun flushCache() { + cache?.let { + container.add(it.toString().toMessage()) + } + cache = null + } + + private inline fun withCache(block: StringBuilder.() -> Unit): MessageChainBuilder { + if (cache == null) { + cache = StringBuilder().apply(block) + } else { + cache!!.apply(block) + } + return this } operator fun plusAssign(charSequence: CharSequence) { - this.add(PlainText(charSequence)) + withCache { append(charSequence) } } - override fun append(value: Char): Appendable = apply { - this.add(PlainText(value.toString())) - } + override fun append(value: Char): Appendable = withCache { append(value) } + override fun append(value: CharSequence?): Appendable = withCache { append(value) } + override fun append(value: CharSequence?, startIndex: Int, endIndex: Int) = + withCache { append(value, startIndex, endIndex) } - override fun append(value: CharSequence?): Appendable = apply { - when { - value == null -> this.add(PlainText.Null) - value.isEmpty() -> this.add(PlainText.Empty) - else -> this.add(PlainText(value)) - } - } - - override fun append(value: CharSequence?, startIndex: Int, endIndex: Int): Appendable = apply { - when { - value == null -> this.add(PlainText.Null) - value.isEmpty() -> this.add(PlainText.Empty) - else -> this.add(PlainText(value.substring(startIndex, endIndex))) - } + fun asMessageChain(): MessageChain { + this.flushCache() + return (this as MutableCollection).asMessageChain() } } \ No newline at end of file