From: Wolfgang Bangerth Date: Tue, 2 Jul 2019 15:09:39 +0000 (-0600) Subject: Add a couple of comments to functions in utilities.cc. X-Git-Tag: v9.2.0-rc1~1415^2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1b661d7f78fcce4a8b57a4a8ab0d15ad883e3c30;p=dealii.git Add a couple of comments to functions in utilities.cc. --- diff --git a/source/base/utilities.cc b/source/base/utilities.cc index 08f40984cb..fbf823f7be 100644 --- a/source/base/utilities.cc +++ b/source/base/utilities.cc @@ -494,15 +494,24 @@ namespace Utilities while ((s.size() > 0) && (s[s.size() - 1] == ' ')) s.erase(s.end() - 1); - // now convert and see whether we succeed. note that strtol only + // 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 + // was before, whatever that was. char *p; errno = 0; const int i = std::strtol(s.c_str(), &p, 10); + + // We have an error if one of the following conditions is true: + // - strtol sets errno != 0 + // - The original string was empty (we could have checked that + // earlier already) + // - The string has non-zero length and strtol converted the + // first part to something useful, but stopped converting short + // of the terminating '\0' character. This happens, for example, + // if the given string is "1234 abc". AssertThrow(!((errno != 0) || (s.size() == 0) || ((s.size() > 0) && (*p != '\0'))), ExcMessage("Can't convert <" + s + "> to an integer.")); @@ -533,15 +542,24 @@ namespace Utilities while ((s.size() > 0) && (s[s.size() - 1] == ' ')) s.erase(s.end() - 1); - // now convert and see whether we succeed. note that strtol only + // 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 + // was before, whatever that was. char *p; errno = 0; const double d = std::strtod(s.c_str(), &p); + + // We have an error if one of the following conditions is true: + // - strtod sets errno != 0 + // - The original string was empty (we could have checked that + // earlier already) + // - The string has non-zero length and strtod converted the + // first part to something useful, but stopped converting short + // of the terminating '\0' character. This happens, for example, + // if the given string is "1.234 abc". AssertThrow(!((errno != 0) || (s.size() == 0) || ((s.size() > 0) && (*p != '\0'))), ExcMessage("Can't convert <" + s + "> to a double."));