]> https://gitweb.dealii.org/ - dealii.git/commitdiff
fix handling of whitespace in ParameterHandler
authorTimo Heister <timo.heister@gmail.com>
Sat, 11 Jul 2015 23:02:02 +0000 (19:02 -0400)
committerTimo Heister <timo.heister@gmail.com>
Sun, 12 Jul 2015 18:52:23 +0000 (14:52 -0400)
No longer remove multiple spaces inside the 'set' expression, but only
at the beginning and end of the lines and before after '='.

source/base/parameter_handler.cc
tests/bits/parameter_handler_21.output

index d25b6f92a523caaa0d186fbeb5367143fdc6bda0..bdbd71e4165aa270e461b34139c2f2575366132a 100644 (file)
@@ -2522,25 +2522,18 @@ ParameterHandler::scan_line (std::string         line,
   // if there is a comment, delete it
   if (line.find('#') != std::string::npos)
     line.erase (line.find("#"), std::string::npos);
-  // replace every whitespace sequence
-  // by " "
+
+  // replace \t by space:
   while (line.find('\t') != std::string::npos)
     line.replace (line.find('\t'), 1, " ");
-  while (line.find("  ") != std::string::npos)
-    line.erase (line.find("  "), 1);
-  // now every existing whitespace
-  // should be exactly one ' ';
-  // if at beginning: delete
-  if ((line.length() != 0) && (std::isspace (line[0])))
-    line.erase (0, 1);
+
+  //trim start and end:
+  line = Utilities::trim(line);
+
   // if line is now empty: leave
   if (line.length() == 0)
     return true;
 
-  // also delete spaces at the end
-  if (std::isspace (line[line.length()-1]))
-    line.erase (line.size()-1, 1);
-
   // enter subsection
   if ((line.find ("SUBSECTION ") == 0) ||
       (line.find ("subsection ") == 0))
@@ -2548,7 +2541,7 @@ ParameterHandler::scan_line (std::string         line,
       // delete this prefix
       line.erase (0, std::string("subsection").length()+1);
 
-      const std::string subsection = line;
+      const std::string subsection = Utilities::trim(line);
 
       // check whether subsection exists
       if (!entries->get_child_optional (get_current_full_path(subsection)))
@@ -2575,7 +2568,7 @@ ParameterHandler::scan_line (std::string         line,
       (line.find ("end") == 0))
     {
       line.erase (0, 3);
-      while ((line.size() > 0) && (line[0] == ' '))
+      while ((line.size() > 0) && (std::isspace(line[0])))
         line.erase (0, 1);
 
       if (line.size()>0)
@@ -2605,17 +2598,21 @@ ParameterHandler::scan_line (std::string         line,
   if ((line.find ("SET ") == 0) ||
       (line.find ("set ") == 0))
     {
-      // erase "set" statement and eliminate
-      // spaces around the '='
+      // erase "set" statement
       line.erase (0, 4);
-      if (line.find(" =") != std::string::npos)
-        line.replace (line.find(" ="), 2, "=");
-      if (line.find("= ") != std::string::npos)
-        line.replace (line.find("= "), 2, "=");
 
-      // extract entry name and value
-      std::string entry_name  (line, 0, line.find('='));
-      std::string entry_value (line, line.find('=')+1, std::string::npos);
+      std::string::size_type pos = line.find("=");
+      if (pos == std::string::npos)
+        {
+          std::cerr << "Line <" << lineno
+                    << "> of file <" << input_filename
+                    << ">: invalid format of set expression!" << std::endl;
+          return false;
+        }
+
+      // extract entry name and value and trim
+      std::string entry_name = Utilities::trim(std::string(line, 0, pos));
+      std::string entry_value = Utilities::trim(std::string(line, pos+1, std::string::npos));
 
       // resolve aliases before we look up the entry. if necessary, print
       // a warning that the alias is deprecated
@@ -2688,8 +2685,7 @@ ParameterHandler::scan_line (std::string         line,
   if ((line.find ("INCLUDE ") == 0) ||
       (line.find ("include ") == 0))
     {
-      // erase "set" statement and eliminate
-      // spaces around the '='
+      // erase "include " statement and eliminate spaces
       line.erase (0, 7);
       while ((line.size() > 0) && (line[0] == ' '))
         line.erase (0, 1);
index cd1140f6759949b6ec852c9917ba21d8b1ed6722..4e9d8382473e9de87eda723f583c30c1bf814fa1 100644 (file)
@@ -9,5 +9,5 @@ DEAL::defaults='' defined='bla|bla 2|1' input='set v=bla,bla,bla' result='bla,bl
 DEAL::defaults='default,alsodefault' defined='default|nodefault|alsodefault' input='' result='default,alsodefault'
 DEAL::defaults='default,alsodefault' defined='default|nodefault|alsodefault' input='set v=nodefault' result='nodefault'
 DEAL::defaults='  input 2  ,  have spaces  ' defined='have spaces|input 2' input='' result='  input 2  ,  have spaces  '
-DEAL::defaults='input 2' defined='have spaces|input 2' input='set v=   input 2  ,   have spaces  ' result='input 2 , have spaces'
-DEAL::defaults='' defined='double  spaces|input 2' input='set v = double  spaces  ,  double  spaces' result=''
+DEAL::defaults='input 2' defined='have spaces|input 2' input='set v=   input 2  ,   have spaces  ' result='input 2  ,   have spaces'
+DEAL::defaults='' defined='double  spaces|input 2' input='set v = double  spaces  ,  double  spaces' result='double  spaces  ,  double  spaces'

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.