From 9a6d9a3aa97619ab2356a964e968b42e568a7fff Mon Sep 17 00:00:00 2001 From: Rudi Grinberg Date: Wed, 28 Feb 2018 23:31:24 +0700 Subject: [PATCH] Extend stdlib for configurator --- src/io.ml | 17 +++++++++++++---- src/io.mli | 4 ++++ src/stdune/string.ml | 16 ++++++++++++++++ src/stdune/string.mli | 4 ++++ 4 files changed, 37 insertions(+), 4 deletions(-) diff --git a/src/io.ml b/src/io.ml index ecdfe590..78acf2d6 100644 --- a/src/io.ml +++ b/src/io.ml @@ -37,15 +37,24 @@ let input_lines = in fun ic -> loop ic [] -let read_file fn = - with_file_in fn ~f:(fun ic -> - let len = in_channel_length ic in - really_input_string ic len) +let read_all ic = + let len = in_channel_length ic in + really_input_string ic len + +let read_file fn = with_file_in fn ~f:read_all let lines_of_file fn = with_file_in fn ~f:input_lines ~binary:false let write_file fn data = with_file_out fn ~f:(fun oc -> output_string oc data) +let write_lines fn lines = + with_file_out fn ~f:(fun oc -> + List.iter ~f:(fun line -> + output_string oc line; + output_string oc "\n" + ) lines + ) + let copy_channels = let buf_len = 65536 in let buf = Bytes.create buf_len in diff --git a/src/io.mli b/src/io.mli index 9ea95a0e..227dbfb9 100644 --- a/src/io.mli +++ b/src/io.mli @@ -20,6 +20,10 @@ val write_file : string -> string -> unit val compare_files : string -> string -> Ordering.t +val write_lines : string -> string list -> unit + val copy_channels : in_channel -> out_channel -> unit val copy_file : src:string -> dst:string -> unit + +val read_all : in_channel -> string diff --git a/src/stdune/string.ml b/src/stdune/string.ml index 55bde18d..fdd727de 100644 --- a/src/stdune/string.ml +++ b/src/stdune/string.ml @@ -22,6 +22,8 @@ let break s ~pos = (sub s ~pos:0 ~len:pos, sub s ~pos ~len:(length s - pos)) +let is_empty s = length s = 0 + let is_prefix s ~prefix = let len = length s in let prefix_len = length prefix in @@ -79,6 +81,11 @@ let lsplit2 s ~on = (sub s ~pos:0 ~len:i, sub s ~pos:(i + 1) ~len:(length s - i - 1)) +let lsplit2_exn s ~on = + match lsplit2 s ~on with + | Some s -> s + | None -> invalid_arg "lsplit2_exn" + let rsplit2 s ~on = match rindex s on with | exception Not_found -> None @@ -153,3 +160,12 @@ let longest_map l ~f = max acc (length (f x))) let longest l = longest_map l ~f:(fun x -> x) + +let exists s ~f = + try + for i=0 to length s - 1 do + if (f s.[i]) then raise_notrace Exit + done; + false + with Exit -> + true diff --git a/src/stdune/string.mli b/src/stdune/string.mli index df7914e4..48e45275 100644 --- a/src/stdune/string.mli +++ b/src/stdune/string.mli @@ -4,6 +4,7 @@ val compare : t -> t -> Ordering.t val break : t -> pos:int -> t * t +val is_empty : t -> bool val is_prefix : t -> prefix:t -> bool val is_suffix : t -> suffix:t -> bool @@ -22,6 +23,7 @@ val extract_comma_space_separated_words : t -> t list val extract_blank_separated_words : t -> t list val lsplit2 : t -> on:char -> (t * t) option +val lsplit2_exn : t -> on:char -> t * t val rsplit2 : t -> on:char -> (t * t) option val split : t -> on:char -> t list @@ -35,3 +37,5 @@ val escape_double_quote : t -> t (** Return the length of the longest string in the list *) val longest : string list -> int val longest_map : 'a list -> f:('a -> string) -> int + +val exists : t -> f:(char -> bool) -> bool