easier to use interface for Bij.Sexpr

This commit is contained in:
Simon Cruanes 2013-05-15 11:23:32 +02:00
parent a570a34951
commit 1e72332ac6
2 changed files with 19 additions and 5 deletions

12
bij.ml
View file

@ -395,11 +395,19 @@ module SexpStr = struct
module SexpEncodeBuf = SexpEncode(SinkBuf)
module SexpDecodeString = SexpDecode(SourceStr)
let to_string ~bij x =
let b = Buffer.create 15 in
let to_string ?(bufsize=64) ~bij x =
let b = Buffer.create bufsize in
SexpEncodeBuf.encode ~bij b x;
Buffer.contents b
let of_string ~bij s =
SexpDecodeString.decode ~bij (SourceStr.create s)
end
module SexpChan = struct
module SexpEncodeChan = SexpEncode(SinkChan)
module SexpDecodeChan = SexpDecode(SourceChan)
include SexpEncodeChan
include SexpDecodeChan
end

12
bij.mli
View file

@ -94,9 +94,9 @@ module type SINK = sig
val write_float : t -> float -> unit
end
module SinkBuf : SINK with type t := Buffer.t
module SinkBuf : SINK with type t = Buffer.t
module SinkChan : SINK with type t := out_channel
module SinkChan : SINK with type t = out_channel
(** {2 Encoding/decoding} *)
@ -115,6 +115,12 @@ module SexpDecode(Source : SOURCE) : DECODE with type source = Source.t
(** Specific instance for encoding to/from strings *)
module SexpStr : sig
val to_string : bij:'a t -> 'a -> string
val to_string : ?bufsize:int -> bij:'a t -> 'a -> string
val of_string : bij:'a t -> string -> 'a
end
(** Specific instance for encoding to/from channels *)
module SexpChan : sig
include ENCODE with type sink = SinkChan.t
include DECODE with type source = SourceChan.t
end