fs/Path: add WithSuffix()
This commit is contained in:
parent
458084d79b
commit
fe3ab7b937
|
@ -48,6 +48,21 @@ AllocatedPath::FromUTF8Throw(std::string_view path_utf8)
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
AllocatedPath::SetSuffix(const_pointer new_suffix) noexcept
|
||||||
|
{
|
||||||
|
assert(new_suffix != nullptr);
|
||||||
|
assert(*new_suffix == '.');
|
||||||
|
|
||||||
|
const auto end = value.end();
|
||||||
|
auto begin = end;
|
||||||
|
|
||||||
|
if (auto old = GetSuffix())
|
||||||
|
begin = std::next(value.begin(), old - value.data());
|
||||||
|
|
||||||
|
value.replace(begin, end, new_suffix);
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
AllocatedPath::ChopSeparators() noexcept
|
AllocatedPath::ChopSeparators() noexcept
|
||||||
{
|
{
|
||||||
|
|
|
@ -304,6 +304,27 @@ public:
|
||||||
return Path{*this}.GetSuffix();
|
return Path{*this}.GetSuffix();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Replace the suffix of this path (or append the suffix if
|
||||||
|
* there is none currently).
|
||||||
|
*
|
||||||
|
* @param new_suffix the new filename suffix (must start with
|
||||||
|
* a dot)
|
||||||
|
*/
|
||||||
|
void SetSuffix(const_pointer new_suffix) noexcept;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return a copy of this path but with the given suffix
|
||||||
|
* (replacing the existing suffix if there is one).
|
||||||
|
*
|
||||||
|
* @param new_suffix the new filename suffix (must start with
|
||||||
|
* a dot)
|
||||||
|
*/
|
||||||
|
[[gnu::pure]]
|
||||||
|
AllocatedPath WithSuffix(const_pointer new_suffix) const noexcept {
|
||||||
|
return Path{*this}.WithSuffix(new_suffix);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the filename extension (excluding the dot) or
|
* Returns the filename extension (excluding the dot) or
|
||||||
* nullptr if the path does not have one.
|
* nullptr if the path does not have one.
|
||||||
|
|
|
@ -173,6 +173,16 @@ public:
|
||||||
[[gnu::pure]]
|
[[gnu::pure]]
|
||||||
const_pointer GetSuffix() const noexcept;
|
const_pointer GetSuffix() const noexcept;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return a copy of this path but with the given suffix
|
||||||
|
* (replacing the existing suffix if there is one).
|
||||||
|
*
|
||||||
|
* @param new_suffix the new filename suffix (must start with
|
||||||
|
* a dot)
|
||||||
|
*/
|
||||||
|
[[gnu::pure]]
|
||||||
|
AllocatedPath WithSuffix(const_pointer new_suffix) const noexcept;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the filename extension (excluding the dot) or
|
* Returns the filename extension (excluding the dot) or
|
||||||
* nullptr if the path does not have one.
|
* nullptr if the path does not have one.
|
||||||
|
|
|
@ -26,6 +26,14 @@ Path::GetDirectoryName() const noexcept
|
||||||
return AllocatedPath::FromFS(PathTraitsFS::GetParent(c_str()));
|
return AllocatedPath::FromFS(PathTraitsFS::GetParent(c_str()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
AllocatedPath
|
||||||
|
Path::WithSuffix(const_pointer new_suffix) const noexcept
|
||||||
|
{
|
||||||
|
AllocatedPath result{*this};
|
||||||
|
result.SetSuffix(new_suffix);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
AllocatedPath
|
AllocatedPath
|
||||||
operator/(Path a, Path b) noexcept
|
operator/(Path a, Path b) noexcept
|
||||||
{
|
{
|
||||||
|
|
|
@ -96,3 +96,14 @@ TEST(Path, Suffix)
|
||||||
EXPECT_STREQ(Path::FromFS(PATH_LITERAL("/foo/.bar.abc")).GetSuffix(), ".abc");
|
EXPECT_STREQ(Path::FromFS(PATH_LITERAL("/foo/.bar.abc")).GetSuffix(), ".abc");
|
||||||
EXPECT_STREQ(Path::FromFS(PATH_LITERAL("/foo/.bar.abc.def")).GetSuffix(), ".def");
|
EXPECT_STREQ(Path::FromFS(PATH_LITERAL("/foo/.bar.abc.def")).GetSuffix(), ".def");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(Path, WithSuffix)
|
||||||
|
{
|
||||||
|
EXPECT_STREQ(Path::FromFS(PATH_LITERAL("foo")).WithSuffix(".abc").c_str(), "foo.abc");
|
||||||
|
EXPECT_STREQ(Path::FromFS(PATH_LITERAL("/foo/bar")).WithSuffix(".abc").c_str(), "/foo/bar.abc");
|
||||||
|
EXPECT_STREQ(Path::FromFS(PATH_LITERAL("/foo.xyz/bar")).WithSuffix(".abc").c_str(), "/foo.xyz/bar.abc");
|
||||||
|
EXPECT_STREQ(Path::FromFS(PATH_LITERAL("/foo.abc/bar.def")).WithSuffix(".xyz").c_str(), "/foo.abc/bar.xyz");
|
||||||
|
EXPECT_STREQ(Path::FromFS(PATH_LITERAL("/foo.abc/bar.def.ghi")).WithSuffix(".xyz").c_str(), "/foo.abc/bar.def.xyz");
|
||||||
|
EXPECT_STREQ(Path::FromFS(PATH_LITERAL("/foo/.bar.abc")).WithSuffix(".xyz").c_str(), "/foo/.bar.xyz");
|
||||||
|
EXPECT_STREQ(Path::FromFS(PATH_LITERAL("/foo/.bar.abc.def")).WithSuffix(".xyz").c_str(), "/foo/.bar.abc.xyz");
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue