Commits

Anonymous committed 4324d91

uri: segments of path, normalizing segments

Comments (0)

Files changed (2)

   [ _ -> None ]
 ;
 
+
+
+value seg_of_path path =
+  let spl = ExtString.String.split_exact ( ( = ) '/' ) path in
+  inner spl
+  where rec inner spl =
+    match spl with
+    [ [ "" ] -> spl
+    | [ "" :: tl ] -> inner tl
+    | _ -> spl
+    ]
+;
+
+
+value normalize_seg segs =
+  if List.exists (fun s -> s = "." || s = "..") segs
+  then
+    inner [] segs
+    where rec inner acc = fun
+      [ [] -> List.rev acc
+      | ["." :: tl] -> inner acc tl
+      | [".." :: tl] ->
+          let new_acc =
+            match acc with
+            [ [] -> []
+            | [_ :: tl] -> tl
+            ]
+          in
+            inner new_acc tl
+      | [hd :: tl] -> inner [hd :: acc] tl
+      ]
+  else
+    segs
+;
+
+
+value normseg_of_path path =
+  normalize_seg (seg_of_path path)
+;
+
+value normseg_of_uri uri = normseg_of_path uri.path
+;
+
 
 value dump_uri : uri -> string
 ;
+
+(* uri.path -- segmented, normalized *)
+value normseg_of_uri : uri -> list string
+;