Skip to content

Commit 893916a

Browse files
committed
Merge pull request monero-project#9435
89ad8ac epee: string_tools: keep full path in cut_off_extension (tobtoht) c51ca53 epee: string_tools: remove dot from get_extension (tobtoht)
2 parents 7df0d9b + 89ad8ac commit 893916a

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

contrib/epee/src/string_tools.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,13 +178,18 @@ namespace string_tools
178178

179179
std::string get_extension(const std::string& str)
180180
{
181-
return boost::filesystem::path(str).extension().string();
181+
std::string ext_with_dot = boost::filesystem::path(str).extension().string();
182+
183+
if (ext_with_dot.empty())
184+
return {};
185+
186+
return ext_with_dot.erase(0, 1);
182187
}
183188

184189
//----------------------------------------------------------------------------
185190
std::string cut_off_extension(const std::string& str)
186191
{
187-
return boost::filesystem::path(str).stem().string();
192+
return boost::filesystem::path(str).replace_extension("").string();
188193
}
189194

190195
#ifdef _WIN32

tests/unit_tests/epee_utils.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1443,6 +1443,21 @@ TEST(StringTools, GetIpInt32)
14431443
EXPECT_EQ(htonl(0xff0aff00), ip);
14441444
}
14451445

1446+
TEST(StringTools, GetExtension)
1447+
{
1448+
EXPECT_EQ(std::string{}, epee::string_tools::get_extension(""));
1449+
EXPECT_EQ(std::string{}, epee::string_tools::get_extension("."));
1450+
EXPECT_EQ(std::string{"keys"}, epee::string_tools::get_extension("wallet.keys"));
1451+
EXPECT_EQ(std::string{"3"}, epee::string_tools::get_extension("1.2.3"));
1452+
}
1453+
1454+
TEST(StringTools, CutOffExtension)
1455+
{
1456+
EXPECT_EQ(std::string{}, epee::string_tools::cut_off_extension(""));
1457+
EXPECT_EQ(std::string{"/home/user/Monero/wallets/wallet"}, epee::string_tools::cut_off_extension("/home/user/Monero/wallets/wallet"));
1458+
EXPECT_EQ(std::string{"/home/user/Monero/wallets/wallet"}, epee::string_tools::cut_off_extension("/home/user/Monero/wallets/wallet.keys"));
1459+
}
1460+
14461461
TEST(NetUtils, IPv4NetworkAddress)
14471462
{
14481463
static_assert(epee::net_utils::ipv4_network_address::get_type_id() == epee::net_utils::address_type::ipv4, "bad ipv4 type id");

0 commit comments

Comments
 (0)