These revisions were apparently lost in the svn-to-git conversion.
<h3>Specific improvements</h3>
<ol>
- <li> Fixed: Utilities::string_to_int() did not catch if the
- string given started with an integer but contained additional
- text. It now throws an exception if this happens.
+ <li> Fixed: Utilities::string_to_int() and
+ Utilities::string_to_double() did not catch if the
+ string given started with an integer or double but contained additional
+ text. They now throw an exception if this happens.
<br>
- (Wolfgang Bangerth, 2014/07/14)
+ (Wolfgang Bangerth, 2014/07/20)
</li>
<li> New: The function GridOut::write, can now be used also in
while ((s.size() > 0) && (s[s.size()-1] == ' '))
s.erase (s.end()-1);
+ // now convert and see whether we succeed. note that strtol only
+ // touches errno if an error occurred, so if we want to check
+ // whether an error happened, we need to make sure that errno==0
+ // before calling strtol since otherwise it may be that the
+ // conversion succeeds and that errno remains at the value it
+ // was before, whatever that was
char *p;
errno = 0;
const int i = std::strtol(s.c_str(), &p, 10);
double
- string_to_double (const std::string &s)
+ string_to_double (const std::string &s_)
{
- std::istringstream ss(s);
-
- double i = std::numeric_limits<double>::max();
- ss >> i;
-
- // check for errors
- AssertThrow (i != std::numeric_limits<double>::max(),
- ExcCantConvertString (s));
+ // trim whitespace on either side of the text if necessary
+ std::string s = s_;
+ while ((s.size() > 0) && (s[0] == ' '))
+ s.erase (s.begin());
+ while ((s.size() > 0) && (s[s.size()-1] == ' '))
+ s.erase (s.end()-1);
-//TODO: The test for errors above doesn't work, as can easily be
-//verified. furthermore, it doesn't catch cases like when calling
-//string_to_int("1.23.4") since it just reads in however much it can, without
-//realizing that there is more
+ // now convert and see whether we succeed. note that strtol only
+ // touches errno if an error occurred, so if we want to check
+ // whether an error happened, we need to make sure that errno==0
+ // before calling strtol since otherwise it may be that the
+ // conversion succeeds and that errno remains at the value it
+ // was before, whatever that was
+ char *p;
+ errno = 0;
+ const double d = std::strtod(s.c_str(), &p);
+ AssertThrow ( !((errno != 0) || (s.size() == 0) || ((s.size()>0) && (*p != '\0'))),
+ ExcMessage ("Can't convert <" + s + "> to an integer."));
- return i;
+ return d;
}
--- /dev/null
+// ---------------------------------------------------------------------
+// $Id$
+//
+// Copyright (C) 2005 - 2014 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.
+//
+// ---------------------------------------------------------------------
+
+
+// verify that Utilities::string_to_double actually catches errors
+
+#include "../tests.h"
+#include <iomanip>
+#include <iomanip>
+#include <fstream>
+#include <cmath>
+#include <sstream>
+
+#include <deal.II/base/utilities.h>
+
+using namespace dealii;
+
+
+
+
+void verify (const std::string &s)
+{
+ bool exception_caught = false;
+ try
+ {
+ Utilities::string_to_double(s);
+ }
+ catch (...)
+ {
+ exception_caught = true;
+ }
+ Assert (exception_caught == true, ExcMessage ("Function is broken!"));
+
+ deallog << "Done correctly: " << s << std::endl;
+}
+
+
+
+
+int main()
+{
+ std::ofstream logfile("output");
+ deallog.attach(logfile);
+ deallog.depth_console(0);
+ deallog.threshold_double(1.e-10);
+
+ verify ("abc");
+ verify ("1.23.4");
+ verify ("1 23 4");
+ verify ("123abc");
+}
--- /dev/null
+
+DEAL::Done correctly: abc
+DEAL::Done correctly: 1.23.4
+DEAL::Done correctly: 1 23 4
+DEAL::Done correctly: 123abc