| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
let re_delim = Str.regexp (if Sys.win32 then "[/\\]+" else "/+" ) |
| 12 |
|
| 13 |
let to_relative_path : string -> string = fun full_path -> |
| 14 |
if Filename.is_relative full_path then full_path else |
| 15 |
let cwd = Str.split_delim re_delim (Sys.getcwd ()) in |
| 16 |
let path = Str.split_delim re_delim full_path in |
| 17 |
let rec remove_common_prefix l1 l2 = |
| 18 |
match (l1, l2) with |
| 19 |
| (x1 :: l1, x2 :: l2) when x1 = x2 -> remove_common_prefix l1 l2 |
| 20 |
| (_ , _ ) -> (l1, String.concat "/" l2) |
| 21 |
in |
| 22 |
let (cwd, path) = remove_common_prefix cwd path in |
| 23 |
let add_parent path _ = Filename.concat Filename.parent_dir_name path in |
| 24 |
List.fold_left add_parent path cwd |
| 25 |
|
| 26 |
let normalize_path : string -> string = fun path -> |
| 27 |
let path = Str.split_delim re_delim path in |
| 28 |
let rec normalize acc path = |
| 29 |
match (path, acc) with |
| 30 |
| ([] , _ ) -> List.rev acc |
| 31 |
| ("." :: path, _ ) -> normalize acc path |
| 32 |
| (".." :: path, [] ) -> normalize (".." :: []) path |
| 33 |
| (".." :: path, ".." :: _ ) -> normalize (".." :: acc) path |
| 34 |
| (".." :: path, _ :: acc) -> normalize acc path |
| 35 |
| (dir :: path, _ ) -> normalize (dir :: acc) path |
| 36 |
in |
| 37 |
match normalize [] path with |
| 38 |
| [] -> "." |
| 39 |
| path -> String.concat "/" path |
| •40 |
|
| 41 |
let is_prefix a b = |
| 42 |
let a = Str.split_delim re_delim a in |
| 43 |
let b = Str.split_delim re_delim b in |
| 44 |
let rec aux a b = |
| 45 |
match a, b with |
| 46 |
| [], _ -> true |
| 47 |
| x :: a, y :: b -> String.equal x y && aux a b |
| 48 |
| _ :: _, [] -> false |
| 49 |
in |
| 50 |
aux a b |