From bc37fdb4b746ce0f5f5c49ad9779464acb488284 Mon Sep 17 00:00:00 2001 From: crupest Date: Tue, 18 Jan 2022 22:40:39 +0800 Subject: ... --- include/cru/common/String.hpp | 79 +------------------------------------------ 1 file changed, 1 insertion(+), 78 deletions(-) (limited to 'include/cru/common/String.hpp') diff --git a/include/cru/common/String.hpp b/include/cru/common/String.hpp index dd3da52f..8db012cb 100644 --- a/include/cru/common/String.hpp +++ b/include/cru/common/String.hpp @@ -14,6 +14,7 @@ #include #include #include +#include #include #include @@ -320,84 +321,6 @@ inline String operator+(const String& left, const String& right) { return result; } -inline String ToString(bool value) { - return value ? String(u"true") : String(u"false"); -} - -template -std::enable_if_t, String> ToString(T value) { - std::array buffer; - auto result = - std::to_chars(buffer.data(), buffer.data() + buffer.size(), value); - - if (result.ec == std::errc{}) { - } else { - throw std::invalid_argument("Failed to convert value to chars."); - } - - auto size = result.ptr - buffer.data(); - auto b = new char16_t[size + 1]; - b[size] = 0; - std::copy(buffer.data(), result.ptr, b); - return String::FromBuffer(b, size, size); -} - -template -std::enable_if_t, String> ToString(T value) { - auto str = std::to_string(value); - return String(str.cbegin(), str.cend()); -} - -inline String ToString(String value) { return value; } - -namespace details { -enum class FormatTokenType { PlaceHolder, Text }; - -struct FormatToken { - FormatTokenType type; - String data; -}; - -std::vector CRU_BASE_API ParseToFormatTokenList(const String& str); - -void CRU_BASE_API FormatAppendFromFormatTokenList( - String& current, const std::vector& format_token_list, - Index index); - -template -void FormatAppendFromFormatTokenList( - String& current, const std::vector& format_token_list, - Index index, TA&& args0, T&&... args) { - for (Index i = index; i < static_cast(format_token_list.size()); i++) { - const auto& token = format_token_list[i]; - if (token.type == FormatTokenType::PlaceHolder) { - current += ToString(std::forward(args0)); - FormatAppendFromFormatTokenList(current, format_token_list, i + 1, - std::forward(args)...); - return; - } else { - current += token.data; - } - } -} -} // namespace details - -template -String Format(const String& format, T&&... args) { - String result; - - details::FormatAppendFromFormatTokenList( - result, details::ParseToFormatTokenList(format), 0, - std::forward(args)...); - - return result; -} - -template -String String::Format(T&&... args) const { - return cru::Format(*this, std::forward(args)...); -} - CRU_DEFINE_COMPARE_OPERATORS(StringView) inline String::iterator String::insert(const_iterator pos, StringView str) { -- cgit v1.2.3