add num type(BIN HEX etc..)

This commit is contained in:
Jie 2025-01-24 15:21:15 +08:00
parent 05eb9e5e6f
commit 53bb8c0c5d

View File

@ -29,8 +29,15 @@ concept _num_type = requires {
namespace toolkit namespace toolkit
{ {
enum class num_type{
BIN,
HEX,
OCT,
DEC
}
template <typename T> template <typename T>
std::expected<std::string, std::string> to_string(T value) inline std::expected<std::string, std::string> to_string(T value)
{ {
if constexpr (std::is_same_v<T, int> || std::is_same_v<T, float> || std::is_same_v<T, double>) if constexpr (std::is_same_v<T, int> || std::is_same_v<T, float> || std::is_same_v<T, double>)
{ {
@ -50,7 +57,7 @@ namespace toolkit
} }
template <typename T = double> // requires std::is_same_v<T, std::string> template <typename T = double> // requires std::is_same_v<T, std::string>
std::expected<T, std::string> stoi(const std::string &str) inline std::expected<T, std::string> stoi(const std::string &str)
{ {
T value; T value;
auto res = std::from_chars(str.c_str(), str.c_str() + str.size(), value); auto res = std::from_chars(str.c_str(), str.c_str() + str.size(), value);
@ -63,7 +70,7 @@ namespace toolkit
template <typename T> template <typename T>
requires std::is_same_v<std::string, T> || std::is_same_v<const char *, T> requires std::is_same_v<std::string, T> || std::is_same_v<const char *, T>
std::string replace_string(const std::string &str, T d, T e) inline std::string replace_string(const std::string &str, T d, T e)
{ {
std::string result = str; std::string result = str;
if (d == e) if (d == e)
@ -93,7 +100,7 @@ namespace toolkit
template <typename T = std::string> template <typename T = std::string>
requires std::is_same_v<T, std::string> || requires std::is_same_v<T, std::string> ||
std::is_same_v<T, std::string_view> std::is_same_v<T, std::string_view>
std::vector<T> split(T str, T d) inline std::vector<T> split(T str, T d)
{ {
auto v = views::split(str, d) | views::transform([](auto word) auto v = views::split(str, d) | views::transform([](auto word)
{ return T(word.begin(), word.end()); }); { return T(word.begin(), word.end()); });