Skip to content

Commit 21c0090

Browse files
authored
fix trailing comment parse in properties (#2371)
1 parent 435199f commit 21c0090

File tree

6 files changed

+62
-8
lines changed

6 files changed

+62
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"id": "db2095be-6831-4e47-882d-4a8881cc72ff",
3+
"type": "bugfix",
4+
"description": "Fix recognition of trailing comments in shared config properties. # or ; separators that aren't preceded by whitespace at the end of a property value should be considered part of it.",
5+
"modules": [
6+
"internal/ini"
7+
]
8+
}

internal/ini/parse.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func (p *parser) handleSubProperty(tok *lineTokenSubProperty) {
8282
// "promote" this to a normal property.
8383
p.handleProperty(&lineTokenProperty{
8484
Key: tok.Key,
85-
Value: strings.TrimSpace(trimComment(tok.Value)),
85+
Value: strings.TrimSpace(trimPropertyComment(tok.Value)),
8686
})
8787
return
8888
}

internal/ini/strings.go

+15-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
package ini
22

3-
import "strings"
3+
import (
4+
"strings"
5+
)
46

5-
func trimComment(v string) string {
6-
rest, _, _ := strings.Cut(v, "#")
7-
rest, _, _ = strings.Cut(rest, ";")
8-
return rest
7+
func trimProfileComment(s string) string {
8+
r, _, _ := strings.Cut(s, "#")
9+
r, _, _ = strings.Cut(r, ";")
10+
return r
11+
}
12+
13+
func trimPropertyComment(s string) string {
14+
r, _, _ := strings.Cut(s, " #")
15+
r, _, _ = strings.Cut(r, " ;")
16+
r, _, _ = strings.Cut(r, "\t#")
17+
r, _, _ = strings.Cut(r, "\t;")
18+
return r
919
}
1020

1121
// assumes no surrounding comment

internal/ini/testdata/valid/comments

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[profile comments];comment
2+
a = foo#notcomment
3+
b = foo;notcomment
4+
c = foo#
5+
d = foo;
6+
e = foo bar # comment
7+
f = foo bar ; comment
8+
g = foo # comment
9+
h = foo ; comment
10+
i = # comment
11+
j = ; comment
12+
k = foo # ; comment
13+
l = foo ; # comment
14+
m = foo
15+
n = foo #
16+
o = foo ;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"profile comments": {
3+
"a": "foo#notcomment",
4+
"b": "foo;notcomment",
5+
"c": "foo#",
6+
"d": "foo;",
7+
"e": "foo bar",
8+
"f": "foo \t bar",
9+
"g": "foo",
10+
"h": "foo",
11+
"i": "",
12+
"j": "",
13+
"k": "foo",
14+
"l": "foo",
15+
"m": "foo",
16+
"n": "foo",
17+
"o": "foo"
18+
}
19+
}

internal/ini/tokenize.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func isLineComment(line string) bool {
3030
}
3131

3232
func asProfile(line string) *lineTokenProfile { // " [ type name ] ; comment"
33-
trimmed := strings.TrimSpace(trimComment(line)) // "[ type name ]"
33+
trimmed := strings.TrimSpace(trimProfileComment(line)) // "[ type name ]"
3434
if !isBracketed(trimmed) {
3535
return nil
3636
}
@@ -48,7 +48,8 @@ func asProperty(line string) *lineTokenProperty {
4848
return nil
4949
}
5050

51-
trimmed := strings.TrimRight(trimComment(line), " \t")
51+
trimmed := trimPropertyComment(line)
52+
trimmed = strings.TrimRight(trimmed, " \t")
5253
k, v, ok := splitProperty(trimmed)
5354
if !ok {
5455
return nil

0 commit comments

Comments
 (0)