From c51ca53daaefc432f17a9c73406ea7772f09aba6 Mon Sep 17 00:00:00 2001 From: tobtoht Date: Wed, 14 Aug 2024 16:13:35 +0200 Subject: [PATCH 1/2] epee: string_tools: remove dot from get_extension Fixes a regression introduced in #9254. Previously it did not include the dot. --- contrib/epee/src/string_tools.cpp | 7 ++++++- tests/unit_tests/epee_utils.cpp | 8 ++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/contrib/epee/src/string_tools.cpp b/contrib/epee/src/string_tools.cpp index 525af1c46..aeb490d5d 100644 --- a/contrib/epee/src/string_tools.cpp +++ b/contrib/epee/src/string_tools.cpp @@ -178,7 +178,12 @@ namespace string_tools std::string get_extension(const std::string& str) { - return boost::filesystem::path(str).extension().string(); + std::string ext_with_dot = boost::filesystem::path(str).extension().string(); + + if (ext_with_dot.empty()) + return {}; + + return ext_with_dot.erase(0, 1); } //---------------------------------------------------------------------------- diff --git a/tests/unit_tests/epee_utils.cpp b/tests/unit_tests/epee_utils.cpp index 31bdc698d..46b27d85b 100644 --- a/tests/unit_tests/epee_utils.cpp +++ b/tests/unit_tests/epee_utils.cpp @@ -1443,6 +1443,14 @@ TEST(StringTools, GetIpInt32) EXPECT_EQ(htonl(0xff0aff00), ip); } +TEST(StringTools, GetExtension) +{ + EXPECT_EQ(std::string{}, epee::string_tools::get_extension("")); + EXPECT_EQ(std::string{}, epee::string_tools::get_extension(".")); + EXPECT_EQ(std::string{"keys"}, epee::string_tools::get_extension("wallet.keys")); + EXPECT_EQ(std::string{"3"}, epee::string_tools::get_extension("1.2.3")); +} + TEST(NetUtils, IPv4NetworkAddress) { static_assert(epee::net_utils::ipv4_network_address::get_type_id() == epee::net_utils::address_type::ipv4, "bad ipv4 type id"); From 89ad8ac8b1307788940966aa373526153f2191d8 Mon Sep 17 00:00:00 2001 From: tobtoht Date: Wed, 14 Aug 2024 19:45:55 +0200 Subject: [PATCH 2/2] epee: string_tools: keep full path in cut_off_extension --- contrib/epee/src/string_tools.cpp | 2 +- tests/unit_tests/epee_utils.cpp | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/contrib/epee/src/string_tools.cpp b/contrib/epee/src/string_tools.cpp index aeb490d5d..36356d77e 100644 --- a/contrib/epee/src/string_tools.cpp +++ b/contrib/epee/src/string_tools.cpp @@ -189,7 +189,7 @@ namespace string_tools //---------------------------------------------------------------------------- std::string cut_off_extension(const std::string& str) { - return boost::filesystem::path(str).stem().string(); + return boost::filesystem::path(str).replace_extension("").string(); } #ifdef _WIN32 diff --git a/tests/unit_tests/epee_utils.cpp b/tests/unit_tests/epee_utils.cpp index 46b27d85b..64ed83694 100644 --- a/tests/unit_tests/epee_utils.cpp +++ b/tests/unit_tests/epee_utils.cpp @@ -1451,6 +1451,13 @@ TEST(StringTools, GetExtension) EXPECT_EQ(std::string{"3"}, epee::string_tools::get_extension("1.2.3")); } +TEST(StringTools, CutOffExtension) +{ + EXPECT_EQ(std::string{}, epee::string_tools::cut_off_extension("")); + EXPECT_EQ(std::string{"/home/user/Monero/wallets/wallet"}, epee::string_tools::cut_off_extension("/home/user/Monero/wallets/wallet")); + EXPECT_EQ(std::string{"/home/user/Monero/wallets/wallet"}, epee::string_tools::cut_off_extension("/home/user/Monero/wallets/wallet.keys")); +} + TEST(NetUtils, IPv4NetworkAddress) { static_assert(epee::net_utils::ipv4_network_address::get_type_id() == epee::net_utils::address_type::ipv4, "bad ipv4 type id");