This commit is contained in:
kunfei 2022-04-23 23:29:23 +08:00
parent b1f3e10453
commit be6c8f8a38

View File

@ -2,9 +2,8 @@ package io.legado.app.utils
import io.legado.app.exception.RegexTimeoutException import io.legado.app.exception.RegexTimeoutException
import io.legado.app.help.CrashHandler import io.legado.app.help.CrashHandler
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.suspendCancellableCoroutine import kotlinx.coroutines.suspendCancellableCoroutine
import splitties.init.appCtx import splitties.init.appCtx
@ -14,11 +13,10 @@ import kotlin.coroutines.resume
* 带有超时检测的正则替换 * 带有超时检测的正则替换
* 超时重启apk,线程不能强制结束,只能重启apk * 超时重启apk,线程不能强制结束,只能重启apk
*/ */
@DelicateCoroutinesApi
suspend fun CharSequence.replace(regex: Regex, replacement: String, timeout: Long): String { suspend fun CharSequence.replace(regex: Regex, replacement: String, timeout: Long): String {
val charSequence = this val charSequence = this
return suspendCancellableCoroutine { block -> return suspendCancellableCoroutine { block ->
val scope = GlobalScope.launch(IO) { val scope = MainScope().launch(IO) {
try { try {
val result = regex.replace(charSequence, replacement) val result = regex.replace(charSequence, replacement)
block.resume(result) block.resume(result)