Fix encoding of string that contains "- " (#657)

* fix encoding of string that contains "- "

* fix test case
This commit is contained in:
Masaaki Goshima 2025-02-15 12:00:39 +09:00 committed by GitHub
parent 2ac8cffa27
commit 89a66008de
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 7 additions and 2 deletions

View file

@ -171,7 +171,7 @@ func TestEncoder(t *testing.T) {
},
},
{
"a: -\n",
"a: \"-\"\n",
map[string]string{"a": "-"},
nil,
},

View file

@ -679,6 +679,9 @@ func IsNeedQuoted(value string) bool {
if isNumber(value) {
return true
}
if value == "-" {
return true
}
first := value[0]
switch first {
case '*', '&', '[', '{', '}', ']', ',', '!', '|', '>', '%', '\'', '"', '@', ' ', '`':
@ -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
}

View file

@ -133,6 +133,8 @@ func TestIsNeedQuoted(t *testing.T) {
"Null",
"NULL",
"~",
"-",
"- --foo",
}
for i, test := range needQuotedTests {
if !token.IsNeedQuoted(test) {