| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
type ('e,'a) t = { mutable count : int; mutable data : ('e,'a) data } |
| 17 |
and ('e,'a) data = |
| 18 |
Sempty |
| 19 |
| Scons of 'a * ('e,'a) data |
| 20 |
| Sgen of ('e,'a) gen |
| 21 |
| Sbuffio : buffio -> (unit,char) data |
| 22 |
and ('e,'a) gen = { mutable curr : 'a option option; func : 'e -> 'a option } |
| 23 |
and buffio = |
| 24 |
{ ic : in_channel; buff : bytes; mutable len : int; mutable ind : int } |
| 25 |
|
| 26 |
let count { count } = count |
| 27 |
|
| 28 |
let fill_buff b = |
| 29 |
b.len <- input b.ic b.buff 0 (Bytes.length b.buff); b.ind <- 0 |
| 30 |
|
| 31 |
let peek : type e v. e -> (e,v) t -> v option = fun e s -> |
| 32 |
|
| 33 |
match s.data with |
| 34 |
Sempty -> None |
| 35 |
| Scons (a, _) -> Some a |
| 36 |
| Sgen {curr = Some a} -> a |
| 37 |
| Sgen g -> let x = g.func e in g.curr <- Some x; x |
| 38 |
| Sbuffio b -> |
| 39 |
if b.ind >= b.len then fill_buff b; |
| 40 |
if b.len == 0 then begin s.data <- Sempty; None end |
| 41 |
else Some (Bytes.unsafe_get b.buff b.ind) |
| 42 |
|
| 43 |
|
| 44 |
let rec junk : type e v. e -> (e,v) t -> unit = fun e s -> |
| 45 |
match s.data with |
| 46 |
Scons (_, d) -> s.count <- (succ s.count); s.data <- d |
| 47 |
| Sgen ({curr = Some _} as g) -> s.count <- (succ s.count); g.curr <- None |
| 48 |
| Sbuffio b -> |
| 49 |
if b.ind >= b.len then fill_buff b; |
| 50 |
if b.len == 0 then s.data <- Sempty |
| 51 |
else (s.count <- (succ s.count); b.ind <- succ b.ind) |
| 52 |
| Sempty -> () |
| 53 |
| Sgen { curr = None } -> |
| 54 |
match peek e s with |
| 55 |
None -> () |
| 56 |
| Some _ -> junk e s |
| 57 |
|
| 58 |
let rec nget e n s = |
| 59 |
if n <= 0 then [], s.data, 0 |
| 60 |
else |
| 61 |
match peek e s with |
| 62 |
Some a -> |
| 63 |
junk e s; |
| 64 |
let (al, d, k) = nget e (pred n) s in a :: al, Scons (a, d), succ k |
| 65 |
| None -> [], s.data, 0 |
| 66 |
|
| 67 |
|
| 68 |
let npeek e n s = |
| 69 |
let (al, d, len) = nget e n s in |
| 70 |
s.count <- (s.count - len); |
| 71 |
s.data <- d; |
| 72 |
al |
| 73 |
|
| 74 |
let nth e n st = |
| 75 |
List.nth_opt (npeek e (n+1) st) n |
| 76 |
|
| 77 |
let rec njunk e n st = |
| 78 |
if n <> 0 then (junk e st; njunk e (n-1) st) |
| 79 |
|
| 80 |
let next e s = |
| 81 |
match peek e s with |
| 82 |
| Some _ as a -> junk e s; a |
| 83 |
| None -> None |
| 84 |
|
| 85 |
|
| 86 |
let is_empty e s = |
| 87 |
match peek e s with |
| 88 |
| Some _ -> false |
| 89 |
| None -> true |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
let from ?(offset=0) f = {count = offset; data = Sgen {curr = None; func = f}} |
| 95 |
|
| 96 |
|
| 97 |
let empty () = {count = 0; data = Sempty} |
| 98 |
|
| 99 |
let of_string ?(offset=0) s = |
| 100 |
let count = ref 0 in |
| 101 |
from ~offset (fun () -> |
| 102 |
let c = !count in |
| 103 |
if c < String.length s |
| 104 |
then (incr count; Some s.[c]) |
| 105 |
else None) |
| 106 |
|
| 107 |
|
| 108 |
let of_channel ic = |
| 109 |
{count = 0; |
| 110 |
data = Sbuffio {ic = ic; buff = Bytes.create 4096; len = 0; ind = 0}} |