From f81940e3ea386a6dfdda2ff4a42d8cede35f2c8a Mon Sep 17 00:00:00 2001 From: crupest Date: Fri, 17 Apr 2020 00:49:46 +0800 Subject: ... --- include/cru/common/format.hpp | 122 ++++++++++----------------- include/cru/platform/graph_base.hpp | 28 ++---- include/cru/platform/native/input_method.hpp | 9 +- 3 files changed, 57 insertions(+), 102 deletions(-) (limited to 'include') diff --git a/include/cru/common/format.hpp b/include/cru/common/format.hpp index ecc4b30b..4405c8c5 100644 --- a/include/cru/common/format.hpp +++ b/include/cru/common/format.hpp @@ -1,104 +1,68 @@ #pragma once #include "pre_config.hpp" +#include #include #include namespace cru::util { namespace details { template -struct TypeTag {}; - -constexpr std::wstring_view PlaceHolder(TypeTag) { - return std::wstring_view(L"{}"); -} - -constexpr std::string_view PlaceHolder(TypeTag) { - return std::string_view("{}"); -} - -template -void FormatInternal(TString& string) { - const auto find_result = string.find(PlaceHolder(TypeTag{})); - if (find_result != TString::npos) +struct FormatTrait {}; + +template <> +struct FormatTrait { + static constexpr std::string_view placeholder = "{}"; + using ResultType = std::string; + using StreamType = std::stringstream; +}; + +template <> +struct FormatTrait { + static constexpr std::wstring_view placeholder = L"{}"; + using ResultType = std::wstring; + using StreamType = std::wstringstream; +}; + +template +void FormatInternal(typename FormatTrait::StreamType& stream, + const TStringView& string) { + const auto find_result = string.find(FormatTrait::placeholder); + if (find_result != TStringView::npos) throw std::invalid_argument("There is more placeholders than args."); + stream << string; } -template -void FormatInternal(TString& string, const T& arg, const TRest&... args) { - const auto find_result = string.find(PlaceHolder(TypeTag{})); - if (find_result == TString::npos) +template +void FormatInternal(typename FormatTrait::StreamType& stream, + const TStringView& string, const T& arg, + const TRest&... args) { + const auto find_result = string.find(FormatTrait::placeholder); + if (find_result == TStringView::npos) throw std::invalid_argument("There is less placeholders than args."); - string.replace(find_result, 2, FormatToString(arg, TypeTag{})); - FormatInternal(string, args...); + stream << string.substr(0, find_result); + stream << arg; + + FormatInternal(stream, string.substr(find_result + 2), args...); +} + +template +auto FormatTemplate(const TStringView& format, const T&... args) -> + typename FormatTrait::ResultType { + typename FormatTrait::StreamType stream; + FormatInternal(stream, format, args...); + return stream.str(); } } // namespace details template std::wstring Format(const std::wstring_view& format, const T&... args) { - std::wstring result(format); - details::FormatInternal(result, args...); - return result; + return details::FormatTemplate(format, args...); } template std::string Format(const std::string_view& format, const T&... args) { - std::string result(format); - details::FormatInternal(result, args...); - return result; -} - -#define CRU_FORMAT_NUMBER(type) \ - inline std::string FormatToString(const type number, \ - details::TypeTag) { \ - return std::to_string(number); \ - } \ - inline std::wstring FormatToString(const type number, \ - details::TypeTag) { \ - return std::to_wstring(number); \ - } - -CRU_FORMAT_NUMBER(int) -CRU_FORMAT_NUMBER(short) -CRU_FORMAT_NUMBER(long) -CRU_FORMAT_NUMBER(long long) -CRU_FORMAT_NUMBER(unsigned int) -CRU_FORMAT_NUMBER(unsigned short) -CRU_FORMAT_NUMBER(unsigned long) -CRU_FORMAT_NUMBER(unsigned long long) -CRU_FORMAT_NUMBER(float) -CRU_FORMAT_NUMBER(double) - -#undef CRU_FORMAT_NUMBER - -inline std::wstring_view FormatToString(const std::wstring& string, - details::TypeTag) { - return string; -} - -inline std::string_view FormatToString(const std::string& string, - details::TypeTag) { - return string; -} - -inline std::wstring_view FormatToString(const std::wstring_view& string, - details::TypeTag) { - return string; -} - -inline std::string_view FormatToString(const std::string_view& string, - details::TypeTag) { - return string; -} - -inline std::wstring_view FormatToString(const wchar_t* string, - details::TypeTag) { - return std::wstring_view(string); -} - -inline std::string_view FormatToString(const char* string, - details::TypeTag) { - return std::string_view(string); + return details::FormatTemplate(format, args...); } } // namespace cru::util diff --git a/include/cru/platform/graph_base.hpp b/include/cru/platform/graph_base.hpp index 5840ce18..98ac3993 100644 --- a/include/cru/platform/graph_base.hpp +++ b/include/cru/platform/graph_base.hpp @@ -1,5 +1,5 @@ #pragma once -#include "cru/common/pre_config.hpp" +#include "cru/common/base.hpp" #include #include @@ -214,30 +214,18 @@ constexpr bool operator!=(const Ellipse& left, const Ellipse& right) { } struct TextRange final { - constexpr static std::optional FromTwoSides(unsigned first, - unsigned second) { - if (first > second) - return std::make_optional(second, first - second); - if (first < second) - return std::make_optional(first, second - first); - return std::nullopt; - } - - constexpr static std::pair ToTwoSides( - std::optional text_range, unsigned default_position = 0) { - if (text_range.has_value()) - return std::make_pair( - text_range.value().position, - text_range.value().position + text_range.value().count); - return std::make_pair(default_position, default_position); + constexpr static TextRange FromTwoSides(gsl::index start, gsl::index end) { + return TextRange(start, end - start); } constexpr TextRange() = default; - constexpr TextRange(const unsigned position, const unsigned count) + constexpr TextRange(const gsl::index position, const gsl::index count) : position(position), count(count) {} - unsigned position = 0; - unsigned count = 0; + gsl::index GetEnd() const { return position + count; } + + gsl::index position = 0; + gsl::index count = 0; }; struct Color { diff --git a/include/cru/platform/native/input_method.hpp b/include/cru/platform/native/input_method.hpp index 00017502..56e2fb27 100644 --- a/include/cru/platform/native/input_method.hpp +++ b/include/cru/platform/native/input_method.hpp @@ -7,15 +7,18 @@ #include namespace cru::platform::native { -struct CompositionUnderline { +struct CompositionClause { int start; int end; + bool target; }; +using CompositionClauses = std::vector; + struct CompositionText { std::string text; - std::vector underlines; - int caret_position; + CompositionClauses clauses; + TextRange selection; }; struct IInputMethodContext : virtual INativeResource { -- cgit v1.2.3