Reimplement Utils.filename_to_relative

Summary: Do not base implementation on string prefixes.

Reviewed By: jeremydubreil

Differential Revision: D2958759

fbshipit-source-id: 344c491
master
Josh Berdine 8 years ago committed by Facebook Github Bot
parent 9f30059397
commit fb9fed3441

@ -183,16 +183,14 @@ let filename_to_absolute fname =
(** Convert an absolute filename to one relative to the current directory. *) (** Convert an absolute filename to one relative to the current directory. *)
let filename_to_relative root fname = let filename_to_relative root fname =
let string_strict_subtract s1 s2 = let rec relativize_if_under origin target =
let n1, n2 = String.length s1, String.length s2 in match origin, target with
if n1 < n2 && String.sub s2 ~pos:0 ~len:n1 = s1 then | x :: xs, y :: ys when x = y -> relativize_if_under xs ys
String.sub s2 ~pos:(n1 + 1) ~len:(n2 - (n1 + 1)) | [], [] -> "."
else s2 in | [], ys -> Filename.of_parts ys
let norm_root = (* norm_root is root without any trailing / *) | _ -> fname
Filename.dirname root ^/ Filename.basename root in in
let remainder = (* remove the path prefix to root including trailing / *) relativize_if_under (Filename.parts root) (Filename.parts fname)
string_strict_subtract norm_root fname in
remainder
let directory_fold f init path = let directory_fold f init path =

Loading…
Cancel
Save