···1122+- Safe-string suport.
23- Uchar.t support. At the API level only `Jsonm.error` changes.
34- Build depend on topkg.
45- Relicensed from BSD3 to ISC.
···99let io_buffer_size = 65536 (* IO_BUFFER_SIZE 4.0.0 *)
1010let pp = Format.fprintf
11111212-(* Unsafe string byte manipulations. If you don't believe the authors's
1212+(* Unsafe string and bytes manipulations. If you don't believe the authors's
1313 invariants, replacing with safe versions makes everything safe in the
1414 module. He won't be upset. *)
15151616-let unsafe_blit = String.unsafe_blit
1717-let unsafe_set_byte s j byte = String.unsafe_set s j (Char.unsafe_chr byte)
1816let unsafe_byte s j = Char.code (String.unsafe_get s j)
1717+1818+let unsafe_blit s soff d doff =
1919+ Bytes.unsafe_blit (Bytes.unsafe_of_string s) soff d doff
2020+2121+let unsafe_set_byte s j byte = Bytes.unsafe_set s j (Char.unsafe_chr byte)
19222023(* Characters and their classes *)
2124···426429type encoder =
427430 { dst : dst; (* output destination. *)
428431 minify : bool; (* [true] for compact output. *)
429429- mutable o : string; (* current output chunk. *)
432432+ mutable o : Bytes.t; (* current output chunk. *)
430433 mutable o_pos : int; (* next output position to write. *)
431434 mutable o_max : int; (* maximal output position to write. *)
432435 buf : Buffer.t; (* buffer to format floats. *)
···439442440443let o_rem e = e.o_max - e.o_pos + 1 (* remaining bytes to write in [e.o]. *)
441444let dst e s j l = (* set [e.o] with [s]. *)
442442- if (j < 0 || l < 0 || j + l > String.length s) then invalid_bounds j l;
445445+ if (j < 0 || l < 0 || j + l > Bytes.length s) then invalid_bounds j l;
443446 e.o <- s; e.o_pos <- j; e.o_max <- j + l - 1
444447445448let partial k e = function `Await -> k e | v -> expect_await v
446449let flush k e = match e.dst with (* get free space in [d.o] and [k]ontinue. *)
447450| `Manual -> e.k <- partial k; `Partial
448448-| `Buffer b -> Buffer.add_substring b e.o 0 e.o_pos; e.o_pos <- 0; k e
449451| `Channel oc -> output oc e.o 0 e.o_pos; e.o_pos <- 0; k e
452452+| `Buffer b ->
453453+ let o = Bytes.unsafe_to_string e.o in
454454+ Buffer.add_substring b o 0 e.o_pos; e.o_pos <- 0; k e
455455+450456451457let rec writeb b k e = (* write byte [b] and [k]ontinue. *)
452458 if e.o_pos > e.o_max then flush (writeb b k) e else
···594600595601let encoder ?(minify = true) dst =
596602 let o, o_pos, o_max = match dst with
597597- | `Manual -> "", 1, 0 (* implies [o_rem e = 0]. *)
603603+ | `Manual -> Bytes.empty, 1, 0 (* implies [o_rem e = 0]. *)
598604 | `Buffer _
599599- | `Channel _ -> String.create io_buffer_size, 0, io_buffer_size - 1
605605+ | `Channel _ -> Bytes.create io_buffer_size, 0, io_buffer_size - 1
600606 in
601607 { dst = (dst :> dst); minify; o; o_pos; o_max; buf = Buffer.create 30;
602608 stack = []; nest = 0; next_name = false; last_start = false;
+7-7
vendor/opam/jsonm/src/jsonm.mli
···193193 {b Warning.} Use only with [`Manual] decoders and encoders. *)
194194module Manual : sig
195195196196- val src : decoder -> string -> int -> int -> unit
196196+ val src : decoder -> Bytes.t -> int -> int -> unit
197197 (** [src d s j l] provides [d] with [l] bytes to read, starting
198198 at [j] in [s]. This byte range is read by calls to {!decode} until
199199 [`Await] is returned. To signal the end of input call the function
200200 with [l = 0]. *)
201201202202- val dst : encoder -> string -> int -> int -> unit
202202+ val dst : encoder -> Bytes.t -> int -> int -> unit
203203 (** [dst e s j l] provides [e] with [l] bytes to write, starting
204204 at [j] in [s]. This byte range is written by calls to {!encode} with [e]
205205 until [`Partial] is returned. Use {!dst_rem} to know the remaining
···389389 let wc = write fd s j l in
390390 if wc < l then unix_write fd s (j + wc) (l - wc) else ()
391391 in
392392- unix_write fd s 0 (String.length s - Jsonm.Manual.dst_rem e);
392392+ unix_write fd s 0 (Bytes.length s - Jsonm.Manual.dst_rem e);
393393 Jsonm.Manual.dst e s 0 (String.length s);
394394 encode fd s e `Await
395395 in
···401401 let rec unix_read fd s j l = try Unix.read fd s j l with
402402 | Unix.Unix_error (Unix.EINTR, _, _) -> unix_read fd s j l
403403 in
404404- let rc = unix_read fdi ds 0 (String.length ds) in
404404+ let rc = unix_read fdi ds 0 (Bytes.length ds) in
405405 Jsonm.Manual.src d ds 0 rc; loop fdi fdo ds es d e
406406 in
407407- let ds = String.create 65536 (* UNIX_BUFFER_SIZE in 4.0.0 *) in
408408- let es = String.create 65536 (* UNIX_BUFFER_SIZE in 4.0.0 *) in
407407+ let ds = Bytes.create 65536 (* UNIX_BUFFER_SIZE in 4.0.0 *) in
408408+ let es = Bytes.create 65536 (* UNIX_BUFFER_SIZE in 4.0.0 *) in
409409 let d = Jsonm.decoder ?encoding `Manual in
410410 let e = Jsonm.encoder ?minify `Manual in
411411- Jsonm.Manual.dst e es 0 (String.length es);
411411+ Jsonm.Manual.dst e es 0 (Bytes.length es);
412412 loop fdi fdo ds es d e
413413]}
414414 {2:memsel Member selection}
+6-6
vendor/opam/jsonm/test/examples.ml
···3030 let wc = write fd s j l in
3131 if wc < l then unix_write fd s (j + wc) (l - wc) else ()
3232 in
3333- unix_write fd s 0 (String.length s - Jsonm.Manual.dst_rem e);
3434- Jsonm.Manual.dst e s 0 (String.length s);
3333+ unix_write fd s 0 (Bytes.length s - Jsonm.Manual.dst_rem e);
3434+ Jsonm.Manual.dst e s 0 (Bytes.length s);
3535 encode fd s e `Await
3636 in
3737 let rec loop fdi fdo ds es d e = match Jsonm.decode d with
···4242 let rec unix_read fd s j l = try Unix.read fd s j l with
4343 | Unix.Unix_error (Unix.EINTR, _, _) -> unix_read fd s j l
4444 in
4545- let rc = unix_read fdi ds 0 (String.length ds) in
4545+ let rc = unix_read fdi ds 0 (Bytes.length ds) in
4646 Jsonm.Manual.src d ds 0 rc; loop fdi fdo ds es d e
4747 in
4848- let ds = String.create 65536 (* UNIX_BUFFER_SIZE in 4.0.0 *) in
4949- let es = String.create 65536 (* UNIX_BUFFER_SIZE in 4.0.0 *) in
4848+ let ds = Bytes.create 65536 (* UNIX_BUFFER_SIZE in 4.0.0 *) in
4949+ let es = Bytes.create 65536 (* UNIX_BUFFER_SIZE in 4.0.0 *) in
5050 let d = Jsonm.decoder ?encoding `Manual in
5151 let e = Jsonm.encoder ?minify `Manual in
5252- Jsonm.Manual.dst e es 0 (String.length es);
5252+ Jsonm.Manual.dst e es 0 (Bytes.length es);
5353 loop fdi fdo ds es d e
54545555(* Member selection *)
+22-20
vendor/opam/jsonm/test/jsontrip.ml
···3333 let b = Buffer.create unix_buffer_size in
3434 let input, s =
3535 if use_unix
3636- then unix_read (Unix.descr_of_in_channel ic), String.create unix_buffer_size
3737- else input ic, String.create io_buffer_size
3636+ then unix_read (Unix.descr_of_in_channel ic), Bytes.create unix_buffer_size
3737+ else input ic, Bytes.create io_buffer_size
3838 in
3939 let rec loop b input s =
4040- let rc = input s 0 (String.length s) in
4040+ let rc = input s 0 (Bytes.length s) in
4141 if rc = 0 then Buffer.contents b else
4242- (Buffer.add_substring b s 0 rc; loop b input s)
4242+ let us = Bytes.unsafe_to_string s in
4343+ (Buffer.add_substring b us 0 rc; loop b input s)
4344 in
4445 loop b input s
45464646-let string_to_channel use_unix oc s =
4747- if use_unix
4848- then unix_write (Unix.descr_of_out_channel oc) s 0 (String.length s)
4949- else output_string oc s
4747+let string_to_channel use_unix oc s = match use_unix with
4848+| false -> output_string oc s
4949+| true ->
5050+ let s = Bytes.unsafe_of_string s in
5151+ unix_write (Unix.descr_of_out_channel oc) s 0 (Bytes.length s)
50525153let dst_for sout = if sout then `Buffer (Buffer.create 512) else `Channel stdout
5254let src_for inf sin use_unix =
···68706971let rec encode_unix encode fd s e v = match encode e v with `Ok -> ()
7072| `Partial ->
7171- unix_write fd s 0 (String.length s - Jsonm.Manual.dst_rem e);
7272- Jsonm.Manual.dst e s 0 (String.length s);
7373+ unix_write fd s 0 (Bytes.length s - Jsonm.Manual.dst_rem e);
7474+ Jsonm.Manual.dst e s 0 (Bytes.length s);
7375 encode_unix encode fd s e `Await
74767577(* Dump *)
···8688 let decode = if uncut then Jsonm.Uncut.decode else Jsonm.decode in
8789 let rec loop decode fd s d = match decode d with
8890 | `Await ->
8989- let rc = unix_read fd s 0 (String.length s) in
9191+ let rc = unix_read fd s 0 (Bytes.length s) in
9092 Jsonm.Manual.src d s 0 rc; loop decode fd s d
9193 | v ->
9294 pr_decode Format.std_formatter inf d v;
9395 if v <> `End then loop decode fd s d
9496 in
9595- loop decode fd (String.create usize) (Jsonm.decoder ?encoding `Manual);
9797+ loop decode fd (Bytes.create usize) (Jsonm.decoder ?encoding `Manual);
9698 close_src_unix fd
979998100let dump inf sin use_unix usize ie uncut =
···127129 | `Comment _ | `White _ -> loop decode fd s d
128130 | `Error e -> log_error inf d e; loop decode fd s d
129131 | `Await ->
130130- let rc = unix_read fd s 0 (String.length s) in
132132+ let rc = unix_read fd s 0 (Bytes.length s) in
131133 Jsonm.Manual.src d s 0 rc; loop decode fd s d
132134 in
133133- loop decode fd (String.create usize) (Jsonm.decoder ?encoding `Manual)
135135+ loop decode fd (Bytes.create usize) (Jsonm.decoder ?encoding `Manual)
134136135137let decode inf sin use_unix usize ie uncut =
136138 if sin || not use_unix then decode_ inf ie uncut (src_for inf use_unix sin)
···235237 fun v -> r_uncut enc buf; enc v; r_uncut enc buf
236238237239let encode_f_unix usize buf uncut minify fd =
238238- let e, s = Jsonm.encoder ~minify `Manual, String.create usize in
239239- Jsonm.Manual.dst e s 0 (String.length s);
240240+ let e, s = Jsonm.encoder ~minify `Manual, Bytes.create usize in
241241+ Jsonm.Manual.dst e s 0 (Bytes.length s);
240242 if not uncut then (fun v -> encode_unix Jsonm.encode fd s e v) else
241243 let enc v = encode_unix Jsonm.Uncut.encode fd s e v in
242244 fun v -> r_uncut enc buf; enc v; r_uncut enc buf
···280282 loop decode fdi fdo ds es d e
281283 | `Error err -> log_error inf d err
282284 | `Await ->
283283- let rc = unix_read fdi ds 0 (String.length ds) in
285285+ let rc = unix_read fdi ds 0 (Bytes.length ds) in
284286 Jsonm.Manual.src d ds 0 rc; loop decode fdi fdo ds es d e
285287 in
286286- let d, ds = Jsonm.decoder ?encoding `Manual, String.create usize in
287287- let e, es = Jsonm.encoder ~minify `Manual, String.create usize in
288288- Jsonm.Manual.dst e es 0 (String.length es);
288288+ let d, ds = Jsonm.decoder ?encoding `Manual, Bytes.create usize in
289289+ let e, es = Jsonm.encoder ~minify `Manual, Bytes.create usize in
290290+ Jsonm.Manual.dst e es 0 (Bytes.length es);
289291 loop decode fdi fdo ds es d e; close_src_unix fdi
290292291293let trip inf sin sout use_unix usize ie uncut minify =