Nix 2.26.3
Nix, the purely functional 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#include <vector>
10
11namespace nix {
12
41{
42 std::string path;
43
44public:
45
50 CanonPath(std::string_view raw);
51
52 explicit CanonPath(const char * raw)
53 : CanonPath(std::string_view(raw))
54 { }
55
56 struct unchecked_t { };
57
58 CanonPath(unchecked_t _, std::string path)
59 : path(std::move(path))
60 { }
61
65 CanonPath(const std::vector<std::string> & elems);
66
67 static CanonPath root;
68
74 CanonPath(std::string_view raw, const CanonPath & root);
75
76 bool isRoot() const
77 { return path.size() <= 1; }
78
79 explicit operator std::string_view() const
80 { return path; }
81
82 const std::string & abs() const
83 { return path; }
84
89 const std::string & absOrEmpty() const
90 {
91 const static std::string epsilon;
92 return isRoot() ? epsilon : path;
93 }
94
95 const char * c_str() const
96 { return path.c_str(); }
97
98 std::string_view rel() const
99 { return ((std::string_view) path).substr(1); }
100
101 const char * rel_c_str() const
102 {
103 auto cs = path.c_str();
104 assert(cs[0]); // for safety if invariant is broken
105 return &cs[1];
106 }
107
108 struct Iterator
109 {
110 std::string_view remaining;
111 size_t slash;
112
113 Iterator(std::string_view remaining)
114 : remaining(remaining)
115 , slash(remaining.find('/'))
116 { }
117
118 bool operator != (const Iterator & x) const
119 { return remaining.data() != x.remaining.data(); }
120
121 bool operator == (const Iterator & x) const
122 { return !(*this != x); }
123
124 const std::string_view operator * () const
125 { return remaining.substr(0, slash); }
126
127 void operator ++ ()
128 {
129 if (slash == remaining.npos)
130 remaining = remaining.substr(remaining.size());
131 else {
132 remaining = remaining.substr(slash + 1);
133 slash = remaining.find('/');
134 }
135 }
136 };
137
138 Iterator begin() const { return Iterator(rel()); }
139 Iterator end() const { return Iterator(rel().substr(path.size() - 1)); }
140
141 std::optional<CanonPath> parent() const;
142
146 void pop();
147
148 std::optional<std::string_view> dirOf() const
149 {
150 if (isRoot()) return std::nullopt;
151 return ((std::string_view) path).substr(0, path.rfind('/'));
152 }
153
154 std::optional<std::string_view> baseName() const
155 {
156 if (isRoot()) return std::nullopt;
157 return ((std::string_view) path).substr(path.rfind('/') + 1);
158 }
159
160 bool operator == (const CanonPath & x) const
161 { return path == x.path; }
162
163 bool operator != (const CanonPath & x) const
164 { return path != x.path; }
165
172 auto operator <=> (const CanonPath & x) const
173 {
174 auto i = path.begin();
175 auto j = x.path.begin();
176 for ( ; i != path.end() && j != x.path.end(); ++i, ++j) {
177 auto c_i = *i;
178 if (c_i == '/') c_i = 0;
179 auto c_j = *j;
180 if (c_j == '/') c_j = 0;
181 if (auto cmp = c_i <=> c_j; cmp != 0) return cmp;
182 }
183 return (i != path.end()) <=> (j != x.path.end());
184 }
185
190 bool isWithin(const CanonPath & parent) const;
191
192 CanonPath removePrefix(const CanonPath & prefix) const;
193
197 void extend(const CanonPath & x);
198
202 CanonPath operator / (const CanonPath & x) const;
203
207 void push(std::string_view c);
208
209 CanonPath operator / (std::string_view c) const;
210
217 bool isAllowed(const std::set<CanonPath> & allowed) const;
218
223 std::string makeRelative(const CanonPath & path) const;
224
225 friend class std::hash<CanonPath>;
226};
227
228std::ostream & operator << (std::ostream & stream, const CanonPath & path);
229
230}
231
232template<>
233struct std::hash<nix::CanonPath>
234{
235 std::size_t operator ()(const nix::CanonPath & s) const noexcept
236 {
237 return std::hash<std::string>{}(s.path);
238 }
239};
Definition canon-path.hh:41
bool isWithin(const CanonPath &parent) const
Definition canon-path.cc:45
void pop()
Definition canon-path.cc:39
std::string makeRelative(const CanonPath &path) const
Definition canon-path.cc:120
auto operator<=>(const CanonPath &x) const
Definition canon-path.hh:172
const std::string & absOrEmpty() const
Definition canon-path.hh:89
CanonPath operator/(const CanonPath &x) const
Definition canon-path.cc:71
bool isAllowed(const std::set< CanonPath > &allowed) const
Definition canon-path.cc:93
void push(std::string_view c)
Definition canon-path.cc:78
CanonPath(std::string_view raw)
Definition canon-path.cc:15
void extend(const CanonPath &x)
Definition canon-path.cc:62
PosIdx end
Definition lexer.l:5814
std::optional< CanonPath > parent() const
auto i
Definition lexer.l:2745
return s
Definition lexer.l:459
T x
Definition lexer.l:2648
CanonPath(std::string_view raw)
ExprAttrs::AttrDefs::iterator j
Definition lexer.l:8572
std::string path
Definition lexer.l:1399
Definition lexer.l:1466
Definition canon-path.hh:109
Definition canon-path.hh:56