61 lines
1.5 KiB
C++
61 lines
1.5 KiB
C++
#pragma once
|
|
|
|
#include <format>
|
|
|
|
#include "tl/detail/prologue.h"
|
|
#include <tl/fixed_vector.h>
|
|
#include "tl/string.h"
|
|
#include "string.h"
|
|
|
|
namespace tl
|
|
{
|
|
|
|
template <typename DST>
|
|
constexpr DST format() noexcept
|
|
{
|
|
return DST();
|
|
}
|
|
|
|
template <typename DST, typename... Args>
|
|
constexpr DST format(std::format_string<Args...> format_str, Args&&... args) noexcept
|
|
{
|
|
DST buffer;
|
|
std::format_to(std::back_inserter(buffer), format_str, std::forward<Args>(args)...);
|
|
return buffer;
|
|
}
|
|
|
|
template <typename DST, typename... Args>
|
|
constexpr DST& format_append(DST& dst, std::format_string<Args...> format_str, Args&&... args) noexcept
|
|
{
|
|
std::format_to(std::back_inserter(dst), format_str, std::forward<Args>(args)...);
|
|
return dst;
|
|
}
|
|
|
|
//template <typename DST, typename S>
|
|
//constexpr DST format(const S& format_str) noexcept
|
|
//{
|
|
// return DST(format_str);
|
|
//}
|
|
|
|
|
|
|
|
template <typename... Args>
|
|
tl::string format(std::format_string<Args...> format_str, Args&&... args) noexcept
|
|
{
|
|
tl::fixed_vector<char, 1024> buffer;
|
|
std::format_to(std::back_inserter(buffer), format_str, std::forward<Args>(args)...);
|
|
return tl::string(buffer.data(), buffer.size());
|
|
}
|
|
|
|
template <typename... Args>
|
|
tl::string& format_append(tl::string& dst, std::format_string<Args...> format_str, Args&&... args) noexcept
|
|
{
|
|
tl::fixed_vector<char, 1024> buffer(dst.size());
|
|
if (!dst.empty())
|
|
memcpy(buffer.data(), dst.data(), dst.size());
|
|
std::format_to(std::back_inserter(buffer), format_str, std::forward<Args>(args)...);
|
|
dst = tl::string(buffer.data(), buffer.size());
|
|
return dst;
|
|
}
|
|
|
|
} |