mirror of
https://github.com/gedoor/legado.git
synced 2025-08-10 00:52:30 +00:00
优化
This commit is contained in:
@@ -8,8 +8,8 @@ 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 kotlinx.coroutines.withTimeout
|
||||
import splitties.init.appCtx
|
||||
import java.lang.ref.WeakReference
|
||||
import java.util.concurrent.CopyOnWriteArrayList
|
||||
@@ -134,12 +134,10 @@ class ContentProcessor private constructor(
|
||||
getContentReplaceRules().forEach { item ->
|
||||
if (item.pattern.isNotEmpty()) {
|
||||
kotlin.runCatching {
|
||||
withTimeout(1000) {
|
||||
mContent = if (item.isRegex) {
|
||||
mContent.replace(item.pattern.toRegex(), item.replacement)
|
||||
} else {
|
||||
mContent.replace(item.pattern, item.replacement)
|
||||
}
|
||||
mContent = if (item.isRegex) {
|
||||
mContent.regexReplace(item.pattern, item.replacement, 1000L)
|
||||
} else {
|
||||
mContent.replace(item.pattern, item.replacement)
|
||||
}
|
||||
}.onFailure {
|
||||
AppLog.put("${item.name}替换出错\n${it.localizedMessage}")
|
||||
|
||||
24
app/src/main/java/io/legado/app/utils/RegexExtensions.kt
Normal file
24
app/src/main/java/io/legado/app/utils/RegexExtensions.kt
Normal file
@@ -0,0 +1,24 @@
|
||||
package io.legado.app.utils
|
||||
|
||||
import io.legado.app.exception.NoStackTraceException
|
||||
import java.util.regex.Pattern
|
||||
|
||||
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()
|
||||
}
|
||||
Reference in New Issue
Block a user