-
Notifications
You must be signed in to change notification settings - Fork 0
/
relative_path.ml
80 lines (61 loc) · 1.9 KB
/
relative_path.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
(**
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the "hack" directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*)
open Utils
type prefix =
| Root
| Hhi
| Dummy
let root = ref None
let hhi = ref None
let path_ref_of_prefix = function
| Root -> root
| Hhi -> hhi
| Dummy -> ref (Some "")
let path_of_prefix x =
unsafe_opt_note "Prefix has not been set!" !(path_ref_of_prefix x)
let string_of_prefix = function
| Root -> "root"
| Hhi -> "hhi"
| Dummy -> ""
let set_path_prefix prefix v =
let v = Fbpath.to_string v in
assert (String.length v > 0);
(* Ensure that there is a trailing slash *)
let v =
if str_ends_with v Filename.dir_sep then v
else v ^ Filename.dir_sep
in
match prefix with
| Dummy -> raise (Failure "Dummy is always represented by an empty string")
| _ -> path_ref_of_prefix prefix := Some v
type t = prefix * string
let prefix (p : t) = fst p
let suffix (p : t) = snd p
let default = (Dummy, "")
module S = struct
type path = t
type t = path
let compare = Pervasives.compare
(* We could have simply used Marshal.to_string here, but this does slightly
* better on space usage. *)
let to_string (p, rest) = string_of_prefix p ^ "|" ^ rest
end
module Set = Set.Make(S)
module Map = Utils.MyMap(S)
let to_absolute (p, rest) = path_of_prefix p ^ rest
let create prefix s =
let prefix_s = path_of_prefix prefix in
let prefix_len = String.length prefix_s in
if not (str_starts_with s prefix_s)
then raise (Failure (Printf.sprintf "%s is not a prefix of %s" prefix_s s));
prefix, String.sub s prefix_len (String.length s - prefix_len)
let concat prefix s = prefix, s
let relativize_set prefix m =
SSet.fold (fun k a -> Set.add (create prefix k) a) m Set.empty