Skip to content

Commit

Permalink
Lines: add transform item_to_string
Browse files Browse the repository at this point in the history
  • Loading branch information
smondet committed Feb 20, 2013
1 parent 0f92052 commit 062b189
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
21 changes: 19 additions & 2 deletions src/lib/biocaml_lines.ml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ module Transform = struct

let string_to_item () =
let buf = Buffer.make () in
Biocaml_transform.make ~name:"lines"
Biocaml_transform.make ~name:"string_to_lines"
~feed:(Buffer.feed_string buf)
~next:(function
| true -> (match Buffer.next_line buf with
Expand All @@ -101,6 +101,23 @@ module Transform = struct
)
()

let item_to_string ?(buffer:[`clear of int | `reset of int]= `reset 1024) () =
let module Buffer = Core.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 : Biocaml_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
Expand Down Expand Up @@ -165,7 +182,7 @@ let of_char_stream cstr =
let c = Stream.next_exn cstr in
if c <> '\n' then (Buffer.add_char ans c; loop())
with Core.Std.Caml.Stream.Failure -> ()
in
in
loop();
Some (Buffer.contents ans |! Line.of_string_unsafe)
in
Expand Down
5 changes: 4 additions & 1 deletion src/lib/biocaml_lines.mli
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(** Lines of a file. *)

type item = private Biocaml_line.t
type item = Biocaml_line.t

(** Errors.
Expand Down Expand Up @@ -81,6 +81,9 @@ module Transform : sig
(item,
(item * item, [> `premature_end_of_input ]) Core.Std.Result.t) Biocaml_transform.t

val item_to_string: ?buffer:[ `clear of int | `reset of int ] ->
unit -> (item, string) Biocaml_transform.t

(** Build a stoppable line-oriented parsing_buffer. *)
val make : ?name:string -> ?filename:string ->
next:(Buffer.t ->
Expand Down

0 comments on commit 062b189

Please sign in to comment.