结构调整
This commit is contained in:
parent
53bb8c0c5d
commit
78169021c3
BIN
.cache/clangd/index/main.cpp.DA64C8250A878209.idx
Normal file
BIN
.cache/clangd/index/main.cpp.DA64C8250A878209.idx
Normal file
Binary file not shown.
BIN
.cache/clangd/index/toolkit.h.B9107599F4B246CF.idx
Normal file
BIN
.cache/clangd/index/toolkit.h.B9107599F4B246CF.idx
Normal file
Binary file not shown.
15
.vscode/c_cpp_properties.json
vendored
Normal file
15
.vscode/c_cpp_properties.json
vendored
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
{
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"name": "Linux",
|
||||||
|
"includePath": [
|
||||||
|
"${workspaceFolder}/**"
|
||||||
|
],
|
||||||
|
"defines": [],
|
||||||
|
"compilerPath": "/usr/bin/clang",
|
||||||
|
"configurationProvider": "mvector-of-bool.cmake-tools",
|
||||||
|
"cppStandard": "c++23"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"version": 4
|
||||||
|
}
|
6
.vscode/settings.json
vendored
Normal file
6
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"files.associations": {
|
||||||
|
"*.cpp": "cpp",
|
||||||
|
"type_traits": "cpp"
|
||||||
|
}
|
||||||
|
}
|
0
include/ini.h
Normal file
0
include/ini.h
Normal file
9
include/pch.h
Normal file
9
include/pch.h
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
#ifndef PCH_H
|
||||||
|
#define PCH_H
|
||||||
|
|
||||||
|
#include <cstring>
|
||||||
|
#include <string>
|
||||||
|
#include <ios>
|
||||||
|
#include <ranges>
|
||||||
|
|
||||||
|
#endif
|
66
include/stringconv.h
Normal file
66
include/stringconv.h
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
#ifndef STRINGCONV_H
|
||||||
|
#define STRINGCONV_H
|
||||||
|
#include <type_traits>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <cmath>
|
||||||
|
#include <cstring>
|
||||||
|
#include <ranges>
|
||||||
|
|
||||||
|
namespace ranges = std::ranges;
|
||||||
|
namespace views = std::ranges::views;
|
||||||
|
constexpr size_t buffer_size = 32;
|
||||||
|
|
||||||
|
namespace string{
|
||||||
|
template <typename T>
|
||||||
|
requires std::is_same_v<std::string, T> || std::is_same_v<const char *, T>
|
||||||
|
inline std::string replace_string(const std::string &str, T d, T e)
|
||||||
|
{
|
||||||
|
std::string result = str;
|
||||||
|
if (d == e)
|
||||||
|
{
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
size_t len = 0;
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
auto pos = result.find(d);
|
||||||
|
if (pos == std::string::npos)
|
||||||
|
{
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
if constexpr (std::is_same_v<T, const char *>)
|
||||||
|
{
|
||||||
|
len = std::strlen(d);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
len = d.length();
|
||||||
|
}
|
||||||
|
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>
|
||||||
|
inline 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());
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
requires std::is_same_v<T, double> || std::is_same_v<T, float>
|
||||||
|
double round(T value, int c){
|
||||||
|
auto temp = 1;
|
||||||
|
for(int i=0;i<c;i++){
|
||||||
|
temp=temp*10;
|
||||||
|
}
|
||||||
|
return std::round(value*temp)/temp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
@ -1,136 +1,7 @@
|
|||||||
#ifndef TOOLKIT_H
|
#ifndef TOOLKIT_H
|
||||||
#define TOOLKIT_H
|
#define TOOLKIT_H
|
||||||
|
|
||||||
#include <charconv>
|
#include "util.h"
|
||||||
#include <cstring>
|
#include "stringconv.h"
|
||||||
#include <expected>
|
|
||||||
#include <type_traits>
|
|
||||||
#include <string>
|
|
||||||
#include <cstdlib>
|
|
||||||
#include <ios>
|
|
||||||
#include <vector>
|
|
||||||
#include <ranges>
|
|
||||||
#include <cmath>
|
|
||||||
#include <chrono>
|
|
||||||
|
|
||||||
namespace ranges = std::ranges;
|
|
||||||
namespace views = std::ranges::views;
|
|
||||||
|
|
||||||
// use for to_chars
|
|
||||||
constexpr size_t buffer_size = 32;
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
concept _num_type = requires {
|
|
||||||
std::is_same_v<T, int> ||
|
|
||||||
std::is_same_v<T, float> ||
|
|
||||||
std::is_same_v<T, long> ||
|
|
||||||
std::is_same_v<T, double>;
|
|
||||||
};
|
|
||||||
|
|
||||||
namespace toolkit
|
|
||||||
{
|
|
||||||
enum class num_type{
|
|
||||||
BIN,
|
|
||||||
HEX,
|
|
||||||
OCT,
|
|
||||||
DEC
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
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>)
|
|
||||||
{
|
|
||||||
char buffer[buffer_size];
|
|
||||||
auto res = std::to_chars(buffer, buffer + buffer_size, value);
|
|
||||||
if (res.ec != std::errc())
|
|
||||||
{
|
|
||||||
return std::unexpected(std::make_error_code(res.ec).message());
|
|
||||||
}
|
|
||||||
return std::string(buffer, res.ptr - buffer);
|
|
||||||
}
|
|
||||||
else if constexpr (std::is_same_v<typename std::remove_const<T>::type,
|
|
||||||
char *>)
|
|
||||||
{
|
|
||||||
return std::to_string(value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T = double> // requires std::is_same_v<T, std::string>
|
|
||||||
inline 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
requires std::is_same_v<std::string, T> || std::is_same_v<const char *, T>
|
|
||||||
inline std::string replace_string(const std::string &str, T d, T e)
|
|
||||||
{
|
|
||||||
std::string result = str;
|
|
||||||
if (d == e)
|
|
||||||
{
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
size_t len = 0;
|
|
||||||
while (true)
|
|
||||||
{
|
|
||||||
auto pos = result.find(d);
|
|
||||||
if (pos == std::string::npos)
|
|
||||||
{
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
if constexpr (std::is_same_v<T, const char *>)
|
|
||||||
{
|
|
||||||
len = std::strlen(d);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
len = d.length();
|
|
||||||
}
|
|
||||||
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>
|
|
||||||
inline 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());
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
requires std::is_same_v<T, double> || std::is_same_v<T, float>
|
|
||||||
double round(T value, int c){
|
|
||||||
auto temp = 1;
|
|
||||||
for(int i=0;i<c;i++){
|
|
||||||
temp=temp*10;
|
|
||||||
}
|
|
||||||
return std::round(value*temp)/temp;
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
60
include/util.h
Normal file
60
include/util.h
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
#ifndef UTIL_H
|
||||||
|
#define UTIL_H
|
||||||
|
|
||||||
|
#include <stringconv.h>
|
||||||
|
#include <expected>
|
||||||
|
#include <chrono>
|
||||||
|
|
||||||
|
namespace util{
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
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>)
|
||||||
|
{
|
||||||
|
char buffer[buffer_size];
|
||||||
|
auto res = std::to_chars(buffer, buffer + buffer_size, value);
|
||||||
|
if (res.ec != std::errc())
|
||||||
|
{
|
||||||
|
return std::unexpected(std::make_error_code(res.ec).message());
|
||||||
|
}
|
||||||
|
return std::string(buffer, res.ptr - buffer);
|
||||||
|
}
|
||||||
|
else if constexpr (std::is_same_v<typename std::remove_const<T>::type,
|
||||||
|
char *>)
|
||||||
|
{
|
||||||
|
return std::to_string(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T = double> // requires std::is_same_v<T, std::string>
|
||||||
|
inline 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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 = string::replace_string(result, "\n", "");
|
||||||
|
return std::move(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
Loading…
Reference in New Issue
Block a user