Skip to content

Commit 8b60f52

Browse files
committed
add byte_slice module, fix warnings
1 parent 4ff6040 commit 8b60f52

File tree

7 files changed

+95
-23
lines changed

7 files changed

+95
-23
lines changed

fuzz/dune

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(executables
22
(flags
3-
(-w "+a-4-9-29-37-40-42-44-48-50-32" -g))
3+
(-w "+a-4-9-29-37-40-42-44-48-50-32-70" -g))
44
(names
55
ccsexp_parse_string_does_not_crash
66
ccutf8_string_uchar_to_bytes_is_same_as_simple_version

src/core/CCByte_buffer.ml

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
type 'a iter = ('a -> unit) -> unit
22

33
type t = {
4-
mutable bytes: bytes;
4+
mutable bs: bytes;
55
mutable len: int;
66
}
77

88
let create ?(cap = 0) () : t =
9-
let bytes =
9+
let bs =
1010
if cap = 0 then
1111
Bytes.unsafe_of_string ""
1212
else
1313
Bytes.create cap
1414
in
15-
{ len = 0; bytes }
15+
{ len = 0; bs }
1616

17-
let[@inline] capacity self : int = Bytes.length self.bytes
18-
let[@inline] bytes self = self.bytes
17+
let[@inline] capacity self : int = Bytes.length self.bs
18+
let[@inline] bytes self = self.bs
1919
let[@inline] length self = self.len
2020
let[@inline] is_empty self = self.len = 0
2121
let[@inline] clear self = self.len <- 0
@@ -28,8 +28,8 @@ let grow_cap_ self =
2828
let grow_to_ self newcap =
2929
if newcap = capacity self then invalid_arg "byte_buf: cannot grow further";
3030
let newbytes = Bytes.create newcap in
31-
Bytes.blit self.bytes 0 newbytes 0 self.len;
32-
self.bytes <- newbytes
31+
Bytes.blit self.bs 0 newbytes 0 self.len;
32+
self.bs <- newbytes
3333

3434
let[@inline never] grow_ self =
3535
let newcap = grow_cap_ self in
@@ -46,12 +46,12 @@ let shrink_to self n = if self.len > n then self.len <- n
4646
let append_buf (self : t) buf : unit =
4747
let n = Buffer.length buf in
4848
ensure_cap self (length self + n);
49-
Buffer.blit buf 0 self.bytes self.len n;
49+
Buffer.blit buf 0 self.bs self.len n;
5050
self.len <- self.len + n
5151

5252
let append_subbytes self b off len =
5353
ensure_cap self (length self + len);
54-
Bytes.blit b off self.bytes self.len len;
54+
Bytes.blit b off self.bs self.len len;
5555
self.len <- self.len + len
5656

5757
let append_bytes self b = append_subbytes self b 0 (Bytes.length b)
@@ -61,15 +61,15 @@ let append_substring self s off len =
6161
append_subbytes self (Bytes.unsafe_of_string s) off len
6262

6363
let[@inline] add_char_unsafe_ self c =
64-
Bytes.unsafe_set self.bytes self.len c;
64+
Bytes.unsafe_set self.bs self.len c;
6565
self.len <- self.len + 1
6666

6767
let[@inline] add_char self c =
6868
if self.len = capacity self then grow_ self;
6969
add_char_unsafe_ self c
7070

71-
let[@inline] unsafe_get self i = Bytes.unsafe_get self.bytes i
72-
let[@inline] unsafe_set self i c = Bytes.unsafe_set self.bytes i c
71+
let[@inline] unsafe_get self i = Bytes.unsafe_get self.bs i
72+
let[@inline] unsafe_set self i c = Bytes.unsafe_set self.bs i c
7373

7474
let[@inline] get self i =
7575
if i < 0 || i >= self.len then invalid_arg "Byte_buf.get";
@@ -79,26 +79,26 @@ let[@inline] set self i c =
7979
if i < 0 || i >= self.len then invalid_arg "Byte_buf.set";
8080
unsafe_set self i c
8181

82-
let[@inline] contents self = Bytes.sub_string self.bytes 0 self.len
83-
let[@inline] contents_bytes self = Bytes.sub self.bytes 0 self.len
82+
let[@inline] contents self = Bytes.sub_string self.bs 0 self.len
83+
let[@inline] contents_bytes self = Bytes.sub self.bs 0 self.len
8484
let[@inline] append_iter self i = i (add_char self)
8585
let[@inline] append_seq self seq = Seq.iter (add_char self) seq
8686

8787
let fold_left f acc self =
88-
let { bytes; len } = self in
88+
let { bs; len } = self in
8989

9090
(* capture current content *)
9191
let acc = ref acc in
9292
for i = 0 to len do
93-
acc := f !acc (Bytes.unsafe_get bytes i)
93+
acc := f !acc (Bytes.unsafe_get bs i)
9494
done;
9595
!acc
9696

9797
let iter f self =
98-
let { bytes; len } = self in
98+
let { bs; len } = self in
9999
(* capture current content *)
100100
for i = 0 to len do
101-
f (Bytes.unsafe_get bytes i)
101+
f (Bytes.unsafe_get bs i)
102102
done
103103

104104
let of_seq seq =
@@ -114,12 +114,12 @@ let of_iter iter =
114114
let to_iter self yield = iter yield self
115115

116116
let to_seq self =
117-
let { bytes; len } = self in
117+
let { bs; len } = self in
118118
let rec s i () =
119119
if i = len then
120120
Seq.Nil
121121
else
122-
Seq.Cons (Bytes.unsafe_get bytes i, s (i + 1))
122+
Seq.Cons (Bytes.unsafe_get bs i, s (i + 1))
123123
in
124124
s 0
125125

src/core/CCByte_buffer.mli

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55
*)
66

77
type t = {
8-
mutable bytes: bytes;
8+
mutable bs: bytes; (** The backing bytes buffer *)
99
mutable len: int;
10+
(** Length of the "active" slice in [bs]. The actual content
11+
of the buffer is [bs[0]..bs[len-1]]. What comes after
12+
is undefined garbage. *)
1013
}
1114
(** The byte buffer.
12-
The definition is public since NEXT_RELEASE *)
15+
The definition is public since NEXT_RELEASE . *)
1316

1417
type 'a iter = ('a -> unit) -> unit
1518

src/core/CCByte_slice.ml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
type t = {
2+
bs: bytes;
3+
mutable off: int;
4+
mutable len: int;
5+
}
6+
7+
let show self = Printf.sprintf "<slice len=%d>" self.len
8+
let pp out self = Format.pp_print_string out (show self)
9+
10+
let create ?(off = 0) ?len bs =
11+
let len =
12+
match len with
13+
| None -> Bytes.length bs - off
14+
| Some n ->
15+
if n < 0 || off + n > Bytes.length bs then
16+
invalid_arg "Bslice: invalid length";
17+
n
18+
in
19+
{ bs; off; len }
20+
21+
let[@inline] of_string s = create (Bytes.unsafe_of_string s)
22+
let[@inline] len self = self.len
23+
let[@inline] contents self = Bytes.sub_string self.bs self.off self.len
24+
25+
let[@inline] clear self =
26+
self.len <- 0;
27+
self.off <- 0
28+
29+
let[@inline] get self i : char =
30+
if i >= self.len then invalid_arg "Bslice: out of bound access";
31+
Bytes.unsafe_get self.bs (self.off + i)
32+
33+
let[@inline] set self i c : unit =
34+
if i >= self.len then invalid_arg "Bslice: out of bound access";
35+
Bytes.unsafe_set self.bs (self.off + i) c
36+
37+
let sub self off len =
38+
if off + len > self.len then invalid_arg "Bslice: invalid length";
39+
{ bs = self.bs; off = self.off + off; len }
40+
41+
let[@inline] consume self n : unit =
42+
if n > self.len then invalid_arg "Bslice: consuming too many bytes";
43+
self.off <- self.off + n;
44+
self.len <- self.len - n

src/core/CCByte_slice.mli

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
(** A simple byte slice.
2+
3+
@since NEXT_RELEASE *)
4+
5+
type t = {
6+
bs: bytes; (** The bytes, potentially shared between many slices *)
7+
mutable off: int; (** Offset in [bs] *)
8+
mutable len: int;
9+
(** Length of the slice. Valid indices are [bs[off]…bs[off+len-1]],
10+
inclusive. *)
11+
}
12+
[@@deriving show]
13+
14+
val create : ?off:int -> ?len:int -> bytes -> t
15+
val clear : t -> unit
16+
val of_string : string -> t
17+
val len : t -> int
18+
val get : t -> int -> char
19+
val set : t -> int -> char -> unit
20+
val consume : t -> int -> unit
21+
val contents : t -> string
22+
val sub : t -> int -> int -> t

src/core/containers.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
module Array = CCArray
66
module Bool = CCBool
77
module Byte_buffer = CCByte_buffer
8+
module Byte_slice = CCByte_slice
89
module Char = CCChar
910
module Equal = CCEqual
1011
module Either = CCEither

tests/core/t_fun.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
2+
[@@@ocaml.warning "-33"]
13
open CCFun
24
module T = (val Containers_testlib.make ~__FILE__ ())
35
include T;;

0 commit comments

Comments
 (0)