From dc4bedee63c2c4d8c1dbd3c6aa9a12f4e072681b Mon Sep 17 00:00:00 2001 From: Daniil Baturin Date: Sun, 18 Feb 2018 21:54:46 +0700 Subject: Do not quote values that don't need quoting in Config_tree.render i.e. if they don't contain whitespace or characters of special significance to the parser (;[]{}#). --- src/config_tree.ml | 14 +++++++++++--- test/config_tree_test.ml | 6 +++--- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/config_tree.ml b/src/config_tree.ml index c816d65..31b6f0f 100644 --- a/src/config_tree.ml +++ b/src/config_tree.ml @@ -144,9 +144,17 @@ struct name] |> S.concat "" - let render_values = function - | [v] -> PF.sprintf "\"%s\";" v - | vs -> S.concat "; " vs |> PF.sprintf "[%s];" + let render_values values = + let quote_if_needed s = + try + let _ = Pcre.exec ~pat:"[\\s;{}#\\[\\]\"\']" s in + Printf.sprintf "\"%s\"" s + with Not_found -> s + in + match values with + | [] -> raise (Failure "Internal error: get_values got an empty list") + | [v] -> PF.sprintf "%s;" (quote_if_needed v) + | _ as vs -> S.concat "; " (List.map quote_if_needed vs) |> PF.sprintf "[%s];" let render_inner_and_outer indents inner outer = if inner = "" diff --git a/test/config_tree_test.ml b/test/config_tree_test.ml index be8b320..442c4c8 100644 --- a/test/config_tree_test.ml +++ b/test/config_tree_test.ml @@ -160,7 +160,7 @@ let test_render_at_level test_ctxt = let rendered = CT.render_at_level node ["foo"] in assert_equal (String.trim rendered) "bar { - baz \"quux\"; + baz quux; }" let test_render_at_level_top test_ctxt = @@ -172,10 +172,10 @@ let test_render_at_level_top test_ctxt = let rendered = CT.render_at_level node [] in assert_equal (String.trim rendered) "baz { - quux \"xyzzy\"; + quux xyzzy; } foo { - bar \"quuux\"; + bar quuux; }" (**** Reftree-based rendering *) -- cgit v1.2.3