2 * Copyright (c) 2015, Facebook, Inc.
5 * This source code is licensed under the BSD-style license found in the
6 * LICENSE file in the "hack" directory of this source tree. An additional grant
7 * of patent rights can be found in the PATENTS file in the same directory.
12 open Reordered_argument_collections
27 let path_ref_of_prefix = function
31 | Dummy
-> ref (Some
"")
33 let path_of_prefix x
=
34 unsafe_opt_note
"Prefix has not been set!" !(path_ref_of_prefix x
)
36 let string_of_prefix = function
42 let set_path_prefix prefix v
=
43 let v = Path.to_string
v in
44 assert (String.length
v > 0);
45 (* Ensure that there is a trailing slash *)
47 if string_ends_with
v Filename.dir_sep
then v
48 else v ^
Filename.dir_sep
51 | Dummy
-> raise
(Failure
"Dummy is always represented by an empty string")
52 | _
-> path_ref_of_prefix prefix
:= Some
v
54 type relative_path
= prefix
* string
55 type t
= relative_path
57 let prefix (p
: t
) = fst p
59 let suffix (p
: t
) = snd p
61 let default = (Dummy
, "")
64 type t
= relative_path
66 let compare = Pervasives.compare
68 (* We could have simply used Marshal.to_string here, but this does slightly
69 * better on space usage. *)
70 let to_string (p
, rest
) = string_of_prefix p ^
"|" ^ rest
73 let to_absolute (p
, rest
) = path_of_prefix p ^ rest
75 let to_tmp (_
, rest
) = (Tmp
, rest
)
77 let to_root (_
, rest
) = (Root
, rest
)
79 let pp fmt rp
= Format.pp_print_string fmt
(S.to_string rp
)
81 module Set
= Reordered_argument_set
(Set.Make
(S
))
82 module Map
= Reordered_argument_map
(MyMap.Make
(S
))
85 let prefix_s = path_of_prefix prefix in
86 let prefix_len = String.length
prefix_s in
87 if not
(string_starts_with s
prefix_s)
89 Printf.eprintf
"%s is not a prefix of %s" prefix_s s
;
90 assert_false_log_backtrace None
;
92 prefix, String.sub s
prefix_len (String.length s
- prefix_len)
94 let from_root (s
: string) : t
= Root
, s
96 let relativize_set prefix m
=
97 SSet.fold m ~init
:Set.empty ~f
:(fun k a
-> Set.add a
(create prefix k
))
100 List.fold_left xs ~f
:Set.add ~init
:Set.empty