Instead of throwing return 0 for invalid values. This happens anyway if the input is invalid so it makes the most sense to return 0 for invalid ranges.

Originally committed to SVN as r5343.
This commit is contained in:
Amar Takhar 2011-02-10 01:36:25 +00:00
parent ef7b3c04e3
commit 2c6abb9a98
2 changed files with 4 additions and 7 deletions

View file

@ -44,11 +44,8 @@ void str_lower(std::string &str) {
int strtoi(std::string &str) {
long l = strtol(str.c_str(), NULL, 10);
if (errno == ERANGE)
throw("Cannot convert to long (invalid range)");
if ((l < INT_MIN) || (l > INT_MAX))
throw("Cannot convert to int (invalid range)");
if ((errno == ERANGE) || (l < INT_MIN) || (l > INT_MAX))
return 0;
return (int)l;
}

View file

@ -73,10 +73,10 @@ TEST_F(lagi_util, Utilstr_lower) {
TEST_F(lagi_util, UtilstrtoiInvalidRange) {
std::string str("2147483650");
EXPECT_ANY_THROW(util::strtoi(str));
EXPECT_EQ(util::strtoi(str), 0);
str.assign("-2147483650");
EXPECT_ANY_THROW(util::strtoi(str));
EXPECT_EQ(util::strtoi(str), 0);
}
TEST_F(lagi_util, UtilstrtoiInvalidString) {