Den 2011-02-21 21:23 skrev Ralf Wildenhues: > * Stefano Lattarini wrote on Mon, Feb 21, 2011 at 03:11:27PM CET: >> On Monday 21 February 2011, Ralf Wildenhues wrote: >>> It will be unintelligible without the comments, however. > >>> gnulib-tool has a sed comment removal, search for >>> $sed_comments. What I wouldn't like is invoking an extra sed in each >>> test startup just to remove comments from a helper function, that will >>> noticeably slow down things >>> >> Or the comments could be stripped lazily upon the first invocation of >> 'unindent'; e.g.: > > Good idea.
How about this? Should the sed command to strip comments be "s/ *#.*//", or does that fall into this bucket: Unicos 9 sed loops endlessly on patterns like ‘.*\n.*’. Cheers, Peter >From cf3433b2c3ac7e79839138d61b3c1f26ab325457 Mon Sep 17 00:00:00 2001 From: Peter Rosin <p...@lysator.liu.se> Date: Mon, 28 Feb 2011 10:57:49 +0100 Subject: [PATCH] test defs: unindent without temporary file * tests/defs.in (commented_sed_unindent_prog): Commented Sed program that strips the "proper" amount of leading whitespace. (unindent): Lazily strip comments from the above program and use it to unindent without using a temporary file. * tests/unindent.test: New test. * tests/Makefile.am (TESTS): Update. Signed-off-by: Peter Rosin <p...@lysator.liu.se> --- ChangeLog | 10 ++++++++ tests/Makefile.am | 1 + tests/Makefile.in | 1 + tests/defs.in | 27 +++++++++++++++------ tests/unindent.test | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 94 insertions(+), 8 deletions(-) create mode 100755 tests/unindent.test diff --git a/ChangeLog b/ChangeLog index 8d03512..0b4f0dd 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +2011-02-28 Peter Rosin <p...@lysator.liu.se> + + test defs: unindent without temporary file + * tests/defs.in (commented_sed_unindent_prog): Commented Sed program + that strips the "proper" amount of leading whitespace. + (unindent): Lazily strip comments from the above program and use it + to unindent without using a temporary file. + * tests/unindent.test: New test. + * tests/Makefile.am (TESTS): Update. + 2011-02-20 Stefano Lattarini <stefano.lattar...@gmail.com> tests: tempdirs with restrictive permissions are cleaned correctly diff --git a/tests/Makefile.am b/tests/Makefile.am index 047bc7b..e60c8d9 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -780,6 +780,7 @@ txinfo32.test \ txinfo33.test \ transform.test \ transform2.test \ +unindent.test \ unused.test \ upc.test \ upc2.test \ diff --git a/tests/Makefile.in b/tests/Makefile.in index fe6238b..3238d17 100644 --- a/tests/Makefile.in +++ b/tests/Makefile.in @@ -1050,6 +1050,7 @@ txinfo32.test \ txinfo33.test \ transform.test \ transform2.test \ +unindent.test \ unused.test \ upc.test \ upc2.test \ diff --git a/tests/defs.in b/tests/defs.in index fd0cc9c..89facff 100644 --- a/tests/defs.in +++ b/tests/defs.in @@ -440,6 +440,21 @@ AUTOMAKE_fails () AUTOMAKE_run 1 ${1+"$@"} } +commented_sed_unindent_prog=' + /^$/b # Nothing to do for empty lines. + x # Get x<indent> into pattern space. + /^$/{ # No prior x<indent>, go prepare it. + g # Copy this 1st non-blank line into pattern space. + s/^\(['"$tab"' ]*\).*/x\1/ # Prepare x<indent> in pattern space. + } # Now: x<indent> in pattern and <line> in hold. + G # Build x<indent>\n<line> in pattern space, and + h # duplicate it into hold space. + s/\n.*$// # Restore x<indent> in pattern space, and + x # exchange with the above duplicate in hold space. + s/^x\(.*\)\n\1// # Remove leading <indent> from <line>. + s/^x.*\n// # Restore <line> when there is no leading <indent>. +' + # unindent [input files...] # ------------------------- # Remove the "proper" amount of leading whitespace from the given files, @@ -448,14 +463,10 @@ AUTOMAKE_fails () # files. If no input file is specified, standard input is implied. unindent () { - cat ${1+"$@"} > deindent.tmp - indentation=`sed <deindent.tmp -n " - /[^ $tab].*$/{ - s///p - q - }"` - sed "s/^$indentation//" deindent.tmp - rm -f deindent.tmp + if test x"$sed_unindent_prog" = x; then + sed_unindent_prog=`echo "$commented_sed_unindent_prog" | sed -e "s/ #.*//"` + fi + sed "$sed_unindent_prog" ${1+"$@"} } # Turn on shell traces. diff --git a/tests/unindent.test b/tests/unindent.test new file mode 100755 index 0000000..a52896e --- /dev/null +++ b/tests/unindent.test @@ -0,0 +1,63 @@ +#! /bin/sh +# Copyright (C) 2011 Free Software Foundation, Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2, or (at your option) +# any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +# Make sure the `unindent' subroutine behaves properly. + +. ./defs || Exit 1 + +set -e + +cat << EOF | unindent > result1 + hello + world +EOF + +grep "^hello" result1 +grep "^world" result1 + + +cat << EOF | unindent > result2 + hello + world + nice +EOF + +grep "^hello" result2 +grep "^ world" result2 +grep "^nice" result2 + + +cat << EOF | unindent > result3 + + hello + world + nice +EOF + +grep "^hello" result3 +grep "^ world" result3 +grep "^ nice" result3 + + +cat << EOF | unindent > result4 +hello + world +EOF + +grep "^hello" result4 +grep "^ world" result4 + +: -- 1.7.2.3