Extend stdlib for configurator

This commit is contained in:
Rudi Grinberg 2018-02-28 23:31:24 +07:00
parent 45535f7afd
commit 9a6d9a3aa9
4 changed files with 37 additions and 4 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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