From eaf083e093c3ad2f770b19aebfa5688f4eb3f0f1 Mon Sep 17 00:00:00 2001 From: Alex Alabuzhev Date: Wed, 13 Dec 2023 22:11:00 +0000 Subject: [PATCH] Refactoring --- far/cmdline.cpp | 2 +- far/common.tests.cpp | 3 --- far/common/enum_tokens.hpp | 4 ++-- far/common/preprocessor.hpp | 12 ------------ far/common/string_utils.hpp | 13 ------------- far/dialog.cpp | 6 ++---- far/editor.cpp | 14 +++++++------- far/editor.hpp | 2 +- far/execute.cpp | 2 +- 9 files changed, 14 insertions(+), 44 deletions(-) diff --git a/far/cmdline.cpp b/far/cmdline.cpp index 1761e94988..62dfb4ee4e 100644 --- a/far/cmdline.cpp +++ b/far/cmdline.cpp @@ -637,7 +637,7 @@ std::list CommandLine::GetPrompt() { bool Stop; auto NewColor = PrefixColor; - const auto Str = make_string_view(Iterator, Format.cend()); + string_view const Str{ Iterator, Format.cend() }; const auto Tail = colors::ExtractColorInNewFormat(Str, NewColor, Stop); if (Tail.size() == Str.size()) { diff --git a/far/common.tests.cpp b/far/common.tests.cpp index 8fa84ea396..e90fdc0d47 100644 --- a/far/common.tests.cpp +++ b/far/common.tests.cpp @@ -1679,9 +1679,6 @@ TEST_CASE("string_utils.misc") REQUIRE(L"123"s + L"45"sv == L"12345"sv); REQUIRE(L"123"sv + L"45"s == L"12345"sv); REQUIRE(L"123"sv + L"45"sv == L"12345"sv); - - const auto Str = L"12345"sv; - REQUIRE(make_string_view(Str.begin() + 1, Str.end() - 1) == L"234"sv); } #ifdef __cpp_lib_generic_unordered_lookup diff --git a/far/common/enum_tokens.hpp b/far/common/enum_tokens.hpp index 6307cfea8e..94ea310f0e 100644 --- a/far/common/enum_tokens.hpp +++ b/far/common/enum_tokens.hpp @@ -169,7 +169,7 @@ namespace detail std::wstring_view extract(std::wstring_view const View, const std::wstring_view Separators, std::wstring_view& Value) const { const auto NewIterator = std::ranges::find_first_of(View, Separators); - Value = make_string_view(View.cbegin(), NewIterator); + Value = { View.cbegin(), NewIterator }; return { NewIterator, View.cend() }; } }; @@ -188,7 +188,7 @@ namespace detail return !m_Overrider.active(i) && contains(Separators, i); }); - Value = make_string_view(View.cbegin(), NewIterator); + Value = { View.cbegin(), NewIterator }; m_Overrider.postprocess(Value); diff --git a/far/common/preprocessor.hpp b/far/common/preprocessor.hpp index eee49b9faf..0180618342 100644 --- a/far/common/preprocessor.hpp +++ b/far/common/preprocessor.hpp @@ -67,12 +67,6 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #define CONST_REVERSE_ITERATOR(Object) DETAIL_ITERATOR_IMPL(Object, DETAIL_STD_CONST_REVERSE_MUTATOR) -#define DETAIL_LAMBDA_PREDICATE_IMPL(Object, i, REFERENCE_PARAM, ...) [&](REFERENCE_PARAM(Object) i, ##__VA_ARGS__) - -#define LAMBDA_PREDICATE(Object, i, ...) DETAIL_LAMBDA_PREDICATE_IMPL(Object, i, REFERENCE, ##__VA_ARGS__) -#define CONST_LAMBDA_PREDICATE(Object, i, ...) DETAIL_LAMBDA_PREDICATE_IMPL(Object, i, CONST_REFERENCE, ##__VA_ARGS__) - - #define DETAIL_ALL_RANGE_IMPL(Object, MUTATOR_PARAM) MUTATOR_PARAM(begin)(Object), MUTATOR_PARAM(end)(Object) #define ALL_RANGE(Object) DETAIL_ALL_RANGE_IMPL(Object, DETAIL_STD_DEFAULT_MUTATOR) @@ -81,12 +75,6 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #define ALL_CONST_REVERSE_RANGE(Object) DETAIL_ALL_RANGE_IMPL(Object, DETAIL_STD_CONST_REVERSE_MUTATOR) -#define RANGE(Object, i, ...) ALL_RANGE(Object), LAMBDA_PREDICATE(Object, i, ##__VA_ARGS__) -#define CONST_RANGE(Object, i, ...) ALL_CONST_RANGE(Object), CONST_LAMBDA_PREDICATE(Object, i, ##__VA_ARGS__) -#define REVERSE_RANGE(Object, i, ...) ALL_REVERSE_RANGE(Object), LAMBDA_PREDICATE(Object, i, ##__VA_ARGS__) -#define CONST_REVERSE_RANGE(Object, i, ...) ALL_CONST_REVERSE_RANGE(Object), CONST_LAMBDA_PREDICATE(Object, i, ##__VA_ARGS__) - - #define DETAIL_FOR_RANGE_IMPL(Object, i, MUTATOR_PARAM) for(auto i = MUTATOR_PARAM(begin)(Object), CONCATENATE(end, __LINE__) = MUTATOR_PARAM(end)(Object); i != CONCATENATE(end, __LINE__); ++i) #define FOR_RANGE(Object, i) DETAIL_FOR_RANGE_IMPL(Object, i, DETAIL_STD_DEFAULT_MUTATOR) diff --git a/far/common/string_utils.hpp b/far/common/string_utils.hpp index ecbd2dfbe0..237752eaf4 100644 --- a/far/common/string_utils.hpp +++ b/far/common/string_utils.hpp @@ -653,19 +653,6 @@ auto operator+(T const Lhs, std::basic_string_view const Rhs) } -// string_view has iterators, but you cannot construct it from them -// "Design by committee" *facepalm* -template -[[nodiscard]] -constexpr auto make_string_view(T const Begin, T const End) noexcept -{ - using char_type = std::iter_value_t; - static_assert(std::same_as::const_iterator, T>); - - const auto Size = static_cast(End - Begin); - return std::basic_string_view{ Size ? &*Begin : nullptr, Size }; -} - class lvalue_string_view { public: diff --git a/far/dialog.cpp b/far/dialog.cpp index 0887ee2506..34b52137d1 100644 --- a/far/dialog.cpp +++ b/far/dialog.cpp @@ -983,8 +983,6 @@ void Dialog::InitDialogObjects(size_t ID) string Dialog::GetTitle() const { - const DialogItemEx *CurItemList=nullptr; - FOR_CONST_RANGE(Items, i) { // по первому попавшемуся "тексту" установим заголовок консоли! @@ -996,10 +994,10 @@ string Dialog::GetTitle() const } if (i->Type==DI_LISTBOX && i == Items.begin()) - CurItemList = &*i; + return i->ListPtr->GetTitle(); } - return CurItemList? CurItemList->ListPtr->GetTitle() : L""s; + return {}; } void Dialog::ProcessLastHistory(DialogItemEx& CurItem, int MsgIndex) diff --git a/far/editor.cpp b/far/editor.cpp index 25423bf2e8..640f1a8641 100644 --- a/far/editor.cpp +++ b/far/editor.cpp @@ -6643,17 +6643,17 @@ static std::string_view GetLineBytes(string_view const Str, std::vector& B } } -bool Editor::SetLineCodePage(iterator const& Iterator, uintptr_t CurrentCodepage, uintptr_t const NewCodepage, bool const Validate) +bool Editor::SetLineCodePage(Edit& Line, uintptr_t CurrentCodepage, uintptr_t const NewCodepage, bool const Validate) { - if (Iterator->m_Str.empty()) + if (Line.m_Str.empty()) return true; encoding::diagnostics Diagnostics; - const auto Bytes = GetLineBytes(Iterator->m_Str, decoded, CurrentCodepage, Validate? &Diagnostics : nullptr); + const auto Bytes = GetLineBytes(Line.m_Str, decoded, CurrentCodepage, Validate? &Diagnostics : nullptr); auto Result = !Bytes.empty() && !Diagnostics.ErrorPosition; - encoding::get_chars(NewCodepage, Bytes, Iterator->m_Str, &Diagnostics); - Result = Result && !Iterator->m_Str.empty() && !Diagnostics.ErrorPosition; - Iterator->Changed(); + encoding::get_chars(NewCodepage, Bytes, Line.m_Str, &Diagnostics); + Result = Result && !Line.m_Str.empty() && !Diagnostics.ErrorPosition; + Line.Changed(); return Result; } @@ -6715,7 +6715,7 @@ bool Editor::SetCodePage(uintptr_t const CurrentCodepage, uintptr_t const NewCod auto Result = true; - FOR_RANGE(Lines, i) + for(auto& i: Lines) { if (!SetLineCodePage(i, CurrentCodepage, NewCodepage, Result)) Result = false; diff --git a/far/editor.hpp b/far/editor.hpp index 1cbcc707cf..fbef314ba4 100644 --- a/far/editor.hpp +++ b/far/editor.hpp @@ -277,7 +277,7 @@ class Editor final: public SimpleScreenObject string Block2Text(); string VBlock2Text(); void Change(EDITOR_CHANGETYPE Type,int StrNum); - bool SetLineCodePage(iterator const& Iterator, uintptr_t CurrentCodepage, uintptr_t NewCodepage, bool Validate); + bool SetLineCodePage(Edit& Line, uintptr_t CurrentCodepage, uintptr_t NewCodepage, bool Validate); numbered_iterator InsertString(string_view Str, const numbered_iterator& Where); numbered_iterator PushString(const string_view Str) { return InsertString(Str, EndIterator()); } void TurnOffMarkingBlock(); diff --git a/far/execute.cpp b/far/execute.cpp index 01b20a264d..557bff4dd7 100644 --- a/far/execute.cpp +++ b/far/execute.cpp @@ -294,7 +294,7 @@ static bool PartCmdLine(string_view const FullCommand, string& Command, string& } } - const auto Cmd = make_string_view(Begin, CmdEnd); + string_view const Cmd{ Begin, CmdEnd }; const auto ExcludeCmds = enum_tokens(exclude_cmds(), L";"sv); if (std::ranges::any_of(ExcludeCmds, [&](string_view const i){ return equal_icase(i, Cmd); })) return false;