Skip to content

Commit 00ff5aa

Browse files
authored
Merge pull request #262 from hannesm/cap
cstruct_cap: order interface
2 parents 1342786 + 7fd10ac commit 00ff5aa

File tree

4 files changed

+339
-253
lines changed

4 files changed

+339
-253
lines changed

lib/cstruct.ml

+13-12
Original file line numberDiff line numberDiff line change
@@ -384,22 +384,19 @@ let fillv ~src ~dst =
384384
) in
385385
aux dst 0 src
386386

387-
388-
let to_bytes t =
389-
let sz = len t in
390-
let b = Bytes.create sz in
391-
unsafe_blit_bigstring_to_bytes t.buffer t.off b 0 sz;
392-
b
393-
394-
let to_string t =
387+
let to_string ?(off=0) ?len:sz t =
388+
let len = match sz with None -> len t - off | Some l -> l in
395389
(* The following call is safe, since this is the only reference to the
396390
freshly-created value built by [to_bytes t]. *)
397-
Bytes.unsafe_to_string (to_bytes t)
391+
copy t off len
398392

399-
let of_data_abstract blitfun lenfun ?allocator ?(off=0) ?len buf =
393+
let to_bytes ?off ?len t =
394+
Bytes.unsafe_of_string (to_string ?off ?len t)
395+
396+
let [@inline always] of_data_abstract blitfun lenfun ?allocator ?(off=0) ?len buf =
400397
let buflen =
401398
match len with
402-
| None -> lenfun buf
399+
| None -> lenfun buf - off
403400
| Some len -> len in
404401
match allocator with
405402
| None ->
@@ -417,7 +414,11 @@ let of_string ?allocator ?off ?len buf =
417414
let of_bytes ?allocator ?off ?len buf =
418415
of_data_abstract blit_from_bytes Bytes.length ?allocator ?off ?len buf
419416

420-
let of_hex str =
417+
let of_hex ?(off=0) ?len str =
418+
let str =
419+
let l = match len with None -> String.length str - off | Some l -> l in
420+
String.sub str off l
421+
in
421422
let string_fold ~f ~z str =
422423
let st = ref z in
423424
( String.iter (fun c -> st := f !st c) str ; !st )

lib/cstruct.mli

+36-17
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,8 @@ val empty : t
177177

178178
val of_bigarray: ?off:int -> ?len:int -> buffer -> t
179179
(** [of_bigarray ~off ~len b] is the cstruct contained in [b] starting
180-
at [off], of length [len]. *)
180+
at offset [off] (default [0]) of length [len]
181+
(default [Bigarray.Array1.dim b - off]). *)
181182

182183
val to_bigarray: t -> buffer
183184
(** [to_bigarray t] converts a {!t} into a {!buffer} Bigarray, using
@@ -202,24 +203,34 @@ val create_unsafe : int -> t
202203

203204
val of_string: ?allocator:(int -> t) -> ?off:int -> ?len:int -> string -> t
204205
(** [of_string ~allocator ~off ~len str] is the cstruct representation of [str]
205-
slice located at [off] offset and of [len] length,
206+
slice located at offset [off] (default [0]) and of length [len] (default
207+
[String.length str - off]),
206208
with the underlying buffer allocated by [alloc]. If [allocator] is not
207-
provided, [create] is used. *)
209+
provided, [create] is used.
210+
211+
@raise Invalid_argument if [off] or [len] is negative, or
212+
[String.length str - off] < [len].
213+
*)
208214

209215
val of_bytes: ?allocator:(int -> t) -> ?off:int -> ?len:int -> bytes -> t
210216
(** [of_bytes ~allocator byt] is the cstruct representation of [byt]
211-
slice located at [off] offset and of [len] length,
217+
slice located at offset [off] (default [0]) and of length [len] (default
218+
[Bytes.length byt - off]),
212219
with the underlying buffer allocated by [alloc]. If [allocator] is not
213-
provided, [create] is used. *)
220+
provided, [create] is used.
221+
222+
@raise Invalid_argument if [off] or [len] is negative, or
223+
[Bytes.length str - off] < [len]. *)
214224

215-
val of_hex: string -> t
216-
(** [of_hex str] is the cstruct [cs]. Every pair of hex-encoded characters in
217-
[str] are converted to one byte in [cs]. Whitespaces (space, newline, tab,
218-
carriage return) in [str] are skipped. The resulting cstruct is exactly
219-
half the size of the non-skipped characters of [str].
225+
val of_hex: ?off:int -> ?len:int -> string -> t
226+
(** [of_hex ~off ~len str] is the cstruct [cs]. Every pair of hex-encoded
227+
characters in [str] starting at offset [off] (default [0]) of length [len]
228+
(default [String.length str - off]) are converted to one byte in [cs].
229+
Whitespaces (space, newline, tab, carriage return) in [str] are skipped.
220230
221231
@raise Invalid_argument if the input string contains invalid characters or
222-
has an odd numbers of non-whitespace characters. *)
232+
has an odd numbers of non-whitespace characters, or if [off] or [len] are
233+
negative, or [String.length str - off] < [len]. *)
223234

224235
(** {2 Comparison } *)
225236

@@ -351,13 +362,21 @@ val split: ?start:int -> t -> int -> t * t
351362
@raise Invalid_argument if [start] exceeds the cstruct length,
352363
or if there is a bounds violation of the cstruct via [len+start]. *)
353364

354-
val to_string: t -> string
355-
(** [to_string t] will allocate a fresh OCaml [string] and copy the
356-
contents of the cstruct into it, and return that string copy. *)
365+
val to_string: ?off:int -> ?len:int -> t -> string
366+
(** [to_string ~off ~len t] will allocate a fresh OCaml [string] and copy the
367+
contents of the cstruct starting at offset [off] (default [0]) of length
368+
[len] (default [Cstruct.len t - off]) into it, and return that string.
369+
370+
@raise Invalid_argument if [off] or [len] is negative, or
371+
[Cstruct.len str - off] < [len]. *)
372+
373+
val to_bytes: ?off:int -> ?len:int -> t -> bytes
374+
(** [to_bytes ~off ~len t] will allocate a fresh OCaml [bytes] and copy the
375+
contents of the cstruct starting at offset [off] (default [0]) of length
376+
[len] (default [Cstruct.len t - off]) into it, and return that bytes.
357377
358-
val to_bytes: t -> bytes
359-
(** [to_bytes t] will allocate a fresh OCaml [bytes] and copy the
360-
contents of the cstruct into it, and return that byte copy. *)
378+
@raise Invalid_argument if [off] or [len] is negative, or
379+
[Cstruct.len str - off] < [len]. *)
361380

362381
(** {2 Debugging } *)
363382

lib/cstruct_cap.ml

+4-18
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,8 @@ type wo = < wr: unit; >
2929
external ro : 'a rd t -> ro t = "%identity"
3030
external wo : 'a wr t -> wo t = "%identity"
3131

32-
let of_string ?off ?len x =
33-
Cstruct.of_string ?off ?len x
34-
let of_bytes ?off ?len x =
35-
Cstruct.of_bytes ?off ?len x
36-
37-
let to_string ?(off= 0) ?len t =
38-
let len = match len with
39-
| Some len -> len
40-
| None -> Cstruct.length t - off in
41-
Cstruct.copy t off len
42-
43-
let to_bytes ?(off= 0) ?len t =
44-
let len = match len with
45-
| Some len -> len
46-
| None -> Cstruct.length t - off in
47-
(* XXX(dinosaure): this is safe when [copy] allocates itself [bytes]
48-
and uses [Bytes.unsafe_to_string]. *)
49-
Bytes.unsafe_of_string (Cstruct.copy t off len)
32+
let of_string = Cstruct.of_string ?allocator:None
33+
let of_bytes = Cstruct.of_bytes ?allocator:None
5034

5135
let pp ppf t = Cstruct.hexdump_pp ppf t
5236

@@ -72,6 +56,8 @@ let sub t ~off ~len =
7256
Cstruct.sub t off len
7357
[@@inline]
7458

59+
let unsafe_to_bigarray = Cstruct.to_bigarray
60+
7561
let concat vss =
7662
let res = create_unsafe (Cstruct.sum_lengths ~caller:"Cstruct.Cap.concat" vss) in
7763
let go off v =

0 commit comments

Comments
 (0)