From ca863af9c4a3809044611218e8f6cd2ecae9ae39 Mon Sep 17 00:00:00 2001 From: kayomn Date: Tue, 20 Dec 2022 21:53:34 +0000 Subject: [PATCH] Fix inconsistency between specification and implementation --- ini.go | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/ini.go b/ini.go index c82374b..e7b0529 100644 --- a/ini.go +++ b/ini.go @@ -64,26 +64,15 @@ func (parser *Parser) Parse() Entry { if assignmentIndex := strings.Index(line, "="); assignmentIndex > -1 { // Key with value. - var value = strings.TrimSpace(line[assignmentIndex+1:]) - var valueLen = len(value) - - if valueLen != 0 { - var valueTail = len(value) - 1 - - if (value[0] == '"') && (value[valueTail] == '"') { - value = value[1:valueTail] - } - } - return Entry{ Section: parser.section, - Key: strings.TrimSpace(line[0:assignmentIndex]), - Value: value, + Key: unquote(strings.TrimSpace(line[0:assignmentIndex])), + Value: unquote(strings.TrimSpace(line[assignmentIndex+1:])), } } // Key which is its own value. - var keyValue = line[1:lineTail] + var keyValue = unquote(line[1:lineTail]) return Entry{ Section: parser.section, @@ -105,3 +94,18 @@ type Parser struct { section string isEnd bool } + +// Returns a string with the the outer-most quotes surrounding `s` trimmed, should they exist. +func unquote(s string) string { + var sLen = len(s) + + if sLen != 0 { + var sTail = sLen - 1 + + if (s[0] == '"') && (s[sTail] == '"') { + return s[1:sTail] + } + } + + return s +}