replace ref with mutable

This commit is contained in:
atagen 2024-12-01 23:57:10 +11:00
parent e463d43047
commit 9ae2e4e91e
2 changed files with 11 additions and 16 deletions

View File

@ -1,6 +1,6 @@
open Types
type serialiser = { buf : string ref; sz : int; pos : int ref }
type serialiser = { mutable buf : string; sz : int; mutable pos : int }
let write_ansi a =
let rec intersperse sep ls =
@ -28,9 +28,9 @@ let write_ansi a =
"\x1b[" ^ ansi ^ "m"
let flush t =
if t.pos.contents > 0 then (
print_string (String.sub t.buf.contents 0 t.pos.contents);
t.pos.contents <- 0)
if t.pos > 0 then (
print_string (String.sub t.buf 0 t.pos);
t.pos <- 0)
let serialise t chunk =
let input =
@ -41,19 +41,14 @@ let serialise t chunk =
| Ansi a -> write_ansi a
in
let input_sz = String.length input in
if t.pos.contents + input_sz > t.sz then flush t;
if t.pos + input_sz > t.sz then flush t;
if input_sz > t.sz then print_string input
else
t.buf.contents <-
(if t.pos.contents > 0 then String.sub t.buf.contents 0 t.pos.contents
else "")
^ input;
t.pos.contents <- t.pos.contents + input_sz
else t.buf <- (if t.pos > 0 then String.sub t.buf 0 t.pos else "") ^ input;
t.pos <- t.pos + input_sz
let create = { buf = ref String.empty; sz = 4096; pos = ref 0 }
let create = { buf = String.empty; sz = 4096; pos = 0 }
let print_debug t =
print_endline
("pos is "
^ string_of_int t.pos.contents
^ "\nsz is " ^ string_of_int t.sz ^ "\ncontents are: " ^ t.buf.contents)
("pos is " ^ string_of_int t.pos ^ "\nsz is " ^ string_of_int t.sz
^ "\ncontents are: " ^ t.buf)

View File

@ -1,4 +1,4 @@
type serialiser = { buf : string ref; sz : int; pos : int ref; }
type serialiser = { mutable buf : string; sz : int; mutable pos : int; }
val flush : serialiser -> unit
val serialise : serialiser -> Types.chunk -> unit
val create : serialiser