Fix inconsistency between specification and implementation

This commit is contained in:
kayomn 2022-12-20 21:53:34 +00:00
parent 7cd6ac1c51
commit ca863af9c4
1 changed files with 18 additions and 14 deletions

32
ini.go
View File

@ -64,26 +64,15 @@ func (parser *Parser) Parse() Entry {
if assignmentIndex := strings.Index(line, "="); assignmentIndex > -1 { if assignmentIndex := strings.Index(line, "="); assignmentIndex > -1 {
// Key with value. // 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{ return Entry{
Section: parser.section, Section: parser.section,
Key: strings.TrimSpace(line[0:assignmentIndex]), Key: unquote(strings.TrimSpace(line[0:assignmentIndex])),
Value: value, Value: unquote(strings.TrimSpace(line[assignmentIndex+1:])),
} }
} }
// Key which is its own value. // Key which is its own value.
var keyValue = line[1:lineTail] var keyValue = unquote(line[1:lineTail])
return Entry{ return Entry{
Section: parser.section, Section: parser.section,
@ -105,3 +94,18 @@ type Parser struct {
section string section string
isEnd bool 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
}