From 43f4d51146b80fff55e7bc7a63e43fe7241e8c6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Tue, 5 Jul 2022 11:18:53 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=85=B3=E9=94=AE=E5=AD=97?= =?UTF-8?q?=E5=88=A4=E6=96=ADBUG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lexical.go | 54 ++++++++++++++++-------------------------------------- 1 file changed, 16 insertions(+), 38 deletions(-) diff --git a/lexical.go b/lexical.go index 978c6b9..77e062e 100644 --- a/lexical.go +++ b/lexical.go @@ -8,8 +8,6 @@ package filter import ( - "strings" - "git.zhangdeman.cn/zhangdeman/util" "github.com/pkg/errors" @@ -70,14 +68,15 @@ func (l *lexical) Parse(jsonData string) ([]*lexicalNode, error) { tmpStr = "" } else { // 不是关键词, 继续向后走 - if currentChar == " " { - // 当前字符是空格, 只有在字符串内方才有效 - if l.keyLeftRightTokenCnt%2 == 0 { - // 关键字之间的空格, 忽略即可 - continue - } + if currentChar == " " && l.keyLeftRightTokenCnt%2 == 0 { + // 当前字符是空格, 只有在 "" 之间方才有效 , 关键字之间的空格, 忽略即可 + continue } tmpStr = tmpStr + currentChar + if (tmpStr == "\n" || tmpStr == "\t") && l.keyLeftRightTokenCnt%2 == 0 { + // 不在 "" 之间的 \n \t 无意义, 过滤掉 + tmpStr = "" + } } } if len(tmpStr) > 0 { @@ -87,28 +86,7 @@ func (l *lexical) Parse(jsonData string) ([]*lexicalNode, error) { }) tmpStr = "" } - // 格式化, 去掉 \n \t 等换行符 - format := make([]*lexicalNode, 0) - for idx, val := range l.lexicalResult { - formatVal := strings.ReplaceAll(strings.ReplaceAll(val.Val, "\n", ""), "\t", "") - // 说明是 \n\t 组成的 - if len(formatVal) == 0 { - if idx == 0 { - continue - } - if idx == len(l.lexicalResult)-1 { - break - } - if (!l.lexicalResult[idx-1].IsToken || l.lexicalResult[idx-1].Val != keyLeftRightToken) || - (!l.lexicalResult[idx+1].IsToken || l.lexicalResult[idx+1].Val != keyLeftRightToken) { - // 不是 "" 之间的 \n \t 没有实际意义 - continue - } - } - format = append(format, val) - } - l.lexicalResult = format - // util.JSON.ConsoleOutput(l.lexicalResult) + return l.lexicalResult, nil } @@ -137,7 +115,7 @@ func (l *lexical) inputCharIsToken(inputChar string, tmpStr string) bool { break } // 0 个 或者 偶数个转义符, " 是关键字 - if escapeCharacterTokenCnt%2 == 0 && inputChar == keyLeftRightToken { + if inputChar == keyLeftRightToken && escapeCharacterTokenCnt%2 == 0 { return true } } @@ -159,15 +137,15 @@ func (l *lexical) inputCharIsToken(inputChar string, tmpStr string) bool { // [[],[]] // [1,2,3] // [true,false,true] - if inputChar == commaToken && len(tmpStr) == 0 && - // 对应 {"name":"zhangsan", "age":"18"} - (nil != preNode && preNode.Val == keyLeftRightToken) || + if inputChar == commaToken && len(tmpStr) == 0 && ( + // 对应 {"name":"zhangsan", "age":"18"} + (nil != preNode && preNode.Val == keyLeftRightToken) || // 对应[{"name":"zhangsan", "age":"18"}, {"name":"zhangsan", "age":"18"}] (nil != preNode && preNode.Val == objectRightToken) || // 对应[[],[]] (nil != preNode && preNode.Val == listRightToken) || // 对应 [true,false,true] / [1,2,3] / [1,true,2,false] - (nil != preNode && (preNode.Val == listLeftToken || preNode.Val == commaToken) && (tmpStrType == "number" || tmpStr == "bool")) { // 对应 + (nil != preNode && (preNode.Val == listLeftToken || preNode.Val == commaToken) && (tmpStrType == "number" || tmpStr == "bool"))) { // 对应 return true } @@ -212,7 +190,7 @@ func (l *lexical) inputCharIsToken(inputChar string, tmpStr string) bool { // [] // [[],[]] // "a": [] - if inputChar == listLeftToken && (nil == preNode || // 对应 [] + if inputChar == listLeftToken && len(tmpStr) == 0 && (nil == preNode || // 对应 [] (nil != preNode && preNode.Val == listLeftToken) || // 对应 [[],[]] (nil != preNode && preNode.Val == colonToken)) { // 对应 "a": [] // [ 是关键字 @@ -226,7 +204,7 @@ func (l *lexical) inputCharIsToken(inputChar string, tmpStr string) bool { // [1,2,3] // [true, false] // ["", "" ] - if inputChar == listLeftToken && ( + if inputChar == listRightToken && len(tmpStr) == 0 && ( //对应 [] (nil != preNode && preNode.Val == listLeftToken) || // 对应 [[],[]] @@ -237,7 +215,7 @@ func (l *lexical) inputCharIsToken(inputChar string, tmpStr string) bool { (nil != preNode && (tmpStrType == "number" || tmpStrType == "bool")) || // 对应 ["", "" ] (nil != preNode && preNode.Val == keyLeftRightToken)) { - + return true } return false }