revive str_to_int so that the good exception type is thrown on error

This commit is contained in:
Maxime Coste 2013-05-17 14:09:42 +02:00
parent 37a2363301
commit c3d53d588d
6 changed files with 21 additions and 8 deletions

View File

@ -125,9 +125,9 @@ void edit(const CommandParameters& params, Context& context)
if (param_count > 1) if (param_count > 1)
{ {
int line = std::max(0, stoi(parser[1]) - 1); int line = std::max(0, str_to_int(parser[1]) - 1);
int column = param_count > 2 ? int column = param_count > 2 ?
std::max(0, stoi(parser[2]) - 1) : 0; std::max(0, str_to_int(parser[2]) - 1) : 0;
context.editor().select(context.buffer().iterator_at({ line, column })); context.editor().select(context.buffer().iterator_at({ line, column }));
if (context.has_window()) if (context.has_window())

View File

@ -134,7 +134,7 @@ HighlighterAndId colorize_regex_factory(const HighlighterParameters params, cons
throw runtime_error("wrong colorspec: '" + *it + throw runtime_error("wrong colorspec: '" + *it +
"' expected <capture>:<fgcolor>[,<bgcolor>]"); "' expected <capture>:<fgcolor>[,<bgcolor>]");
int capture = stoi(res[1].str()); int capture = str_to_int(res[1].str());
const ColorPair*& color = colors[capture]; const ColorPair*& color = colors[capture];
color = &get_color(res[2].str()); color = &get_color(res[2].str());
} }

View File

@ -536,17 +536,17 @@ static BufferCompletion complete_opt(const BufferIterator& pos, OptionManager& o
boost::smatch match; boost::smatch match;
if (boost::regex_match(desc.begin(), desc.end(), match, re)) if (boost::regex_match(desc.begin(), desc.end(), match, re))
{ {
BufferCoord coord{ stoi(match[1].str()) - 1, stoi(match[2].str()) - 1 }; BufferCoord coord{ str_to_int(match[1].str()) - 1, str_to_int(match[2].str()) - 1 };
if (not pos.buffer().is_valid(coord)) if (not pos.buffer().is_valid(coord))
return {}; return {};
BufferIterator beg{pos.buffer(), coord}; BufferIterator beg{pos.buffer(), coord};
BufferIterator end = beg; BufferIterator end = beg;
if (match[3].matched) if (match[3].matched)
{ {
ByteCount len = stoi(match[3].str()); ByteCount len = str_to_int(match[3].str());
end = beg + len; end = beg + len;
} }
size_t timestamp = (size_t)stoi(match[4].str()); size_t timestamp = (size_t)str_to_int(match[4].str());
size_t longest_completion = 0; size_t longest_completion = 0;
for (auto it = opt.begin() + 1; it != opt.end(); ++it) for (auto it = opt.begin() + 1; it != opt.end(); ++it)

View File

@ -16,7 +16,7 @@ inline String option_to_string(const String& opt) { return opt; }
inline void option_from_string(const String& str, String& opt) { opt = str; } inline void option_from_string(const String& str, String& opt) { opt = str; }
inline String option_to_string(int opt) { return to_string(opt); } inline String option_to_string(int opt) { return to_string(opt); }
inline void option_from_string(const String& str, int& opt) { opt = stoi(str); } inline void option_from_string(const String& str, int& opt) { opt = str_to_int(str); }
inline bool option_add(int& opt, int val) { opt += val; return val != 0; } inline bool option_add(int& opt, int val) { opt += val; return val != 0; }
inline String option_to_string(bool opt) { return opt ? "true" : "false"; } inline String option_to_string(bool opt) { return opt ? "true" : "false"; }
@ -154,7 +154,7 @@ inline String option_to_string(const StronglyTypedNumber<RealType, ValueType>& o
template<typename RealType, typename ValueType> template<typename RealType, typename ValueType>
inline void option_from_string(const String& str, StronglyTypedNumber<RealType, ValueType>& opt) inline void option_from_string(const String& str, StronglyTypedNumber<RealType, ValueType>& opt)
{ {
opt = StronglyTypedNumber<RealType, ValueType>{stoi(str)}; opt = StronglyTypedNumber<RealType, ValueType>{str_to_int(str)};
} }
template<typename RealType, typename ValueType> template<typename RealType, typename ValueType>

View File

@ -23,6 +23,18 @@ std::vector<String> split(const String& str, char separator)
return res; return res;
} }
int str_to_int(const String& str)
{
try
{
return stoi(str);
}
catch (std::logic_error&)
{
throw runtime_error(str + "is not a number");
}
}
String option_to_string(const Regex& re) String option_to_string(const Regex& re)
{ {
return String{re.str()}; return String{re.str()};

View File

@ -101,6 +101,7 @@ inline String codepoint_to_str(Codepoint cp)
String option_to_string(const Regex& re); String option_to_string(const Regex& re);
void option_from_string(const String& str, Regex& re); void option_from_string(const String& str, Regex& re);
int str_to_int(const String& str);
using std::to_string; using std::to_string;