Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions ini_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,32 @@ func TestLoad(t *testing.T) {
assert.Equal(t, "U", githubValue)
})

t.Run("keep comment symbols inside quoted values", func(t *testing.T) {
f, err := Load([]byte(`
[background]
primary-color = '#e18477'
secondary-color = "#000000"
hash-in-backticks = ` + "`" + `#e18477` + "`" + `
semi-single = 'foo;bar'
semi-double = "foo;bar"
quoted-then-comment = '#e18477' ; trailing
double-then-comment = "#000000" # trailing
`))
require.NoError(t, err)
require.NotNil(t, f)

sec := f.Section("background")
assert.Equal(t, "#e18477", sec.Key("primary-color").String())
assert.Equal(t, "#000000", sec.Key("secondary-color").String())
assert.Equal(t, "#e18477", sec.Key("hash-in-backticks").String())
assert.Equal(t, "foo;bar", sec.Key("semi-single").String())
assert.Equal(t, "foo;bar", sec.Key("semi-double").String())
assert.Equal(t, "#e18477", sec.Key("quoted-then-comment").String())
assert.Equal(t, "; trailing", sec.Key("quoted-then-comment").Comment)
assert.Equal(t, "#000000", sec.Key("double-then-comment").String())
assert.Equal(t, "# trailing", sec.Key("double-then-comment").Comment)
})

t.Run("cannot parse small python-compatible INI files", func(t *testing.T) {
f, err := Load([]byte(`
[long]
Expand Down
77 changes: 65 additions & 12 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,70 @@ func hasSurroundedQuote(in string, quote byte) bool {
strings.IndexByte(in[1:], quote) == len(in)-2
}

func isValueQuote(c byte) bool {
return c == '\'' || c == '"' || c == '`'
}

// indexOutsideQuotes is strings.Index for sep, skipping bytes inside quotes.
func indexOutsideQuotes(line, sep string) int {
if sep == "" {
return 0
}
inQuote := byte(0)
n := len(sep)
for i := 0; i <= len(line)-n; i++ {
c := line[i]
if inQuote != 0 {
if c == inQuote {
inQuote = 0
}
continue
}
if isValueQuote(c) {
inQuote = c
continue
}
if line[i:i+n] == sep {
return i
}
}
return -1
}

func indexAnyOutsideQuotes(line, chars string) int {
inQuote := byte(0)
for i := 0; i < len(line); i++ {
c := line[i]
if inQuote != 0 {
if c == inQuote {
inQuote = 0
}
continue
}
if isValueQuote(c) {
inQuote = c
continue
}
if strings.IndexByte(chars, c) >= 0 {
return i
}
}
return -1
}

// indexInlineComment finds an inline comment, ignoring # and ; inside quotes.
// SpaceBeforeInlineComment keeps the existing search order: " #" then " ;".
func indexInlineComment(line string, spaceBefore bool) int {
if spaceBefore {
i := indexOutsideQuotes(line, " #")
if i == -1 {
i = indexOutsideQuotes(line, " ;")
}
return i
}
return indexAnyOutsideQuotes(line, "#;")
}

func (p *parser) readValue(in []byte, bufferSize int) (string, error) {

line := strings.TrimLeftFunc(string(in), unicode.IsSpace)
Expand Down Expand Up @@ -278,22 +342,11 @@ func (p *parser) readValue(in []byte, bufferSize int) (string, error) {

// Check if ignore inline comment
if !p.options.IgnoreInlineComment {
var i int
if p.options.SpaceBeforeInlineComment {
i = strings.Index(line, " #")
if i == -1 {
i = strings.Index(line, " ;")
}

} else {
i = strings.IndexAny(line, "#;")
}

i := indexInlineComment(line, p.options.SpaceBeforeInlineComment)
if i > -1 {
p.comment.WriteString(line[i:])
line = strings.TrimSpace(line[:i])
}

}

// Trim single and double quotes
Expand Down