First implementation of custom path type.
This commit is contained in:
40
source/mijin/types/path.cpp
Normal file
40
source/mijin/types/path.cpp
Normal file
@@ -0,0 +1,40 @@
|
||||
|
||||
#include "./path.hpp"
|
||||
|
||||
#include <filesystem>
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
namespace mijin
|
||||
{
|
||||
|
||||
//
|
||||
// internal defines
|
||||
//
|
||||
|
||||
//
|
||||
// internal constants
|
||||
//
|
||||
|
||||
//
|
||||
// internal types
|
||||
//
|
||||
|
||||
//
|
||||
// internal variables
|
||||
//
|
||||
|
||||
//
|
||||
// internal functions
|
||||
//
|
||||
|
||||
|
||||
//
|
||||
// public functions
|
||||
//
|
||||
|
||||
NativePath getWorkingDirectory()
|
||||
{
|
||||
return fs::current_path().generic_string();
|
||||
}
|
||||
} // namespace mijin
|
||||
@@ -4,55 +4,384 @@
|
||||
#if !defined(MIJIN_TYPES_PATH_HPP_INCLUDED)
|
||||
#define MIJIN_TYPES_PATH_HPP_INCLUDED 1
|
||||
|
||||
#include "../debug/assert.hpp"
|
||||
#include "../internal/common.hpp"
|
||||
#include "../util/traits.hpp"
|
||||
|
||||
#include <ranges>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
|
||||
namespace mijin
|
||||
{
|
||||
template<typename TChar = char, TChar separator = TChar('/'), typename TTraits = std::char_traits<TChar>, template<typename> typename TAllocator = MIJIN_DEFAULT_ALLOCATOR>
|
||||
class BasePath
|
||||
|
||||
template<typename T>
|
||||
concept BasePathType = requires(const T& path)
|
||||
{
|
||||
typename T::traits_t;
|
||||
typename T::char_t;
|
||||
typename T::char_traits_t;
|
||||
typename T::allocator_t;
|
||||
typename T::string_t;
|
||||
typename T::string_view_t;
|
||||
typename T::size_type;
|
||||
typename T::difference_type;
|
||||
typename T::path_view_t;
|
||||
{ T::SEPARATOR } -> std::convertible_to<typename T::char_t>;
|
||||
{ path.stringView() } -> std::convertible_to<typename T::string_view_t>;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
concept PathType = BasePathType<T> && std::is_same_v<typename T::char_t, char>;
|
||||
|
||||
template<typename T>
|
||||
concept WPathType = BasePathType<T> && std::is_same_v<typename T::char_t, wchar_t>;
|
||||
|
||||
template<typename TChar, TChar separator = TChar('/')>
|
||||
struct DefaultPathTraits
|
||||
{
|
||||
using char_t = TChar;
|
||||
using char_traits_t = std::char_traits<TChar>;
|
||||
using allocator_t = std::allocator<TChar>;
|
||||
using string_t = std::basic_string<char_t, char_traits_t, allocator_t>;
|
||||
using string_view_t = std::basic_string_view<char_t, char_traits_t>;
|
||||
using size_type = string_view_t::size_type;
|
||||
using difference_type = string_view_t::difference_type;
|
||||
static constexpr char_t SEPARATOR = separator;
|
||||
|
||||
static constexpr bool isAbsolute(string_view_t path) MIJIN_NOEXCEPT
|
||||
{
|
||||
return !path.empty() && path[0] == SEPARATOR;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename TChar = char, typename TTraits = DefaultPathTraits<TChar>>
|
||||
class BasePathView;
|
||||
|
||||
template<typename TConcrete, typename TChar = char, typename TTraits = DefaultPathTraits<TChar>>
|
||||
class MixinPath;
|
||||
|
||||
namespace impl
|
||||
{
|
||||
template<typename TChar, typename TTraits, TChar separator>
|
||||
class BasePathIterator
|
||||
{
|
||||
public:
|
||||
using string_t = std::basic_string<TChar, TTraits, TAllocator<char>>;
|
||||
using size_type = string_t::size_type;
|
||||
using difference_type = string_t::difference_type;
|
||||
private:
|
||||
string_t storage_;
|
||||
std::string_view full_;
|
||||
std::string_view::iterator pos_;
|
||||
std::string_view::iterator next_;
|
||||
|
||||
BasePathIterator(std::string_view full, std::string_view::iterator pos) MIJIN_NOEXCEPT : full_(full), pos_(pos) {
|
||||
if (pos != full_.end() && *pos == separator) {
|
||||
++pos;
|
||||
}
|
||||
findNext();
|
||||
}
|
||||
public:
|
||||
BasePath() = default;
|
||||
BasePath(const BasePath&) = default;
|
||||
BasePath(BasePath&&) = default;
|
||||
BasePath(string_t string) MIJIN_NOEXCEPT_IF(std::is_nothrow_move_constructible<string_t>()) : storage_(std::move(string)) {
|
||||
simplify();
|
||||
BasePathIterator() MIJIN_NOEXCEPT : pos_(full_.end()), next_(full_.end()) {}
|
||||
BasePathIterator(const BasePathIterator&) noexcept = default;
|
||||
|
||||
BasePathIterator& operator=(const BasePathIterator&) noexcept = default;
|
||||
|
||||
bool operator==(const BasePathIterator& other) MIJIN_NOEXCEPT { MIJIN_ASSERT(full_ == other.full_, "Comparing unrelated iterators."); return pos_ == other.pos_; }
|
||||
bool operator!=(const BasePathIterator& other) MIJIN_NOEXCEPT { MIJIN_ASSERT(full_ == other.full_, "Comparing unrelated iterators."); return pos_ != other.pos_; }
|
||||
bool operator<(const BasePathIterator& other) MIJIN_NOEXCEPT { MIJIN_ASSERT(full_ == other.full_, "Comparing unrelated iterators."); return pos_ < other.pos_; }
|
||||
bool operator<=(const BasePathIterator& other) MIJIN_NOEXCEPT { MIJIN_ASSERT(full_ == other.full_, "Comparing unrelated iterators."); return pos_ <= other.pos_; }
|
||||
bool operator>(const BasePathIterator& other) MIJIN_NOEXCEPT { MIJIN_ASSERT(full_ == other.full_, "Comparing unrelated iterators."); return pos_ > other.pos_; }
|
||||
bool operator>=(const BasePathIterator& other) MIJIN_NOEXCEPT { MIJIN_ASSERT(full_ == other.full_, "Comparing unrelated iterators."); return pos_ >= other.pos_; }
|
||||
|
||||
std::string_view operator*() const MIJIN_NOEXCEPT
|
||||
{
|
||||
MIJIN_ASSERT(pos_ != full_.end(), "Dereferencing an invalid iterator.");
|
||||
return {pos_, next_};
|
||||
}
|
||||
|
||||
BasePath& operator=(const BasePath&) = default;
|
||||
BasePath& operator=(BasePath&&) = default;
|
||||
BasePathIterator& operator++() MIJIN_NOEXCEPT
|
||||
{
|
||||
MIJIN_ASSERT(pos_ != full_.end(), "Iterating past end.");
|
||||
if (next_ == full_.end()) {
|
||||
pos_ = full_.end();
|
||||
}
|
||||
else
|
||||
{
|
||||
pos_ = std::next(next_);
|
||||
findNext();
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
auto operator<=>(const BasePath&) const noexcept = default;
|
||||
BasePathIterator operator++(int) const MIJIN_NOEXCEPT
|
||||
{
|
||||
BasePathIterator copy(*this);
|
||||
++copy;
|
||||
return copy;
|
||||
}
|
||||
|
||||
// TODO
|
||||
// BasePathIterator& operator--() MIJIN_NOEXCEPT
|
||||
// {
|
||||
// MIJIN_ASSERT(pos_ != full_.begin(), "Iterating past begin.");
|
||||
// next_ = std::prev(pos_);
|
||||
// pos_ = std::find(std::reverse_iterator(next_), std::reverse_iterator(full_.begin()), separator).base();
|
||||
// }
|
||||
private:
|
||||
void findNext()
|
||||
{
|
||||
next_ = std::find(pos_, full_.end(), separator);
|
||||
}
|
||||
|
||||
template<typename TConcrete, typename TCharOther, typename TTraitsOther>
|
||||
friend class ::mijin::MixinPath;
|
||||
};
|
||||
}
|
||||
|
||||
template<typename TConcrete, typename TChar, typename TTraits>
|
||||
class MixinPath
|
||||
{
|
||||
public:
|
||||
using traits_t = TTraits;
|
||||
using char_t = typename traits_t::char_t;
|
||||
using char_traits_t = typename traits_t::char_traits_t;
|
||||
using allocator_t = typename traits_t::allocator_t;
|
||||
using string_t = typename traits_t::string_t;
|
||||
using string_view_t = typename traits_t::string_view_t;
|
||||
using size_type = typename traits_t::size_type;
|
||||
using difference_type = typename traits_t::difference_type;
|
||||
using path_view_t = BasePathView<TChar, traits_t>;
|
||||
using iterator = impl::BasePathIterator<char_t, char_traits_t, traits_t::SEPARATOR>;
|
||||
using const_iterator = iterator;
|
||||
static constexpr char_t SEPARATOR = traits_t::SEPARATOR;
|
||||
|
||||
constexpr bool operator==(string_view_t stringView) const MIJIN_NOEXCEPT { return stringView() == stringView; }
|
||||
constexpr bool operator==(const TChar* cString) const MIJIN_NOEXCEPT { return stringView() == cString; }
|
||||
|
||||
[[nodiscard]]
|
||||
const string_t& string() const MIJIN_NOEXCEPT
|
||||
constexpr string_view_t getName() const MIJIN_NOEXCEPT;
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr path_view_t getParent() const MIJIN_NOEXCEPT;
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr string_view_t stringView() const MIJIN_NOEXCEPT
|
||||
{
|
||||
return static_cast<const TConcrete*>(this)->stringViewImpl();
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr bool empty() const MIJIN_NOEXCEPT { return stringView().empty(); }
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr size_type size() const MIJIN_NOEXCEPT { return stringView().size(); }
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr bool isAbsolute() const MIJIN_NOEXCEPT
|
||||
{
|
||||
return traits_t::isAbsolute(stringView());
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
iterator begin() const MIJIN_NOEXCEPT { return iterator(stringView(), stringView().begin()); }
|
||||
|
||||
[[nodiscard]]
|
||||
iterator end() const MIJIN_NOEXCEPT { return iterator(stringView(), stringView().end()); }
|
||||
private:
|
||||
|
||||
template<typename TConcreteOther, typename TCharOther, typename TTraitsOther>
|
||||
friend class MixinPath;
|
||||
};
|
||||
|
||||
template<typename TConcreteA, typename TConcreteB, typename TChar, typename TTraits>
|
||||
constexpr bool operator==(const MixinPath<TConcreteA, TChar, TTraits>& pathA, const MixinPath<TConcreteB, TChar, TTraits>& pathB) MIJIN_NOEXCEPT
|
||||
{
|
||||
return pathA.stringView() == pathB.stringView();
|
||||
}
|
||||
|
||||
template<typename TChar = char, typename TTraits = DefaultPathTraits<TChar>>
|
||||
class BasePath : public MixinPath<BasePath<TChar, TTraits>, TChar, TTraits>
|
||||
{
|
||||
public:
|
||||
using mixin_t = MixinPath<BasePath<TChar, TTraits>, TChar, TTraits>;
|
||||
using typename mixin_t::traits_t;
|
||||
using typename mixin_t::char_t;
|
||||
using typename mixin_t::char_traits_t;
|
||||
using typename mixin_t::allocator_t;
|
||||
using typename mixin_t::string_t;
|
||||
using typename mixin_t::string_view_t;
|
||||
using typename mixin_t::difference_type;
|
||||
using typename mixin_t::path_view_t;
|
||||
static constexpr char_t SEPARATOR = mixin_t::SEPARATOR;
|
||||
private:
|
||||
struct NoSimplify {};
|
||||
|
||||
string_t storage_;
|
||||
|
||||
constexpr BasePath(string_t storage, NoSimplify) MIJIN_NOEXCEPT_IF(std::is_nothrow_move_constructible<string_t>()) : storage_(std::move(storage)) {}
|
||||
public:
|
||||
constexpr BasePath() = default;
|
||||
constexpr BasePath(const BasePath&) = default;
|
||||
constexpr BasePath(BasePath&&) = default;
|
||||
constexpr BasePath(string_t string) MIJIN_NOEXCEPT_IF(std::is_nothrow_move_constructible<string_t>()) : storage_(std::move(string)) {
|
||||
simplify();
|
||||
}
|
||||
constexpr BasePath(const char_t* cString, allocator_t allocator = {}) : BasePath(string_t(cString, std::move(allocator))) {}
|
||||
constexpr BasePath(string_view_t stringView, allocator_t allocator = {}) : BasePath(string_t(stringView, std::move(allocator))) {}
|
||||
template<typename TConcreteOther> requires (!std::is_same_v<BasePath<TChar, TTraits>, TConcreteOther>)
|
||||
explicit constexpr BasePath(const MixinPath<TConcreteOther, TChar, TTraits> other, allocator_t allocator = {}) : BasePath(string_t(other.stringView(), std::move(allocator)), NoSimplify()) {}
|
||||
|
||||
constexpr BasePath& operator=(const BasePath&) = default;
|
||||
constexpr BasePath& operator=(BasePath&&) = default;
|
||||
|
||||
constexpr auto operator<=>(const BasePath&) const noexcept = default;
|
||||
using mixin_t::operator==;
|
||||
|
||||
template<typename TConcreteOther>
|
||||
BasePath& operator/=(const MixinPath<TConcreteOther, char_t, traits_t>& other);
|
||||
BasePath& operator/=(string_view_t more);
|
||||
|
||||
template<typename TConcreteOther>
|
||||
BasePath operator/(const MixinPath<TConcreteOther, char_t, traits_t>& other) const;
|
||||
BasePath operator/(string_view_t more) const;
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr const string_t& string() const MIJIN_NOEXCEPT
|
||||
{
|
||||
return storage_;
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr string_view_t stringViewImpl() const MIJIN_NOEXCEPT
|
||||
{
|
||||
return storage_;
|
||||
}
|
||||
private:
|
||||
void simplify() MIJIN_NOEXCEPT;
|
||||
constexpr void simplify() MIJIN_NOEXCEPT;
|
||||
};
|
||||
|
||||
using Path = BasePath<char>;
|
||||
using WPath = BasePath<wchar_t>;
|
||||
|
||||
template<typename TChar, TChar separator, typename TTraits, template<typename> typename TAllocator>
|
||||
void BasePath<TChar, separator, TTraits, TAllocator>::simplify() MIJIN_NOEXCEPT
|
||||
using NativePath = Path; // TODO
|
||||
|
||||
template<typename TChar, typename TTraits>
|
||||
class BasePathView : public MixinPath<BasePathView<TChar, TTraits>, TChar, TTraits>
|
||||
{
|
||||
public:
|
||||
using mixin_t = MixinPath<BasePathView<TChar, TTraits>, TChar, TTraits>;
|
||||
using typename mixin_t::char_t;
|
||||
using typename mixin_t::string_view_t;
|
||||
private:
|
||||
string_view_t view_;
|
||||
public:
|
||||
constexpr BasePathView() noexcept = default;
|
||||
constexpr BasePathView(const BasePathView&) noexcept = default;
|
||||
explicit constexpr BasePathView(string_view_t view) MIJIN_NOEXCEPT : view_(view) {}
|
||||
template<typename TIterator>
|
||||
constexpr BasePathView(const TIterator& begin, const TIterator& end) MIJIN_NOEXCEPT : view_(begin, end) {}
|
||||
template<typename TConcreteOther> requires (!std::is_same_v<BasePathView<TChar, TTraits>, TConcreteOther>)
|
||||
constexpr BasePathView(const MixinPath<TConcreteOther>& other) MIJIN_NOEXCEPT : view_(other.stringView()) {}
|
||||
|
||||
constexpr BasePathView& operator=(const BasePathView&) noexcept = default;
|
||||
|
||||
constexpr auto operator<=>(const BasePathView&) const noexcept = default;
|
||||
using mixin_t::operator==;
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr string_view_t stringViewImpl() const MIJIN_NOEXCEPT
|
||||
{
|
||||
return view_;
|
||||
}
|
||||
};
|
||||
|
||||
using PathView = BasePathView<char>;
|
||||
using WPathView = BasePathView<wchar_t>;
|
||||
|
||||
using NativePathView = PathView; // TODO
|
||||
|
||||
template<typename TConcrete, typename TChar, typename TTraits>
|
||||
constexpr auto MixinPath<TConcrete, TChar, TTraits>::getName() const MIJIN_NOEXCEPT -> string_view_t
|
||||
{
|
||||
const string_view_t strView = stringView();
|
||||
auto it = std::ranges::find(std::ranges::reverse_view(strView), SEPARATOR).base();
|
||||
return {it, strView.end()};
|
||||
}
|
||||
|
||||
template<typename TConcrete, typename TChar, typename TTraits>
|
||||
constexpr auto MixinPath<TConcrete, TChar, TTraits>::getParent() const MIJIN_NOEXCEPT -> path_view_t
|
||||
{
|
||||
const string_view_t strView = stringView();
|
||||
auto it = std::ranges::find(std::ranges::reverse_view(strView), SEPARATOR).base();
|
||||
if (it == strView.begin()) {
|
||||
return {};
|
||||
}
|
||||
if (std::prev(it) == strView.begin()) {
|
||||
return {strView.begin(), it}; // edge case, return "/" instead of nothing
|
||||
}
|
||||
return {strView.begin(), std::prev(it)};
|
||||
}
|
||||
|
||||
template<typename TChar, typename TTraits>
|
||||
template<typename TConcreteOther>
|
||||
auto BasePath<TChar, TTraits>::operator/=(const MixinPath<TConcreteOther, char_t, traits_t>& other) -> BasePath&
|
||||
{
|
||||
if (other.isAbsolute())
|
||||
{
|
||||
storage_ = other.stringView();
|
||||
}
|
||||
else if (!other.empty())
|
||||
{
|
||||
if (other.stringView()[0] != SEPARATOR)
|
||||
{
|
||||
storage_.reserve(storage_.size() + other.size() + 1);
|
||||
storage_.push_back(SEPARATOR);
|
||||
}
|
||||
storage_.append_range(other.stringView());
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename TChar, typename TTraits>
|
||||
auto BasePath<TChar, TTraits>::operator/=(string_view_t more) -> BasePath&
|
||||
{
|
||||
operator/=(path_view_t(more));
|
||||
simplify();
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename TChar, typename TTraits>
|
||||
template<typename TConcreteOther>
|
||||
auto BasePath<TChar, TTraits>::operator/(const MixinPath<TConcreteOther, char_t, traits_t>& other) const -> BasePath
|
||||
{
|
||||
if (other.isAbsolute() || other.empty())
|
||||
{
|
||||
return BasePath(string_t(other.stringView(), storage_.get_allocator()), NoSimplify());
|
||||
}
|
||||
const bool addSeparator = other.stringView()[0] != SEPARATOR;
|
||||
string_t combined(storage_.get_allocator());
|
||||
combined.reserve(storage_.size() + other.stringView().size() + (addSeparator ? 1 : 0));
|
||||
combined.append_range(storage_);
|
||||
if (addSeparator) {
|
||||
combined.push_back(SEPARATOR);
|
||||
}
|
||||
combined.append_range(other.stringView());
|
||||
return BasePath(std::move(combined), NoSimplify());
|
||||
}
|
||||
|
||||
template<typename TChar, typename TTraits>
|
||||
auto BasePath<TChar, TTraits>::operator/(string_view_t other) const -> BasePath
|
||||
{
|
||||
BasePath combined = (*this / path_view_t(other));
|
||||
combined.simplify();
|
||||
return combined;
|
||||
}
|
||||
|
||||
template<typename TChar, typename TTraits>
|
||||
constexpr void BasePath<TChar, TTraits>::simplify() MIJIN_NOEXCEPT
|
||||
{
|
||||
// step 1: remove double slashes
|
||||
difference_type moveBy = 0;
|
||||
for (auto it = std::next(storage_.begin()); it < storage_.end(); ++it)
|
||||
{
|
||||
const bool doubleSlash = (*it == separator && *std::prev(it) == separator); // check this before moving the current character, as that might create a double slash itself
|
||||
const bool doubleSlash = (*it == SEPARATOR && *std::prev(it) == SEPARATOR); // check this before moving the current character, as that might create a double slash itself
|
||||
if (moveBy > 0) {
|
||||
*std::prev(it, moveBy) = *it;
|
||||
}
|
||||
@@ -61,7 +390,7 @@ void BasePath<TChar, separator, TTraits, TAllocator>::simplify() MIJIN_NOEXCEPT
|
||||
}
|
||||
}
|
||||
// step 1.5: remove trailing slash (but only if it's not the only remaining char)
|
||||
if (moveBy < static_cast<difference_type>(storage_.size() - 1) && storage_[storage_.size() - moveBy - 1] == separator) {
|
||||
if (moveBy < static_cast<difference_type>(storage_.size() - 1) && storage_[storage_.size() - moveBy - 1] == SEPARATOR) {
|
||||
++moveBy;
|
||||
}
|
||||
storage_.resize(storage_.size() - moveBy);
|
||||
@@ -74,8 +403,8 @@ void BasePath<TChar, separator, TTraits, TAllocator>::simplify() MIJIN_NOEXCEPT
|
||||
{
|
||||
*std::prev(it, moveBy) = *it;
|
||||
}
|
||||
if (*std::prev(it, moveBy) == TChar('.') && *std::prev(it, moveBy + 1) == TChar('.') && *std::prev(it, moveBy + 2) == separator
|
||||
&& (std::next(it) == storage_.end() || *std::next(it) == separator))
|
||||
if (*std::prev(it, moveBy) == TChar('.') && *std::prev(it, moveBy + 1) == TChar('.') && *std::prev(it, moveBy + 2) == SEPARATOR
|
||||
&& (std::next(it) == storage_.end() || *std::next(it) == SEPARATOR))
|
||||
{
|
||||
if (std::prev(it, moveBy + 2) == storage_.begin())
|
||||
{
|
||||
@@ -86,7 +415,7 @@ void BasePath<TChar, separator, TTraits, TAllocator>::simplify() MIJIN_NOEXCEPT
|
||||
// find the start of the preceeding segment
|
||||
for (auto itStart = std::prev(it, moveBy + 3);; --itStart)
|
||||
{
|
||||
if (*std::prev(itStart) == separator || std::prev(itStart) == storage_.begin())
|
||||
if (*std::prev(itStart) == SEPARATOR || std::prev(itStart) == storage_.begin())
|
||||
{
|
||||
// /path/with/../double/dot
|
||||
// itStart --A A
|
||||
@@ -110,8 +439,8 @@ void BasePath<TChar, separator, TTraits, TAllocator>::simplify() MIJIN_NOEXCEPT
|
||||
const bool atStart = (it == storage_.begin());
|
||||
const bool atEnd = (std::next(it) == storage_.end());
|
||||
const bool emptyEle = (*it == TChar('.') // char is a dot
|
||||
&& (atStart || *std::prev(it, moveBy + 1) == separator) // previous is a slash or doesn't exist
|
||||
&& (atEnd || *std::next(it) == separator)); // next is a slash or doesn't exist
|
||||
&& (atStart || *std::prev(it, moveBy + 1) == SEPARATOR) // previous is a slash or doesn't exist
|
||||
&& (atEnd || *std::next(it) == SEPARATOR)); // next is a slash or doesn't exist
|
||||
if (moveBy > 0) {
|
||||
*std::prev(it, moveBy) = *it;
|
||||
}
|
||||
@@ -124,6 +453,59 @@ void BasePath<TChar, separator, TTraits, TAllocator>::simplify() MIJIN_NOEXCEPT
|
||||
}
|
||||
storage_.resize(storage_.size() - moveBy);
|
||||
}
|
||||
} // namespace shiken
|
||||
|
||||
template<typename TChar, TChar separator = TChar('/'), typename TTraits = std::char_traits<TChar>>
|
||||
constexpr bool verifyPathString(std::basic_string_view<TChar, TTraits> stringView) MIJIN_NOEXCEPT
|
||||
{
|
||||
for (auto it = std::next(stringView.begin()); it < stringView.end(); ++it)
|
||||
{
|
||||
if(*it == separator && *std::prev(it) == separator) {
|
||||
return false; // double slash
|
||||
}
|
||||
if (it != std::next(stringView.begin())
|
||||
&& *it == TChar('.') && *std::prev(it) == TChar('.') && *std::prev(it, 2) == separator
|
||||
&& (std::next(it) == stringView.end() || *std::next(it) == separator)) {
|
||||
return false; // "/.."
|
||||
}
|
||||
const bool atStart = (it == stringView.begin());
|
||||
const bool atEnd = (std::next(it) == stringView.end());
|
||||
if(*it == TChar('.') // char is a dot
|
||||
&& (atStart || *std::prev(it) == separator) // previous is a slash or doesn't exist
|
||||
&& (atEnd || *std::next(it) == separator)) // next is a slash or doesn't exist
|
||||
{
|
||||
return false; // "/."
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
consteval PathView operator""_pv(const char* cString, std::size_t len)
|
||||
{
|
||||
if (!verifyPathString(std::string_view(cString, len))) {
|
||||
throw "Invalid path string.";
|
||||
}
|
||||
return PathView(std::string_view(cString, len));
|
||||
}
|
||||
|
||||
consteval WPathView operator""_pv(const wchar_t* cString, std::size_t len)
|
||||
{
|
||||
if (!verifyPathString(std::wstring_view(cString, len))) {
|
||||
throw "Invalid path string.";
|
||||
}
|
||||
return WPathView(std::wstring_view(cString, len));
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
NativePath getWorkingDirectory();
|
||||
} // namespace mijin
|
||||
|
||||
template<mijin::BasePathType TPath>
|
||||
struct std::hash<TPath>
|
||||
{
|
||||
std::size_t operator()(const TPath& path) const MIJIN_NOEXCEPT
|
||||
{
|
||||
return std::hash<std::string_view>()(path.stringView());
|
||||
}
|
||||
};
|
||||
|
||||
#endif // !defined(MIJIN_TYPES_PATH_HPP_INCLUDED)
|
||||
|
||||
Reference in New Issue
Block a user