[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Reimplementing AS_BASENAME
From: |
Eric Blake |
Subject: |
Reimplementing AS_BASENAME |
Date: |
Thu, 9 Oct 2008 21:10:53 +0000 (UTC) |
User-agent: |
Loom/3.14 (http://gmane.org/) |
Paolo Bonzini <bonzini <at> gnu.org> writes:
> > I like this idea. It's been on my mental to-do list for quite some
> > time, and it's an obvious improvement. We can't simply use "${1##*/}",
> > though, as that mishandles the basenames of strings like "a/b/" and "///".
>
> So that would rule out no-fork AS_BASENAME, unless we can rely also on
> extglob and ${1%%*(/)}. Or unless a case statement and a rarely
> executed while loop would be faster than a fork:
>
> case $x in
> */) while :; do
> x=${x%/}
> case $x in */) ;; *) break ;;
> esac; done
> esac
>
> I honestly have no idea if this is the case.
Trust me - on cygwin, even a while loop is faster than forking. But who says
we need a while loop, or even multiple case statements?
as_func_basename ()
{
test "x$1" = "x--" && shift
case $1 in
/ | // ) result=$1 ;; # root dir corner cases
*[!/]*/ ) # trailing slash, but not root
result=${1##*[!/]} # compute the suffix of slashes
result=${1%"$result"} # strip the trailing slashes
result=${result##*/} ;; # compute the basename
*/ ) result=/ ;; # collapse alternate spelling of root
* ) result=${result##*/} ;; # no trailing slash
esac
# add code here for stripping $2, if wanted
}
(Okay, since this is m4, I'd need to use [[!/]] instead of [!/]. But no one
has reported a testsuite failure on "Negated classes in globbing", and if we
were really paranoid about the portability of this, we could make the check for
XSI parameter expansion also check for negated class globs).
--
Eric Blake
Re: [PATCH] more cleanup before adding shell functions, Paul Eggert, 2008/10/09