#ifndef TOOLKIT_H #define TOOLKIT_H #include #include #include #include #include #include #include //use for to_chars constexpr size_t buffer_size = 32; template concept _num_type = requires { std::is_same_v || std::is_same_v || std::is_same_v || std::is_same_v; }; namespace toolkit{ template std::expected to_string(T value){ if constexpr (std::is_same_v || std::is_same_v || std::is_same_v){ 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::type, char *>) { return std::to_string(value); } } template// requires std::is_same_v std::expected 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 requires std::is_same_v || std::is_same_v 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){ len = std::strlen(d); }else{ len = d.length(); } result = result.replace(pos, len ,e); } } } #endif