From c3d53d588dedb007b26809d85187027d1eba8183 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Fri, 17 May 2013 14:09:42 +0200 Subject: [PATCH] revive str_to_int so that the good exception type is thrown on error --- src/commands.cc | 4 ++-- src/highlighters.cc | 2 +- src/input_handler.cc | 6 +++--- src/option_types.hh | 4 ++-- src/string.cc | 12 ++++++++++++ src/string.hh | 1 + 6 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/commands.cc b/src/commands.cc index 7f3818534..8540bc2cb 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -125,9 +125,9 @@ void edit(const CommandParameters& params, Context& context) 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 ? - 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 })); if (context.has_window()) diff --git a/src/highlighters.cc b/src/highlighters.cc index 1cab851f9..9bbcc7aa9 100644 --- a/src/highlighters.cc +++ b/src/highlighters.cc @@ -134,7 +134,7 @@ HighlighterAndId colorize_regex_factory(const HighlighterParameters params, cons throw runtime_error("wrong colorspec: '" + *it + "' expected :[,]"); - int capture = stoi(res[1].str()); + int capture = str_to_int(res[1].str()); const ColorPair*& color = colors[capture]; color = &get_color(res[2].str()); } diff --git a/src/input_handler.cc b/src/input_handler.cc index 7f3387c16..875618ce1 100644 --- a/src/input_handler.cc +++ b/src/input_handler.cc @@ -536,17 +536,17 @@ static BufferCompletion complete_opt(const BufferIterator& pos, OptionManager& o boost::smatch match; 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)) return {}; BufferIterator beg{pos.buffer(), coord}; BufferIterator end = beg; if (match[3].matched) { - ByteCount len = stoi(match[3].str()); + ByteCount len = str_to_int(match[3].str()); 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; for (auto it = opt.begin() + 1; it != opt.end(); ++it) diff --git a/src/option_types.hh b/src/option_types.hh index ac7e4c32d..c9a2d7f31 100644 --- a/src/option_types.hh +++ b/src/option_types.hh @@ -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 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 String option_to_string(bool opt) { return opt ? "true" : "false"; } @@ -154,7 +154,7 @@ inline String option_to_string(const StronglyTypedNumber& o template inline void option_from_string(const String& str, StronglyTypedNumber& opt) { - opt = StronglyTypedNumber{stoi(str)}; + opt = StronglyTypedNumber{str_to_int(str)}; } template diff --git a/src/string.cc b/src/string.cc index e7dd167f7..2c180e50d 100644 --- a/src/string.cc +++ b/src/string.cc @@ -23,6 +23,18 @@ std::vector split(const String& str, char separator) 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) { return String{re.str()}; diff --git a/src/string.hh b/src/string.hh index e579b7417..fb80d4253 100644 --- a/src/string.hh +++ b/src/string.hh @@ -101,6 +101,7 @@ inline String codepoint_to_str(Codepoint cp) String option_to_string(const Regex& re); void option_from_string(const String& str, Regex& re); +int str_to_int(const String& str); using std::to_string;