1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
(*
* Copyright (C) 2006-2009 Citrix Systems Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation; version 2.1 only. with the special
* exception on linking described in file LICENSE.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*)
external is_all_zeros : string -> int -> bool = "is_all_zeros"
external _find_a_nonzero : string -> int -> int -> int = "find_a_nonzero"
external _find_a_zero : string -> int -> int -> int = "find_a_zero"
let wrap f x len offset =
let remaining = len - offset in
if remaining <= 0 then raise (Invalid_argument "offset > length");
let result = f x offset remaining in
if result = remaining then None else Some (result + offset)
let find_a_nonzero = wrap _find_a_nonzero
let find_a_zero = wrap _find_a_zero
type substring = {
buf: string;
offset: int;
len: int
}
let fold_over_nonzeros x len roundup f initial =
let rec inner acc offset =
if offset = len then acc
else
match find_a_nonzero x len offset with
| None -> acc (* no more *)
| Some s ->
let e = match find_a_zero x len s with
| None -> len
| Some e -> e in
let e = min len (roundup e) in
inner (f acc { buf = x; offset = s; len = e - s }) e in
inner initial 0
|