add split function
This commit is contained in:
parent
0e57341bec
commit
d12728ddb5
@ -8,65 +8,96 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <ios>
|
#include <ios>
|
||||||
|
#include <vector>
|
||||||
|
#include <ranges>
|
||||||
|
|
||||||
//use for to_chars
|
namespace ranges = std::ranges;
|
||||||
|
namespace views = std::ranges::views;
|
||||||
|
|
||||||
|
// use for to_chars
|
||||||
constexpr size_t buffer_size = 32;
|
constexpr size_t buffer_size = 32;
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
concept _num_type = requires {
|
concept _num_type = requires {
|
||||||
std::is_same_v<T, int> ||
|
std::is_same_v<T, int> ||
|
||||||
std::is_same_v<T, float> ||
|
std::is_same_v<T, float> ||
|
||||||
std::is_same_v<T, long> ||
|
std::is_same_v<T, long> ||
|
||||||
std::is_same_v<T, double>;
|
std::is_same_v<T, double>;
|
||||||
};
|
};
|
||||||
|
|
||||||
namespace toolkit{
|
namespace toolkit
|
||||||
template<typename T>
|
{
|
||||||
std::expected<std::string, std::string> to_string(T value){
|
template <typename T>
|
||||||
if constexpr (std::is_same_v<T, int> || std::is_same_v<T, float> || std::is_same_v<T, double>){
|
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>)
|
||||||
|
{
|
||||||
char buffer[buffer_size];
|
char buffer[buffer_size];
|
||||||
auto res = std::to_chars(buffer, buffer + buffer_size, value);
|
auto res = std::to_chars(buffer, buffer + buffer_size, value);
|
||||||
if (res.ec != std::errc()) {
|
if (res.ec != std::errc())
|
||||||
|
{
|
||||||
return std::unexpected(std::make_error_code(res.ec).message());
|
return std::unexpected(std::make_error_code(res.ec).message());
|
||||||
}
|
}
|
||||||
return std::string(buffer, res.ptr - buffer);
|
return std::string(buffer, res.ptr - buffer);
|
||||||
} else if constexpr (std::is_same_v<typename std::remove_const<T>::type,
|
}
|
||||||
char *>) {
|
else if constexpr (std::is_same_v<typename std::remove_const<T>::type,
|
||||||
return std::to_string(value);
|
char *>)
|
||||||
|
{
|
||||||
|
return std::to_string(value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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){
|
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);
|
||||||
if(res.ec != std::errc()){
|
if (res.ec != std::errc())
|
||||||
|
{
|
||||||
return std::unexpected(std::make_error_code(res.ec).message());
|
return std::unexpected(std::make_error_code(res.ec).message());
|
||||||
}
|
}
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
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){
|
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)
|
||||||
|
{
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
size_t len = 0;
|
size_t len = 0;
|
||||||
while(true){
|
while (true)
|
||||||
|
{
|
||||||
auto pos = result.find(d);
|
auto pos = result.find(d);
|
||||||
if(pos == std::string::npos){
|
if (pos == std::string::npos)
|
||||||
|
{
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
if constexpr(std::is_same_v<T, const char*>){
|
if constexpr (std::is_same_v<T, const char *>)
|
||||||
|
{
|
||||||
len = std::strlen(d);
|
len = std::strlen(d);
|
||||||
}else{
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
len = d.length();
|
len = d.length();
|
||||||
}
|
}
|
||||||
result = result.replace(pos, len ,e);
|
result = result.replace(pos, len, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename T = std::string>
|
||||||
|
requires std::is_same_v<T, std::string> ||
|
||||||
|
std::is_same_v<T, std::string_view>
|
||||||
|
std::vector<T> split(T str, T d)
|
||||||
|
{
|
||||||
|
auto v = views::split(str, d) | views::transform([](auto word)
|
||||||
|
{ return T(word.begin(), word.end()); });
|
||||||
|
return std::vector<T>(v.begin(), v.end());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user