fix: linux 换行符处理
This commit is contained in:
parent
35c0e70c67
commit
7c01c34bc0
|
|
@ -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
|
||||
}
|
||||
|
||||
// 删除指定行范围的内容
|
||||
|
|
|
|||
|
|
@ -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())
|
||||
|
|
|
|||
Loading…
Reference in New Issue