]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Ignore surrounding whitespace if continuing lines.
authorDavid Wells <wellsd2@rpi.edu>
Fri, 22 Jan 2016 22:57:41 +0000 (17:57 -0500)
committerDavid Wells <wellsd2@rpi.edu>
Mon, 25 Jan 2016 03:44:43 +0000 (22:44 -0500)
This commit modifies the behavior present in both 23f306dfc5 (which did
not join lines if there was whitespace following a '\') and
0b962b1103 (which did not trim whitespace at the beginning of continued
lines).

We decided to remove whitespace at the beginning of a continued line
both because it was implemented that way in 23f306dfc5 (i.e., the 'fix'
in 0b962b1103 was wrong) and because it makes writing things
like (example provided by Alberto Sartori)

set Finite element space = FESystem[FE_Q(1)^d-\
                           FE_DGPMonomial(0)-\
                           FE_DGPMonomial(0)]

possible.

For the sake of posterity, here is part of the discussion (PR #2101)
which lead to the decision to remove trailing whitespace:

drwells:

I agree with @asartori86 that trailing whitespace is a mistake. I
suppose that I ultimately disagree because I think it is one that we
should 'forgive'. I think we are in agreement that we should join lines
if the last non-whitespace character is a `\`, then.

bangerth:

I agree, too, that trailing whitespace is a mistake. But there are two
options:

* Enforce it. In those cases you'll get an error about the next line not
  being what the parser expects, while the file looks completely correct
  visually. People will probably spend a long time rubbing their eyes
  before they realize the problem.

* Forgive it, at the cost of not being able to support valid cases where
  the user wants to have a backslash within the text, followed by only
  spaces. That seems like a rare case to happen in practice, and also a
  poor choice of syntax for whatever the user wanted to achieve.

From a usability standpoint, option 1 seems more appealing.

source/base/parameter_handler.cc
tests/bits/parameter_handler_backslash_06.cc [new file with mode: 0644]
tests/bits/parameter_handler_backslash_06.output [new file with mode: 0644]
tests/bits/parameter_handler_backslash_07.cc [new file with mode: 0644]
tests/bits/parameter_handler_backslash_07.output [new file with mode: 0644]
tests/bits/prm/parameter_handler_backslash_06.prm [new file with mode: 0644]
tests/bits/prm/parameter_handler_backslash_07.prm [new file with mode: 0644]

index f41185d0cab8da5e26107ab05bc1cdbc49161a5a..b8ae2f99dcaa017384f1b5f6af3b7ce372fae78b 100644 (file)
@@ -1346,6 +1346,9 @@ bool ParameterHandler::read_input (std::istream &input,
   while (std::getline (input, input_line))
     {
       ++current_line_n;
+      // Trim the whitespace at the ends of the line here instead of in
+      // scan_line. This makes the continuation line logic a lot simpler.
+      input_line = Utilities::trim (input_line);
 
       // check whether or not the current line should be joined with the next
       // line before calling scan_line.
diff --git a/tests/bits/parameter_handler_backslash_06.cc b/tests/bits/parameter_handler_backslash_06.cc
new file mode 100644 (file)
index 0000000..8678704
--- /dev/null
@@ -0,0 +1,59 @@
+// ---------------------------------------------------------------------
+//
+// Copyright (C) 2016 by the deal.II authors
+//
+// This file is part of the deal.II library.
+//
+// The deal.II library is free software; you can use it, redistribute
+// it, and/or modify it under the terms of the GNU Lesser General
+// Public License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+// The full text of the license can be found in the file LICENSE at
+// the top level of the deal.II distribution.
+//
+// ---------------------------------------------------------------------
+
+
+#include "../tests.h"
+#include <deal.II/base/logstream.h>
+#include <deal.II/base/parameter_handler.h>
+#include <fstream>
+
+
+int main ()
+{
+  std::ofstream logfile("output");
+  deallog.attach(logfile);
+  deallog.threshold_double(1.e-10);
+
+  for (unsigned int i = 0; i < 2; ++i)
+    {
+      ParameterHandler prm;
+      prm.enter_subsection ("Testing");
+      prm.declare_entry ("Function",
+                         "a",
+                         Patterns::List(Patterns::Selection("a|b|c|d|e|f|g|h")));
+      prm.leave_subsection ();
+
+      // test both relevant read_input functions
+      if (i == 0)
+        {
+          prm.read_input(SOURCE_DIR "/prm/parameter_handler_backslash_06.prm");
+        }
+      else
+        {
+          std::ifstream input_stream
+          (SOURCE_DIR "/prm/parameter_handler_backslash_06.prm");
+          prm.read_input(input_stream);
+        }
+
+      std::string list;
+      prm.enter_subsection ("Testing");
+      list = prm.get ("Function");
+      prm.leave_subsection ();
+
+      deallog << list << std::endl;
+    }
+
+  return 0;
+}
diff --git a/tests/bits/parameter_handler_backslash_06.output b/tests/bits/parameter_handler_backslash_06.output
new file mode 100644 (file)
index 0000000..5c8ecde
--- /dev/null
@@ -0,0 +1,3 @@
+
+DEAL::a, b, c
+DEAL::a, b, c
diff --git a/tests/bits/parameter_handler_backslash_07.cc b/tests/bits/parameter_handler_backslash_07.cc
new file mode 100644 (file)
index 0000000..765352f
--- /dev/null
@@ -0,0 +1,57 @@
+// ---------------------------------------------------------------------
+//
+// Copyright (C) 2016 by the deal.II authors
+//
+// This file is part of the deal.II library.
+//
+// The deal.II library is free software; you can use it, redistribute
+// it, and/or modify it under the terms of the GNU Lesser General
+// Public License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+// The full text of the license can be found in the file LICENSE at
+// the top level of the deal.II distribution.
+//
+// ---------------------------------------------------------------------
+
+
+#include "../tests.h"
+#include <deal.II/base/logstream.h>
+#include <deal.II/base/parameter_handler.h>
+#include <fstream>
+
+
+int main ()
+{
+  std::ofstream logfile("output");
+  deallog.attach(logfile);
+  deallog.threshold_double(1.e-10);
+
+  for (unsigned int i = 0; i < 2; ++i)
+    {
+      ParameterHandler prm;
+      prm.enter_subsection ("Testing");
+      prm.declare_entry ("value", "value", Patterns::Anything());
+      prm.leave_subsection ();
+
+      // test both relevant read_input functions
+      if (i == 0)
+        {
+          prm.read_input(SOURCE_DIR "/prm/parameter_handler_backslash_07.prm");
+        }
+      else
+        {
+          std::ifstream input_stream
+          (SOURCE_DIR "/prm/parameter_handler_backslash_07.prm");
+          prm.read_input(input_stream);
+        }
+
+      std::string list;
+      prm.enter_subsection ("Testing");
+      list = prm.get ("value");
+      prm.leave_subsection ();
+
+      deallog << list << std::endl;
+    }
+
+  return 0;
+}
diff --git a/tests/bits/parameter_handler_backslash_07.output b/tests/bits/parameter_handler_backslash_07.output
new file mode 100644 (file)
index 0000000..d56fbce
--- /dev/null
@@ -0,0 +1,3 @@
+
+DEAL::value
+DEAL::value
diff --git a/tests/bits/prm/parameter_handler_backslash_06.prm b/tests/bits/prm/parameter_handler_backslash_06.prm
new file mode 100644 (file)
index 0000000..0e25e84
--- /dev/null
@@ -0,0 +1,7 @@
+# Whitespace characters after '\'s are ignored.
+#----------------------------------------------
+subsection Testing # note the spaces and tab at the end of the next line.
+  set Function = a,\           
+                 b,\
+                 c
+end
diff --git a/tests/bits/prm/parameter_handler_backslash_07.prm b/tests/bits/prm/parameter_handler_backslash_07.prm
new file mode 100644 (file)
index 0000000..58f82e9
--- /dev/null
@@ -0,0 +1,7 @@
+# whitespace at the beginning of a line is always ignored.
+#---------------------------------------------------------
+subsection Testing
+  set value = val\
+              u\
+              e
+end

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.