Skip to content

Commit

Permalink
fix: linux 换行符处理
Browse files Browse the repository at this point in the history
  • Loading branch information
BaoXuebin committed Oct 29, 2024
1 parent 35c0e70 commit 7c01c34
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
27 changes: 20 additions & 7 deletions script/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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] {
Expand Down Expand Up @@ -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
}

// 删除指定行范围的内容
Expand Down
12 changes: 6 additions & 6 deletions service/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
}
}
Expand All @@ -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())
Expand All @@ -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 {
Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit 7c01c34

Please sign in to comment.