Skip to content

Commit

Permalink
Fix encoding of string that contains "- " (#657)
Browse files Browse the repository at this point in the history
* fix encoding of string that contains "- "

* fix test case
  • Loading branch information
goccy authored Feb 15, 2025
1 parent 2ac8cff commit 89a6600
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 2 deletions.
2 changes: 1 addition & 1 deletion encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func TestEncoder(t *testing.T) {
},
},
{
"a: -\n",
"a: \"-\"\n",
map[string]string{"a": "-"},
nil,
},
Expand Down
5 changes: 4 additions & 1 deletion token/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,9 @@ func IsNeedQuoted(value string) bool {
if isNumber(value) {
return true
}
if value == "-" {
return true
}
first := value[0]
switch first {
case '*', '&', '[', '{', '}', ']', ',', '!', '|', '>', '%', '\'', '"', '@', ' ', '`':
Expand All @@ -696,7 +699,7 @@ func IsNeedQuoted(value string) bool {
switch c {
case '#', '\\':
return true
case ':':
case ':', '-':
if i+1 < len(value) && value[i+1] == ' ' {
return true
}
Expand Down
2 changes: 2 additions & 0 deletions token/token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ func TestIsNeedQuoted(t *testing.T) {
"Null",
"NULL",
"~",
"-",
"- --foo",
}
for i, test := range needQuotedTests {
if !token.IsNeedQuoted(test) {
Expand Down

0 comments on commit 89a6600

Please # to comment.