diff options
Diffstat (limited to 'src/util.ml')
-rw-r--r-- | src/util.ml | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/src/util.ml b/src/util.ml index 168589d..cbee955 100644 --- a/src/util.ml +++ b/src/util.ml @@ -85,3 +85,28 @@ let lexical_numeric_compare s t = (** Convert a relative path to an absolute path based on the current working directory *) let absolute_path relative_path = FilePath.make_absolute (Sys.getcwd ()) relative_path + +(** Convert a list of strings to a string of unquoted, space separated words *) +let string_of_list ss = + let rec aux xs acc = + match xs with + | [] -> acc + | x :: xs' -> aux xs' (Printf.sprintf "%s %s" acc x) + in + match ss with + | [] -> "" + | x :: xs -> Printf.sprintf "%s%s" x (aux xs "") + +(** Convert a list of strings to JSON *) +let json_of_list ss = + let ss = List.map (fun x -> `String x) ss in + Yojson.Safe.to_string (`List ss) + +(** Split string on whitespace, excluding single-quoted phrases, + as needed for parsing vyconf request path option **) +let list_of_path p = + let seg = String.trim p |> String.split_on_char '\'' in + match seg with + | [h] -> Pcre.split ~pat:"\\s+" h + | h :: h' :: _ -> (Pcre.split ~pat:"\\s+" h) @ [h'] + | _ -> [] |