Skip to content
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

Introduce CArray.of_string and expand the string view documentation #562

Merged
merged 3 commits into from
Apr 5, 2018
Merged
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
4 changes: 4 additions & 0 deletions src/ctypes/ctypes.mli
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,10 @@ sig
(** [unsafe_set a n v] behaves like [set a n v] except that the check that
[n] between [0] and [(CArray.length a - 1)] is not performed. *)

val of_string : string -> char t
(** [of_string s] builds an array of the same length as [s], and writes
the elements of [s] to the corresponding elements of the array. *)

val of_list : 'a typ -> 'a list -> 'a t
(** [of_list t l] builds an array of type [t] of the same length as [l], and
writes the elements of [l] to the corresponding elements of the array. *)
Expand Down
5 changes: 5 additions & 0 deletions src/ctypes/ctypes_memory.ml
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,11 @@ struct

let element_type { astart } = reference_type astart

let of_string string =
let arr = make char (String.length string) in
String.iteri (set arr) string;
arr

let of_list typ list =
let arr = make typ (List.length list) in
List.iteri (set arr) list;
Expand Down
5 changes: 5 additions & 0 deletions src/ctypes/ctypes_types.mli
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@ sig
To avoid problems with the garbage collector, values passed using
{!string} are copied into immovable C-managed storage before being passed
to C.

When the memory is not owned by the C code, -- i.e. when creating or
initializing a struct in OCaml before passing it to C -- then the
{!string} view isn't a good choice, because there's no way to manage the
lifetime of the C copy of the generated OCaml string.
*)

val string_opt : string option typ
Expand Down
19 changes: 18 additions & 1 deletion tests/test-arrays/test_array.ml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ let test_multidimensional_arrays _ =
done
done


(*
Test the CArray.iter function
*)
Expand Down Expand Up @@ -211,6 +210,21 @@ let test_sub _ =
end


(*
Test the CArray.of_string function
*)
let test_of_string _ =
let s = "abcdefghiABCDEFGHI" in
let a = CArray.of_string s in
let s' = coerce (ptr char) string (CArray.start a) in
assert_equal s s';

let s = "" in
let a = CArray.of_string s in
let s' = coerce (ptr char) string (CArray.start a) in
assert_equal s s'


(*
Test that creating an array initializes all elements appropriately.
*)
Expand Down Expand Up @@ -375,6 +389,9 @@ let suite = "Array tests" >:::
"CArray.sub"
>:: test_sub;

"CArray.of_string"
>:: test_of_string;

"array initialization"
>:: test_array_initialiation;

Expand Down