From 27fb393698b1704de03816532fca18ce6bebc447 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Mon, 11 Nov 2019 19:55:43 -0600 Subject: [PATCH] feat(CCIO): add `_gen` suffixes to some functions deprecate the previous versions see #261 --- src/core/CCIO.ml | 8 ++++++-- src/core/CCIO.mli | 20 ++++++++++++++++---- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/core/CCIO.ml b/src/core/CCIO.ml index 0e6aecc3..e1469ee8 100644 --- a/src/core/CCIO.ml +++ b/src/core/CCIO.ml @@ -65,7 +65,7 @@ let with_in ?(mode=0o644) ?(flags=[Open_text]) filename f = let ic = open_in_gen (Open_rdonly::flags) mode filename in finally_ f ic ~h:close_in -let read_chunks ?(size=1024) ic = +let read_chunks_gen ?(size=1024) ic = let buf = Bytes.create size in let next() = let n = input ic buf 0 size in @@ -75,17 +75,21 @@ let read_chunks ?(size=1024) ic = in next +let read_chunks = read_chunks_gen + let read_line ic = try Some (input_line ic) with End_of_file -> None -let read_lines ic = +let read_lines_gen ic = let stop = ref false in fun () -> if !stop then None else try Some (input_line ic) with End_of_file -> (stop:=true; None) +let read_lines = read_lines_gen + let read_lines_l ic = let l = ref [] in try diff --git a/src/core/CCIO.mli b/src/core/CCIO.mli index 9b7882d0..6d2bcf48 100644 --- a/src/core/CCIO.mli +++ b/src/core/CCIO.mli @@ -20,7 +20,7 @@ # CCIO.( with_in "/tmp/input" (fun ic -> - let chunks = read_chunks ic in + let chunks = read_chunks_gen ic in with_out ~flags:[Open_binary; Open_creat] ~mode:0o644 "/tmp/output" (fun oc -> write_gen oc chunks @@ -37,7 +37,7 @@ # CCIO.( let chunks = with_in "/tmp/input" - (fun ic ->read_chunks ic) + (fun ic -> read_chunks_gen ic) in with_out ~flags:[Open_binary;Open_creat] ~mode:0o644 "/tmp/output" (fun oc -> @@ -67,14 +67,26 @@ 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 : ?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 [size]. + @deprecated use {!read_chunks_gen} instead. *) + +val read_chunks_gen : ?size:int -> in_channel -> string gen +(** Read the channel's content into chunks of size [size]. + {b NOTE} the generator must be used within the lifetime of the channel, + see warning at the top of the file. *) 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. *) val read_lines : in_channel -> string gen -(** Read all lines. The generator should be traversed only once. *) +(** Read all lines. The generator should be traversed only once. + @deprecated use {!read_lines_gen} instead. *) + +val read_lines_gen : in_channel -> string gen +(** Read all lines. The generator should be traversed only once. + {b NOTE} the generator must be used within the lifetime of the channel, + see warning at the top of the file. *) val read_lines_l : in_channel -> string list (** Read all lines into a list. *)