Merge pull request #337 from Chris00/meta

Escape double quotes for META field values
This commit is contained in:
François Bobot 2017-11-24 16:24:09 +01:00 committed by GitHub
commit 511801c0d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 7 deletions

View File

@ -306,6 +306,30 @@ module String = struct
loop i (j + 1) ~last_is_cr:false
in
loop 0 0 ~last_is_cr:false
(* Escape ONLY double quotes. String.escape also escapes
'\n',... and transforms all chars above '~' into '\xxx' which is
not suitable for UTF-8 strings. *)
let escape_double_quote s =
let n = ref 0 in
let len = String.length s in
for i = 0 to len - 1 do
if String.unsafe_get s i = '"' then incr n;
done;
if !n = 0 then s
else (
let b = Bytes.create (len + !n) in
n := 0;
for i = 0 to len - 1 do
if String.unsafe_get s i = '"' then (
Bytes.unsafe_set b !n '\\';
incr n;
);
Bytes.unsafe_set b !n (String.unsafe_get s i);
incr n
done;
Bytes.unsafe_to_string b
)
end
module Sys = struct

View File

@ -236,13 +236,23 @@ let pp_list f ppf l =
Format.pp_print_cut ppf ();
f ppf x)
let pp_value var =
let pp_print_text ppf s =
Format.fprintf ppf "\"@[<hv>";
Format.pp_print_text ppf (String.escape_double_quote s);
Format.fprintf ppf "@]\""
let pp_print_string ppf s =
Format.fprintf ppf "\"@[<hv>";
Format.pp_print_string ppf (String.escape_double_quote s);
Format.fprintf ppf "@]\""
let pp_quoted_value var =
match var with
| "archive" | "plugin" | "requires"
| "ppx_runtime_deps" | "linkopts" | "jsoo_runtime" ->
Format.pp_print_text
pp_print_text
| _ ->
Format.pp_print_string
pp_print_string
let rec pp ppf entries =
Format.fprintf ppf "@[<v>%a@]" (pp_list pp_entry) entries
@ -253,12 +263,12 @@ and pp_entry ppf entry =
| Comment s ->
fprintf ppf "# %s" s
| Rule { var; predicates = []; action; value } ->
fprintf ppf "@[%s %s \"@[<hv>%a@]\"@]"
var (string_of_action action) (pp_value var) value
fprintf ppf "@[%s %s %a@]"
var (string_of_action action) (pp_quoted_value var) value
| Rule { var; predicates; action; value } ->
fprintf ppf "@[%s(%s) %s \"@[<hv>%a@]\"@]"
fprintf ppf "@[%s(%s) %s %a@]"
var (String.concat ~sep:"," (List.map predicates ~f:string_of_predicate))
(string_of_action action) (pp_value var) value
(string_of_action action) (pp_quoted_value var) value
| Package { name; entries } ->
fprintf ppf "@[<v 2>package %S (@,%a@]@,)"
name pp entries