Add str_to_int_ifp that returns an Optional<int> instead of throwing
This commit is contained in:
parent
d3607bc773
commit
d7159a9af0
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
#ifndef optional_hh_INCLUDED
|
||||
#define optional_hh_INCLUDED
|
||||
|
||||
#include "assert.hh"
|
||||
|
||||
namespace Kakoune
|
||||
{
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue
Block a user