Skip to content

Escape structural characters in keys when writing - #90

Open
gaoflow wants to merge 1 commit into
magiconair:mainfrom
gaoflow:fix-write-key-escape-structural-chars
Open

Escape structural characters in keys when writing#90
gaoflow wants to merge 1 commit into
magiconair:mainfrom
gaoflow:fix-write-key-escape-structural-chars

Conversation

@gaoflow

@gaoflow gaoflow commented Jul 28, 2026

Copy link
Copy Markdown

Write escaped only space and : in a key, but the lexer treats two more characters as structural in a key position (lex.go): = ends a key (isEndOfKey) and a leading #/! starts a comment (isComment). So Load(Write(p)) was not identity for such keys — = split the key and a leading #/! dropped the whole line.

p := properties.NewProperties()
p.MustSet("a=b", "c")
var b bytes.Buffer
p.Write(&b, properties.UTF8)            // a=b = c
p2, _ := properties.LoadString(b.String())
_, ok := p2.Get("a=b")                 // ok == false, key parsed as "a"

Escaping = and a leading #/! fixes it; the parser already reads \=, \#, \!. Mid-key #/! are left unescaped since they are only structural at line start (and already parse fine, e.g. key#2 in the read tests). The value path is unchanged — none of these are structural after the separator.

Added round-trip and exact-output tests covering every special key character in both encodings. Full suite, gofmt and go vet pass.

Fixes #59

Write escaped only space and colon in a key, but the lexer also ends a
key at '=' and treats a leading '#'/'!' as a comment. Writing such a key
therefore did not round-trip: '=' split the key and a leading '#'/'!'
dropped the whole line. Escape '=' and a leading '#'/'!' so Load(Write(p))
is identity; the parser already reads the escaped forms.

Fixes magiconair#59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Equal sign is incorrectly escaped in key

1 participant