Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

epee: string_tools: remove dot from get_extension [RELEASE] #9434

Merged
merged 2 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions contrib/epee/src/string_tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,18 @@ 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);
}

//----------------------------------------------------------------------------
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
Expand Down
15 changes: 15 additions & 0 deletions tests/unit_tests/epee_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1427,6 +1427,21 @@ 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(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");
Expand Down
Loading