diff --git a/src/ncurses_ui.cc b/src/ncurses_ui.cc index e275bbf0..a4ec7586 100644 --- a/src/ncurses_ui.cc +++ b/src/ncurses_ui.cc @@ -904,16 +904,14 @@ void NCursesUI::set_ui_options(const Options& options) auto wheel_down_it = options.find("ncurses_wheel_down_button"); if (wheel_down_it == options.end()) m_wheel_down_button = 2; - else try { - m_wheel_down_button = str_to_int(wheel_down_it->second);; - } catch(...) {} + else if (auto down = str_to_int_ifp(wheel_down_it->second)) + m_wheel_down_button = *down; auto wheel_up_it = options.find("ncurses_wheel_up_button"); if (wheel_up_it == options.end()) m_wheel_up_button = 4; - else try { - m_wheel_up_button = str_to_int(wheel_up_it->second);; - } catch(...) {} + else if (auto up = str_to_int_ifp(wheel_up_it->second)) + m_wheel_up_button = *up; } } diff --git a/src/optional.hh b/src/optional.hh index 57147b43..67b2352a 100644 --- a/src/optional.hh +++ b/src/optional.hh @@ -1,6 +1,8 @@ #ifndef optional_hh_INCLUDED #define optional_hh_INCLUDED +#include "assert.hh" + namespace Kakoune { diff --git a/src/string.cc b/src/string.cc index bc02b4a3..2735f087 100644 --- a/src/string.cc +++ b/src/string.cc @@ -98,7 +98,7 @@ String indent(StringView str, StringView indent) return res; } -int str_to_int(StringView str) +Optional str_to_int_ifp(StringView str) { unsigned int res = 0; bool negative = false; @@ -110,11 +110,18 @@ int str_to_int(StringView str) else if (c >= '0' and c <= '9') res = res * 10 + c - '0'; else - throw runtime_error(str + "is not a number"); + return {}; } return negative ? -(int)res : (int)res; } +int str_to_int(StringView str) +{ + if (auto val = str_to_int_ifp(str)) + return *val; + throw str + " is not a number"; +} + InplaceString<16> to_string(int val) { InplaceString<16> res; diff --git a/src/string.hh b/src/string.hh index da235744..b5ad8e5a 100644 --- a/src/string.hh +++ b/src/string.hh @@ -1,11 +1,12 @@ #ifndef string_hh_INCLUDED #define string_hh_INCLUDED +#include "array_view.hh" +#include "hash.hh" +#include "optional.hh" #include "units.hh" #include "utf8.hh" -#include "hash.hh" #include "vector.hh" -#include "array_view.hh" #include #include @@ -256,7 +257,8 @@ inline String codepoint_to_str(Codepoint cp) return str; } -int str_to_int(StringView str); +int str_to_int(StringView str); // throws on error +Optional str_to_int_ifp(StringView str); template struct InplaceString