From bc0d8eb8aa1bc8f1722cef1c705b957f254f4357 Mon Sep 17 00:00:00 2001 From: Eliot Horowitz Date: Thu, 14 Mar 2013 10:36:08 -0400 Subject: [PATCH] A colon should be only be split on if its followed by a space --- yaml/parser.go | 41 +++++++++++++++++++++-------------------- yaml/parser_test.go | 12 ++++++++++-- 2 files changed, 31 insertions(+), 22 deletions(-) diff --git a/yaml/parser.go b/yaml/parser.go index 02fb24c..32174f8 100644 --- a/yaml/parser.go +++ b/yaml/parser.go @@ -208,8 +208,9 @@ func getType(line []byte) (typ, split int) { return } + typ = typScalar + if line[0] == ' ' || line[0] == '"' { - typ = typScalar return } @@ -220,38 +221,38 @@ func getType(line []byte) (typ, split int) { idx := bytes.IndexAny( line, " \":" ) if idx < 0 { - typ = typScalar return } if line[idx] == '"' { - typ = typScalar return } if line[idx] == ':' { typ = typMapping split = idx - return - } - - // we have a space - // need to see if its all spaces until a : - for i := idx; i < len(line); i++ { - switch ch := line[i]; ch { - case ' ': - continue - case ':': - typ = typMapping - split = i - return - default: - typ = typScalar - return + } else if line[idx] == ' ' { + // we have a space + // need to see if its all spaces until a : + for i := idx; i < len(line); i++ { + switch ch := line[i]; ch { + case ' ': + continue + case ':': + typ = typMapping + split = i + break + default: + break + } } } - typ = typScalar + if typ == typMapping && split + 1 < len(line) && line[split+1] != ' ' { + typ = typScalar + split = 0 + } + return } diff --git a/yaml/parser_test.go b/yaml/parser_test.go index d635740..4825013 100644 --- a/yaml/parser_test.go +++ b/yaml/parser_test.go @@ -14,8 +14,16 @@ var parseTests = []struct { Output: "key1: val1\n", }, { - Input: "key1 : val1\n", - Output: "key1: val1\n", + Input: "key2 : val1\n", + Output: "key2: val1\n", + }, + { + Input: "key3:val1\n", + Output: "key3:val1\n", + }, + { + Input: "key4 :val1\n", + Output: "key4 :val1\n", }, { Input: "key: nest: val\n",