add std::string to number funtion
This commit is contained in:
parent
012d44d958
commit
bac6287497
@ -3,6 +3,8 @@
|
|||||||
|
|
||||||
#include <charconv>
|
#include <charconv>
|
||||||
#include <expected>
|
#include <expected>
|
||||||
|
#include <type_traits>
|
||||||
|
#include <string_view>
|
||||||
|
|
||||||
//use for to_chars
|
//use for to_chars
|
||||||
constexpr size_t buffer_size = 32;
|
constexpr size_t buffer_size = 32;
|
||||||
@ -18,6 +20,18 @@ namespace toolkit{
|
|||||||
}
|
}
|
||||||
return std::string(buffer, res.ptr - buffer);
|
return std::string(buffer, res.ptr - buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename T = double>// requires std::is_same_v<T, std::string>
|
||||||
|
std::expected<T, std::string> stoi(const std::string& str){
|
||||||
|
T value;
|
||||||
|
|
||||||
|
auto res = std::from_chars(str.c_str(), str.c_str() + str.size(), value);
|
||||||
|
if(res.ec != std::errc()){
|
||||||
|
return std::unexpected(std::make_error_code(res.ec).message());
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -4,8 +4,8 @@ using namespace std;
|
|||||||
|
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
double num = .2;
|
std::string num = "2";
|
||||||
auto str = toolkit::itos(num);
|
auto str = toolkit::stoi(num);
|
||||||
cout<<str.value()<<"\n";
|
cout<<*str<<"\n";
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user