Last active
February 18, 2017 07:18
-
-
Save cystbear/8eaeee2643e5062d198124db9f658d8e to your computer and use it in GitHub Desktop.
Normalize path exercise by @vlm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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