test defs: unindent without temporary file
authorPeter Rosin <peda@lysator.liu.se>
Tue, 1 Mar 2011 07:25:24 +0000 (08:25 +0100)
committerPeter Rosin <peda@lysator.liu.se>
Tue, 1 Mar 2011 07:25:24 +0000 (08:25 +0100)
* 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.

Signed-off-by: Peter Rosin <peda@lysator.liu.se>
ChangeLog
tests/defs.in

index 47bc9fc..6f67d36 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2011-03-01  Peter Rosin  <peda@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.
+
 2011-02-26  Stefano Lattarini  <stefano.lattarini@gmail.com>
 
        libtool: suggest LT_INIT if LTLIBRARIES primary is used
index fd0cc9c..2685112 100644 (file)
@@ -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.