Nix 2.93.3
Lix: A modern, delicious implementation of the Nix package manager; unstable internal interfaces
Loading...
Searching...
No Matches
canon-path.hh
Go to the documentation of this file.
1#pragma once
3
4#include <string>
5#include <optional>
6#include <cassert>
7#include <iostream>
8#include <set>
9
10namespace nix {
11
28{
29 std::string path;
30
31public:
32
37 CanonPath(std::string_view raw);
38
39 explicit CanonPath(const char * raw)
40 : CanonPath(std::string_view(raw))
41 { }
42
43 struct unchecked_t { };
44
45 CanonPath(unchecked_t _, std::string path)
46 : path(std::move(path))
47 { }
48
49 static CanonPath fromCwd(std::string_view path = ".");
50
51 static CanonPath root;
52
58 CanonPath(std::string_view raw, const CanonPath & root);
59
60 bool isRoot() const
61 { return path.size() <= 1; }
62
63 explicit operator std::string_view() const
64 { return path; }
65
66 const std::string & abs() const
67 { return path; }
68
73 const std::string & absOrEmpty() const
74 {
75 const static std::string epsilon;
76 return isRoot() ? epsilon : path;
77 }
78
79 const char * c_str() const
80 { return path.c_str(); }
81
82 std::string_view rel() const
83 { return ((std::string_view) path).substr(1); }
84
85 struct Iterator
86 {
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;
91
92 std::string_view remaining;
93 size_t slash;
94
95 Iterator(std::string_view remaining)
96 : remaining(remaining)
97 , slash(remaining.find('/'))
98 { }
99
100 bool operator != (const Iterator & x) const
101 { return remaining.data() != x.remaining.data(); }
102
103 bool operator == (const Iterator & x) const
104 { return !(*this != x); }
105
106 const std::string_view operator * () const
107 { return remaining.substr(0, slash); }
108
109 Iterator & operator ++ ()
110 {
111 if (slash == remaining.npos)
112 remaining = remaining.substr(remaining.size());
113 else {
114 remaining = remaining.substr(slash + 1);
115 slash = remaining.find('/');
116 }
117 return *this;
118 }
119
120 Iterator operator++(int)
121 {
122 auto result = *this;
123 ++*this;
124 return result;
125 }
126 };
127
128 Iterator begin() const { return Iterator(rel()); }
129 Iterator end() const { return Iterator(rel().substr(path.size() - 1)); }
130
131 std::optional<CanonPath> parent() const;
132
136 void pop();
137
138 std::optional<std::string_view> dirOf() const
139 {
140 if (isRoot()) return std::nullopt;
141 return ((std::string_view) path).substr(0, path.rfind('/'));
142 }
143
144 std::optional<std::string_view> baseName() const
145 {
146 if (isRoot()) return std::nullopt;
147 return ((std::string_view) path).substr(path.rfind('/') + 1);
148 }
149
150 bool operator == (const CanonPath & x) const
151 { return path == x.path; }
152
153 bool operator != (const CanonPath & x) const
154 { return path != x.path; }
155
162 bool operator < (const CanonPath & x) const
163 {
164 auto i = path.begin();
165 auto j = x.path.begin();
166 for ( ; i != path.end() && j != x.path.end(); ++i, ++j) {
167 auto c_i = *i;
168 if (c_i == '/') c_i = 0;
169 auto c_j = *j;
170 if (c_j == '/') c_j = 0;
171 if (c_i < c_j) return true;
172 if (c_i > c_j) return false;
173 }
174 return i == path.end() && j != x.path.end();
175 }
176
181 bool isWithin(const CanonPath & parent) const;
182
183 CanonPath removePrefix(const CanonPath & prefix) const;
184
188 void extend(const CanonPath & x);
189
193 CanonPath operator + (const CanonPath & x) const;
194
198 void push(std::string_view c);
199
200 CanonPath operator + (std::string_view c) const;
201
208 bool isAllowed(const std::set<CanonPath> & allowed) const;
209
214 std::string makeRelative(const CanonPath & path) const;
215};
216
217std::ostream & operator << (std::ostream & stream, const CanonPath & path);
218
219}
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