From 8bfa1f227d4dc514e03c0dc896d2dd6caf0c26c1 Mon Sep 17 00:00:00 2001 From: kunfei Date: Mon, 11 Apr 2022 23:41:41 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/help/ContentProcessor.kt | 5 ++-- .../io/legado/app/utils/RegexExtensions.kt | 24 ++++--------------- 2 files changed, 7 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/ContentProcessor.kt b/app/src/main/java/io/legado/app/help/ContentProcessor.kt index 8c6b04af8..e3162fa2e 100644 --- a/app/src/main/java/io/legado/app/help/ContentProcessor.kt +++ b/app/src/main/java/io/legado/app/help/ContentProcessor.kt @@ -8,7 +8,6 @@ import io.legado.app.data.entities.BookChapter import io.legado.app.data.entities.ReplaceRule import io.legado.app.help.config.AppConfig import io.legado.app.help.config.ReadBookConfig -import io.legado.app.utils.regexReplace import io.legado.app.utils.toastOnUi import splitties.init.appCtx import java.lang.ref.WeakReference @@ -135,12 +134,12 @@ class ContentProcessor private constructor( if (item.pattern.isNotEmpty()) { kotlin.runCatching { mContent = if (item.isRegex) { - mContent.regexReplace(item.pattern, item.replacement, 1000L) + mContent.replace(item.pattern.toRegex(), item.replacement) } else { mContent.replace(item.pattern, item.replacement) } }.onFailure { - AppLog.put("${item.name}替换出错\n${it.localizedMessage}") + AppLog.put("${item.name}替换出错\n${it.localizedMessage}", it) appCtx.toastOnUi("${item.name}替换出错") } } diff --git a/app/src/main/java/io/legado/app/utils/RegexExtensions.kt b/app/src/main/java/io/legado/app/utils/RegexExtensions.kt index 4988d8d78..1fcf83922 100644 --- a/app/src/main/java/io/legado/app/utils/RegexExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/RegexExtensions.kt @@ -1,24 +1,10 @@ package io.legado.app.utils -import io.legado.app.exception.NoStackTraceException -import java.util.regex.Pattern +import kotlinx.coroutines.suspendCancellableCoroutine + +suspend fun CharSequence.regexReplace(regex: String, replacement: String, timeout: Long): String { + return suspendCancellableCoroutine { + -fun CharSequence.regexReplace(regex: String, replacement: String, timeout: Long): String { - val timeEnd = System.currentTimeMillis() + timeout - val pattern = Pattern.compile(regex) - val matcher = pattern.matcher(this) - var result: Boolean = matcher.find() - if (result) { - val sb = StringBuffer() - do { - //matcher.appendReplacement(sb, replacement) - if (System.currentTimeMillis() > timeEnd) { - throw NoStackTraceException("替换超时") - } - result = matcher.find() - } while (result) - matcher.appendTail(sb) - return sb.toString() } - return this.toString() }