Replace usage of List.replaceAll with Kotlin's

This commit is contained in:
Him188 2021-02-24 14:51:43 +08:00
parent 0529d7679b
commit d3583162a5
2 changed files with 9 additions and 1 deletions

View File

@ -18,6 +18,7 @@ import net.mamoe.mirai.message.data.Image.Key.IMAGE_ID_REGEX
import net.mamoe.mirai.message.data.Image.Key.IMAGE_RESOURCE_ID_REGEX_1
import net.mamoe.mirai.message.data.Image.Key.IMAGE_RESOURCE_ID_REGEX_2
import net.mamoe.mirai.utils.MiraiExperimentalApi
import net.mamoe.mirai.utils.replaceAllKotlin
import kotlin.native.concurrent.SharedImmutable
// region image
@ -128,7 +129,7 @@ internal fun constrainSingleMessagesImpl(sequence: Sequence<SingleMessage>): Lis
if (singleMessage is ConstrainSingle) {
val key = singleMessage.key.topmostKey
val firstOccurrence = list.first { it != null && key.isInstance(it) } // may be singleMessage itself
list.replaceAll {
list.replaceAllKotlin {
when {
it == null -> null
it === firstOccurrence -> singleMessage

View File

@ -157,3 +157,10 @@ public inline fun <R> runCatchingExceptions(block: () -> R): Result<R> {
Result.failure(e)
}
}
public inline fun <E> MutableList<E>.replaceAllKotlin(operator: (E) -> E) {
val li: MutableListIterator<E> = this.listIterator()
while (li.hasNext()) {
li.set(operator(li.next()))
}
}