Add join function to create normalized path
To reduce the number of allocations. Add append member function as /= implementation having naming consistent with std::filsystem::path. Make sure append for empty Normalized does not add leading separator (consistent with std::filesystem::path).
This commit is contained in:
@@ -210,6 +210,20 @@ namespace VFS::Path
|
||||
EXPECT_EQ(value.value(), "foo/bar/a/b");
|
||||
}
|
||||
|
||||
TEST(VFSPathNormalizedTest, operatorDivForNormalizedViewShouldNotPrependSeparatorToEmpty)
|
||||
{
|
||||
Normalized value;
|
||||
value /= NormalizedView("a");
|
||||
EXPECT_EQ(value.value(), "a");
|
||||
}
|
||||
|
||||
TEST(VFSPathNormalizedTest, operatorDivForStringViewShouldNotPrependSeparatorToEmpty)
|
||||
{
|
||||
Normalized value;
|
||||
value /= std::string_view("a");
|
||||
EXPECT_EQ(value.value(), "a");
|
||||
}
|
||||
|
||||
TEST(VFSPathNormalizedTest, changeExtensionShouldReplaceAfterLastDot)
|
||||
{
|
||||
Normalized value("foo/ba.r.a");
|
||||
@@ -254,6 +268,20 @@ namespace VFS::Path
|
||||
EXPECT_EQ(value.filename(), "foo");
|
||||
}
|
||||
|
||||
TEST(VFSPathNormalizedTest, appendShouldAddExtensionWithSeparator)
|
||||
{
|
||||
Normalized value("foo");
|
||||
value.append(ExtensionView("ext"));
|
||||
EXPECT_EQ(value.value(), "foo.ext");
|
||||
}
|
||||
|
||||
TEST(VFSPathNormalizedTest, appendForEmptyShouldAddExtensionWithSeparator)
|
||||
{
|
||||
Normalized value;
|
||||
value.append(ExtensionView("ext"));
|
||||
EXPECT_EQ(value.value(), ".ext");
|
||||
}
|
||||
|
||||
template <class T>
|
||||
struct VFSPathNormalizedOperatorsTest : Test
|
||||
{
|
||||
@@ -356,5 +384,87 @@ namespace VFS::Path
|
||||
const NormalizedView value("last voyage of the u.s.s. constitution.swf");
|
||||
EXPECT_EQ(value.stem(), "last voyage of the u.s.s. constitution");
|
||||
}
|
||||
|
||||
TEST(VFSPathNormalizedJoinTest, shouldHandleSingleValue)
|
||||
{
|
||||
EXPECT_EQ(join(Normalized("foo")), "foo");
|
||||
}
|
||||
|
||||
TEST(VFSPathNormalizedJoinTest, shouldHandleSingleView)
|
||||
{
|
||||
EXPECT_EQ(join(NormalizedView("foo")), "foo");
|
||||
}
|
||||
|
||||
TEST(VFSPathNormalizedJoinTest, shouldHandleSingleString)
|
||||
{
|
||||
EXPECT_EQ(join(std::string("FOO")), "foo");
|
||||
}
|
||||
|
||||
TEST(VFSPathNormalizedJoinTest, shouldHandleSingleStringView)
|
||||
{
|
||||
EXPECT_EQ(join(std::string_view("FOO")), "foo");
|
||||
}
|
||||
|
||||
TEST(VFSPathNormalizedJoinTest, shouldHandleSingleConstCharArray)
|
||||
{
|
||||
EXPECT_EQ(join("FOO"), "foo");
|
||||
}
|
||||
|
||||
TEST(VFSPathNormalizedJoinTest, shouldHandleSingleConstCharPtr)
|
||||
{
|
||||
const char* const ptr = "FOO";
|
||||
EXPECT_EQ(join(ptr), "foo");
|
||||
}
|
||||
|
||||
TEST(VFSPathNormalizedJoinTest, shouldHandleSingleExtension)
|
||||
{
|
||||
constexpr ExtensionView extension("foo");
|
||||
EXPECT_EQ(join(extension), ".foo");
|
||||
}
|
||||
|
||||
TEST(VFSPathNormalizedJoinTest, shouldJoinTwoPathComponents)
|
||||
{
|
||||
EXPECT_EQ(join(NormalizedView("foo"), std::string_view("BAR")), "foo/bar");
|
||||
}
|
||||
|
||||
TEST(VFSPathNormalizedJoinTest, shouldJoinComponentAndExtension)
|
||||
{
|
||||
EXPECT_EQ(join(NormalizedView("foo"), ExtensionView("bar")), "foo.bar");
|
||||
}
|
||||
|
||||
TEST(VFSPathNormalizedJoinTest, shouldJoinTwoExtensions)
|
||||
{
|
||||
EXPECT_EQ(join(ExtensionView("foo"), ExtensionView("bar")), ".foo.bar");
|
||||
}
|
||||
|
||||
TEST(VFSPathNormalizedJoinTest, shouldJoinTreePathComponents)
|
||||
{
|
||||
EXPECT_EQ(join(NormalizedView("a"), std::string_view("B"), Normalized("c")), "a/b/c");
|
||||
}
|
||||
|
||||
TEST(VFSPathNormalizedJoinedCapacityTest, forNormalizedShouldReturnSizeOnly)
|
||||
{
|
||||
EXPECT_EQ(joinedCapacity(Normalized("a")), 1);
|
||||
}
|
||||
|
||||
TEST(VFSPathNormalizedJoinedCapacityTest, forConstCharPtrShouldReturnSizeOnly)
|
||||
{
|
||||
EXPECT_EQ(joinedCapacity("a"), 1);
|
||||
}
|
||||
|
||||
TEST(VFSPathNormalizedJoinedCapacityTest, forExtensionViewShouldReturnSizePlusSeparator)
|
||||
{
|
||||
EXPECT_EQ(joinedCapacity(ExtensionView("a")), 2);
|
||||
}
|
||||
|
||||
TEST(VFSPathNormalizedJoinedCapacityTest, shouldReturnSumOfSizesPlusNumberOfSeparators)
|
||||
{
|
||||
constexpr NormalizedView b("b");
|
||||
const Normalized c("c");
|
||||
constexpr std::string_view d("d");
|
||||
const std::string e("e");
|
||||
constexpr ExtensionView f("f");
|
||||
EXPECT_EQ(joinedCapacity("a", b, c, d, e, f), 11);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+88
-10
@@ -4,6 +4,7 @@
|
||||
#include <components/misc/strings/lower.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <ostream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
@@ -106,6 +107,8 @@ namespace VFS::Path
|
||||
|
||||
constexpr bool empty() const noexcept { return mValue.empty(); }
|
||||
|
||||
constexpr std::size_t size() const { return mValue.size(); }
|
||||
|
||||
friend constexpr bool operator==(const ExtensionView& lhs, const ExtensionView& rhs) = default;
|
||||
|
||||
friend constexpr bool operator==(const ExtensionView& lhs, const auto& rhs) { return lhs.mValue == rhs; }
|
||||
@@ -167,6 +170,8 @@ namespace VFS::Path
|
||||
|
||||
constexpr bool empty() const noexcept { return mValue.empty(); }
|
||||
|
||||
constexpr std::size_t size() const { return mValue.size(); }
|
||||
|
||||
friend constexpr bool operator==(const NormalizedView& lhs, const NormalizedView& rhs) = default;
|
||||
|
||||
friend constexpr bool operator==(const NormalizedView& lhs, const auto& rhs) { return lhs.mValue == rhs; }
|
||||
@@ -275,6 +280,8 @@ namespace VFS::Path
|
||||
|
||||
bool empty() const { return mValue.empty(); }
|
||||
|
||||
constexpr std::size_t size() const { return mValue.size(); }
|
||||
|
||||
operator std::string_view() const { return mValue; }
|
||||
|
||||
operator const std::string&() const { return mValue; }
|
||||
@@ -291,6 +298,49 @@ namespace VFS::Path
|
||||
|
||||
void clear() { mValue.clear(); }
|
||||
|
||||
void reserve(std::size_t capacity) { mValue.reserve(capacity); }
|
||||
|
||||
void append(NormalizedView value)
|
||||
{
|
||||
const bool isEmpty = mValue.empty();
|
||||
mValue.reserve(mValue.size() + static_cast<std::size_t>(!isEmpty) + value.value().size());
|
||||
if (!isEmpty)
|
||||
mValue += separator;
|
||||
mValue += value.value();
|
||||
}
|
||||
|
||||
void append(const Normalized& value) { append(NormalizedView(value)); }
|
||||
|
||||
void append(std::string_view value)
|
||||
{
|
||||
const bool isEmpty = mValue.empty();
|
||||
mValue.reserve(mValue.size() + static_cast<std::size_t>(!isEmpty) + value.size());
|
||||
if (!isEmpty)
|
||||
mValue += separator;
|
||||
const std::size_t offset = mValue.size();
|
||||
mValue += value;
|
||||
const auto [begin, end] = normalizeFilenameInPlace(mValue.begin() + offset, mValue.end());
|
||||
std::copy(begin, end, mValue.begin() + offset);
|
||||
mValue.resize(offset + (end - begin));
|
||||
}
|
||||
|
||||
void append(const std::string& value) { append(std::string_view(value)); }
|
||||
|
||||
template <std::size_t size>
|
||||
void append(const char (&value)[size])
|
||||
{
|
||||
append(std::string_view(value));
|
||||
}
|
||||
|
||||
void append(const char* value) { append(std::string_view(value)); }
|
||||
|
||||
void append(ExtensionView extension)
|
||||
{
|
||||
mValue.reserve(mValue.size() + 1 + extension.size());
|
||||
mValue += extensionSeparator;
|
||||
mValue += extension.value();
|
||||
}
|
||||
|
||||
Normalized& operator=(NormalizedView value)
|
||||
{
|
||||
mValue = value.value();
|
||||
@@ -299,21 +349,13 @@ namespace VFS::Path
|
||||
|
||||
Normalized& operator/=(NormalizedView value)
|
||||
{
|
||||
mValue.reserve(mValue.size() + value.value().size() + 1);
|
||||
mValue += separator;
|
||||
mValue += value.value();
|
||||
append(value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Normalized& operator/=(std::string_view value)
|
||||
{
|
||||
mValue.reserve(mValue.size() + value.size() + 1);
|
||||
mValue += separator;
|
||||
const std::size_t offset = mValue.size();
|
||||
mValue += value;
|
||||
const auto [begin, end] = normalizeFilenameInPlace(mValue.begin() + offset, mValue.end());
|
||||
std::copy(begin, end, mValue.begin() + offset);
|
||||
mValue.resize(offset + (end - begin));
|
||||
append(value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
@@ -425,6 +467,42 @@ namespace VFS::Path
|
||||
Normalized toNormalized(NormalizedView value) = delete;
|
||||
|
||||
Normalized toNormalized(Normalized value) = delete;
|
||||
|
||||
constexpr std::size_t singleCapacity(const auto& value)
|
||||
{
|
||||
return std::size(value);
|
||||
}
|
||||
|
||||
constexpr std::size_t singleCapacity(const char* value)
|
||||
{
|
||||
return std::string_view(value).size();
|
||||
}
|
||||
|
||||
constexpr std::size_t joinedCapacity(const auto& value)
|
||||
{
|
||||
return singleCapacity(value);
|
||||
}
|
||||
|
||||
constexpr std::size_t joinedCapacity(ExtensionView value)
|
||||
{
|
||||
return 1 + value.size();
|
||||
}
|
||||
|
||||
constexpr std::size_t joinedCapacity(const auto& head, const auto&... tail)
|
||||
{
|
||||
return joinedCapacity(head) + (singleCapacity(tail) + ...) + sizeof...(tail);
|
||||
}
|
||||
|
||||
template <class... Args>
|
||||
Normalized join(Args&&... args)
|
||||
{
|
||||
const std::size_t size = joinedCapacity(args...);
|
||||
VFS::Path::Normalized result;
|
||||
result.reserve(size);
|
||||
(result.append(args), ...);
|
||||
assert(result.size() <= size);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user