Skip to content

Rename Cstruct.lenv -> Cstruct.lengthv #313

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions fuzz/fuzz.ml
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ let () =
);
add_test ~name:"shiftv" [list cstruct; int] (fun ts n ->
match Cstruct.shiftv ts n with
| exception Invalid_argument _ -> check (n < 0 || n > Cstruct.lenv ts)
| exception Invalid_argument _ -> check (n < 0 || n > Cstruct.lengthv ts)
| ts' ->
assert (Cstruct.equal (Cstruct.concat ts') (Cstruct.shift (Cstruct.concat ts) n));
assert ((Cstruct.lenv ts = n) = (ts' = []));
assert ((Cstruct.lengthv ts = n) = (ts' = []));
match ts' with
| hd :: _ -> assert (not (Cstruct.is_empty hd))
| [] -> ()
Expand Down Expand Up @@ -142,21 +142,21 @@ let () =
| () -> check in_range
| exception Invalid_argument _ -> check (not in_range)
);
add_test ~name:"lenv" [list cstruct] (fun cs ->
check (Cstruct.lenv cs >= 0)
add_test ~name:"lengthv" [list cstruct] (fun cs ->
check (Cstruct.lengthv cs >= 0)
);
add_test ~name:"copyv" [list cstruct] (fun cs ->
check (String.equal (Cstruct.copyv cs) (Cstruct.concat cs |> Cstruct.to_string))
);
add_test ~name:"fillv" [list cstruct; cstruct] (fun src dst ->
let copied, rest = Cstruct.fillv ~src ~dst in
check (copied + Cstruct.lenv rest = Cstruct.lenv src);
check (copied + Cstruct.lengthv rest = Cstruct.lengthv src);
(* OCaml tends to underestimate how much space bigarrays are using: *)
Gc.minor ()
);
add_test ~name:"concat" [list cstruct] (fun cs ->
let x = Cstruct.concat cs in
check (Cstruct.length x = Cstruct.lenv cs)
check (Cstruct.length x = Cstruct.lengthv cs)
);
add_test ~name:"span" [ cstruct; list char ] (fun cs p ->
let sat chr = List.exists ((=) chr) p in
Expand Down
1 change: 1 addition & 0 deletions lib/cstruct.ml
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ let rec sum_lengths_aux ~caller acc = function
let sum_lengths ~caller l = sum_lengths_aux ~caller 0 l

let lenv l = sum_lengths ~caller:"Cstruct.lenv" l
let lengthv l = sum_lengths ~caller:"Cstruct.lengthv" l

let copyv ts =
let sz = sum_lengths ~caller:"Cstruct.copyv" ts in
Expand Down
10 changes: 7 additions & 3 deletions lib/cstruct.mli
Original file line number Diff line number Diff line change
Expand Up @@ -504,10 +504,14 @@ val debug: t -> string

(** {2 List of buffers} *)

val lenv: t list -> int
(** [lenv cstrs] is the combined length of all cstructs in [cstrs].
val lengthv: t list -> int
(** [lengthv cstrs] is the combined length of all cstructs in [cstrs].
@raise Invalid_argument if computing the sum overflows. *)

val lenv : t list -> int
[@@ocaml.alert deprecated "use lengthv instead"]
(** [lenv] is deprecated in favor of [lengthv]. *)

val copyv: t list -> string
(** [copyv cstrs] is the string representation of the concatenation of
all cstructs in [cstrs].
Expand All @@ -524,7 +528,7 @@ val shiftv: t list -> int -> t list
It has the property that [equal (concat (shiftv ts n)) (shift (concat ts) n)].
This operation is fairly fast, as it will share the tail of the list.
The first item in the returned list is never an empty cstruct,
so you'll get [[]] if and only if [lenv ts = n]. *)
so you'll get [[]] if and only if [lengthv ts = n]. *)

(** {2 Iterations} *)

Expand Down
12 changes: 8 additions & 4 deletions lib/cstruct_cap.mli
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,15 @@ val check_alignment : 'a t -> int -> bool

@raise Invalid_argument if [alignment] is not a positive integer. *)

val lenv : 'a t list -> int
(** [lenv vs] is the combined length of all {!t} in [vs].
val lengthv : 'a t list -> int
(** [lengthv vs] is the combined length of all {!t} in [vs].

@raise Invalid_argument if computing the sum overflows. *)

val lenv : 'a t list -> int
[@@ocaml.alert deprecated "use lengthv instead"]
(** [lenv] is deprecated in favor of [lengthv]. *)

(** {2 Constructors} *)

val create : int -> rdwr t
Expand Down Expand Up @@ -166,7 +170,7 @@ val shiftv: 'a t list -> int -> 'a t list
It has the property that [equal (concat (shiftv ts n)) (shift (concat ts) n)].
This operation is fairly fast, as it will share the tail of the list.
The first item in the returned list is never an empty cstruct,
so you'll get [[]] if and only if [lenv ts = n]. *)
so you'll get [[]] if and only if [lengthv ts = n]. *)

val split : ?start:int -> 'a t -> int -> 'a t * 'a t
(** [split ~start t len] returns two proxies extracted from [t]. The first
Expand All @@ -190,7 +194,7 @@ val append : 'a rd t -> 'b rd t -> rdwr t
the returned value has both read and write capabilities. *)

val concat : 'a rd t list -> rdwr t
(** [concat vss] allocates a buffer [r] of size [lenv vss]. Each [v] of [vss]
(** [concat vss] allocates a buffer [r] of size [lengthv vss]. Each [v] of [vss]
is copied into the buffer [r]. Each [v] of [vss] need at least read
capability {!rd}, the returned value has both read and write capabilities.
*)
Expand Down
8 changes: 4 additions & 4 deletions lib_test/bounds.ml
Original file line number Diff line number Diff line change
Expand Up @@ -347,14 +347,14 @@ let test_view_bounds_too_small_get_he64 () =
with
Invalid_argument _ -> ()

let test_lenv_overflow () =
let test_lengthv_overflow () =
if Sys.word_size = 32 then (
(* free-up some space *)
Gc.major ();
let b = Cstruct.create max_int and c = Cstruct.create 3 in
try
let _ = Cstruct.lenv [b; b; c] in
failwith "test_lenv_overflow"
let _ = Cstruct.lengthv [b; b; c] in
failwith "test_lengthv_overflow"
with
Invalid_argument _ -> ())

Expand Down Expand Up @@ -494,7 +494,7 @@ let suite = [
"test_view_bounds_too_small_get_he16" , `Quick, test_view_bounds_too_small_get_he16;
"test_view_bounds_too_small_get_he32" , `Quick, test_view_bounds_too_small_get_he32;
"test_view_bounds_too_small_get_he64" , `Quick, test_view_bounds_too_small_get_he64;
"test_lenv_overflow", `Quick, test_lenv_overflow;
"test_lengthv_overflow", `Quick, test_lengthv_overflow;
"test_copyv_overflow", `Quick, test_copyv_overflow;
"test_subview_containment_get_char", `Quick, test_subview_containment_get_char;
"test_subview_containment_get_8" , `Quick, test_subview_containment_get_8;
Expand Down
2 changes: 1 addition & 1 deletion lib_test/tests.ml
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ let append_is_concat ~n () =
let fillv () =
let test src buf_size =
let dst = Cstruct.create buf_size in
let src_len = Cstruct.lenv src in
let src_len = Cstruct.lengthv src in
let len, remaining = Cstruct.fillv ~src ~dst in
assert (len = min src_len buf_size);
let whole = Cstruct.concat (Cstruct.sub dst 0 len :: remaining) in
Expand Down