Skip to content

Commit

Permalink
Merge pull request #43 from avsm/master
Browse files Browse the repository at this point in the history
Add `fillv`
  • Loading branch information
avsm committed Jan 29, 2015
2 parents 2d2cd14 + 1fb5b86 commit 8da4f4f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
1.6.0 (trunk):
* Add `fillv` to copy over a list of buffers (from Thomas Leonard).

1.5.0 (2014-11-24):
* Make `camlp4` an optional build-time dependency (#35).
* Remove `ounit` as a dependency in the `opam` file.
Expand Down
17 changes: 17 additions & 0 deletions lib/cstruct.ml
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,23 @@ let copyv ts =
) 0 ts in
dst

let fillv ~src ~dst =
let rec aux dst n = function
| [] -> n, []
| hd::tl ->
let avail = len dst in
let first = len hd in
if first <= avail then (
blit hd 0 dst 0 first;
aux (shift dst first) (n + first) tl
) else (
blit hd 0 dst 0 avail;
let rest_hd = shift hd first in
(n + avail, rest_hd :: tl)
) in
aux dst 0 src


let to_string t =
let sz = len t in
let s = String.create sz in
Expand Down
5 changes: 5 additions & 0 deletions lib/cstruct.mli
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,11 @@ val copyv: t list -> string
(** [copyv cstrs] is the string representation of the concatenation of
all cstructs in [cstrs]. *)

val fillv: src:t list -> dst:t -> int * t list
(** [fillv ~src ~dst] copies from [src] to [dst] until [src] is exhausted or [dst] is full.
* Returns the number of bytes copied and the remaining data from [src], if any.
* This is useful if you want buffer data into fixed-sized chunks. *)

(** {2 Iterations} *)

type 'a iter = unit -> 'a option
Expand Down

0 comments on commit 8da4f4f

Please sign in to comment.