Skip to content

Commit a2538b3

Browse files
committed
add a slow normalize path
1 parent 1832627 commit a2538b3

10 files changed

+472
-5
lines changed

jscomp/bin/bsb.ml

+65
Original file line numberDiff line numberDiff line change
@@ -952,6 +952,8 @@ val module_name_of_file_if_any : string -> string
952952
*)
953953
val combine : string -> string -> string
954954

955+
val normalize_absolute_path : string -> string
956+
955957
end = struct
956958
#1 "ext_filename.ml"
957959
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
@@ -1191,6 +1193,69 @@ let combine p1 p2 =
11911193
Filename.concat p1 p2
11921194
else p2
11931195

1196+
1197+
1198+
(**
1199+
{[
1200+
split_aux "//ghosg//ghsogh/";;
1201+
- : string * string list = ("/", ["ghosg"; "ghsogh"])
1202+
]}
1203+
*)
1204+
let split_aux p =
1205+
let rec go p acc =
1206+
let dir = Filename.dirname p in
1207+
if dir = p then dir, acc
1208+
else go dir (Filename.basename p :: acc)
1209+
in go p []
1210+
1211+
1212+
1213+
1214+
(*TODO: could be hgighly optimized later
1215+
{[
1216+
normalize_absolute_path "/gsho/./..";;
1217+
1218+
normalize_absolute_path "/a/b/../c../d/e/f";;
1219+
1220+
normalize_absolute_path "/gsho/./..";;
1221+
1222+
normalize_absolute_path "/gsho/./../..";;
1223+
1224+
normalize_absolute_path "/a/b/c/d";;
1225+
1226+
normalize_absolute_path "/a/b/c/d/";;
1227+
1228+
normalize_absolute_path "/a/";;
1229+
1230+
normalize_absolute_path "/a";;
1231+
]}
1232+
*)
1233+
let normalize_absolute_path x =
1234+
let drop_if_exist xs =
1235+
match xs with
1236+
| [] -> []
1237+
| _ :: xs -> xs in
1238+
let rec normalize_list acc paths =
1239+
match paths with
1240+
| [] -> acc
1241+
| "." :: xs -> normalize_list acc xs
1242+
| ".." :: xs ->
1243+
normalize_list (drop_if_exist acc ) xs
1244+
1245+
| x :: xs ->
1246+
normalize_list (x::acc) xs
1247+
in
1248+
let root, paths = split_aux x in
1249+
let rev_paths = normalize_list [] paths in
1250+
let rec go acc rev_paths =
1251+
match rev_paths with
1252+
| [] -> Filename.concat root acc
1253+
| last::rest -> go (Filename.concat last acc ) rest in
1254+
match rev_paths with
1255+
| [] -> root
1256+
| last :: rest -> go last rest
1257+
1258+
11941259
end
11951260
module String_map : sig
11961261
#1 "string_map.mli"

jscomp/bin/bsdep.ml

+65
Original file line numberDiff line numberDiff line change
@@ -3747,6 +3747,8 @@ val module_name_of_file_if_any : string -> string
37473747
*)
37483748
val combine : string -> string -> string
37493749

3750+
val normalize_absolute_path : string -> string
3751+
37503752
end = struct
37513753
#1 "ext_filename.ml"
37523754
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
@@ -3986,6 +3988,69 @@ let combine p1 p2 =
39863988
Filename.concat p1 p2
39873989
else p2
39883990

3991+
3992+
3993+
(**
3994+
{[
3995+
split_aux "//ghosg//ghsogh/";;
3996+
- : string * string list = ("/", ["ghosg"; "ghsogh"])
3997+
]}
3998+
*)
3999+
let split_aux p =
4000+
let rec go p acc =
4001+
let dir = Filename.dirname p in
4002+
if dir = p then dir, acc
4003+
else go dir (Filename.basename p :: acc)
4004+
in go p []
4005+
4006+
4007+
4008+
4009+
(*TODO: could be hgighly optimized later
4010+
{[
4011+
normalize_absolute_path "/gsho/./..";;
4012+
4013+
normalize_absolute_path "/a/b/../c../d/e/f";;
4014+
4015+
normalize_absolute_path "/gsho/./..";;
4016+
4017+
normalize_absolute_path "/gsho/./../..";;
4018+
4019+
normalize_absolute_path "/a/b/c/d";;
4020+
4021+
normalize_absolute_path "/a/b/c/d/";;
4022+
4023+
normalize_absolute_path "/a/";;
4024+
4025+
normalize_absolute_path "/a";;
4026+
]}
4027+
*)
4028+
let normalize_absolute_path x =
4029+
let drop_if_exist xs =
4030+
match xs with
4031+
| [] -> []
4032+
| _ :: xs -> xs in
4033+
let rec normalize_list acc paths =
4034+
match paths with
4035+
| [] -> acc
4036+
| "." :: xs -> normalize_list acc xs
4037+
| ".." :: xs ->
4038+
normalize_list (drop_if_exist acc ) xs
4039+
4040+
| x :: xs ->
4041+
normalize_list (x::acc) xs
4042+
in
4043+
let root, paths = split_aux x in
4044+
let rev_paths = normalize_list [] paths in
4045+
let rec go acc rev_paths =
4046+
match rev_paths with
4047+
| [] -> Filename.concat root acc
4048+
| last::rest -> go (Filename.concat last acc ) rest in
4049+
match rev_paths with
4050+
| [] -> root
4051+
| last :: rest -> go last rest
4052+
4053+
39894054
end
39904055
module String_map : sig
39914056
#1 "string_map.mli"

jscomp/bin/bsppx.ml

+65
Original file line numberDiff line numberDiff line change
@@ -7568,6 +7568,8 @@ val module_name_of_file_if_any : string -> string
75687568
*)
75697569
val combine : string -> string -> string
75707570

7571+
val normalize_absolute_path : string -> string
7572+
75717573
end = struct
75727574
#1 "ext_filename.ml"
75737575
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
@@ -7807,6 +7809,69 @@ let combine p1 p2 =
78077809
Filename.concat p1 p2
78087810
else p2
78097811

7812+
7813+
7814+
(**
7815+
{[
7816+
split_aux "//ghosg//ghsogh/";;
7817+
- : string * string list = ("/", ["ghosg"; "ghsogh"])
7818+
]}
7819+
*)
7820+
let split_aux p =
7821+
let rec go p acc =
7822+
let dir = Filename.dirname p in
7823+
if dir = p then dir, acc
7824+
else go dir (Filename.basename p :: acc)
7825+
in go p []
7826+
7827+
7828+
7829+
7830+
(*TODO: could be hgighly optimized later
7831+
{[
7832+
normalize_absolute_path "/gsho/./..";;
7833+
7834+
normalize_absolute_path "/a/b/../c../d/e/f";;
7835+
7836+
normalize_absolute_path "/gsho/./..";;
7837+
7838+
normalize_absolute_path "/gsho/./../..";;
7839+
7840+
normalize_absolute_path "/a/b/c/d";;
7841+
7842+
normalize_absolute_path "/a/b/c/d/";;
7843+
7844+
normalize_absolute_path "/a/";;
7845+
7846+
normalize_absolute_path "/a";;
7847+
]}
7848+
*)
7849+
let normalize_absolute_path x =
7850+
let drop_if_exist xs =
7851+
match xs with
7852+
| [] -> []
7853+
| _ :: xs -> xs in
7854+
let rec normalize_list acc paths =
7855+
match paths with
7856+
| [] -> acc
7857+
| "." :: xs -> normalize_list acc xs
7858+
| ".." :: xs ->
7859+
normalize_list (drop_if_exist acc ) xs
7860+
7861+
| x :: xs ->
7862+
normalize_list (x::acc) xs
7863+
in
7864+
let root, paths = split_aux x in
7865+
let rev_paths = normalize_list [] paths in
7866+
let rec go acc rev_paths =
7867+
match rev_paths with
7868+
| [] -> Filename.concat root acc
7869+
| last::rest -> go (Filename.concat last acc ) rest in
7870+
match rev_paths with
7871+
| [] -> root
7872+
| last :: rest -> go last rest
7873+
7874+
78107875
end
78117876
module Js_config : sig
78127877
#1 "js_config.mli"

jscomp/bin/whole_compiler.ml

+65
Original file line numberDiff line numberDiff line change
@@ -21360,6 +21360,8 @@ val module_name_of_file_if_any : string -> string
2136021360
*)
2136121361
val combine : string -> string -> string
2136221362

21363+
val normalize_absolute_path : string -> string
21364+
2136321365
end = struct
2136421366
#1 "ext_filename.ml"
2136521367
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
@@ -21599,6 +21601,69 @@ let combine p1 p2 =
2159921601
Filename.concat p1 p2
2160021602
else p2
2160121603

21604+
21605+
21606+
(**
21607+
{[
21608+
split_aux "//ghosg//ghsogh/";;
21609+
- : string * string list = ("/", ["ghosg"; "ghsogh"])
21610+
]}
21611+
*)
21612+
let split_aux p =
21613+
let rec go p acc =
21614+
let dir = Filename.dirname p in
21615+
if dir = p then dir, acc
21616+
else go dir (Filename.basename p :: acc)
21617+
in go p []
21618+
21619+
21620+
21621+
21622+
(*TODO: could be hgighly optimized later
21623+
{[
21624+
normalize_absolute_path "/gsho/./..";;
21625+
21626+
normalize_absolute_path "/a/b/../c../d/e/f";;
21627+
21628+
normalize_absolute_path "/gsho/./..";;
21629+
21630+
normalize_absolute_path "/gsho/./../..";;
21631+
21632+
normalize_absolute_path "/a/b/c/d";;
21633+
21634+
normalize_absolute_path "/a/b/c/d/";;
21635+
21636+
normalize_absolute_path "/a/";;
21637+
21638+
normalize_absolute_path "/a";;
21639+
]}
21640+
*)
21641+
let normalize_absolute_path x =
21642+
let drop_if_exist xs =
21643+
match xs with
21644+
| [] -> []
21645+
| _ :: xs -> xs in
21646+
let rec normalize_list acc paths =
21647+
match paths with
21648+
| [] -> acc
21649+
| "." :: xs -> normalize_list acc xs
21650+
| ".." :: xs ->
21651+
normalize_list (drop_if_exist acc ) xs
21652+
21653+
| x :: xs ->
21654+
normalize_list (x::acc) xs
21655+
in
21656+
let root, paths = split_aux x in
21657+
let rev_paths = normalize_list [] paths in
21658+
let rec go acc rev_paths =
21659+
match rev_paths with
21660+
| [] -> Filename.concat root acc
21661+
| last::rest -> go (Filename.concat last acc ) rest in
21662+
match rev_paths with
21663+
| [] -> root
21664+
| last :: rest -> go last rest
21665+
21666+
2160221667
end
2160321668
module Js_config : sig
2160421669
#1 "js_config.mli"

jscomp/ext/ext_filename.ml

+63
Original file line numberDiff line numberDiff line change
@@ -234,3 +234,66 @@ let combine p1 p2 =
234234
if Filename.is_relative p2 then
235235
Filename.concat p1 p2
236236
else p2
237+
238+
239+
240+
(**
241+
{[
242+
split_aux "//ghosg//ghsogh/";;
243+
- : string * string list = ("/", ["ghosg"; "ghsogh"])
244+
]}
245+
*)
246+
let split_aux p =
247+
let rec go p acc =
248+
let dir = Filename.dirname p in
249+
if dir = p then dir, acc
250+
else go dir (Filename.basename p :: acc)
251+
in go p []
252+
253+
254+
255+
256+
(*TODO: could be hgighly optimized later
257+
{[
258+
normalize_absolute_path "/gsho/./..";;
259+
260+
normalize_absolute_path "/a/b/../c../d/e/f";;
261+
262+
normalize_absolute_path "/gsho/./..";;
263+
264+
normalize_absolute_path "/gsho/./../..";;
265+
266+
normalize_absolute_path "/a/b/c/d";;
267+
268+
normalize_absolute_path "/a/b/c/d/";;
269+
270+
normalize_absolute_path "/a/";;
271+
272+
normalize_absolute_path "/a";;
273+
]}
274+
*)
275+
let normalize_absolute_path x =
276+
let drop_if_exist xs =
277+
match xs with
278+
| [] -> []
279+
| _ :: xs -> xs in
280+
let rec normalize_list acc paths =
281+
match paths with
282+
| [] -> acc
283+
| "." :: xs -> normalize_list acc xs
284+
| ".." :: xs ->
285+
normalize_list (drop_if_exist acc ) xs
286+
287+
| x :: xs ->
288+
normalize_list (x::acc) xs
289+
in
290+
let root, paths = split_aux x in
291+
let rev_paths = normalize_list [] paths in
292+
let rec go acc rev_paths =
293+
match rev_paths with
294+
| [] -> Filename.concat root acc
295+
| last::rest -> go (Filename.concat last acc ) rest in
296+
match rev_paths with
297+
| [] -> root
298+
| last :: rest -> go last rest
299+

jscomp/ext/ext_filename.mli

+2
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,5 @@ val module_name_of_file_if_any : string -> string
7979
2. when the second one is absolute, drop the first one
8080
*)
8181
val combine : string -> string -> string
82+
83+
val normalize_absolute_path : string -> string

0 commit comments

Comments
 (0)