add Byte_buffer.copy, fix bugs

This commit is contained in:
Simon Cruanes 2026-04-09 00:56:37 +00:00
parent 0ad561a8e7
commit c02c76eb0b
2 changed files with 10 additions and 3 deletions

View file

@ -96,7 +96,7 @@ let fold_left f acc self =
(* capture current content *) (* capture current content *)
let acc = ref acc in let acc = ref acc in
for i = 0 to len do for i = 0 to len - 1 do
acc := f !acc (Bytes.unsafe_get bs i) acc := f !acc (Bytes.unsafe_get bs i)
done; done;
!acc !acc
@ -104,16 +104,20 @@ let fold_left f acc self =
let[@inline] iter f self = let[@inline] iter f self =
(* capture current content *) (* capture current content *)
let { bs; len } = self in let { bs; len } = self in
for i = 0 to len do for i = 0 to len - 1 do
f (Bytes.unsafe_get bs i) f (Bytes.unsafe_get bs i)
done done
let[@inline] iteri f self = let[@inline] iteri f self =
let { bs; len } = self in let { bs; len } = self in
for i = 0 to len do for i = 0 to len - 1 do
f i (Bytes.unsafe_get bs i) f i (Bytes.unsafe_get bs i)
done done
let copy self =
let bs = Bytes.copy self.bs in
{ bs; len = self.len }
let of_seq seq = let of_seq seq =
let self = create ~cap:32 () in let self = create ~cap:32 () in
append_seq self seq; append_seq self seq;

View file

@ -91,6 +91,9 @@ val to_slice : t -> CCByte_slice.t
The slice shares the same byte array as [buf] (until [buf] is resized). The slice shares the same byte array as [buf] (until [buf] is resized).
@since 3.13.1 *) @since 3.13.1 *)
val copy : t -> t
(** [copy buf] returns an independent copy of [buf]. *)
val contents : t -> string val contents : t -> string
(** Copy the internal data to a string. Allocates. *) (** Copy the internal data to a string. Allocates. *)