Skip to content

Commit c29083c

Browse files
committed
richer API for byte_buf
1 parent 14dc772 commit c29083c

File tree

3 files changed

+42
-18
lines changed

3 files changed

+42
-18
lines changed

src/core/CCByte_buffer.ml

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,19 @@ let[@inline never] grow_ self =
3535
let newcap = grow_cap_ self in
3636
grow_to_ self newcap
3737

38-
let ensure_cap self n =
39-
if n > capacity self then (
40-
let newcap = max n (grow_cap_ self) in
41-
grow_to_ self newcap
42-
)
38+
let[@inline never] ensure_cap_grow_ self n =
39+
(* new capacity, make sure it's at least [grow_cap_] so
40+
that repeated calls to [ensure_cap] have the amortized complexity *)
41+
let newcap = max n (grow_cap_ self) in
42+
grow_to_ self newcap
43+
44+
let[@inline] ensure_cap self n =
45+
if n > capacity self then ensure_cap_grow_ self n
46+
47+
let[@inline] ensure_free self n =
48+
if n > capacity self - self.len then ensure_cap_grow_ self (self.len + n)
4349

44-
let shrink_to self n = if self.len > n then self.len <- n
50+
let[@inline] shrink_to self n = if self.len > n then self.len <- n
4551

4652
let append_buf (self : t) buf : unit =
4753
let n = Buffer.length buf in
@@ -54,10 +60,10 @@ let append_subbytes self b off len =
5460
Bytes.blit b off self.bs self.len len;
5561
self.len <- self.len + len
5662

57-
let append_bytes self b = append_subbytes self b 0 (Bytes.length b)
58-
let append_string self s = append_bytes self (Bytes.unsafe_of_string s)
63+
let[@inline] append_bytes self b = append_subbytes self b 0 (Bytes.length b)
64+
let[@inline] append_string self s = append_bytes self (Bytes.unsafe_of_string s)
5965

60-
let append_substring self s off len =
66+
let[@inline] append_substring self s off len =
6167
append_subbytes self (Bytes.unsafe_of_string s) off len
6268

6369
let[@inline] add_char_unsafe_ self c =
@@ -94,13 +100,19 @@ let fold_left f acc self =
94100
done;
95101
!acc
96102

97-
let iter f self =
98-
let { bs; len } = self in
103+
let[@inline] iter f self =
99104
(* capture current content *)
105+
let { bs; len } = self in
100106
for i = 0 to len do
101107
f (Bytes.unsafe_get bs i)
102108
done
103109

110+
let[@inline] iteri f self =
111+
let { bs; len } = self in
112+
for i = 0 to len do
113+
f i (Bytes.unsafe_get bs i)
114+
done
115+
104116
let of_seq seq =
105117
let self = create ~cap:32 () in
106118
append_seq self seq;

src/core/CCByte_buffer.mli

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
(** Byte buffer.
22
3-
A dynamic vector of bytes.
3+
A dynamic vector of bytes that doesn't hide its internal from you.
4+
Same use case as [Buffer.t] but with more power.
45
@since 3.7
56
*)
67

@@ -33,16 +34,24 @@ val bytes : t -> bytes
3334
operations that affect the capacity (e.g. {!add_char}). *)
3435

3536
val clear : t -> unit
37+
(** [clear buf] sets [buf.len <- 0]. This doesn't resize the byte buffer. *)
3638

3739
val ensure_cap : t -> int -> unit
38-
(** [ensure_cap self n] ensures that [capacity self >= n]. *)
40+
(** [ensure_cap self n] ensures that [capacity self >= n].
41+
@raise Invalid_argument if this requires the buffer to grow beyond system limits. *)
42+
43+
val ensure_free : t -> int -> unit
44+
(** [ensure_free buf n] ensures that the free space at the end of the
45+
buffer is at least [n].
46+
@raise Invalid_argument if this requires the buffer to grow beyond system limits. *)
3947

4048
val shrink_to : t -> int -> unit
4149
(** [shrink_to buf n] reduces [length buf] to at most [n].
4250
Does nothing if the length is already <= n. *)
4351

4452
val add_char : t -> char -> unit
45-
(** Push a character at the end. *)
53+
(** Push a character at the end.
54+
@raise Invalid_argument if this requires the buffer to grow beyond system limits. *)
4655

4756
val append_bytes : t -> bytes -> unit
4857
val append_subbytes : t -> bytes -> int -> int -> unit
@@ -57,12 +66,13 @@ val set : t -> int -> char -> unit
5766
val unsafe_set : t -> int -> char -> unit
5867

5968
val contents : t -> string
60-
(** Copy the internal data to a string *)
69+
(** Copy the internal data to a string. Allocates. *)
6170

6271
val contents_bytes : t -> bytes
63-
(** Copy the internal data to a byte buffer *)
72+
(** Copy the internal data to a {!bytes}. Allocates. *)
6473

6574
val iter : (char -> unit) -> t -> unit
75+
val iteri : (int -> char -> unit) -> t -> unit
6676
val fold_left : ('a -> char -> 'a) -> 'a -> t -> 'a
6777
val of_iter : char iter -> t
6878
val of_seq : char Seq.t -> t

src/core/containers.ml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
module Array = CCArray
66
module Bool = CCBool
7-
module Byte_buffer = CCByte_buffer
8-
module Byte_slice = CCByte_slice
97
module Char = CCChar
108
module Equal = CCEqual
119
module Either = CCEither
@@ -30,6 +28,10 @@ module Hashtbl = struct
3028
module Make' = CCHashtbl.Make
3129
end
3230

31+
(** {2 Additional modules} *)
32+
33+
module Byte_buffer = CCByte_buffer
34+
module Byte_slice = CCByte_slice
3335
module Heap = CCHeap
3436
module Int = CCInt
3537
module Int32 = CCInt32

0 commit comments

Comments
 (0)