ligo/lib_stdlib/tzString.ml

76 lines
2.2 KiB
OCaml
Raw Normal View History

(**************************************************************************)
(* *)
(* Copyright (c) 2014 - 2017. *)
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
(* *)
(* All rights reserved. No warranty, explicit or implicit, provided. *)
(* *)
(**************************************************************************)
module Map = Map.Make (String)
let split delim ?(dup = true) ?(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
if dup then
do_slashes acc limit (i + 1)
else
do_split 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 has_prefix ~prefix s =
let x = String.length prefix in
let n = String.length s in
n >= x && String.sub s 0 x = prefix
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 common_prefix s1 s2 =
let last = min (String.length s1) (String.length s2) in
let rec loop i =
if last <= i then last
else if s1.[i] = s2.[i] then
loop (i+1)
else
i in
loop 0
let mem_char s c =
match String.index s c with
| exception Not_found -> false
| _ -> true