From 8db7ba4de337584a002f1802b52f31084c149c25 Mon Sep 17 00:00:00 2001 From: atagen Date: Tue, 3 Dec 2024 01:31:18 +1100 Subject: [PATCH] use bytes for emitter --- lib/emitter.ml | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/lib/emitter.ml b/lib/emitter.ml index 13b46dd..668f054 100644 --- a/lib/emitter.ml +++ b/lib/emitter.ml @@ -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)