[FarGroup/FarManager] master: Refactoring (bf6838ad6)

0 views
Skip to first unread message

farg...@farmanager.com

unread,
Aug 31, 2025, 1:15:55 PM (7 days ago) Aug 31
to farco...@googlegroups.com
Repository : https://github.com/FarGroup/FarManager
On branch : master
Link : https://github.com/FarGroup/FarManager/commit/bf6838ad62a03b711d2f59b3d9fed1bd9405a505

>---------------------------------------------------------------

commit bf6838ad62a03b711d2f59b3d9fed1bd9405a505
Author: Alex Alabuzhev <alab...@gmail.com>
Date: Sun Aug 31 15:55:41 2025 +0100

Refactoring


>---------------------------------------------------------------

bf6838ad62a03b711d2f59b3d9fed1bd9405a505
far/common.tests.cpp | 2 +-
far/common/algorithm.hpp | 6 ++++--
far/common/base64.hpp | 15 ++++++++-------
far/common/expected.hpp | 4 +++-
far/common/from_string.hpp | 6 ++++--
far/common/uuid.hpp | 13 ++++++++++---
far/console.cpp | 31 +++++++++++++++----------------
far/main.cpp | 4 ++--
far/sqlitedb.cpp | 4 ++--
9 files changed, 49 insertions(+), 36 deletions(-)

diff --git a/far/common.tests.cpp b/far/common.tests.cpp
index d96378e19..aab8ab98a 100644
--- a/far/common.tests.cpp
+++ b/far/common.tests.cpp
@@ -659,7 +659,7 @@ TEST_CASE("expected")

REQUIRE(GoodValue.has_value());
REQUIRE(GoodValue.value() == L"42"sv);
- REQUIRE_THROWS_AS(GoodValue.error(), std::logic_error);
+ REQUIRE_THROWS_AS(GoodValue.error(), far_exception);

REQUIRE(!BadValue.has_value());
REQUIRE(BadValue.error() == 42);
diff --git a/far/common/algorithm.hpp b/far/common/algorithm.hpp
index 50da28f29..94acadda8 100644
--- a/far/common/algorithm.hpp
+++ b/far/common/algorithm.hpp
@@ -67,12 +67,14 @@ void apply_permutation(std::ranges::random_access_range auto& Range, std::random
if (next < 0 || next >= length)
{
indices[i] = next;
- throw_exception("Invalid index in permutation");
+ using namespace std::string_view_literals;
+ throw_exception("Invalid index in permutation"sv);
}
if (next == current)
{
indices[i] = next;
- throw_exception("Not a permutation");
+ using namespace std::string_view_literals;
+ throw_exception("Not a permutation"sv);
}

std::ranges::swap(first[current], first[next]);
diff --git a/far/common/base64.hpp b/far/common/base64.hpp
index 7f73ddfcc..e35fd274d 100644
--- a/far/common/base64.hpp
+++ b/far/common/base64.hpp
@@ -34,6 +34,7 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "algorithm.hpp"
#include "bytes_view.hpp"
+#include "exception.hpp"

//----------------------------------------------------------------------------

@@ -86,18 +87,18 @@ namespace base64
no, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, no, no, no, no, no,
};

- const auto error = [&]
+ if (static_cast<size_t>(Char) >= std::size(DecodingTable))
{
using namespace std::string_literals;
- throw std::runtime_error("Invalid base64 character "s + Char);
- };
-
- if (static_cast<size_t>(Char) >= std::size(DecodingTable))
- error();
+ throw_exception("Invalid base64 character "s + Char);
+ }

const auto Bits = DecodingTable[static_cast<size_t>(Char)];
if (Bits == no)
- error();
+ {
+ using namespace std::string_literals;
+ throw_exception("Invalid base64 character "s + Char);
+ }

return static_cast<bits_set>((Bits & 0b111111) << (6 * (3 - i)));
}
diff --git a/far/common/expected.hpp b/far/common/expected.hpp
index 142c157ce..947616ef6 100644
--- a/far/common/expected.hpp
+++ b/far/common/expected.hpp
@@ -32,6 +32,7 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

+#include "exception.hpp"
#include "preprocessor.hpp"

#include <stdexcept>
@@ -98,7 +99,8 @@ public:
if (!has_value())
return std::get<1>(m_Data);

- throw std::logic_error("No error stored");
+ using namespace std::string_view_literals;
+ throw_exception("No error stored"sv);
}

private:
diff --git a/far/common/from_string.hpp b/far/common/from_string.hpp
index e0d5ad93c..516c03b28 100644
--- a/far/common/from_string.hpp
+++ b/far/common/from_string.hpp
@@ -164,16 +164,18 @@ template<typename T>
[[nodiscard]]
T from_string(std::wstring_view const Str, size_t* const Pos = {}, int const Base = 10)
{
+ using namespace std::string_view_literals;
+
switch (T Value; detail::from_string(Str, Value, Pos, Base))
{
case detail::result::ok:
return Value;

case detail::result::invalid_argument:
- throw_exception("invalid from_string argument");
+ throw_exception("invalid from_string argument"sv);

case detail::result::out_of_range:
- throw_exception("from_string argument is out of range");
+ throw_exception("from_string argument is out of range"sv);

default:
std::unreachable();
diff --git a/far/common/uuid.hpp b/far/common/uuid.hpp
index ef1648539..1a2a3f227 100644
--- a/far/common/uuid.hpp
+++ b/far/common/uuid.hpp
@@ -62,7 +62,8 @@ namespace uuid
if (L'A' <= c && c <= L'F')
return c - L'A' + 10;

- throw_exception("Invalid character");
+ using namespace std::string_view_literals;
+ throw_exception("Invalid character"sv);
}

template<size_t... I>
@@ -94,7 +95,10 @@ namespace uuid
constexpr auto& operator+=(std::random_access_iterator auto& Iterator, separator_t)
{
if (*Iterator != L'-')
- throw_exception("Invalid character");
+ {
+ using namespace std::string_view_literals;
+ throw_exception("Invalid character"sv);
+ }

return ++Iterator;
}
@@ -149,7 +153,10 @@ namespace uuid
constexpr auto parse(std::basic_string_view<char_type> const Str)
{
if (!(Str.size() == uuid_length || (Str.size() == uuid_length + 2 && Str.front() == L'{' && Str.back() == L'}')))
- throw_exception("Incorrect format");
+ {
+ using namespace std::string_view_literals;
+ throw_exception("Incorrect format"sv);
+ }

return detail::parse(Str.data() + (Str.size() != uuid_length));
}
diff --git a/far/console.cpp b/far/console.cpp
index 38b40cf3c..67713134b 100644
--- a/far/console.cpp
+++ b/far/console.cpp
@@ -688,13 +688,13 @@ protected:
const auto Prefix = concat(CSI, L'?', Query, L';');
const auto Suffix = L"$y"sv;

- const auto give_up = [&]
+ const auto make_exception = [&](source_location const& Location = source_location::current())
{
- throw far_exception(far::format(L"Incorrect response: {}"sv, ResponseData), false);
+ return far_exception(far::format(L"Incorrect response: {}"sv, ResponseData), false, Location);
};

if (ResponseData.size() != Prefix.size() + 1 + Suffix.size() || !ResponseData.starts_with(Prefix) || !ResponseData.ends_with(Suffix))
- give_up();
+ throw make_exception();

switch (const auto Char = ResponseData[Prefix.size()])
{
@@ -709,8 +709,7 @@ protected:
return Char;

default:
- give_up();
- std::unreachable();
+ throw make_exception();
}
}
catch (std::exception const& e)
@@ -2145,14 +2144,14 @@ protected:
return false;
}

- const auto give_up = [&]
+ const auto make_exception = [&](source_location const& Location = source_location::current())
{
- throw far_exception(far::format(L"Incorrect response: {}"sv, ResponseData), false);
+ return far_exception(far::format(L"Incorrect response: {}"sv, ResponseData), false, Location);
};

string_view Response = ResponseData;
if (!Response.ends_with(L'\\'))
- give_up();
+ throw make_exception();

Response.remove_suffix(1);

@@ -2166,7 +2165,7 @@ protected:
for (auto PaletteToken: enum_tokens(Response, L"\\"sv))
{
if (!PaletteToken.starts_with(Prefix) || !PaletteToken.ends_with(Suffix))
- give_up();
+ throw make_exception();

PaletteToken.remove_prefix(Prefix.size());
PaletteToken.remove_suffix(Suffix.size());
@@ -2175,31 +2174,31 @@ protected:

auto SubIterator = Subtokens.cbegin();
if (SubIterator == Subtokens.cend())
- give_up();
+ throw make_exception();

if (*SubIterator++ != L"4"sv)
- give_up();
+ throw make_exception();

const auto VtIndex = from_string<unsigned>(*SubIterator++);
if (VtIndex >= Palette.size())
- give_up();
+ throw make_exception();

auto& PaletteColor = Palette[vt_color_index(VtIndex)];

auto ColorStr = *SubIterator;
if (!ColorStr.starts_with(RGBPrefix))
- give_up();
+ throw make_exception();

ColorStr.remove_prefix(RGBPrefix.size());

if (ColorStr.size() != L"0000"sv.size() * 3 + 2)
- give_up();
+ throw make_exception();

const auto color = [&](size_t const Offset)
{
const auto Value = from_string<unsigned>(ColorStr.substr(Offset * L"0000/"sv.size(), 4), {}, 16);
if (Value > 0xffff)
- give_up();
+ throw make_exception();

return Value / 0x0101;
};
@@ -2209,7 +2208,7 @@ protected:
}

if (ColorsSet != Palette.size())
- give_up();
+ throw make_exception();

LOGDEBUG(L"VT palette read successfuly"sv);
return true;
diff --git a/far/main.cpp b/far/main.cpp
index d4edac1e3..352b778f4 100644
--- a/far/main.cpp
+++ b/far/main.cpp
@@ -545,9 +545,9 @@ struct args_context
};

[[noreturn]]
-static void invalid_argument(string_view const Argument, string_view const Str)
+static void invalid_argument(string_view const Argument, string_view const Str, source_location const& Location = source_location::current())
{
- throw far_known_exception(far::format(L"Error processing \"{}\": {}"sv, Argument, Str));
+ throw far_known_exception(far::format(L"Error processing \"{}\": {}"sv, Argument, Str), Location);
}

namespace args
diff --git a/far/sqlitedb.cpp b/far/sqlitedb.cpp
index 2fffc96cd..4094df32c 100644
--- a/far/sqlitedb.cpp
+++ b/far/sqlitedb.cpp
@@ -117,7 +117,7 @@ namespace
}

[[noreturn]]
- void throw_exception(string_view const DatabaseName, int const ErrorCode, string_view const ErrorString = {}, int const SystemErrorCode = 0, string_view const Sql = {}, int const ErrorOffset = -1)
+ void throw_exception(string_view const DatabaseName, int const ErrorCode, string_view const ErrorString = {}, int const SystemErrorCode = 0, string_view const Sql = {}, int const ErrorOffset = -1, source_location const& Location = source_location::current())
{
throw far_sqlite_exception(ErrorCode, far::format(L"[{}] - SQLite error {}: {}{}{}{}"sv,
DatabaseName,
@@ -126,7 +126,7 @@ namespace
SystemErrorCode? far::format(L" ({})"sv, os::format_error(SystemErrorCode)) : L""sv,
Sql.empty()? L""sv : far::format(L" while executing \"{}\""sv, Sql),
ErrorOffset == -1? L""sv : far::format(L" at position {}"sv, ErrorOffset)
- ));
+ ), Location);
}

[[noreturn]]


Reply all
Reply to author
Forward
0 new messages