From 76b108203aec8ed291e333cc6ce7c3c60f5f021c Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Thu, 19 Aug 2021 10:21:12 -0400 Subject: [PATCH] add iterator functions to CCIO --- src/core/CCIO.ml | 10 ++++++++++ src/core/CCIO.mli | 20 ++++++++++++++++++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/core/CCIO.ml b/src/core/CCIO.ml index 4a2e6291..36d03d7d 100644 --- a/src/core/CCIO.ml +++ b/src/core/CCIO.ml @@ -101,6 +101,10 @@ let read_chunks_gen ?(size=1024) ic = in next +let read_chunks_iter ?size ic = + let g = read_chunks_gen ?size ic in + fun yield -> gen_iter yield g + let read_chunks_seq ?size ic = seq_of_gen_ (read_chunks_gen ?size ic) @@ -118,6 +122,10 @@ let read_lines_gen ic = let read_lines_seq ic = seq_of_gen_ (read_lines_gen ic) +let read_lines_iter ic = + let g = read_lines_gen ic in + fun yield -> gen_iter yield g + let read_lines_l ic = let l = ref [] in try @@ -218,6 +226,8 @@ let rec write_lines oc g = match g () with let write_lines_seq oc seq = Seq.iter (write_line oc) seq +let write_lines_iter oc i = i (write_line oc) + let write_lines_l oc l = List.iter (write_line oc) l diff --git a/src/core/CCIO.mli b/src/core/CCIO.mli index 87855941..c0d8efb5 100644 --- a/src/core/CCIO.mli +++ b/src/core/CCIO.mli @@ -68,16 +68,20 @@ val with_in : ?mode:int -> ?flags:open_flag list -> @param flags opening flags (default [[Open_text]]). [Open_rdonly] is used in any cases. *) val read_chunks_gen : ?size:int -> in_channel -> string gen -(** Read the channel's content into chunks of size [size]. +(** Read the channel's content into chunks of size at most [size]. {b NOTE} the generator must be used within the lifetime of the channel, see warning at the top of the file. *) val read_chunks_seq : ?size:int -> in_channel -> string Seq.t -(** Read the channel's content into chunks of size [size]. +(** Read the channel's content into chunks of size at most [size]. {b NOTE} the generator must be used within the lifetime of the channel, see warning at the top of the file. @since 3.5 *) +val read_chunks_iter : ?size:int -> in_channel -> string iter +(** Read the channel's content into chunks of size at most [size] + @since NEXT_RELEASE *) + val read_line : in_channel -> string option (** Read a line from the channel. Returns [None] if the input is terminated. The "\n" is removed from the line. *) @@ -93,6 +97,10 @@ val read_lines_seq : in_channel -> string Seq.t see warning at the top of the file. @since 3.5 *) +val read_lines_iter : in_channel -> string iter +(** Read all lines. + @since NEXT_RELEASE *) + val read_lines_l : in_channel -> string list (** Read all lines into a list. *) @@ -136,6 +144,10 @@ val write_seq : ?sep:string -> out_channel -> string Seq.t -> unit val write_lines : out_channel -> string gen -> unit (** Write every string on the output, followed by "\n". *) +val write_lines_iter : out_channel -> string iter -> unit +(** Write every string on the output, followed by "\n". + @since NEXT_RELEASE *) + val write_lines_seq : out_channel -> string Seq.t -> unit (** Write every string on the output, followed by "\n". @since 3.5 *) @@ -260,6 +272,10 @@ module File : sig take some time on large directories). @since 1.1 *) + val walk_seq : t -> walk_item Seq.t + (** Like {!walk} but returns a Seq + @since NEXT_RELEASE *) + val show_walk_item : walk_item -> string val with_temp :