Update RuleAnalyzer.kt

修复上次简化过度,意外造成的逻辑错误,测试后没问题了
pull/1112/head
bushixuanqi 3 years ago committed by GitHub
parent 306fc67901
commit 25e70c816a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 46
      app/src/main/java/io/legado/app/model/analyzeRule/RuleAnalyzer.kt

@ -17,41 +17,18 @@ class RuleAnalyzer(data: String, code: Boolean = false) {
val chompBalanced = if (code) ::chompCodeBalanced else ::chompRuleBalanced val chompBalanced = if (code) ::chompCodeBalanced else ::chompRuleBalanced
fun trim() { // 修剪当前规则之前的"@"或者空白符 fun trim() { // 修剪当前规则之前的"@"或者空白符
if(queue[pos] == '@' || queue[pos] < '!') { //为了减少不必要的去设置start或startX,先来个判断
pos++
while (queue[pos] == '@' || queue[pos] < '!') pos++ while (queue[pos] == '@' || queue[pos] < '!') pos++
start = pos //开始点推移
startX = pos //规则起始点推移
}
} }
//将pos重置为0,方便复用 //将pos重置为0,方便复用
fun reSetPos() { fun reSetPos() {
pos = 0 pos = 0
} startX = 0
//当前拉取字段
fun currString(): String {
return queue.substring(start, pos) //当前拉取到的字段
}
/**
* 是否已无剩余字符?
* @return 若剩余字串中已无字符则返回true
*/
val isEmpty: Boolean
get() = queue.length - pos == 0 //是否处理到最后
/**
* 消耗剩余字串中一个字符
* @return 返回剩余字串中的下个字符
*/
fun consume(): Char {
return queue[pos++]
}
/**
* 字串与剩余字串是否匹配不区分大小写
* @param seq 字符串被检查
* @return 若下字符串匹配返回 true
*/
fun matches(seq: String): Boolean {
return queue.regionMatches(pos, seq, 0, seq.length, ignoreCase = true)
} }
/** /**
@ -415,14 +392,14 @@ class RuleAnalyzer(data: String, code: Boolean = false) {
fr: (String) -> String? fr: (String) -> String?
): String { ): String {
startX = pos //设置规则起点
val st = StringBuilder() val st = StringBuilder()
while (consumeTo(inner)) { //拉取成功返回true,ruleAnalyzes里的字符序列索引变量pos后移相应位置,否则返回false,且isEmpty为true while (consumeTo(inner)) { //拉取成功返回true,ruleAnalyzes里的字符序列索引变量pos后移相应位置,否则返回false,且isEmpty为true
val posPre = pos //记录consumeTo匹配位置
if (chompCodeBalanced('{', '}')) { if (chompCodeBalanced('{', '}')) {
val frv = fr(queue.substring(start + startStep, pos - endStep)) val frv = fr(queue.substring(posPre + startStep, pos - endStep))
if (frv != null) { if (frv != null) {
st.append(queue.substring(startX, start) + frv) //压入内嵌规则前的内容,及内嵌规则解析得到的字符串 st.append(queue.substring(startX, posPre) + frv) //压入内嵌规则前的内容,及内嵌规则解析得到的字符串
startX = pos //记录下次规则起点 startX = pos //记录下次规则起点
continue //获取内容成功,继续选择下个内嵌规则 continue //获取内容成功,继续选择下个内嵌规则
} }
@ -430,8 +407,9 @@ class RuleAnalyzer(data: String, code: Boolean = false) {
pos += inner.length //拉出字段不平衡,inner只是个普通字串,跳到此inner后继续匹配 pos += inner.length //拉出字段不平衡,inner只是个普通字串,跳到此inner后继续匹配
} }
st.append(queue.substring(startX)) //压入剩余字符串 return if(startX == 0) "" else st.apply {
return st.toString() append(queue.substring(startX))
}.toString()
} }
companion object { companion object {

Loading…
Cancel
Save