mirror of
https://github.com/goccy/go-yaml.git
synced 2025-12-08 06:09:57 +00:00
Fix encoding of string that contains "- " (#657)
* fix encoding of string that contains "- " * fix test case
This commit is contained in:
parent
2ac8cffa27
commit
89a66008de
3 changed files with 7 additions and 2 deletions
|
|
@ -171,7 +171,7 @@ func TestEncoder(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"a: -\n",
|
"a: \"-\"\n",
|
||||||
map[string]string{"a": "-"},
|
map[string]string{"a": "-"},
|
||||||
nil,
|
nil,
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -679,6 +679,9 @@ func IsNeedQuoted(value string) bool {
|
||||||
if isNumber(value) {
|
if isNumber(value) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
if value == "-" {
|
||||||
|
return true
|
||||||
|
}
|
||||||
first := value[0]
|
first := value[0]
|
||||||
switch first {
|
switch first {
|
||||||
case '*', '&', '[', '{', '}', ']', ',', '!', '|', '>', '%', '\'', '"', '@', ' ', '`':
|
case '*', '&', '[', '{', '}', ']', ',', '!', '|', '>', '%', '\'', '"', '@', ' ', '`':
|
||||||
|
|
@ -696,7 +699,7 @@ func IsNeedQuoted(value string) bool {
|
||||||
switch c {
|
switch c {
|
||||||
case '#', '\\':
|
case '#', '\\':
|
||||||
return true
|
return true
|
||||||
case ':':
|
case ':', '-':
|
||||||
if i+1 < len(value) && value[i+1] == ' ' {
|
if i+1 < len(value) && value[i+1] == ' ' {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -133,6 +133,8 @@ func TestIsNeedQuoted(t *testing.T) {
|
||||||
"Null",
|
"Null",
|
||||||
"NULL",
|
"NULL",
|
||||||
"~",
|
"~",
|
||||||
|
"-",
|
||||||
|
"- --foo",
|
||||||
}
|
}
|
||||||
for i, test := range needQuotedTests {
|
for i, test := range needQuotedTests {
|
||||||
if !token.IsNeedQuoted(test) {
|
if !token.IsNeedQuoted(test) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue