From bfba859630acb0eb461bed2c2ac428b3364049e0 Mon Sep 17 00:00:00 2001 From: windyboy Date: Mon, 22 Jul 2024 08:49:21 +0800 Subject: [PATCH] refactor: Improve clean function in aviation_parser.go The code changes in aviation_parser.go refactor the clean function to improve its functionality. The changes include updating variable names for clarity and handling the case where the bodyOnly match is empty. This refactor enhances the cleanliness and readability of the code. --- internal/parsers/aviation_parser.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/internal/parsers/aviation_parser.go b/internal/parsers/aviation_parser.go index 00b7932..a6c1566 100644 --- a/internal/parsers/aviation_parser.go +++ b/internal/parsers/aviation_parser.go @@ -163,12 +163,17 @@ func Parse(rawText string) (*domain.ParsedMessage, error) { // removeEmptyLines removes empty lines from a given text. func clean(text string) string { - cleanedMatch := emptyLineRemove.ReplaceAllString(text, "") - cleanText := strings.ReplaceAll(cleanedMatch, "\n\n", "\n") + cleanedText := emptyLineRemove.ReplaceAllString(text, "") + cleanText := strings.ReplaceAll(cleanedText, "\n\n", "\n") if bodyOnly != nil { - bodyOnly := bodyOnly.FindStringSubmatch(cleanText)[1] - removeLast := bodyOnly[:len(bodyOnly)-1] - return removeLast + match := bodyOnly.FindStringSubmatch(cleanText) + if len(match) > 1 { + bodyOnly := match[1] + if bodyOnly[len(bodyOnly)-1] == '\n' { + return bodyOnly[:len(bodyOnly)-1] + } + return bodyOnly + } } return "" }