From 25e70c816ae9b83fbc7ebfa6ffef027543d35c80 Mon Sep 17 00:00:00 2001 From: bushixuanqi <57338301+bushixuanqi@users.noreply.github.com> Date: Fri, 9 Jul 2021 22:43:47 +0800 Subject: [PATCH 1/3] Update RuleAnalyzer.kt 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 { From 249a51531a10d461436de3bb2b33a2a0ff0bf67a Mon Sep 17 00:00:00 2001 From: bushixuanqi <57338301+bushixuanqi@users.noreply.github.com> Date: Fri, 9 Jul 2021 22:50:34 +0800 Subject: [PATCH 2/3] Update RuleAnalyzer.kt MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 格式化代码 --- .../app/model/analyzeRule/RuleAnalyzer.kt | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 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 d4b220f04..6e2da20aa 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 @@ -92,13 +92,13 @@ class RuleAnalyzer(data: String, code: Boolean = false) { //其中js只要符合语法,就不用避开任何阅读关键字,自由发挥 fun chompJsBalanced( - f: ((Char) -> Boolean?) = { - when (it) { - '{' -> true //开始嵌套一层 - '}' -> false //闭合一层嵌套 - else -> null + f: ((Char) -> Boolean?) = { + when (it) { + '{' -> true //开始嵌套一层 + '}' -> false //闭合一层嵌套 + else -> null + } } - } ): Boolean { var pos = pos //声明变量记录临时处理位置 var depth = 0 //嵌套深度 @@ -116,11 +116,11 @@ class RuleAnalyzer(data: String, code: Boolean = false) { var c = queue[pos++] if (c != '\\') { //非转义字符 if (c == '\'' && !commits && !commit && !regex && !inDoubleQuote && !inOtherQuote) inSingleQuote = - !inSingleQuote //匹配具有语法功能的单引号 + !inSingleQuote //匹配具有语法功能的单引号 else if (c == '"' && !commits && !commit && !regex && !inSingleQuote && !inOtherQuote) inDoubleQuote = - !inDoubleQuote //匹配具有语法功能的双引号 + !inDoubleQuote //匹配具有语法功能的双引号 else if (c == '`' && !commits && !commit && !regex && !inSingleQuote && !inDoubleQuote) inOtherQuote = - !inOtherQuote //匹配具有语法功能的'`' + !inOtherQuote //匹配具有语法功能的'`' else if (c == '/' && !commits && !commit && !regex && !inSingleQuote && !inDoubleQuote && !inOtherQuote) { //匹配注释或正则起点 c = queue[pos++] when (c) { @@ -300,7 +300,7 @@ class RuleAnalyzer(data: String, code: Boolean = false) { val next = if (queue[pos] == '[') ']' else ')' //平衡组末尾字符 if (!chompBalanced(queue[pos], next)) throw Error( - queue.substring(0, start) + "后未平衡" + queue.substring(0,start) + "后未平衡" ) //拉出一个筛选器,不平衡则报错 } while (end > pos) @@ -357,7 +357,7 @@ class RuleAnalyzer(data: String, code: Boolean = false) { val next = if (queue[pos] == '[') ']' else ')' //平衡组末尾字符 if (!chompBalanced(queue[pos], next)) throw Error( - queue.substring(0, start) + "后未平衡" + queue.substring(0,start) + "后未平衡" ) //拉出一个筛选器,不平衡则报错 } while (end > pos) @@ -380,10 +380,10 @@ class RuleAnalyzer(data: String, code: Boolean = false) { * * */ fun innerRule( - inner: String, - startStep: Int = 1, - endStep: Int = 1, - fr: (String) -> String? + inner: String, + startStep: Int = 1, + endStep: Int = 1, + fr: (String) -> String? ): String { val st = StringBuilder() From ec51c4af15dfcd800a7a26acef4f4a3d16fd2553 Mon Sep 17 00:00:00 2001 From: bushixuanqi <57338301+bushixuanqi@users.noreply.github.com> Date: Fri, 9 Jul 2021 23:01:29 +0800 Subject: [PATCH 3/3] Update RuleAnalyzer.kt MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 优化注释描述 --- .../main/java/io/legado/app/model/analyzeRule/RuleAnalyzer.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 6e2da20aa..2b6462827 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,7 +17,7 @@ class RuleAnalyzer(data: String, code: Boolean = false) { val chompBalanced = if (code) ::chompCodeBalanced else ::chompRuleBalanced fun trim() { // 修剪当前规则之前的"@"或者空白符 - if(queue[pos] == '@' || queue[pos] < '!') { //为了减少不必要的去设置start或startX,先来个判断 + if(queue[pos] == '@' || queue[pos] < '!') { //在while里重复设置start和startX会拖慢执行速度,所以先来个判断是否存在需要修剪的字段,最后再一次性设置start和startX pos++ while (queue[pos] == '@' || queue[pos] < '!') pos++ start = pos //开始点推移