实现的目的;提高 C/C++ 编译速度,fmt 库模板嵌套过多编译速度非常慢,且编译后程序体积也过大,函数步入的栈帧过多!
只支持格式;{}
不支持格式;{:02x}
class fmt
{
public:
template <typename S, typename ...T>
static std::string format(const S& fmt, T ... args) noexcept
{
std::string str;
if constexpr (std::is_same<S, std::string>::value)
{
str = fmt;
}
else if constexpr (std::is_same<S, std::string_view>::value)
{
str = std::string(fmt.data(), fmt.size());
}
else
{
str = fmt;
}
(..., format_string(str, args));
return str;
}
template <typename OutputIt, typename ...T>
static void format_to(OutputIt&& out, const std::string& fmt, T ... args)
{
std::string result = format(fmt, std::forward<T&&>(args)...);
for (char ch : result)
{
*out = ch;
}
}
private:
template <typename T>
static std::string to_string(const T& value) noexcept
{
if constexpr (std::is_same<T, bool>::value)
{
return value ? "true" : "false";
}
else if constexpr (std::is_pointer<T>::value)
{
using DECAY_T = typename std::decay<T>::type;
if constexpr (std::is_same<char*, DECAY_T>::value || std::is_same<const char*, DECAY_T>::value)
{
return value ? value : "";
}
else
{
if (value)
{
char buf[sizeof(value) << 2];
snprintf(buf, sizeof(buf), "%p", reinterpret_cast<const void*>(value));
return buf;
}
return "null";
}
}
else if constexpr (std::is_same<T, std::string>::value)
{
return value;
}
else if constexpr (std::is_same<T, std::string_view>::value)
{
return std::string(value.data(), value.size());
}
else
{
return std::to_string(value);
}
}
template <typename T>
static std::string to_string(const std::shared_ptr<T>& value) noexcept
{
return fmt::to_string(value.get());
}
template <typename T>
static void format_string(std::string& out, const T& value) noexcept
{
replace_string(out, "{}"sv, fmt::to_string(value));
}
public:
static bool replace_string(std::string& str, const std::string_view& old_string, const std::string_view& new_string) noexcept;
};
inline bool fmt::replace_string(std::string& str, const std::string_view& old_string, const std::string_view& new_string) noexcept
{
size_t pos = str.find(old_string);
if (pos == std::string::npos)
{
return false;
}
str.replace(pos, old_string.length(), new_string);
return true;
}