mirror of
https://github.com/boostorg/locale.git
synced 2026-01-19 04:22:08 +00:00
Fix handling of + prefix in try_to_int
This commit is contained in:
@@ -14,10 +14,13 @@
|
||||
namespace boost { namespace locale { namespace util {
|
||||
|
||||
template<typename Integer>
|
||||
bool try_to_int(const string_view s, Integer& value)
|
||||
bool try_to_int(string_view s, Integer& value)
|
||||
{
|
||||
if(s.empty())
|
||||
return false;
|
||||
if(s.size() >= 2 && s[0] == '+') {
|
||||
if(s[1] == '-') // "+-" is not allowed, invalid "+<number>" is detected by parser
|
||||
return false;
|
||||
s.remove_prefix(1);
|
||||
}
|
||||
const auto res = boost::charconv::from_chars(s, value);
|
||||
return res && res.ptr == (s.data() + s.size());
|
||||
}
|
||||
|
||||
@@ -24,6 +24,9 @@ void test_try_to_int()
|
||||
if TEST(try_to_int("-1337", v))
|
||||
TEST_EQ(v, -1337);
|
||||
|
||||
if TEST(try_to_int("+1337", v))
|
||||
TEST_EQ(v, +1337);
|
||||
|
||||
std::ostringstream ss;
|
||||
ss.imbue(std::locale::classic());
|
||||
empty_stream(ss) << std::numeric_limits<int>::min();
|
||||
@@ -34,6 +37,12 @@ void test_try_to_int()
|
||||
TEST_EQ(v, std::numeric_limits<int>::max());
|
||||
|
||||
TEST(!try_to_int("", v));
|
||||
TEST(!try_to_int("+", v));
|
||||
TEST(!try_to_int("-", v));
|
||||
TEST(!try_to_int("++", v));
|
||||
TEST(!try_to_int("+-", v));
|
||||
TEST(!try_to_int("++1", v));
|
||||
TEST(!try_to_int("+-1", v));
|
||||
TEST(!try_to_int("a", v));
|
||||
TEST(!try_to_int("1.", v));
|
||||
TEST(!try_to_int("1a", v));
|
||||
|
||||
Reference in New Issue
Block a user