From 31ea795e3887ad4b204c9adb239ae15ea637d686 Mon Sep 17 00:00:00 2001 From: bushixuanqi <2208877960@qq.com> Date: Fri, 9 Jul 2021 22:41:32 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=B8=8A=E6=AC=A1=E7=AE=80?= =?UTF-8?q?=E5=8C=96=E8=BF=87=E5=BA=A6=EF=BC=8C=E6=84=8F=E5=A4=96=E9=80=A0?= =?UTF-8?q?=E6=88=90=E7=9A=84=E9=80=BB=E8=BE=91=E9=94=99=E8=AF=AF=EF=BC=8C?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E5=90=8E=E6=B2=A1=E9=97=AE=E9=A2=98=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/model/analyzeRule/RuleAnalyzer.kt | 48 +++++-------------- 1 file changed, 13 insertions(+), 35 deletions(-) diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/RuleAnalyzer.kt b/app/src/main/java/io/legado/app/model/analyzeRule/RuleAnalyzer.kt index a464ea746..414b0d891 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/RuleAnalyzer.kt +++ b/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 fun trim() { // 修剪当前规则之前的"@"或者空白符 - while (queue[pos] == '@' || queue[pos] < '!') pos++ + if(queue[pos] == '@' || queue[pos] < '!') { //为了减少不必要的去设置start或startX,先来个判断 + pos++ + while (queue[pos] == '@' || queue[pos] < '!') pos++ + start = pos //开始点推移 + startX = pos //规则起始点推移 + } } //将pos重置为0,方便复用 fun reSetPos() { pos = 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) + startX = 0 } /** @@ -415,14 +392,14 @@ class RuleAnalyzer(data: String, code: Boolean = false) { fr: (String) -> String? ): String { - startX = pos //设置规则起点 val st = StringBuilder() while (consumeTo(inner)) { //拉取成功返回true,ruleAnalyzes里的字符序列索引变量pos后移相应位置,否则返回false,且isEmpty为true + val posPre = pos //记录consumeTo匹配位置 if (chompCodeBalanced('{', '}')) { - val frv = fr(queue.substring(start + startStep, pos - endStep)) + val frv = fr(queue.substring(posPre + startStep, pos - endStep)) if (frv != null) { - st.append(queue.substring(startX, start) + frv) //压入内嵌规则前的内容,及内嵌规则解析得到的字符串 + st.append(queue.substring(startX, posPre) + frv) //压入内嵌规则前的内容,及内嵌规则解析得到的字符串 startX = pos //记录下次规则起点 continue //获取内容成功,继续选择下个内嵌规则 } @@ -430,8 +407,9 @@ class RuleAnalyzer(data: String, code: Boolean = false) { pos += inner.length //拉出字段不平衡,inner只是个普通字串,跳到此inner后继续匹配 } - st.append(queue.substring(startX)) //压入剩余字符串 - return st.toString() + return if(startX == 0) "" else st.apply { + append(queue.substring(startX)) + }.toString() } companion object {