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. *)