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."));
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."));