Skip to content
Snippets Groups Projects
Commit dc158931 authored by Adrien Béraud's avatar Adrien Béraud
Browse files

string utils: add to_int

Change-Id: I8c3ef6c6aee6b326bf917d3bcb8fc53857072cc3
parent 426c064f
No related branches found
No related tags found
No related merge requests found
......@@ -27,6 +27,7 @@
#include <algorithm>
#include <regex>
#include <iterator>
#include <charconv>
#ifdef _WIN32
#include <WTypes.h>
......@@ -53,6 +54,21 @@ std::string to_string(const std::wstring& wstr, int codePage = CP_UTF8);
std::string to_hex_string(uint64_t id);
uint64_t from_hex_string(const std::string& str);
template<typename T>
T
to_int(std::string_view str)
{
T result;
auto [p, ec] = std::from_chars(str.data(), str.data()+str.size(), result);
if (ec == std::errc())
return result;
if (ec == std::errc::invalid_argument)
throw std::invalid_argument("Can't parse integer: invalid_argument");
else if (ec == std::errc::result_out_of_range)
throw std::out_of_range("Can't parse integer: out of range");
throw std::system_error(std::make_error_code(ec));
}
static inline int
stoi(const std::string& str)
{
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment