toolkit/include/toolkit.h

137 lines
3.7 KiB
C
Raw Permalink Normal View History

2025-01-03 18:17:59 +08:00
#ifndef TOOLKIT_H
#define TOOLKIT_H
#include <charconv>
2025-01-06 14:51:33 +08:00
#include <cstring>
2025-01-03 18:17:59 +08:00
#include <expected>
2025-01-04 12:42:03 +08:00
#include <type_traits>
2025-01-06 14:11:58 +08:00
#include <string>
2025-01-06 14:51:33 +08:00
#include <cstdlib>
2025-01-06 14:11:58 +08:00
#include <ios>
2025-01-06 17:44:00 +08:00
#include <vector>
#include <ranges>
2025-01-07 15:05:57 +08:00
#include <cmath>
#include <chrono>
2025-01-03 18:17:59 +08:00
2025-01-06 17:44:00 +08:00
namespace ranges = std::ranges;
namespace views = std::ranges::views;
// use for to_chars
2025-01-03 18:17:59 +08:00
constexpr size_t buffer_size = 32;
2025-01-06 14:11:58 +08:00
template <typename T>
concept _num_type = requires {
std::is_same_v<T, int> ||
2025-01-06 17:44:00 +08:00
std::is_same_v<T, float> ||
std::is_same_v<T, long> ||
std::is_same_v<T, double>;
2025-01-06 14:11:58 +08:00
};
2025-01-06 17:44:00 +08:00
namespace toolkit
{
2025-01-24 15:21:15 +08:00
enum class num_type{
BIN,
HEX,
OCT,
DEC
2025-02-05 10:03:53 +08:00
};
2025-01-24 15:21:15 +08:00
2025-01-06 17:44:00 +08:00
template <typename T>
2025-01-24 15:21:15 +08:00
inline std::expected<std::string, std::string> to_string(T value)
2025-01-06 17:44:00 +08:00
{
if constexpr (std::is_same_v<T, int> || std::is_same_v<T, float> || std::is_same_v<T, double>)
{
2025-01-06 14:11:58 +08:00
char buffer[buffer_size];
auto res = std::to_chars(buffer, buffer + buffer_size, value);
2025-01-06 17:44:00 +08:00
if (res.ec != std::errc())
{
2025-01-06 14:11:58 +08:00
return std::unexpected(std::make_error_code(res.ec).message());
}
return std::string(buffer, res.ptr - buffer);
2025-01-06 17:44:00 +08:00
}
else if constexpr (std::is_same_v<typename std::remove_const<T>::type,
char *>)
{
return std::to_string(value);
2025-01-03 18:17:59 +08:00
}
}
2025-01-06 14:11:58 +08:00
2025-01-06 17:44:00 +08:00
template <typename T = double> // requires std::is_same_v<T, std::string>
2025-01-24 15:21:15 +08:00
inline std::expected<T, std::string> stoi(const std::string &str)
2025-01-06 17:44:00 +08:00
{
2025-01-04 12:42:03 +08:00
T value;
auto res = std::from_chars(str.c_str(), str.c_str() + str.size(), value);
2025-01-06 17:44:00 +08:00
if (res.ec != std::errc())
{
2025-01-04 12:42:03 +08:00
return std::unexpected(std::make_error_code(res.ec).message());
}
return value;
}
2025-01-06 14:51:33 +08:00
template <typename T>
2025-01-06 17:44:00 +08:00
requires std::is_same_v<std::string, T> || std::is_same_v<const char *, T>
2025-01-24 15:21:15 +08:00
inline std::string replace_string(const std::string &str, T d, T e)
2025-01-06 17:44:00 +08:00
{
2025-01-06 14:51:33 +08:00
std::string result = str;
2025-01-06 17:44:00 +08:00
if (d == e)
{
2025-01-06 14:51:33 +08:00
return result;
}
size_t len = 0;
2025-01-06 17:44:00 +08:00
while (true)
{
2025-01-06 14:58:59 +08:00
auto pos = result.find(d);
2025-01-06 17:44:00 +08:00
if (pos == std::string::npos)
{
2025-01-06 14:51:33 +08:00
return result;
}
2025-01-06 17:44:00 +08:00
if constexpr (std::is_same_v<T, const char *>)
{
2025-01-06 14:51:33 +08:00
len = std::strlen(d);
2025-01-06 17:44:00 +08:00
}
else
{
2025-01-06 14:51:33 +08:00
len = d.length();
}
2025-01-06 17:44:00 +08:00
result = result.replace(pos, len, e);
2025-01-06 14:51:33 +08:00
}
}
2025-01-06 17:44:00 +08:00
template <typename T = std::string>
requires std::is_same_v<T, std::string> ||
std::is_same_v<T, std::string_view>
2025-01-24 15:21:15 +08:00
inline std::vector<T> split(T str, T d)
2025-01-06 17:44:00 +08:00
{
auto v = views::split(str, d) | views::transform([](auto word)
{ return T(word.begin(), word.end()); });
return std::vector<T>(v.begin(), v.end());
}
2025-01-07 15:05:57 +08:00
template<typename T>
requires std::is_same_v<T, double> || std::is_same_v<T, float>
double round(T value, int c){
2025-01-07 15:05:57 +08:00
auto temp = 1;
for(int i=0;i<c;i++){
temp=temp*10;
}
return std::round(value*temp)/temp;
}
2025-01-06 17:44:00 +08:00
2025-01-20 16:39:18 +08:00
inline std::string get_time_now(){
auto time_now = std::chrono::system_clock::now();
auto now_c = std::chrono::system_clock::to_time_t(time_now);
char buffer[32];
#if defined(_WIN32) || defined(_WIN64)
auto _ = ctime_s(buffer, 32, &now_c);
#elif defined(__linux__)
auto _ = ctime_r(&now_c, buffer);
#endif
auto result = std::string(buffer);
result = replace_string(result, "\n", "");
return std::move(result);
}
2025-01-03 18:17:59 +08:00
}
#endif