From abb2aed73a9e5fb1fb2ea9fc77dd4ec134b9e686 Mon Sep 17 00:00:00 2001 From: sarankumar1325 <120080287+sarankumar1325@users.noreply.github.com> Date: Wed, 11 Nov 2026 11:01:33 +0530 Subject: [PATCH] Cut SpaceBeforeInlineComment values at the earlier marker With SpaceBeforeInlineComment, readValue looked for " #" first and only looked for " ;" when " #" was absent. A value such as "foo ; note # ticket" kept the "; note" part. Cut at whichever of " #" or " ;" appears first, matching IndexAny when the option is off. Fixes #426 Signed-off-by: sarankumar1325 <120080287+sarankumar1325@users.noreply.github.com> --- ini_test.go | 13 +++++++++++++ parser.go | 19 +++++++++++++++---- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/ini_test.go b/ini_test.go index a3d3b4c..83eafad 100644 --- a/ini_test.go +++ b/ini_test.go @@ -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()) + }) }) }) diff --git a/parser.go b/parser.go index 513b19e..24296b3 100644 --- a/parser.go +++ b/parser.go @@ -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, "#;") }