优化
This commit is contained in:
@@ -103,7 +103,7 @@ data class BookChapter(
|
|||||||
displayTitle.replaceRegex(
|
displayTitle.replaceRegex(
|
||||||
item.pattern,
|
item.pattern,
|
||||||
item.replacement
|
item.replacement
|
||||||
)
|
).getOrThrow()
|
||||||
} else {
|
} else {
|
||||||
displayTitle.replace(item.pattern, item.replacement)
|
displayTitle.replace(item.pattern, item.replacement)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -144,7 +144,7 @@ class ContentProcessor private constructor(
|
|||||||
mContent.replaceRegex(
|
mContent.replaceRegex(
|
||||||
item.pattern,
|
item.pattern,
|
||||||
item.replacement
|
item.replacement
|
||||||
)
|
).getOrThrow()
|
||||||
} else {
|
} else {
|
||||||
mContent.replace(item.pattern, item.replacement)
|
mContent.replace(item.pattern, item.replacement)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
package io.legado.app.utils
|
package io.legado.app.utils
|
||||||
|
|
||||||
import android.util.Log
|
|
||||||
import com.google.re2j.Pattern
|
import com.google.re2j.Pattern
|
||||||
import com.script.SimpleBindings
|
import com.script.SimpleBindings
|
||||||
import io.legado.app.constant.AppConst
|
import io.legado.app.constant.AppConst
|
||||||
@@ -8,26 +7,27 @@ import io.legado.app.constant.AppConst
|
|||||||
/**
|
/**
|
||||||
* 带有超时检测的正则替换
|
* 带有超时检测的正则替换
|
||||||
*/
|
*/
|
||||||
fun CharSequence.replaceRegex(regex: String, replacement: String): String {
|
fun CharSequence.replaceRegex(regex: String, replacement: String): Result<String> {
|
||||||
val charSequence = this
|
return kotlin.runCatching {
|
||||||
val isJs = replacement.startsWith("@js:")
|
val charSequence = this
|
||||||
val replacement1 = if (isJs) replacement.substring(4) else replacement
|
val isJs = replacement.startsWith("@js:")
|
||||||
val pattern = Pattern.compile(regex)
|
val replacement1 = if (isJs) replacement.substring(4) else replacement
|
||||||
val matcher = pattern.matcher(charSequence)
|
val pattern = Pattern.compile(regex)
|
||||||
val stringBuffer = StringBuffer()
|
val matcher = pattern.matcher(charSequence)
|
||||||
while (matcher.find()) {
|
val stringBuffer = StringBuffer()
|
||||||
if (isJs) {
|
while (matcher.find()) {
|
||||||
val bindings = SimpleBindings()
|
if (isJs) {
|
||||||
bindings["result"] = matcher.group()
|
val bindings = SimpleBindings()
|
||||||
val jsResult =
|
bindings["result"] = matcher.group()
|
||||||
AppConst.SCRIPT_ENGINE.eval(replacement1, bindings).toString()
|
val jsResult =
|
||||||
matcher.appendReplacement(stringBuffer, jsResult)
|
AppConst.SCRIPT_ENGINE.eval(replacement1, bindings).toString()
|
||||||
} else {
|
matcher.appendReplacement(stringBuffer, jsResult)
|
||||||
matcher.appendReplacement(stringBuffer, replacement1)
|
} else {
|
||||||
|
matcher.appendReplacement(stringBuffer, replacement1)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
matcher.appendTail(stringBuffer)
|
||||||
|
stringBuffer.toString()
|
||||||
}
|
}
|
||||||
matcher.appendTail(stringBuffer)
|
|
||||||
Log.e("regex", "end")
|
|
||||||
return stringBuffer.toString()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user