46 : path(std::move(path))
49 static CanonPath fromCwd(std::string_view path =
".");
61 {
return path.size() <= 1; }
63 explicit operator std::string_view()
const
66 const std::string & abs()
const
75 const static std::string epsilon;
76 return isRoot() ? epsilon : path;
79 const char * c_str()
const
80 {
return path.c_str(); }
82 std::string_view rel()
const
83 {
return ((std::string_view) path).substr(1); }
87 using difference_type = void;
88 using value_type = std::string_view;
89 using reference = std::string_view;
90 using iterator_category = std::input_iterator_tag;
92 std::string_view remaining;
95 Iterator(std::string_view remaining)
96 : remaining(remaining)
97 , slash(remaining.find(
'/'))
100 bool operator != (
const Iterator & x)
const
101 {
return remaining.data() != x.remaining.data(); }
103 bool operator == (
const Iterator & x)
const
104 {
return !(*
this != x); }
106 const std::string_view operator * ()
const
107 {
return remaining.substr(0, slash); }
109 Iterator & operator ++ ()
111 if (slash == remaining.npos)
112 remaining = remaining.substr(remaining.size());
114 remaining = remaining.substr(slash + 1);
115 slash = remaining.find(
'/');
120 Iterator operator++(
int)
129 Iterator end()
const {
return Iterator(rel().substr(path.size() - 1)); }
131 std::optional<CanonPath> parent()
const;
138 std::optional<std::string_view> dirOf()
const
140 if (isRoot())
return std::nullopt;
141 return ((std::string_view) path).substr(0, path.rfind(
'/'));
144 std::optional<std::string_view> baseName()
const
146 if (isRoot())
return std::nullopt;
147 return ((std::string_view) path).substr(path.rfind(
'/') + 1);
150 bool operator == (
const CanonPath & x)
const
151 {
return path == x.path; }
153 bool operator != (
const CanonPath & x)
const
154 {
return path != x.path; }
164 auto i = path.begin();
165 auto j = x.path.begin();
166 for ( ; i != path.end() && j != x.path.end(); ++i, ++j) {
168 if (c_i ==
'/') c_i = 0;
170 if (c_j ==
'/') c_j = 0;
171 if (c_i < c_j)
return true;
172 if (c_i > c_j)
return false;
174 return i == path.end() && j != x.path.end();
198 void push(std::string_view c);
208 bool isAllowed(
const std::set<CanonPath> & allowed)
const;
217std::ostream & operator << (std::ostream & stream,
const CanonPath & path);
Definition canon-path.hh:28
bool isWithin(const CanonPath &parent) const
Definition canon-path.cc:33
void pop()
Definition canon-path.cc:27
std::string makeRelative(const CanonPath &path) const
Definition canon-path.cc:108
const std::string & absOrEmpty() const
Definition canon-path.hh:73
bool isAllowed(const std::set< CanonPath > &allowed) const
Definition canon-path.cc:81
void push(std::string_view c)
Definition canon-path.cc:66
CanonPath(std::string_view raw)
Definition canon-path.cc:8
CanonPath operator+(const CanonPath &x) const
Definition canon-path.cc:59
void extend(const CanonPath &x)
Definition canon-path.cc:50
bool operator<(const CanonPath &x) const
Definition canon-path.hh:162
Definition canon-path.hh:86
Definition canon-path.hh:43