Added path views for abs and rel paths

This commit is contained in:
catalinvasile
2024-07-10 11:52:47 +02:00
parent 51cf1887cd
commit 2195ccfc28
15 changed files with 4637 additions and 957 deletions
+227 -154
View File
@@ -2,6 +2,9 @@
#include "tl/detail/prologue.h"
#include <cstddef>
#include "tl/abs_path_view.h"
#include "tl/rel_path_view.h"
#include "tl/path_system.h"
#include "tl/rel_path.h"
#include "tl/detail/path_base.h"
@@ -23,8 +26,11 @@ class abs_path : public detail::path_system::path_base<PathSystems>
public:
using rel_path_type = rel_path<PathSystems>;
using path_view_type = abs_path_view<PathSystems>;
abs_path() noexcept;
abs_path() noexcept = default;
abs_path(abs_path_view<PathSystems> view) noexcept;
abs_path(int path_system_id, span<const string> elements) noexcept;
explicit abs_path(const char* path) noexcept;
abs_path(const char* path, size_t size) noexcept;
@@ -35,15 +41,13 @@ public:
abs_path(abs_path&& other) noexcept;
// operators
const string& operator[](size_t idx) const noexcept;
string& operator[](size_t idx) noexcept;
abs_path& operator=(const abs_path& other) noexcept;
abs_path& operator=(abs_path&& other) noexcept;
abs_path& operator=(const char* path) noexcept;
template<typename Str> requires (!std::is_same_v<Str, const char*> && !std::is_same_v<Str, char*>)
abs_path& operator=(const Str& path) noexcept;
abs_path& operator=(const abs_path_view<PathSystems>& path) noexcept;
abs_path operator+(const char* path) const noexcept;
abs_path operator+(const rel_path<PathSystems>& path) const noexcept;
@@ -56,58 +60,64 @@ public:
abs_path& operator+=(rel_path<PathSystems>&& path) noexcept;
template<typename Str> requires (!std::is_same_v<Str, const char*> && !std::is_same_v<Str, char*>)
abs_path& operator+=(const Str& path) noexcept;
abs_path& operator+=(const rel_path_view<PathSystems>& path) noexcept;
operator abs_path_view<PathSystems>() const noexcept;
bool operator==(const abs_path& other) const noexcept;
bool operator!=(const abs_path& other) const noexcept;
auto operator<=>(const abs_path& other) const noexcept;
bool operator==(const abs_path_view<PathSystems>& other) const noexcept;
bool operator!=(const abs_path_view<PathSystems>& other) const noexcept;
auto operator<=>(const abs_path_view<PathSystems>& other) const noexcept;
void swap(abs_path& other) noexcept;
void clear() noexcept;
string str() const noexcept;
eastl::string eastl_str() const noexcept;
std::string std_str() const noexcept;
abs_path_view<PathSystems> view() const noexcept;
rel_path<PathSystems> subpath(size_t idx, int count = 0) const noexcept;
abs_path parent() const noexcept;
abs_path parent(size_t levels) const noexcept;
rel_path_view<PathSystems> subpath(size_t idx, int count = 0) const noexcept;
abs_path_view<PathSystems> parent() const noexcept;
abs_path_view<PathSystems> parent(size_t levels) const noexcept;
rel_path<PathSystems> path_to(const abs_path& to) const noexcept;
void path_to(rel_path<PathSystems>& dst, const abs_path& to) const noexcept;
bool is_prefix_of(const abs_path& path) const noexcept;
void path_to(rel_path<PathSystems>& dst, const abs_path& to) const noexcept;
bool is_prefix_of(const abs_path& path) const noexcept;
template <typename T>
bool is() const noexcept;
bool is_valid() const noexcept;
bool is() const noexcept;
bool is_valid() const noexcept;
bool push_back(string element) noexcept override;
const string& front() const noexcept;
const string& back() const noexcept;
string& front() noexcept;
string& back() noexcept;
bool push_back(string element) noexcept override;
const string& front() const noexcept;
const string& back() const noexcept;
void replace_back(string element) noexcept;
void take_elements(tl::vector<string>&& elements) noexcept override;
void take_elements(tl::vector<string>&& elements) noexcept override;
//Parses a string.
// 1st attempt is done to parse it as an absolute path. If it succeeds, it returns the path.
// 1st attempt is done to parse it as an abs path. If it succeeds, it returns the path.
// 2nd attempt is done to parse it as a relative path and if it succeeds, it returns it as fallbackRoot + relative path. This happens ONLY if the fallbackRoot is valid
template<typename Str>
static abs_path from_string(const Str& string, const abs_path& fallbackRoot) noexcept;
static abs_path from_string(const Str& string, const abs_path& fallbackRoot) noexcept;
//Parses a string as an absolute path. If it fails it returns an invalid path.
//Parses a string as an abs path. If it fails it returns an invalid path.
template<typename Str>
static abs_path from_string(const Str& string) noexcept;
static abs_path from_string(const Str& string) noexcept;
template<typename Str>
static bool is_valid_string(const Str& string) noexcept;
static bool is_valid_string(const Str& string) noexcept;
template<typename Str>
static bool has_valid_tag(const Str& string) noexcept;
static bool has_valid_tag(const Str& string) noexcept;
bool parse(const char* path, size_t size) noexcept;
bool parse(const char* path, size_t size) noexcept;
private:
bool collapse_path() noexcept;
bool collapse_path() noexcept;
int m_pathSystemId = -1;
};
@@ -124,15 +134,6 @@ namespace tl
{
//////////////////////////////////////////////////////////////////////////
template <typename PathSystems>
abs_path<PathSystems>::abs_path() noexcept
{
m_pathSystemId = -1;
this->m_hash = 0;
}
//////////////////////////////////////////////////////////////////////////
template <typename PathSystems>
abs_path<PathSystems>::abs_path(const char* path, size_t size) noexcept
{
@@ -140,7 +141,26 @@ abs_path<PathSystems>::abs_path(const char* path, size_t size) noexcept
TL_PLAIN_CRASH("null path in abs_path constructor");
if (parse(path, size) == false)
TL_PLAIN_FAIL("Failed to parse absolute path");
TL_PLAIN_FAIL("Failed to parse abs path");
}
//////////////////////////////////////////////////////////////////////////
template <typename PathSystems>
abs_path<PathSystems>::abs_path(abs_path_view<PathSystems> view) noexcept
: m_pathSystemId(view.m_pathSystemId)
{
this->m_elements.insert(this->m_elements.end(), view.begin(), view.end());
this->m_hash = view.hash();
}
//////////////////////////////////////////////////////////////////////////
template <typename PathSystems>
abs_path<PathSystems>::abs_path(int path_system_id, span<const string> elements) noexcept
: m_pathSystemId(path_system_id)
{
this->m_elements.insert(this->m_elements.end(), elements.begin(), elements.end());
}
//////////////////////////////////////////////////////////////////////////
@@ -152,7 +172,7 @@ abs_path<PathSystems>::abs_path(const char* path) noexcept
TL_PLAIN_CRASH("null path in abs_path constructor");
if (parse(path, strlen(path)) == false)
TL_PLAIN_FAIL("Failed to parse absolute path");
TL_PLAIN_FAIL("Failed to parse abs path");
}
//////////////////////////////////////////////////////////////////////////
@@ -162,7 +182,7 @@ template <typename Str> requires (!std::is_same_v<Str, const char*> && !std::is_
abs_path<PathSystems>::abs_path(const Str& path) noexcept
{
if (parse(path.data(), path.size()) == false)
TL_PLAIN_FAIL("Failed to parse absolute path");
TL_PLAIN_FAIL("Failed to parse abs path");
}
//////////////////////////////////////////////////////////////////////////
@@ -186,23 +206,6 @@ abs_path<PathSystems>::abs_path(abs_path&& other) noexcept
other.m_hash = 0;
}
//////////////////////////////////////////////////////////////////////////
template <typename PathSystems>
const string& abs_path<PathSystems>::operator[](size_t idx) const noexcept
{
return this->m_elements[idx];
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
template <typename PathSystems>
string& abs_path<PathSystems>::operator[](size_t idx) noexcept
{
this->m_hash = 0;
return this->m_elements[idx];
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
template <typename PathSystems>
@@ -232,13 +235,25 @@ template<typename Str> requires (!std::is_same_v<Str, const char*> && !std::is_s
abs_path<PathSystems>& abs_path<PathSystems>::operator=(const Str& path) noexcept
{
if (parse(path.data(), path.size()) == false)
TL_PLAIN_FAIL("Failed to parse absolute path");
TL_PLAIN_FAIL("Failed to parse abs path");
return *this;
}
//////////////////////////////////////////////////////////////////////////
template <typename PathSystems>
abs_path<PathSystems>& abs_path<PathSystems>::operator=(const abs_path_view<PathSystems>& path) noexcept
{
this->m_elements.clear();
this->m_elements.insert(this->m_elements.end(), path.m_elements.begin(), path.m_elements.end());
this->m_hash = path.m_hash;
m_pathSystemId = path.m_pathSystemId;
return *this;
}
//////////////////////////////////////////////////////////////////////////
template <typename PathSystems>
abs_path<PathSystems>& abs_path<PathSystems>::operator=(const char* path) noexcept
{
@@ -246,7 +261,7 @@ abs_path<PathSystems>& abs_path<PathSystems>::operator=(const char* path) noexce
TL_PLAIN_CRASH("null path in abs_path assignment");
if (parse(path, strlen(path)) == false)
TL_PLAIN_FAIL("Failed to parse absolute path");
TL_PLAIN_FAIL("Failed to parse abs path");
return *this;
}
@@ -261,7 +276,6 @@ abs_path<PathSystems> abs_path<PathSystems>::operator+(const char* path) const n
abs_path p;
p.reserve(this->size() + 1);
p = *this;
p += path;
return p;
}
@@ -279,7 +293,6 @@ abs_path<PathSystems> abs_path<PathSystems>::operator+(const Str& path) const no
abs_path p;
p.reserve(this->size() + 1);
p = *this;
p += path;
return p;
}
@@ -294,9 +307,8 @@ abs_path<PathSystems> abs_path<PathSystems>::operator+(const rel_path<PathSystem
if (!path.empty())
{
abs_path p;
p.reserve(this->size() + 1);
p.reserve(this->size() + path.size());
p = *this;
p += path;
return p;
}
@@ -311,9 +323,8 @@ abs_path<PathSystems> abs_path<PathSystems>::operator+(rel_path<PathSystems>&& p
if (!path.empty())
{
abs_path p;
p.reserve(this->size() + 1);
p.reserve(this->size() + path.size());
p = *this;
p += std::move(path);
return p;
}
@@ -326,7 +337,7 @@ template <typename PathSystems>
abs_path<PathSystems>& abs_path<PathSystems>::operator+=(const char* path) noexcept
{
if (m_pathSystemId < 0 && this->empty())
this->operator=(path);
*this = path;
else if (path && path[0] != '\0')
*this += rel_path<PathSystems>(path);
@@ -340,7 +351,7 @@ template<typename Str> requires (!std::is_same_v<Str, const char*> && !std::is_s
abs_path<PathSystems>& abs_path<PathSystems>::operator+=(const Str& path) noexcept
{
if (m_pathSystemId < 0 && this->empty())
this->operator=(path);
*this = path;
else if (!path.empty())
*this += rel_path<PathSystems>(path);
@@ -352,12 +363,8 @@ abs_path<PathSystems>& abs_path<PathSystems>::operator+=(const Str& path) noexce
template <typename PathSystems>
abs_path<PathSystems>& abs_path<PathSystems>::operator+=(const rel_path<PathSystems>& path) noexcept
{
if (!path.empty())
{
//use the move version
*this += rel_path<PathSystems>(path);
this->collapse_path();
}
//use the move version
*this += rel_path<PathSystems>(path);
return *this;
}
@@ -374,8 +381,31 @@ abs_path<PathSystems>& abs_path<PathSystems>::operator+=(rel_path<PathSystems>&&
if (!push_back(std::move(e)))
return *this;
}
path.clear();
this->m_hash = 0;
this->collapse_path();
if (!this->collapse_path())
this->clear();
}
return *this;
}
//////////////////////////////////////////////////////////////////////////
template <typename PathSystems>
abs_path<PathSystems>& abs_path<PathSystems>::operator+=(const rel_path_view<PathSystems>& path) noexcept
{
if (!path.empty())
{
this->m_elements.reserve(this->m_elements.size() + path.size());
for (auto& e : path)
{
if (!push_back(std::move(e)))
return *this;
}
this->m_hash = 0;
if (!this->collapse_path())
this->clear();
}
return *this;
}
@@ -403,7 +433,41 @@ bool abs_path<PathSystems>::operator!=(const abs_path& other) const noexcept
template <typename PathSystems>
auto abs_path<PathSystems>::operator<=>(const abs_path& other) const noexcept
{
// return std::compare_three_way{}(this->m_elements, other.m_elements);
const size_t sz1 = this->m_elements.size();
const size_t sz2 = other.m_elements.size();
for (size_t i = 0; i < sz1 && i < sz2; ++i)
{
auto r = this->m_elements[i] <=> other.m_elements[i];
if (r != decltype(r)::equal)
return r;
}
return sz1 <=> sz2;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
template <typename PathSystems>
bool abs_path<PathSystems>::operator==(const abs_path_view<PathSystems>& other) const noexcept
{
return (!this->m_hash || !other.m_hash || this->m_hash == other.m_hash) &&
this->m_pathSystemId == other.m_pathSystemId &&
tl::span(this->m_elements) == other.m_elements;
}
//////////////////////////////////////////////////////////////////////////
template <typename PathSystems>
bool abs_path<PathSystems>::operator!=(const abs_path_view<PathSystems>& other) const noexcept
{
return !(*this == other);
}
//////////////////////////////////////////////////////////////////////////
template <typename PathSystems>
auto abs_path<PathSystems>::operator<=>(const abs_path_view<PathSystems>& other) const noexcept
{
const size_t sz1 = this->m_elements.size();
const size_t sz2 = other.m_elements.size();
for (size_t i = 0; i < sz1 && i < sz2; ++i)
@@ -429,15 +493,7 @@ bool abs_path<PathSystems>::parse(const char* path, size_t size) noexcept
if (m_pathSystemId < 0)
return false;
PathSystems::parse_absolute(this->m_elements, m_pathSystemId, path, size);
for (const string& element : this->m_elements)
{
if (!this->validate_element(element))
{
this->clear();
return false;
}
}
PathSystems::parse_abs(this->m_elements, m_pathSystemId, path, size);
if (collapse_path() == false)
{
@@ -456,7 +512,7 @@ bool abs_path<PathSystems>::collapse_path() noexcept
if (this->empty())
return is_valid();
this->m_hash = 0;
const size_t oldSize = this->m_elements.size();
for (size_t i = 0; i < this->m_elements.size();)
{
if (i >= 1 &&
@@ -467,11 +523,14 @@ bool abs_path<PathSystems>::collapse_path() noexcept
i -= 1;
}
else if (this->m_elements[i] == path_system::current_t::value())
this->m_elements.erase(this->m_elements.begin() + i); //remove the .
this->m_elements.erase(this->m_elements.begin() + i); //remove the .
else
i++;
}
if (oldSize != this->m_elements.size())
this->m_hash = 0;
if (!PathSystems::validate_abs_path(m_pathSystemId, this->m_elements))
return false;
@@ -510,7 +569,7 @@ string abs_path<PathSystems>::str() const noexcept
if (!is_valid() && this->empty())
return {};
return PathSystems::template format_absolute<string>(m_pathSystemId, this->m_elements);
return PathSystems::template format_abs<string>(m_pathSystemId, this->m_elements);
}
//////////////////////////////////////////////////////////////////////////
@@ -521,7 +580,7 @@ eastl::string abs_path<PathSystems>::eastl_str() const noexcept
if (!is_valid() && this->empty())
return {};
return PathSystems::template format_absolute<eastl::string>(m_pathSystemId, this->m_elements);
return PathSystems::template format_abs<eastl::string>(m_pathSystemId, this->m_elements);
}
//////////////////////////////////////////////////////////////////////////
@@ -532,93 +591,68 @@ std::string abs_path<PathSystems>::std_str() const noexcept
if (!is_valid() && this->empty())
return {};
return PathSystems::template format_absolute<std::string>(m_pathSystemId, this->m_elements);
return PathSystems::template format_abs<std::string>(m_pathSystemId, this->m_elements);
}
//////////////////////////////////////////////////////////////////////////
template <typename PathSystems>
rel_path<PathSystems> abs_path<PathSystems>::subpath(size_t idx, int count /* = 0 */) const noexcept
abs_path_view<PathSystems> abs_path<PathSystems>::view() const noexcept
{
return abs_path_view<PathSystems>(m_pathSystemId, this->m_elements);
}
//////////////////////////////////////////////////////////////////////////
template <typename PathSystems>
abs_path<PathSystems>::operator abs_path_view<PathSystems>() const noexcept
{
return abs_path_view<PathSystems>(m_pathSystemId, this->m_elements);
}
//////////////////////////////////////////////////////////////////////////
template <typename PathSystems>
rel_path_view<PathSystems> abs_path<PathSystems>::subpath(size_t index, int count /* = 0 */) const noexcept
{
rel_path<PathSystems> dst;
if (count == 0)
count = (int)this->size() - (int)idx;
count = (int)this->size() - (int)index;
if (count < 0)
count = (int)this->size() - (int)idx + count;
count = (int)this->size() - (int)index + count;
if (count > 0 && idx < this->size())
{
dst.reserve(count);
for (int i = 0; i < count; i++)
dst.push_element(this->m_elements[idx + i]);
}
return dst;
if (count > 0 && index < this->size())
return rel_path_view<PathSystems>(tl::span(this->m_elements.begin() + index, this->m_elements.begin() + index + count));
return {};
}
//////////////////////////////////////////////////////////////////////////
template <typename PathSystems>
abs_path<PathSystems> abs_path<PathSystems>::parent() const noexcept
abs_path_view<PathSystems> abs_path<PathSystems>::parent() const noexcept
{
if (this->empty())
{
TL_PLAIN_FAIL("Invalid path");
return abs_path();
return {};
}
abs_path p = *this;
if (p.back() == path_system::back_t::value())
{
//do not do a pop_back here!!
//The parent of '/..' is '/../..'!!!
p.push_back(path_system::back_t::value());
}
else
p.pop_back();
if (!PathSystems::validate_abs_path(p.m_pathSystemId, p.m_elements))
{
TL_PLAIN_FAIL("Invalid path");
return abs_path();
}
return p;
return abs_path_view<PathSystems>(m_pathSystemId, tl::span(this->m_elements.begin(), this->m_elements.end() - 1));
}
//////////////////////////////////////////////////////////////////////////
template <typename PathSystems>
abs_path<PathSystems> abs_path<PathSystems>::parent(size_t levels) const noexcept
abs_path_view<PathSystems> abs_path<PathSystems>::parent(size_t levels) const noexcept
{
if (this->empty() || this->size() <= levels)
{
TL_PLAIN_FAIL("Invalid path");
return abs_path();
return {};
}
abs_path p = *this;
if (p.back() == path_system::back_t::value())
{
//The parent of '/..' is '/../..'!!!
while (levels > 0)
{
levels--;
p.push_back(path_system::back_t::value());
}
}
else
p.shrink(p.size() - levels);
if (!PathSystems::validate_abs_path(p.m_pathSystemId, p.m_elements))
{
TL_PLAIN_FAIL("Invalid path");
return abs_path();
}
return p;
return abs_path_view<PathSystems>(m_pathSystemId, tl::span(this->m_elements.begin(), this->m_elements.end() - levels));
}
//////////////////////////////////////////////////////////////////////////
@@ -663,7 +697,7 @@ void abs_path<PathSystems>::path_to(rel_path<PathSystems>& dst, const abs_path&
}
else if (to.empty())
{
//ms relative path from an absolute path to an empty path should be an empty path
//ms relative path from an abs path to an empty path should be an empty path
}
else
{
@@ -772,21 +806,13 @@ const tl::string& abs_path<PathSystems>::front() const noexcept
//////////////////////////////////////////////////////////////////////////
template<class PathSystems>
tl::string& abs_path<PathSystems>::front() noexcept
void abs_path<PathSystems>::replace_back(string element) noexcept
{
TL_PLAIN_ASSERT(!this->empty());
this->m_hash = 0;
return this->m_elements.front();
}
//////////////////////////////////////////////////////////////////////////
template<class PathSystems>
tl::string& abs_path<PathSystems>::back() noexcept
{
TL_PLAIN_ASSERT(!this->empty());
this->m_hash = 0;
return this->m_elements.back();
this->m_elements.back() = std::move(element);
if (!collapse_path())
clear();
}
//////////////////////////////////////////////////////////////////////////
@@ -838,6 +864,53 @@ bool abs_path<PathSystems>::has_valid_tag(const Str& string) noexcept
return pathSystemId >= 0;
}
template<class PathSystems>
abs_path<PathSystems> operator+(const abs_path_view<PathSystems>& a, const char* b) noexcept
{
abs_path<PathSystems> p;
p.reserve(a.size() + 1);
p = a;
p += b;
return p;
}
template<class PathSystems, typename Str> requires (!std::is_same_v<Str, const char*> && !std::is_same_v<Str, char*>)
abs_path<PathSystems> operator+(const abs_path_view<PathSystems>& a, const Str& b) noexcept
{
abs_path<PathSystems> p;
p.reserve(a.size() + 1);
p = a;
p += b;
return p;
}
template<class PathSystems>
abs_path<PathSystems> operator+(const abs_path_view<PathSystems>& a, const rel_path<PathSystems>& b) noexcept
{
abs_path<PathSystems> p;
p.reserve(a.size() + b.size());
p = a;
p += b;
return p;
}
template<class PathSystems>
abs_path<PathSystems> operator+(const abs_path_view<PathSystems>& a, const rel_path_view<PathSystems>& b) noexcept
{
abs_path<PathSystems> p;
p.reserve(a.size() + b.size());
p = a;
p += b;
return p;
}
template<class PathSystems>
abs_path<PathSystems> operator+(const abs_path_view<PathSystems>& a, const abs_path_view<PathSystems>& b) noexcept
{
abs_path<PathSystems> p;
p.reserve(a.size() + b.size());
p = a;
p += b;
return p;
}
}
//////////////////////////////////////////////////////////////////////////