From 44e261bf70c7f2f21158a426c6ee00594b5ec581 Mon Sep 17 00:00:00 2001 From: William Bell Date: Wed, 19 Jul 2023 00:05:59 +0100 Subject: [PATCH] fix unquote function --- src/string.go | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/src/string.go b/src/string.go index 430edac..f056b25 100644 --- a/src/string.go +++ b/src/string.go @@ -15,21 +15,36 @@ func isString(code UNPARSEcode) bool { return stringCompile.MatchString(code.code) } +func swapQuotes(text string) string { + result := "" + for _, char := range text { + switch char { + case '"': + result += "'" + case '\'': + result += "\"" + default: + result += string(char) + } + } + return result +} + func unquoted( str string, ) (string, error) { str = strings.Trim(str, " ") - if str[0] == '\'' { - str = strings.Replace(str, "\\\"", "\"", -1) - str = strings.Replace(str, "\"", "\\\"", -1) + char := str[0] + if char == '\'' { + str = swapQuotes(str) } - str = str[1 : len(str)-1] - str = strings.Replace(str, "\\'", "'", -1) - str = "\"" + str + "\"" output, err := strconv.Unquote(str) if err != nil { return "", err } + if char == '\'' { + output = swapQuotes(output) + } return output, nil } @@ -39,7 +54,7 @@ func parseString(code UNPARSEcode) (string, bool, ArErr, int) { unquoted, err := unquoted(trim) if err != nil { - return "", false, ArErr{"Syntax Error", "invalid string", code.line, code.path, code.realcode, true}, 1 + return "", false, ArErr{"Syntax Error", "invalid escape sequence", code.line, code.path, code.realcode, true}, 1 } return unquoted, true, ArErr{}, 1