This commit is contained in:
kunfei
2022-04-11 23:41:41 +08:00
parent 968d9cff83
commit 8bfa1f227d
2 changed files with 7 additions and 22 deletions
@@ -8,7 +8,6 @@ import io.legado.app.data.entities.BookChapter
import io.legado.app.data.entities.ReplaceRule import io.legado.app.data.entities.ReplaceRule
import io.legado.app.help.config.AppConfig import io.legado.app.help.config.AppConfig
import io.legado.app.help.config.ReadBookConfig import io.legado.app.help.config.ReadBookConfig
import io.legado.app.utils.regexReplace
import io.legado.app.utils.toastOnUi import io.legado.app.utils.toastOnUi
import splitties.init.appCtx import splitties.init.appCtx
import java.lang.ref.WeakReference import java.lang.ref.WeakReference
@@ -135,12 +134,12 @@ class ContentProcessor private constructor(
if (item.pattern.isNotEmpty()) { if (item.pattern.isNotEmpty()) {
kotlin.runCatching { kotlin.runCatching {
mContent = if (item.isRegex) { mContent = if (item.isRegex) {
mContent.regexReplace(item.pattern, item.replacement, 1000L) mContent.replace(item.pattern.toRegex(), item.replacement)
} else { } else {
mContent.replace(item.pattern, item.replacement) mContent.replace(item.pattern, item.replacement)
} }
}.onFailure { }.onFailure {
AppLog.put("${item.name}替换出错\n${it.localizedMessage}") AppLog.put("${item.name}替换出错\n${it.localizedMessage}", it)
appCtx.toastOnUi("${item.name}替换出错") appCtx.toastOnUi("${item.name}替换出错")
} }
} }
@@ -1,24 +1,10 @@
package io.legado.app.utils package io.legado.app.utils
import io.legado.app.exception.NoStackTraceException import kotlinx.coroutines.suspendCancellableCoroutine
import java.util.regex.Pattern
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()
} }