From c02c76eb0b5dd0f8bed0bff06aa4030fcee4edee Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Thu, 9 Apr 2026 00:56:37 +0000 Subject: [PATCH] add Byte_buffer.copy, fix bugs --- src/core/CCByte_buffer.ml | 10 +++++++--- src/core/CCByte_buffer.mli | 3 +++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/core/CCByte_buffer.ml b/src/core/CCByte_buffer.ml index 14795f83..943128a2 100644 --- a/src/core/CCByte_buffer.ml +++ b/src/core/CCByte_buffer.ml @@ -96,7 +96,7 @@ let fold_left f acc self = (* capture current content *) 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) done; !acc @@ -104,16 +104,20 @@ let fold_left f acc self = let[@inline] iter f self = (* capture current content *) let { bs; len } = self in - for i = 0 to len do + for i = 0 to len - 1 do f (Bytes.unsafe_get bs i) done let[@inline] iteri f self = 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) done +let copy self = + let bs = Bytes.copy self.bs in + { bs; len = self.len } + let of_seq seq = let self = create ~cap:32 () in append_seq self seq; diff --git a/src/core/CCByte_buffer.mli b/src/core/CCByte_buffer.mli index a67f277c..c54c1abd 100644 --- a/src/core/CCByte_buffer.mli +++ b/src/core/CCByte_buffer.mli @@ -91,6 +91,9 @@ val to_slice : t -> CCByte_slice.t The slice shares the same byte array as [buf] (until [buf] is resized). @since 3.13.1 *) +val copy : t -> t +(** [copy buf] returns an independent copy of [buf]. *) + val contents : t -> string (** Copy the internal data to a string. Allocates. *)