Make stoi_CountEmptyLinesAsMinimum more restrictive.

It now only accepts digits and possibly a single minus character as the first character.
Ordinary std::stoi has too much special magic, e.g. it converts "1 a" to "1".
This commit is contained in:
Andreas Jönsson 2015-05-09 12:49:47 +02:00
parent 0eca4db949
commit bcbe48b13f

View File

@ -757,6 +757,23 @@ int stoi_CountEmptyLinesAsMinimum(const generic_string &input)
}
else
{
// Check minus characters.
const int minuses = std::count(input.begin(), input.end(), TEXT('-'));
if (minuses > 1)
{
throw std::invalid_argument("More than one minus sign.");
}
else if (minuses == 1 && input[0] != TEXT('-'))
{
throw std::invalid_argument("Minus sign must be first.");
}
// Check for other characters which are not allowed.
if (input.find_first_not_of(TEXT("-0123456789")) != std::string::npos)
{
throw new std::invalid_argument("Invalid character found.");
}
return std::stoi(input);
}
}