From 680ea242895966ccc88f9d1a00f4ab78afd5f3dd Mon Sep 17 00:00:00 2001 From: ozraru Date: Thu, 14 Sep 2023 12:36:25 +0900 Subject: [PATCH] Fix handle of space at start or last (#376) --- decode_test.go | 12 ++++++++++++ encode_test.go | 15 +++++++++++++++ token/token.go | 4 ++-- token/token_test.go | 3 +++ 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/decode_test.go b/decode_test.go index 0ac4a7fa..2373804d 100644 --- a/decode_test.go +++ b/decode_test.go @@ -67,6 +67,18 @@ func TestDecoder(t *testing.T) { "v: 1.234\n", map[string]string{"v": "1.234"}, }, + { + "v: \" foo\"\n", + map[string]string{"v": " foo"}, + }, + { + "v: \"foo \"\n", + map[string]string{"v": "foo "}, + }, + { + "v: \" foo \"\n", + map[string]string{"v": " foo "}, + }, { "v: false\n", map[string]bool{"v": false}, diff --git a/encode_test.go b/encode_test.go index e872c611..3ff6f1c1 100644 --- a/encode_test.go +++ b/encode_test.go @@ -303,6 +303,21 @@ func TestEncoder(t *testing.T) { map[string]string{"a": "Hello #comment"}, nil, }, + { + "a: \" b\"\n", + map[string]string{"a": " b"}, + nil, + }, + { + "a: \"b \"\n", + map[string]string{"a": "b "}, + nil, + }, + { + "a: \" b \"\n", + map[string]string{"a": " b "}, + nil, + }, { "a: 100.5\n", map[string]interface{}{ diff --git a/token/token.go b/token/token.go index 182f4bea..c86caab2 100644 --- a/token/token.go +++ b/token/token.go @@ -623,12 +623,12 @@ func IsNeedQuoted(value string) bool { } first := value[0] switch first { - case '*', '&', '[', '{', '}', ']', ',', '!', '|', '>', '%', '\'', '"', '@': + case '*', '&', '[', '{', '}', ']', ',', '!', '|', '>', '%', '\'', '"', '@', ' ': return true } last := value[len(value)-1] switch last { - case ':': + case ':', ' ': return true } if looksLikeTimeValue(value) { diff --git a/token/token_test.go b/token/token_test.go index 4f5764ff..dc1b4df0 100644 --- a/token/token_test.go +++ b/token/token_test.go @@ -118,6 +118,9 @@ func TestIsNeedQuoted(t *testing.T) { "Off", "OFF", "@test", + " a", + " a ", + "a ", } for i, test := range needQuotedTests { if !token.IsNeedQuoted(test) {