aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorBjörn Gustavsson <[email protected]>2011-09-04 09:27:50 +0200
committerBjörn Gustavsson <[email protected]>2011-11-07 15:37:57 +0100
commit3d5cdb216fa5652bd8552ef4fa4eee310e543b42 (patch)
treef67e89f132af01b005129ccefab9e04cc8b2ee42 /lib
parent44d3cf7625aa869829c59567d8fc6403eb0a1518 (diff)
downloadotp-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')
-rw-r--r--lib/stdlib/src/filename.erl17
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;