diff options
author | Björn Gustavsson <[email protected]> | 2011-09-04 09:27:50 +0200 |
---|---|---|
committer | Björn Gustavsson <[email protected]> | 2011-11-07 15:37:57 +0100 |
commit | 3d5cdb216fa5652bd8552ef4fa4eee310e543b42 (patch) | |
tree | f67e89f132af01b005129ccefab9e04cc8b2ee42 /lib/stdlib/src/filename.erl | |
parent | 44d3cf7625aa869829c59567d8fc6403eb0a1518 (diff) | |
download | otp-3d5cdb216fa5652bd8552ef4fa4eee310e543b42.tar.gz otp-3d5cdb216fa5652bd8552ef4fa4eee310e543b42.tar.bz2 otp-3d5cdb216fa5652bd8552ef4fa4eee310e543b42.zip |
Optimize filename:basename/1 to produce less garbage
In most cases, we can simply return a tail of the flattened
filename.
Diffstat (limited to 'lib/stdlib/src/filename.erl')
-rw-r--r-- | lib/stdlib/src/filename.erl | 17 |
1 files changed, 10 insertions, 7 deletions
diff --git a/lib/stdlib/src/filename.erl b/lib/stdlib/src/filename.erl index b9665d7dec..2fc9128e4e 100644 --- a/lib/stdlib/src/filename.erl +++ b/lib/stdlib/src/filename.erl @@ -147,9 +147,10 @@ basename(Name) when is_binary(Name) -> end; basename(Name0) -> - Name = flatten(Name0), + Name1 = flatten(Name0), {DirSep2, DrvSep} = separators(), - basename1(skip_prefix(Name, DrvSep), [], DirSep2). + Name = skip_prefix(Name1, DrvSep), + basename1(Name, Name, DirSep2). win_basenameb(<<Letter,$:,Rest/binary>>) when ?IS_DRIVELETTER(Letter) -> basenameb(Rest,[<<"/">>,<<"\\">>]); @@ -167,16 +168,18 @@ basenameb(Bin,Sep) -> -basename1([$/|[]], Tail, DirSep2) -> - basename1([], Tail, DirSep2); +basename1([$/], Tail0, _DirSep2) -> + %% End of filename -- must get rid of trailing directory separator. + [_|Tail] = lists:reverse(Tail0), + lists:reverse(Tail); basename1([$/|Rest], _Tail, DirSep2) -> - basename1(Rest, [], DirSep2); + basename1(Rest, Rest, DirSep2); basename1([DirSep2|Rest], Tail, DirSep2) when is_integer(DirSep2) -> basename1([$/|Rest], Tail, DirSep2); basename1([Char|Rest], Tail, DirSep2) when is_integer(Char) -> - basename1(Rest, [Char|Tail], DirSep2); + basename1(Rest, Tail, DirSep2); basename1([], Tail, _DirSep2) -> - lists:reverse(Tail). + Tail. skip_prefix(Name, false) -> Name; |