pull/2431/head
kunfei 2 years ago
parent b17e69c6a1
commit 78d5db1bf0
  1. 18
      app/src/main/java/io/legado/app/utils/RegexExtensions.kt

@ -9,7 +9,10 @@ import io.legado.app.exception.RegexTimeoutException
*/ */
fun CharSequence.replace(regex: Regex, replacement: String, timeout: Long): String { fun CharSequence.replace(regex: Regex, replacement: String, timeout: Long): String {
val startTime = System.currentTimeMillis() val startTime = System.currentTimeMillis()
val charSequence = this val charSequence = ListenerCharSequence(this) {
//根本没有执行到这步,和网上的方案不符
print(it)
}
val isJs = replacement.startsWith("@js:") val isJs = replacement.startsWith("@js:")
val replacement1 = if (isJs) replacement.substring(4) else replacement val replacement1 = if (isJs) replacement.substring(4) else replacement
val pattern = regex.toPattern() val pattern = regex.toPattern()
@ -33,19 +36,24 @@ fun CharSequence.replace(regex: Regex, replacement: String, timeout: Long): Stri
return stringBuffer.toString() return stringBuffer.toString()
} }
class ListenerCharSequence(private val inner: CharSequence) : CharSequence { class ListenerCharSequence(
private val inner: CharSequence,
private val charAtListener: ((Int) -> Unit)
) : CharSequence {
var charAtListener: ((Int) -> Unit)? = null override fun toString(): String {
return inner.toString()
}
override val length: Int = inner.length override val length: Int = inner.length
override fun get(index: Int): Char { override fun get(index: Int): Char {
charAtListener?.invoke(index) charAtListener.invoke(index)
return inner[index] return inner[index]
} }
override fun subSequence(startIndex: Int, endIndex: Int): CharSequence { override fun subSequence(startIndex: Int, endIndex: Int): CharSequence {
return inner.subSequence(startIndex, endIndex) return ListenerCharSequence(inner.subSequence(startIndex, endIndex), charAtListener)
} }
} }

Loading…
Cancel
Save