Skip to content

Commit a27a69c

Browse files
author
Vincent Bernardoff
committed
Documented all non-trivial functions in module Cstruct
1 parent 4a0bff1 commit a27a69c

File tree

2 files changed

+79
-9
lines changed

2 files changed

+79
-9
lines changed

lib/cstruct.ml

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,31 +117,39 @@ let set_len t len =
117117
let add_len t len =
118118
{ t with len = t.len + len }
119119

120+
let invalid_arg fmt =
121+
let b = Buffer.create 20 in (* for thread safety. *)
122+
let ppf = Format.formatter_of_buffer b in
123+
let k ppf = Format.pp_print_flush ppf (); invalid_arg (Buffer.contents b) in
124+
Format.kfprintf k ppf fmt
125+
126+
let invalid_bounds j l = invalid_arg "invalid bounds (index %d, length %d)" j l
127+
120128
external unsafe_blit_bigstring_to_bigstring : buffer -> int -> buffer -> int -> int -> unit = "caml_blit_bigstring_to_bigstring" "noalloc"
121129

122130
external unsafe_blit_string_to_bigstring : string -> int -> buffer -> int -> int -> unit = "caml_blit_string_to_bigstring" "noalloc"
123131

124132
external unsafe_blit_bigstring_to_string : buffer -> int -> string -> int -> int -> unit = "caml_blit_bigstring_to_string" "noalloc"
125133

126134
let copy src srcoff len =
127-
if src.len - srcoff < len then raise (Failure "copy");
135+
if src.len - srcoff < len then raise (Invalid_argument (invalid_bounds srcoff len));
128136
let s = String.create len in
129137
unsafe_blit_bigstring_to_string src.buffer (src.off+srcoff) s 0 len;
130138
s
131139

132140
let blit src srcoff dst dstoff len =
133-
if src.len - srcoff < len then raise (Failure "blit");
134-
if dst.len - dstoff < len then raise (Failure "blitdst");
141+
if src.len - srcoff < len then raise (Invalid_argument (invalid_bounds srcoff len));
142+
if dst.len - dstoff < len then raise (Invalid_argument (invalid_bounds dstoff len));
135143
unsafe_blit_bigstring_to_bigstring src.buffer (src.off+srcoff) dst.buffer (dst.off+dstoff) len
136144

137145
let blit_from_string src srcoff dst dstoff len =
138-
if String.length src - srcoff < len then raise (Failure "blit_from_string");
139-
if dst.len - dstoff < len then raise (Failure "blit_from_string dst");
146+
if String.length src - srcoff < len then raise (Invalid_argument (invalid_bounds srcoff len));
147+
if dst.len - dstoff < len then raise (Invalid_argument (invalid_bounds dstoff len));
140148
unsafe_blit_string_to_bigstring src srcoff dst.buffer (dst.off+dstoff) len
141149

142150
let blit_to_string src srcoff dst dstoff len =
143-
if src.len - srcoff < len then raise (Failure "blit_to_string");
144-
if String.length dst - dstoff < len then raise (Failure "blit_to_string dst");
151+
if src.len - srcoff < len then raise (Invalid_argument (invalid_bounds srcoff len));
152+
if String.length dst - dstoff < len then raise (Invalid_argument (invalid_bounds dstoff len));
145153
unsafe_blit_bigstring_to_string src.buffer (src.off+srcoff) dst dstoff len
146154

147155
let set_uint8 t i c =

lib/cstruct.mli

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,41 @@
1414
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1515
*)
1616

17+
(** Manipulate external buffers as C-like structs *)
18+
1719
type buffer = (char, Bigarray.int8_unsigned_elt, Bigarray.c_layout) Bigarray.Array1.t
20+
(** Type of a buffer. A cstruct is composed of an underlying buffer
21+
and position/length within this buffer. *)
1822

1923
type t = private {
2024
buffer: buffer;
2125
off : int;
2226
len : int;
2327
}
28+
(** Type of a cstruct. *)
29+
30+
(** Functions that create a new cstruct. *)
2431

2532
val of_bigarray: ?off:int -> ?len:int -> buffer -> t
33+
(** [of_bigarray ~off ~len b] is the cstruct contained in [b] starting
34+
at [off], of length [len]. *)
35+
2636
val create : int -> t
37+
(** [create len] is a cstruct of size [len] with an offset of 0. *)
38+
39+
val of_string: ?allocator:(int -> t) -> string -> t
40+
(** [of_string ~alloc str] is the cstruct representation of [str],
41+
with the underlying buffer allocated by [alloc]. If [alloc] is not
42+
provided, [create] is used. *)
43+
44+
(** Functions that operate over cstructs. *)
45+
2746
val check_bounds : t -> int -> bool
47+
(** [check_bounds cstr len] is [true] if [cstr.buffer]'s size is
48+
greater or equal than [len], [false] otherwise. *)
2849

2950
type byte = char
51+
3052
val byte : int -> byte
3153
val byte_to_int : byte -> int
3254

@@ -54,16 +76,47 @@ val set_char: t -> int -> char -> unit
5476
val set_uint8: t -> int -> uint8 -> unit
5577

5678
val sub: t -> int -> int -> t
79+
(** [sub cstr off len] is [{ t with off = t.off + off; len }] *)
5780

5881
val shift: t -> int -> t
82+
(** [shift cstr len] is [{ t with off = t.off + off; len = t.len - off
83+
}] *)
5984

6085
val copy: t -> int -> int -> string
86+
(** [copy cstr off len] is the string representation of the segment of
87+
[t] starting at [off] of size [len].
88+
89+
Raise [Invalid_argument] if [off] and [len] do not designate a
90+
valid segment of [t]. *)
6191

6292
val blit: t -> int -> t -> int -> int -> unit
93+
(** [blit src srcoff dst dstoff len] copies [len] characters from
94+
cstruct [src], starting at index [srcoff], to cstruct [dst],
95+
starting at index [dstoff]. It works correctly even if [src] and
96+
[dst] are the same string, and the source and destination
97+
intervals overlap.
98+
99+
Raise [Invalid_argument] if [srcoff] and [len] do not designate a
100+
valid segment of [src], or if [dstoff] and [len] do not designate
101+
a valid segment of [dst]. *)
63102

64103
val blit_from_string: string -> int -> t -> int -> int -> unit
104+
(** [blit_from_string src srcoff dst dstoff len] copies [len]
105+
characters from string [src], starting at index [srcoff], to
106+
string [dst], starting at index [dstoff].
107+
108+
Raise [Invalid_argument] if [srcoff] and [len] do not designate a
109+
valid substring of [src], or if [dstoff] and [len] do not
110+
designate a valid segment of [dst]. *)
65111

66112
val blit_to_string: t -> int -> string -> int -> int -> unit
113+
(** [blit_to_string src srcoff dst dstoff len] copies [len] characters
114+
from cstruct [src], starting at index [srcoff], to string [dst],
115+
starting at index [dstoff].
116+
117+
Raise [Invalid_argument] if [srcoff] and [len] do not designate a
118+
valid segment of [src], or if [dstoff] and [len] do not designate
119+
a valid substring of [dst]. *)
67120

68121
val len: t -> int
69122

@@ -72,11 +125,13 @@ val set_len : t -> int -> t
72125
val add_len : t -> int -> t
73126

74127
val split: ?start:int -> t -> int -> t * t
128+
(** [split ~start cstr len] is a tuple containing the cstruct
129+
extracted from [cstr] at offset [start] (default: 0) of length
130+
[len] as first element, and the rest of [cstr] as second
131+
element. *)
75132

76133
val to_string: t -> string
77134

78-
val of_string: ?allocator:(int -> t) -> string -> t
79-
80135
val hexdump: t -> unit
81136
val debug: t -> string
82137

@@ -103,13 +158,20 @@ end
103158
(** {2 List of buffers} *)
104159

105160
val lenv: t list -> int
161+
(** [lenv cstrs] is the combined length of all cstructs in [cstrs]. *)
106162

107163
val copyv: t list -> string
164+
(** [copyv cstrs] is the string representation of the concatenation of
165+
all cstructs in [cstrs]. *)
108166

109167
(** {2 Iterations} *)
110168

111169
type 'a iter = unit -> 'a option
170+
(** Type of an iterator. *)
112171

113172
val iter: (t -> int option) -> (t -> 'a) -> t -> 'a iter
173+
(** [iter lenf of_cstr cstr] is an iterator over [cstr] that returns
174+
elements of size [lenf cstr] and type [of_cstr cstr]. *)
114175

115176
val fold: ('b -> 'a -> 'b) -> 'a iter -> 'b -> 'b
177+
(** [fold f iter acc] is [(f iterN accN ... (f iter acc)...)]. *)

0 commit comments

Comments
 (0)