ligo/src/minutils/utils.ml

114 lines
3.4 KiB
OCaml
Raw Normal View History

2016-09-08 19:13:10 +02:00
(**************************************************************************)
(* *)
(* Copyright (c) 2014 - 2016. *)
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
(* *)
(* All rights reserved. No warranty, explicit or implicit, provided. *)
(* *)
(**************************************************************************)
module StringMap = Map.Make (String)
let split delim ?(limit = max_int) path =
let l = String.length path in
let rec do_slashes acc limit i =
if i >= l then
List.rev acc
else if String.get path i = delim then
do_slashes acc limit (i + 1)
else
do_split acc limit i
and do_split acc limit i =
if limit <= 0 then
if i = l then
List.rev acc
else
List.rev (String.sub path i (l - i) :: acc)
else
do_component acc (pred limit) i i
and do_component acc limit i j =
if j >= l then
if i = j then
List.rev acc
else
List.rev (String.sub path i (j - i) :: acc)
else if String.get path j = delim then
do_slashes (String.sub path i (j - i) :: acc) limit j
else
do_component acc limit i (j + 1) in
if limit > 0 then
do_slashes [] limit 0
else
[ path ]
let split_path path = split '/' path
let map_option ~f = function
| None -> None
| Some x -> Some (f x)
let apply_option ~f = function
| None -> None
| Some x -> f x
2016-09-08 19:13:10 +02:00
let iter_option ~f = function
| None -> ()
| Some x -> f x
let unopt x = function
| None -> x
| Some x -> x
let unopt_list l =
let may_cons xs x = match x with None -> xs | Some x -> x :: xs in
List.rev @@ List.fold_left may_cons [] l
2016-11-29 14:51:36 +01:00
let first_some a b = match a, b with
| None, None -> None
| None, Some v -> Some v
| Some v, _ -> Some v
2016-09-08 19:13:10 +02:00
let filter_map f l =
let may_cons xs x = match f x with None -> xs | Some x -> x :: xs in
List.rev @@ List.fold_left may_cons [] l
let display_paragraph ppf description =
Format.fprintf ppf "@[%a@]"
(fun ppf words -> List.iter (Format.fprintf ppf "%s@ ") words)
(split ' ' description)
let rec remove_elem_from_list nb = function
| [] -> []
| l when nb <= 0 -> l
| _ :: tl -> remove_elem_from_list (nb - 1) tl
2016-10-06 18:30:04 +02:00
let remove_prefix ~prefix s =
let x = String.length prefix in
let n = String.length s in
if n >= x && String.sub s 0 x = prefix then
Some (String.sub s x (n - x))
else
None
let finalize f g = try let res = f () in g (); res with exn -> g (); raise exn
let read_file ?(bin=false) fn =
let ic = (if bin then open_in_bin else open_in) fn in
finalize (fun () ->
let len = in_channel_length ic in
let buf = Bytes.create len in
let nb_read = input ic buf 0 len in
if nb_read <> len then failwith (Printf.sprintf "read_file: read %d, expected %d" nb_read len)
else Bytes.unsafe_to_string buf)
(fun () -> close_in ic)
let write_file ?(bin=false) fn contents =
let oc = (if bin then open_out_bin else open_out) fn in
finalize (fun () ->
let contents = Bytes.unsafe_of_string contents in
output oc contents 0 @@ Bytes.length contents
)
(fun () -> close_out oc)
let (<<) g f = fun a -> g (f a)