From 980f5037cc47335a760bb1764f6dcb2006229439 Mon Sep 17 00:00:00 2001 From: Rudi Grinberg Date: Mon, 23 Apr 2018 23:51:22 +0700 Subject: [PATCH] Move maybe_quoted to string module --- src/build_system.ml | 2 +- src/import.ml | 9 --------- src/path.ml | 2 +- src/stdune/string.ml | 8 ++++++++ src/stdune/string.mli | 4 ++++ src/utils.ml | 4 ++-- 6 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/build_system.ml b/src/build_system.ml index 0f5ec198..6f9e39c5 100644 --- a/src/build_system.ml +++ b/src/build_system.ml @@ -519,7 +519,7 @@ let add_spec t fn spec ~copy_source = As a result, the rule is currently ignored, however this will become an error \ in the future.\n\ %t" - (maybe_quoted (Path.basename fn)) + (String.maybe_quoted (Path.basename fn)) (fun ppf -> match rule.mode with | Not_a_rule_stanza -> diff --git a/src/import.ml b/src/import.ml index b6b486a2..0fe5989d 100644 --- a/src/import.ml +++ b/src/import.ml @@ -86,15 +86,6 @@ let hint name candidates = sprintf "\nHint: did you mean %s?" (mk_hint l) -(* [maybe_quoted s] is [s] if [s] doesn't need escaping according to OCaml lexing - conventions and [sprintf "%S" s] otherwise. *) -let maybe_quoted s = - let escaped = String.escaped s in - if s == escaped || s = escaped then - s - else - sprintf {|"%s"|} escaped - (* Disable file operations to force to use the IO module *) let open_in = `Use_Io let open_in_bin = `Use_Io diff --git a/src/path.ml b/src/path.ml index e3350a4e..1a4cf3e8 100644 --- a/src/path.ml +++ b/src/path.ml @@ -247,7 +247,7 @@ let to_string = function | t -> t let to_string_maybe_quoted t = - maybe_quoted (to_string t) + String.maybe_quoted (to_string t) let root = "" diff --git a/src/stdune/string.ml b/src/stdune/string.ml index 925def86..84df85d3 100644 --- a/src/stdune/string.ml +++ b/src/stdune/string.ml @@ -175,5 +175,13 @@ let exists s ~f = with Exit -> true +let maybe_quoted s = + let escaped = escaped s in + if s == escaped || s = escaped then + s + else + Printf.sprintf {|"%s"|} escaped + + module Set = Set.Make(T) module Map = Map.Make(T) diff --git a/src/stdune/string.mli b/src/stdune/string.mli index 15f49958..65a267a1 100644 --- a/src/stdune/string.mli +++ b/src/stdune/string.mli @@ -40,5 +40,9 @@ val longest_map : 'a list -> f:('a -> string) -> int val exists : t -> f:(char -> bool) -> bool +(** [maybe_quoted s] is [s] if [s] doesn't need escaping according to OCaml + lexing conventions and [sprintf "%S" s] otherwise. *) +val maybe_quoted : t -> t + module Set : Set.S with type elt = t module Map : Map.S with type key = t diff --git a/src/utils.ml b/src/utils.ml index 801635fe..8189cb65 100644 --- a/src/utils.ml +++ b/src/utils.ml @@ -117,7 +117,7 @@ let executable_object_directory ~dir name = let program_not_found ?context ?hint prog = die "@{Error@}: Program %s not found in the tree or in PATH%s%a" - (maybe_quoted prog) + (String.maybe_quoted prog) (match context with | None -> "" | Some name -> sprintf " (context: %s)" name) @@ -127,7 +127,7 @@ let program_not_found ?context ?hint prog = hint let library_not_found ?context ?hint lib = - die "@{Error@}: Library %s not found%s%a" (maybe_quoted lib) + die "@{Error@}: Library %s not found%s%a" (String.maybe_quoted lib) (match context with | None -> "" | Some name -> sprintf " (context: %s)" name)