From d567e217bc67f7b5be512ae2c80389d2068f8b10 Mon Sep 17 00:00:00 2001 From: bushixuanqi <57338301+bushixuanqi@users.noreply.github.com> Date: Fri, 9 Jul 2021 16:15:18 +0800 Subject: [PATCH 1/5] Update RuleAnalyzer.kt MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 优化 --- .../app/model/analyzeRule/RuleAnalyzer.kt | 101 ++++++++---------- 1 file changed, 42 insertions(+), 59 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 aece2e35c..c26e817d0 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 @@ -5,25 +5,23 @@ package io.legado.app.model.analyzeRule class RuleAnalyzer(data: String, code: Boolean = false) { private var queue: String = data //被处理字符串 - private var pos = 0 //处理到的位置 - private var rule = arrayOf() + private var pos = 0 //当前处理到的位置 + private var start = 0 //当前处理字段的开始 + private var startX = 0 //当前规则的开始 - private var start = 0 //每次处理字段的开始 - private var startX = 0 //规则的开始 - private var innerStr: Boolean = false //true表示以平衡组的起点为规则起始,false表示不这样 + private var rule = arrayOf() //分割出的规则列表 private var step: Int = 0 //分割字符的长度 + var elementsType = "" //当前分割字符串 //设置平衡组函数,json或JavaScript时设置成chompCodeBalanced,否则为chompRuleBalanced val chompBalanced = if (code) ::chompCodeBalanced else ::chompRuleBalanced - var elementsType = "" - //当前平衡字段 fun currBalancedString( - stepStart: Int = 1, - stepEnd: Int = 1 + stepStart: Int = 1, + stepEnd: Int = 1 ): String { //stepStart平衡字符的起始分隔字串长度,stepEnd平衡字符的结束分隔字串长度 - return queue.substring(startX + stepStart, pos - stepEnd) //当前平衡字段 + return queue.substring(start + stepStart, pos - stepEnd) //当前平衡字段 } @@ -40,14 +38,7 @@ class RuleAnalyzer(data: String, code: Boolean = false) { fun currString(): String { return queue.substring(start, pos) //当前拉取到的字段 } - - //剩余字串 - private fun remainingString(): String { - start = pos - pos = queue.length - return queue.substring(start) - } - + /** * 是否已无剩余字符? * @return 若剩余字串中已无字符则返回true @@ -77,15 +68,13 @@ class RuleAnalyzer(data: String, code: Boolean = false) { * @param seq 分隔字符 **区分大小写** * @return 是否找到相应字段。 */ - fun consumeTo(seq: String, setStartPos: Boolean = true): Boolean { - - if (setStartPos) start = pos //将处理到的位置设置为规则起点 + fun consumeTo(seq: String): Boolean { + start = pos //将处理到的位置设置为规则起点 val offset = queue.indexOf(seq, pos) return if (offset != -1) { pos = offset true } else false - } /** @@ -135,13 +124,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 //嵌套深度 @@ -159,11 +148,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) { @@ -203,7 +192,7 @@ class RuleAnalyzer(data: String, code: Boolean = false) { } /** - * 拉出一个代码平衡组,存在转义文本,没有实体字符,通常以{}作为模块 + * 拉出一个非内嵌代码平衡组,存在转义文本 */ fun chompCodeBalanced(open: Char, close: Char): Boolean { @@ -237,7 +226,6 @@ class RuleAnalyzer(data: String, code: Boolean = false) { } while (depth > 0 || otherDepth > 0) //拉出一个平衡字串 return if (depth > 0 || otherDepth > 0) false else { - if (innerStr) startX = this.pos //内嵌规则起始 this.pos = pos //同步位置 true } @@ -344,10 +332,10 @@ 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) @@ -404,10 +392,10 @@ 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) @@ -421,7 +409,6 @@ class RuleAnalyzer(data: String, code: Boolean = false) { } - /** * 替换内嵌规则 * @param inner 起始标志,如{$. 或 {{ @@ -431,36 +418,32 @@ 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 start0 = pos //规则匹配前起点 - - innerStr = true //设置平衡组以平衡组起点为规则起始点 + val startXS = pos //设置最初起点 + startX = pos //设置规则起点 val st = StringBuilder() - while (!isEmpty && consumeTo(inner)) { //拉取成功返回true,ruleAnalyzes里的字符序列索引变量pos后移相应位置,否则返回false,且isEmpty为true - val posPre = pos //记录上次结束位置 - if (chompBalanced('{', '}')) { + while (consumeTo(inner)) { //拉取成功返回true,ruleAnalyzes里的字符序列索引变量pos后移相应位置,否则返回false,且isEmpty为true + if (chompCodeBalanced('{', '}')) { val frv = fr(currBalancedString(startStep, endStep)) if (frv != null) { - st.append(queue.substring(start, posPre) + frv) //压入内嵌规则前的内容,及内嵌规则解析得到的字符串 + st.append(queue.substring(startX, start) + frv) //压入内嵌规则前的内容,及内嵌规则解析得到的字符串 + startX = pos //记录下次规则起点 continue //获取内容成功,继续选择下个内嵌规则 - } } - pos += inner.length //拉出字段不平衡,inner只是个普通字串,跳到此inner后继续匹配 - } - //匹配前起点与当前规则起点相同,证明无替换成功的内嵌规则,返回空字符串。否则返回替换后的字符串 - return if (start0 == start) "" else { - st.append(remainingString()) //压入剩余字符串 + //最初起点与当前规则起点相同,证明无替换成功的内嵌规则,返回空字符串。否则返回替换后的字符串 + return if (startX == startXS) "" else { + st.append(queue.substring(pos)) //压入剩余字符串 st.toString() } } From 3813a7bf10f7f27997ffba74aa318853f616fd92 Mon Sep 17 00:00:00 2001 From: bushixuanqi <57338301+bushixuanqi@users.noreply.github.com> Date: Fri, 9 Jul 2021 16:28:37 +0800 Subject: [PATCH 2/5] Update RuleAnalyzer.kt MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 优化 --- .../io/legado/app/model/analyzeRule/RuleAnalyzer.kt | 13 ++----------- 1 file changed, 2 insertions(+), 11 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 c26e817d0..837a8e554 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 @@ -16,15 +16,6 @@ class RuleAnalyzer(data: String, code: Boolean = false) { //设置平衡组函数,json或JavaScript时设置成chompCodeBalanced,否则为chompRuleBalanced val chompBalanced = if (code) ::chompCodeBalanced else ::chompRuleBalanced - //当前平衡字段 - fun currBalancedString( - stepStart: Int = 1, - stepEnd: Int = 1 - ): String { //stepStart平衡字符的起始分隔字串长度,stepEnd平衡字符的结束分隔字串长度 - return queue.substring(start + stepStart, pos - stepEnd) //当前平衡字段 - } - - fun trim() { // 修剪当前规则之前的"@"或者空白符 while (queue[pos] == '@' || queue[pos] < '!') pos++ } @@ -38,7 +29,7 @@ class RuleAnalyzer(data: String, code: Boolean = false) { fun currString(): String { return queue.substring(start, pos) //当前拉取到的字段 } - + /** * 是否已无剩余字符? * @return 若剩余字串中已无字符则返回true @@ -431,7 +422,7 @@ class RuleAnalyzer(data: String, code: Boolean = false) { while (consumeTo(inner)) { //拉取成功返回true,ruleAnalyzes里的字符序列索引变量pos后移相应位置,否则返回false,且isEmpty为true if (chompCodeBalanced('{', '}')) { - val frv = fr(currBalancedString(startStep, endStep)) + val frv = fr(queue.substring(start + stepStart, pos - stepEnd)) if (frv != null) { st.append(queue.substring(startX, start) + frv) //压入内嵌规则前的内容,及内嵌规则解析得到的字符串 startX = pos //记录下次规则起点 From 07143c172d8fdb293fc0eed9a5b6e2aaa45609e5 Mon Sep 17 00:00:00 2001 From: bushixuanqi <57338301+bushixuanqi@users.noreply.github.com> Date: Fri, 9 Jul 2021 16:35:13 +0800 Subject: [PATCH 3/5] 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 837a8e554..5fd4aa346 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 @@ -422,7 +422,7 @@ class RuleAnalyzer(data: String, code: Boolean = false) { while (consumeTo(inner)) { //拉取成功返回true,ruleAnalyzes里的字符序列索引变量pos后移相应位置,否则返回false,且isEmpty为true if (chompCodeBalanced('{', '}')) { - val frv = fr(queue.substring(start + stepStart, pos - stepEnd)) + val frv = fr(queue.substring(start + startStep, pos - endStep)) if (frv != null) { st.append(queue.substring(startX, start) + frv) //压入内嵌规则前的内容,及内嵌规则解析得到的字符串 startX = pos //记录下次规则起点 From b7e002e1fe53c7253c96a0cfb020fe8c7dd5f147 Mon Sep 17 00:00:00 2001 From: bushixuanqi <57338301+bushixuanqi@users.noreply.github.com> Date: Fri, 9 Jul 2021 17:04:53 +0800 Subject: [PATCH 4/5] Update RuleAnalyzer.kt MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修正与简化 --- .../java/io/legado/app/model/analyzeRule/RuleAnalyzer.kt | 7 ++----- 1 file changed, 2 insertions(+), 5 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 5fd4aa346..aaeda91ab 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 @@ -432,11 +432,8 @@ class RuleAnalyzer(data: String, code: Boolean = false) { pos += inner.length //拉出字段不平衡,inner只是个普通字串,跳到此inner后继续匹配 } - //最初起点与当前规则起点相同,证明无替换成功的内嵌规则,返回空字符串。否则返回替换后的字符串 - return if (startX == startXS) "" else { - st.append(queue.substring(pos)) //压入剩余字符串 - st.toString() - } + st.append(queue.substring(startX)) //压入剩余字符串 + return st.toString() } companion object { From 6dcc556bd1a88833740b2bf482ec2685d87f7806 Mon Sep 17 00:00:00 2001 From: bushixuanqi <57338301+bushixuanqi@users.noreply.github.com> Date: Fri, 9 Jul 2021 17:06:32 +0800 Subject: [PATCH 5/5] 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, 2 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 aaeda91ab..a464ea746 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 @@ -415,9 +415,7 @@ class RuleAnalyzer(data: String, code: Boolean = false) { fr: (String) -> String? ): String { - val startXS = pos //设置最初起点 startX = pos //设置规则起点 - val st = StringBuilder() while (consumeTo(inner)) { //拉取成功返回true,ruleAnalyzes里的字符序列索引变量pos后移相应位置,否则返回false,且isEmpty为true