Skip to content

Instantly share code, notes, and snippets.

@cystbear
Last active February 18, 2017 07:18
Show Gist options
  • Save cystbear/8eaeee2643e5062d198124db9f658d8e to your computer and use it in GitHub Desktop.
Save cystbear/8eaeee2643e5062d198124db9f658d8e to your computer and use it in GitHub Desktop.
Normalize path exercise by @vlm
-module(vlm).
-export([normalize_path/1]).
normalize_path(Path) ->
Suffix = case string:right(Path, 1) of
"/" -> "/";
_ -> ""
end,
"/" ++ string:join(lists:reverse(lists:foldl(
fun(Token,Acc) ->
case Token of
"." -> Acc;
".." -> case Acc of []->[];_->tl(Acc) end;
Tok -> [Tok|Acc]
end
end, [], string:tokens(Path, "/")
)), "/") ++ Suffix.
1> vlm:normalize_path("../bar").
"/bar"
2> vlm:normalize_path("/foo/bar").
"/foo/bar"
3> vlm:normalize_path("/foo/bar/../baz").
"/foo/baz"
4> vlm:normalize_path("/foo/bar/./baz/").
"/foo/bar/baz/"
5> vlm:normalize_path("/foo/../../baz").
"/baz"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment