struct
let string_to_item () =
let buf = Buffer.make () in
Biocaml_transform.make ~name:"string_to_lines"
~feed:(Buffer.feed_string buf)
~next:(function
| true -> (match Buffer.next_line buf with
| Some line -> `output line
| None -> (match Buffer.contents buf with
| [], None -> `end_of_stream
| [], Some unfinished_line ->
(Buffer.empty buf; `output (Line.of_string_unsafe unfinished_line))
| _ -> assert false
)
)
| false -> (match Buffer.next_line buf with
| None -> `not_ready
| Some line -> `output line
)
)
()
let item_to_string ?(buffer:[`clear of int | `reset of int]= `reset 1024) () =
let module Buffer = Caml.Buffer in
let buffer, clear_buffer =
match buffer with
| `clear s -> (Buffer.create s, Buffer.clear)
| `reset s -> (Buffer.create s, Buffer.reset) in
Biocaml_transform.make ~name:"lines_to_string" ()
~feed:(fun l ->
Buffer.add_string buffer (l : Line.t :> string);
Buffer.add_char buffer '\n')
~next:(fun stopped ->
match Buffer.contents buffer with
| "" -> if stopped then `end_of_stream else `not_ready
| s ->
clear_buffer buffer;
`output s)
let group2 () =
let queue : (item * item) Queue.t= Queue.create () in
let item1 = ref None in
Biocaml_transform.make ~name:"group2"
~feed:(function item -> match !item1 with
| Some item1' -> (
Queue.enqueue queue (item1', item);
item1 := None
)
| None -> item1 := Some item
)
~next:(fun stopped -> match Queue.dequeue queue with
| Some ij -> `output (Ok ij)
| None ->
if not stopped then
`not_ready
else
(match !item1 with
| None -> `end_of_stream
| Some _ -> `output (Error `premature_end_of_input)
)
)
()
let make ?name ?filename ~next ~on_error () =
let lo_parser = Buffer.make ?filename () in
Biocaml_transform.make ?name ()
~feed:(Buffer.feed_string lo_parser)
~next:(fun stopped ->
match next lo_parser with
| `output (Ok r) -> `output (Ok r)
| `output (Error r) -> `output (Error (on_error (`next r)))
| `not_ready ->
if stopped then (
if Buffer.is_empty lo_parser then
`end_of_stream
else
let l,o = Buffer.contents lo_parser in
`output
(Error (on_error
(`incomplete_input
(Buffer.current_position lo_parser,
(l :> string list), o))))
) else
`not_ready)
let make_merge_error =
make
~on_error:(function
| `next e -> e
| `incomplete_input e -> `incomplete_input e)
end