diff options
author | crupest <crupest@outlook.com> | 2022-01-18 22:40:39 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2022-01-18 22:40:39 +0800 |
commit | bc37fdb4b746ce0f5f5c49ad9779464acb488284 (patch) | |
tree | 53aa5b98ebba25016be1ec74a2ff161a70de4762 /include/cru/common/String.hpp | |
parent | d28248cc4ccb1a1d81f0d82b8317767f4e2bff44 (diff) | |
download | cru-bc37fdb4b746ce0f5f5c49ad9779464acb488284.tar.gz cru-bc37fdb4b746ce0f5f5c49ad9779464acb488284.tar.bz2 cru-bc37fdb4b746ce0f5f5c49ad9779464acb488284.zip |
...
Diffstat (limited to 'include/cru/common/String.hpp')
-rw-r--r-- | include/cru/common/String.hpp | 79 |
1 files changed, 1 insertions, 78 deletions
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 <string> #include <string_view> #include <system_error> +#include <tuple> #include <type_traits> #include <vector> @@ -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 <typename T> -std::enable_if_t<std::is_integral_v<T>, String> ToString(T value) { - std::array<char, 50> 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 <typename T> -std::enable_if_t<std::is_floating_point_v<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<FormatToken> CRU_BASE_API ParseToFormatTokenList(const String& str); - -void CRU_BASE_API FormatAppendFromFormatTokenList( - String& current, const std::vector<FormatToken>& format_token_list, - Index index); - -template <typename TA, typename... T> -void FormatAppendFromFormatTokenList( - String& current, const std::vector<FormatToken>& format_token_list, - Index index, TA&& args0, T&&... args) { - for (Index i = index; i < static_cast<Index>(format_token_list.size()); i++) { - const auto& token = format_token_list[i]; - if (token.type == FormatTokenType::PlaceHolder) { - current += ToString(std::forward<TA>(args0)); - FormatAppendFromFormatTokenList(current, format_token_list, i + 1, - std::forward<T>(args)...); - return; - } else { - current += token.data; - } - } -} -} // namespace details - -template <typename... T> -String Format(const String& format, T&&... args) { - String result; - - details::FormatAppendFromFormatTokenList( - result, details::ParseToFormatTokenList(format), 0, - std::forward<T>(args)...); - - return result; -} - -template <typename... T> -String String::Format(T&&... args) const { - return cru::Format(*this, std::forward<T>(args)...); -} - CRU_DEFINE_COMPARE_OPERATORS(StringView) inline String::iterator String::insert(const_iterator pos, StringView str) { |