fix: if a line contains multiple # characters, there will be issues w… (#238)

* fix: if a line contains multiple # characters, there will be issues when traversing from back to front

* fix: typo
main v1.6.0-pre.2
柚子uccs 2024-12-16 12:14:19 +08:00 committed by GitHub
parent a7f6c4c583
commit 3a7a190201
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 20 additions and 14 deletions

View File

@ -1,4 +1,7 @@
# Full line comment # Full line comment
qux=thud # fred # other
thud=fred#qux # other
fred=qux#baz # other # more
foo=bar # baz foo=bar # baz
bar=foo#baz bar=foo#baz
baz="foo"#bar baz="foo"#bar

View File

@ -477,6 +477,9 @@ func TestErrorParsing(t *testing.T) {
func TestComments(t *testing.T) { func TestComments(t *testing.T) {
envFileName := "fixtures/comments.env" envFileName := "fixtures/comments.env"
expectedValues := map[string]string{ expectedValues := map[string]string{
"qux": "thud",
"thud": "fred#qux",
"fred": "qux#baz",
"foo": "bar", "foo": "bar",
"bar": "foo#baz", "bar": "foo#baz",
"baz": "foo", "baz": "foo",

View File

@ -143,9 +143,9 @@ func extractVarValue(src []byte, vars map[string]string) (value string, rest []b
} }
// Work backwards to check if the line ends in whitespace then // Work backwards to check if the line ends in whitespace then
// a comment (ie asdasd # some comment) // a comment, ie: foo=bar # baz # other
for i := endOfVar - 1; i >= 0; i-- { for i := 0; i < endOfVar; i++ {
if line[i] == charComment && i > 0 { if line[i] == charComment && i < endOfVar {
if isSpace(line[i-1]) { if isSpace(line[i-1]) {
endOfVar = i endOfVar = i
break break