summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniil Baturin <daniil@baturin.org>2018-02-18 21:54:46 +0700
committerDaniil Baturin <daniil@baturin.org>2018-02-18 22:04:53 +0700
commitdc4bedee63c2c4d8c1dbd3c6aa9a12f4e072681b (patch)
treedac95af90824c6313b3e38f7089b7dc3f2f6c8b0
parentbfa93363f3e8533ef352cad38c2adfa21dbc28a3 (diff)
downloadvyconf-dc4bedee63c2c4d8c1dbd3c6aa9a12f4e072681b.tar.gz
vyconf-dc4bedee63c2c4d8c1dbd3c6aa9a12f4e072681b.zip
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 (;[]{}#).
-rw-r--r--src/config_tree.ml14
-rw-r--r--test/config_tree_test.ml6
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 *)