Skip to content
Merged
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
13 changes: 13 additions & 0 deletions ini_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,19 @@ GITHUB = U;n;k;n;w;o;n
assert.Equal(t, "U#n#k#n#w#o#n", nameValue)
assert.Equal(t, "U;n;k;n;w;o;n", githubValue)
})
t.Run("cuts at the first spaced comment marker when the line has both", func(t *testing.T) {
f, err := LoadSources(
LoadOptions{AllowPythonMultilineValues: false, SpaceBeforeInlineComment: true},
[]byte("key = foo ; note # ticket\n"))
require.NoError(t, err)
assert.Equal(t, "foo", f.Section("").Key("key").String())

f, err = LoadSources(
LoadOptions{AllowPythonMultilineValues: false, SpaceBeforeInlineComment: true},
[]byte("key = foo # ticket ; note\n"))
require.NoError(t, err)
assert.Equal(t, "foo", f.Section("").Key("key").String())
})
})
})

Expand Down
19 changes: 15 additions & 4 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,11 +280,22 @@ func (p *parser) readValue(in []byte, bufferSize int) (string, error) {
if !p.options.IgnoreInlineComment {
var i int
if p.options.SpaceBeforeInlineComment {
i = strings.Index(line, " #")
if i == -1 {
i = strings.Index(line, " ;")
// Cut at the earlier of " #" and " ;". Looking for " #" first
// and only then " ;" left a later " #" to hide an earlier " ;".
iHash := strings.Index(line, " #")
iSemi := strings.Index(line, " ;")
switch {
case iHash == -1:
i = iSemi
case iSemi == -1:
i = iHash
default:
if iHash < iSemi {
i = iHash
} else {
i = iSemi
}
}

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