From 7c01c34bc0b0529b8fb38e4ed5a6bcbd2bb00886 Mon Sep 17 00:00:00 2001 From: BaoXuebin Date: Tue, 29 Oct 2024 15:45:13 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20linux=20=E6=8D=A2=E8=A1=8C=E7=AC=A6?= =?UTF-8?q?=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- script/file.go | 27 ++++++++++++++++++++------- service/transactions.go | 12 ++++++------ 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/script/file.go b/script/file.go index 932cb94..681e193 100644 --- a/script/file.go +++ b/script/file.go @@ -212,7 +212,7 @@ func MkDir(dirPath string) error { // FindConsecutiveMultilineTextInFile 查找文件中连续多行文本片段的开始和结束行号 func FindConsecutiveMultilineTextInFile(filePath string, multilineLines []string) (startLine, endLine int, err error) { for i := range multilineLines { - multilineLines[i] = cleanString(multilineLines[i]) + multilineLines[i] = CleanString(multilineLines[i]) } file, err := os.Open(filePath) @@ -230,7 +230,7 @@ func FindConsecutiveMultilineTextInFile(filePath string, multilineLines []string for scanner.Scan() { lineNumber++ // 清理文件中的当前行 - lineText := cleanString(scanner.Text()) + lineText := CleanString(scanner.Text()) // 检查当前行是否匹配多行文本片段的当前行 if lineText == multilineLines[matchIndex] { @@ -263,11 +263,24 @@ func FindConsecutiveMultilineTextInFile(filePath string, multilineLines []string return startLine, endLine, nil } -// cleanString 去除字符串中的首尾空白和中间的所有空格字符 -func cleanString(str string) string { - all := strings.ReplaceAll(strings.TrimSpace(str), " ", "") - // 去除逗号,处理金额千分位 - return strings.ReplaceAll(all, ",", "") +// CleanString 去除字符串中的首尾空白和中间的所有空格字符 +func CleanString(str string) string { + if IsComment(str) { + return "" + } + // 去除 " ", ";", "\r" + result := strings.ReplaceAll(str, " ", "") + result = strings.ReplaceAll(result, ";", "") + result = strings.ReplaceAll(result, "\r", "") + return result +} + +func IsComment(line string) bool { + trimmed := strings.TrimLeft(line, " ") + if strings.HasPrefix(trimmed, ";") { + return true + } + return false } // 删除指定行范围的内容 diff --git a/service/transactions.go b/service/transactions.go index 75f8557..ddaddcf 100644 --- a/service/transactions.go +++ b/service/transactions.go @@ -349,7 +349,7 @@ func saveTransaction(c *gin.Context, addTransactionForm TransactionForm, ledgerC return errors.New(e.Error()) } // 使用 \r\t 分割多行文本片段,并清理每一行的空白 - oldLines := filterEmptyStrings(strings.Split(result, "\r\n")) + oldLines := filterEmptyStrings(strings.Split(result, "\n")) startLine, endLine, e := script.FindConsecutiveMultilineTextInFile(beanFilePath, oldLines) if e != nil { InternalError(c, e.Error()) @@ -360,7 +360,7 @@ func saveTransaction(c *gin.Context, addTransactionForm TransactionForm, ledgerC InternalError(c, e.Error()) return errors.New(e.Error()) } - newLines := filterEmptyStrings(strings.Split(line, "\r\n")) + newLines := filterEmptyStrings(strings.Split(line, "\n")) newLines = append(newLines, "") lines, e = script.InsertLines(lines, startLine, newLines) if e != nil { @@ -389,7 +389,7 @@ func filterEmptyStrings(arr []string) []string { // 创建一个新切片来存储非空字符串 var result []string for _, str := range arr { - if str != "" { // 检查字符串是否为空 + if script.CleanString(str) != "" { // 检查字符串是否为空 result = append(result, str) } } @@ -416,7 +416,7 @@ func UpdateTransactionRawTextById(c *gin.Context) { return } - oldLines := filterEmptyStrings(strings.Split(result, "\r\n")) + oldLines := filterEmptyStrings(strings.Split(result, "\n")) startLine, endLine, err := script.FindConsecutiveMultilineTextInFile(beanFilePath, oldLines) if err != nil { InternalError(c, err.Error()) @@ -427,7 +427,7 @@ func UpdateTransactionRawTextById(c *gin.Context) { InternalError(c, e.Error()) return } - newLines := filterEmptyStrings(strings.Split(rawTextUpdateTransactionForm.RawText, "\r\n")) + newLines := filterEmptyStrings(strings.Split(rawTextUpdateTransactionForm.RawText, "\n")) if len(newLines) > 0 { lines, e = script.InsertLines(lines, startLine, newLines) if e != nil { @@ -463,7 +463,7 @@ func DeleteTransactionById(c *gin.Context) { return } - oldLines := filterEmptyStrings(strings.Split(result, "\r\n")) + oldLines := filterEmptyStrings(strings.Split(result, "\n")) startLine, endLine, err := script.FindConsecutiveMultilineTextInFile(beanFilePath, oldLines) if err != nil { InternalError(c, err.Error())