Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
alabuzhev committed Dec 13, 2023
1 parent f24e5ab commit eaf083e
Show file tree
Hide file tree
Showing 9 changed files with 14 additions and 44 deletions.
2 changes: 1 addition & 1 deletion far/cmdline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ std::list<CommandLine::segment> 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())
{
Expand Down
3 changes: 0 additions & 3 deletions far/common.tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions far/common/enum_tokens.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() };
}
};
Expand All @@ -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);

Expand Down
12 changes: 0 additions & 12 deletions far/common/preprocessor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
13 changes: 0 additions & 13 deletions far/common/string_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -653,19 +653,6 @@ auto operator+(T const Lhs, std::basic_string_view<T> const Rhs)
}


// string_view has iterators, but you cannot construct it from them
// "Design by committee" *facepalm*
template<typename T>
[[nodiscard]]
constexpr auto make_string_view(T const Begin, T const End) noexcept
{
using char_type = std::iter_value_t<T>;
static_assert(std::same_as<typename std::basic_string_view<char_type>::const_iterator, T>);

const auto Size = static_cast<size_t>(End - Begin);
return std::basic_string_view<char_type>{ Size ? &*Begin : nullptr, Size };
}

class lvalue_string_view
{
public:
Expand Down
6 changes: 2 additions & 4 deletions far/dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -983,8 +983,6 @@ void Dialog::InitDialogObjects(size_t ID)

string Dialog::GetTitle() const
{
const DialogItemEx *CurItemList=nullptr;

FOR_CONST_RANGE(Items, i)
{
// по первому попавшемуся "тексту" установим заголовок консоли!
Expand All @@ -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)
Expand Down
14 changes: 7 additions & 7 deletions far/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6643,17 +6643,17 @@ static std::string_view GetLineBytes(string_view const Str, std::vector<char>& 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;
}
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion far/editor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion far/execute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit eaf083e

Please sign in to comment.