diff options
| author | Maxime Coste <frrrwww@gmail.com> | 2015-05-01 18:47:22 +0100 |
|---|---|---|
| committer | Maxime Coste <frrrwww@gmail.com> | 2015-05-01 18:47:22 +0100 |
| commit | d7159a9af04d88fa2c741557ec436f08d62ef541 (patch) | |
| tree | bc53e8a10ff7c8c724c13b18385ab9f2ed32da33 /src | |
| parent | d3607bc77306c32a86d1b981f1022481ee4acd43 (diff) | |
Add str_to_int_ifp that returns an Optional<int> instead of throwing
Diffstat (limited to 'src')
| -rw-r--r-- | src/ncurses_ui.cc | 10 | ||||
| -rw-r--r-- | src/optional.hh | 2 | ||||
| -rw-r--r-- | src/string.cc | 11 | ||||
| -rw-r--r-- | src/string.hh | 8 |
4 files changed, 20 insertions, 11 deletions
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<int> 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 <string> #include <climits> @@ -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<int> str_to_int_ifp(StringView str); template<size_t N> struct InplaceString |
