use bytes for emitter
This commit is contained in:
parent
0ac5ab9e81
commit
8db7ba4de3
@ -1,7 +1,8 @@
|
||||
open Types
|
||||
|
||||
type serialiser = { mutable buf : string; sz : int; mutable pos : int }
|
||||
type serialiser = { mutable buf : bytes; mutable pos : int }
|
||||
|
||||
(* convert ansi variants to appropriate output codes *)
|
||||
let write_ansi a =
|
||||
let rec intersperse sep ls =
|
||||
match ls with
|
||||
@ -27,9 +28,10 @@ let write_ansi a =
|
||||
in
|
||||
"\x1b[" ^ ansi ^ "m"
|
||||
|
||||
(* print all serialised data and reset buffer *)
|
||||
let flush t =
|
||||
if t.pos > 0 then (
|
||||
print_string (String.sub t.buf 0 t.pos);
|
||||
print_bytes (Bytes.sub t.buf 0 t.pos);
|
||||
t.pos <- 0)
|
||||
|
||||
let serialise t chunk =
|
||||
@ -39,16 +41,18 @@ let serialise t chunk =
|
||||
| Separator s -> s
|
||||
| Delimiter s -> s
|
||||
| Ansi a -> write_ansi a
|
||||
in
|
||||
and sz = Bytes.length t.buf in
|
||||
let input_sz = String.length input in
|
||||
if t.pos + input_sz > t.sz then flush t;
|
||||
if input_sz > t.sz then print_string input
|
||||
else t.buf <- (if t.pos > 0 then String.sub t.buf 0 t.pos else "") ^ input;
|
||||
t.pos <- t.pos + input_sz
|
||||
if t.pos + input_sz > sz then flush t;
|
||||
if input_sz > sz then print_string input
|
||||
else (
|
||||
Bytes.blit_string input 0 t.buf t.pos input_sz;
|
||||
t.pos <- t.pos + input_sz)
|
||||
|
||||
let create = { buf = String.empty; sz = 4096; pos = 0 }
|
||||
let create = { buf = Bytes.create 4096; pos = 0 }
|
||||
|
||||
let print_debug t =
|
||||
print_endline
|
||||
("pos is " ^ string_of_int t.pos ^ "\nsz is " ^ string_of_int t.sz
|
||||
^ "\ncontents are: " ^ t.buf)
|
||||
("pos is " ^ string_of_int t.pos ^ "\nsz is "
|
||||
^ string_of_int (Bytes.length t.buf)
|
||||
^ "\ncontents are: " ^ String.of_bytes t.buf)
|
||||
|
Loading…
Reference in New Issue
Block a user