diff --git a/src/core/CCString.cppo.ml b/src/core/CCString.cppo.ml index 097cd078..6f64b4e1 100644 --- a/src/core/CCString.cppo.ml +++ b/src/core/CCString.cppo.ml @@ -263,6 +263,20 @@ let of_array a = let to_array s = Array.init (String.length s) (fun i -> s.[i]) +let lines s = Split.gen_cpy ~by:"\n" s + +let concat_gen ~sep g = + let b = Buffer.create 256 in + let rec aux ~first () = match g () with + | None -> Buffer.contents b + | Some s -> + if not first then Buffer.add_string b sep; + Buffer.add_string b s; + aux ~first:false () + in aux ~first:true () + +let unlines g = concat_gen ~sep:"\n" g + let pp buf s = Buffer.add_char buf '"'; Buffer.add_string buf s; diff --git a/src/core/CCString.mli b/src/core/CCString.mli index 807bb938..ad0e0317 100644 --- a/src/core/CCString.mli +++ b/src/core/CCString.mli @@ -113,6 +113,22 @@ val suffix : suf:string -> string -> bool not (suffix ~suf:"abcd" "cd") *) +val lines : string -> string gen +(** [lines s] returns a generator of the lines of [s] (splits along '\n') + @since NEXT_RELEASE *) + +val concat_gen : sep:string -> string gen -> string +(** [concat_gen ~sep g] concatenates all strings of [g], separated with [sep]. + @since NEXT_RELEASE *) + +val unlines : string gen -> string +(** [unlines g] concatenates all strings of [g], separated with '\n' + @since NEXT_RELEASE *) + +(*$Q + Q.printable_string (fun s -> unlines (lines s) = s) +*) + include S with type t := string (** {2 Splitting} *)