From 23f08da86515ee0dd892888bb172e4abac8355a0 Mon Sep 17 00:00:00 2001 From: Yuheng Chen Date: Mon, 14 Nov 2016 11:17:14 +0800 Subject: [PATCH] Emitter: quote strings starts/ends with spaces --- parser/src/emitter.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/parser/src/emitter.rs b/parser/src/emitter.rs index 6354c86..e56fc14 100644 --- a/parser/src/emitter.rs +++ b/parser/src/emitter.rs @@ -254,7 +254,14 @@ impl<'a> YamlEmitter<'a> { /// * When the string looks like a number, such as integers (e.g. 2, 14, etc.), floats (e.g. 2.6, 14.9) and exponential numbers (e.g. 12e7, etc.) (otherwise, it would be treated as a numeric value); /// * When the string looks like a date (e.g. 2014-12-31) (otherwise it would be automatically converted into a Unix timestamp). fn need_quotes(string: &str) -> bool { - string.contains(|character: char| { + fn need_quotes_spaces(string: &str) -> bool { + string.starts_with(' ') + || string.ends_with(' ') + } + + string == "" + || need_quotes_spaces(string) + || string.contains(|character: char| { match character { ':' | '{' | '}' | '[' | ']' | ',' | '&' | '*' | '#' | '?' | '|' | '-' | '<' | '>' | '=' | '!' | '%' | '@' | '`' | '\\' | '\0' ... '\x06' | '\t' | '\n' | '\r' | '\x0e' ... '\x1a' | '\x1c' ... '\x1f' => true, _ => false, @@ -264,7 +271,6 @@ fn need_quotes(string: &str) -> bool { || string == "false" || string == "null" || string == "~" - || string == "" || string.parse::().is_ok() || string.parse::().is_ok() } @@ -351,6 +357,9 @@ boolean: "true" boolean2: "false" date: "2014-12-31" empty_string: "" +empty_string1: " " +empty_string2: " a" +empty_string3: " a " exp: "12e7" field: ":" field2: "{"