Properly convert quoted atoms to strings
This commit is contained in:
parent
c27cb3541e
commit
8b50352db7
|
@ -1,5 +1,15 @@
|
||||||
|
module UnlabeledBytes = Bytes
|
||||||
open StdLabels
|
open StdLabels
|
||||||
|
|
||||||
|
module Bytes = struct
|
||||||
|
include StdLabels.Bytes
|
||||||
|
|
||||||
|
(* [blit_string] was forgotten from the labeled version in OCaml
|
||||||
|
4.02—4.04. *)
|
||||||
|
let blit_string ~src ~src_pos ~dst ~dst_pos ~len =
|
||||||
|
UnlabeledBytes.blit_string src src_pos dst dst_pos len
|
||||||
|
end
|
||||||
|
|
||||||
module A = Parser_automaton_internal
|
module A = Parser_automaton_internal
|
||||||
|
|
||||||
module Atom = struct
|
module Atom = struct
|
||||||
|
@ -20,9 +30,18 @@ module Atom = struct
|
||||||
let len = String.length s in
|
let len = String.length s in
|
||||||
len = 0 || escaped_length s > len
|
len = 0 || escaped_length s > len
|
||||||
|
|
||||||
let escaped_internal s ~with_double_quotes =
|
let escaped_internal s ~with_double_quotes ~always_quote =
|
||||||
let n = escaped_length s in
|
let n = escaped_length s in
|
||||||
if n > 0 && n = String.length s then s else begin
|
if n > 0 && n = String.length s then
|
||||||
|
if always_quote then begin
|
||||||
|
let s' = Bytes.create (n + 2) in
|
||||||
|
Bytes.unsafe_set s' 0 '"';
|
||||||
|
Bytes.blit_string ~src:s ~src_pos:0 ~dst:s' ~dst_pos:1 ~len:n;
|
||||||
|
Bytes.unsafe_set s' (n + 1) '"';
|
||||||
|
Bytes.unsafe_to_string s'
|
||||||
|
end
|
||||||
|
else s
|
||||||
|
else begin
|
||||||
let s' = Bytes.create (n + if with_double_quotes then 2 else 0) in
|
let s' = Bytes.create (n + if with_double_quotes then 2 else 0) in
|
||||||
let n = ref 0 in
|
let n = ref 0 in
|
||||||
if with_double_quotes then begin
|
if with_double_quotes then begin
|
||||||
|
@ -58,8 +77,12 @@ module Atom = struct
|
||||||
Bytes.unsafe_to_string s'
|
Bytes.unsafe_to_string s'
|
||||||
end
|
end
|
||||||
|
|
||||||
let escaped s = escaped_internal s ~with_double_quotes:false
|
let escaped s =
|
||||||
let serialize s = escaped_internal s ~with_double_quotes:true
|
escaped_internal s ~with_double_quotes:false ~always_quote:false
|
||||||
|
let serialize s =
|
||||||
|
escaped_internal s ~with_double_quotes:true ~always_quote:false
|
||||||
|
let quote s =
|
||||||
|
escaped_internal s ~with_double_quotes:true ~always_quote:true
|
||||||
end
|
end
|
||||||
|
|
||||||
type t =
|
type t =
|
||||||
|
@ -71,7 +94,7 @@ type sexp = t
|
||||||
|
|
||||||
let rec to_string = function
|
let rec to_string = function
|
||||||
| Atom s -> Atom.serialize s
|
| Atom s -> Atom.serialize s
|
||||||
| String s -> Atom.serialize s
|
| String s -> Atom.quote s
|
||||||
| List l -> Printf.sprintf "(%s)" (List.map l ~f:to_string |> String.concat ~sep:" ")
|
| List l -> Printf.sprintf "(%s)" (List.map l ~f:to_string |> String.concat ~sep:" ")
|
||||||
|
|
||||||
let rec pp ppf = function
|
let rec pp ppf = function
|
||||||
|
|
Loading…
Reference in New Issue