[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [PATCH] test defs: add subroutine for input unindenting
From: |
Stefano Lattarini |
Subject: |
Re: [PATCH] test defs: add subroutine for input unindenting |
Date: |
Wed, 16 Feb 2011 00:29:02 +0100 |
User-agent: |
KMail/1.13.3 (Linux/2.6.30-2-686; KDE/4.4.4; i686; ; ) |
Hello Ralf.
On Tuesday 15 February 2011, Ralf Wildenhues wrote:
> * Stefano Lattarini wrote on Tue, Feb 15, 2011 at 01:36:16PM CET:
> > OK, here's my shot at it. The implementation might be suboptimal,
> > but since it wasn't completely obvious to get right, I'd rather not
> > tweak it anymore, until there's a real need at least.
>
> Lemme tweak it for you. ;-)
>
I'm experiencing a problem with your suggestions (but I think this is
due to just a typo); see below.
> > (By the way, I think that we should really start testing non-obvious
> > subroutines and pieces of code defined in tests/defs. But that's
> > for another thread).
>
> Fine with me, on all accounts.
>
> > Subject: [PATCH] test defs: add subroutine for input unindenting
> >
> > * tests/defs.in (unindent): New subroutine.
>
> > --- a/tests/defs.in
> > +++ b/tests/defs.in
> > @@ -440,6 +440,31 @@ AUTOMAKE_fails ()
> > AUTOMAKE_run 1 ${1+"$@"}
> > }
> >
> > +# unindent [input files...]
> > +# -------------------------
> > +# Remove the "proper" amount of leading whitespace from the given files,
> > +# and output the result on stdout. That amount is determined by looking
> > +# at the leading whitespace of the first non-blank line in the input
> > +# files. If no input file is specified, standard input is implied.
> > +unindent ()
> > +{
> > + cat ${1+"$@"} > deindent.tmp
> > + indentation=`sed <deindent.tmp -n "
> > + /^[ $tab]*$/n
> > + s/[^ $tab].*$//
> > + t end
> > + b
> > +: end
> > + p
> > + q
> > + "`
>
> The space after : is not portable. The sed script could be shortened to
> sed -n "/[^ $tab].*$/{
> s///p
> d
> }"
>
Did you mean 'q' instead of 'd' here, right? Because the above fails
for me:
$ cat foo.sh
tab=' '
deindent ()
{
cat ${1+"$@"} > deindent.tmp
indentation=`sed <deindent.tmp -n "
/[^ $tab].*$/{
s///p
d
}"`
case $indentation in
'') cat deindent.tmp;;
*) sed "s/^$indentation//" deindent.tmp;;
esac
rm -f deindent.tmp
}
deindent "$@"
$ (echo ' x'; echo ' y') | LC_ALL=C sh -x foo.sh
+ tab=' '
+ deindent
+ cat
++ sed -n '
/[^ ].*$/{
s///p
d
}'
+ indentation='
'
+ case $indentation in
+ sed 's/^
//' deindent.tmp
sed: -e expression #1, char 4: unterminated `s' command
+ rm -f deindent.tmp
> > + case $indentation in
> > + '') cat deindent.tmp;;
> > + *) sed "s/^$indentation//" deindent.tmp;;
> > + esac
>
> These four lines can be shortened to
> sed "s/^$indentation//" deindent.tmp
>
Agreed on this.
> > + rm -f deindent.tmp
> > +}
>
> OK with those addressed.
>
> It's a bit of a shame this uses a temp file though. awk should be able
> to cope without
>
Yes, if I could assume "new" awk. But traditional awk lacks the 'sub()'
and 'gsub()' builtins, so I really really don't want to go down that road.
Moreover, the use of a tempfile is not a big issue here, because we can
already assume the existence of a temporary working directory (i.e., the
one the test runs in) and of a cleanup trap (installed by `tests/defs').
But if you truly truly dislike the use of the tempfile, I could try to
use perl instead... WDYT?
> (but be sure to try old Solaris awk if you go for it).
>
> Thanks,
> Ralf
>
Regards,
Stefano
- Re: [Ping PATCHES] {master} Optimize tests `instspc-*.test' for speed., Ralf Wildenhues, 2011/02/14
- Re: [PATCH] test defs: add subroutine for input unindenting, Peter Rosin, 2011/02/17
- Re: [PATCH] test defs: add subroutine for input unindenting, Peter Rosin, 2011/02/17
- Re: [PATCH] test defs: add subroutine for input unindenting, Stefano Lattarini, 2011/02/17
- Re: [PATCH] test defs: add subroutine for input unindenting, Peter Rosin, 2011/02/18
- Re: [PATCH] test defs: add subroutine for input unindenting, Peter Rosin, 2011/02/18
- Re: [PATCH] test defs: add subroutine for input unindenting, Ralf Wildenhues, 2011/02/21
- Re: [PATCH] test defs: add subroutine for input unindenting, Stefano Lattarini, 2011/02/21