blob: 22ad23b42fe55b1a26360dc716565a9b0696742a [file] [log] [blame]
Eric Fiselierc7979582016-06-17 19:46:40 +00001//===--------------------- filesystem/path.cpp ----------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9#include "experimental/filesystem"
Eric Fiselier0fdab5e2016-07-23 03:10:56 +000010#include "string_view"
Eric Fiselierc7979582016-06-17 19:46:40 +000011#include "utility"
12
13_LIBCPP_BEGIN_NAMESPACE_EXPERIMENTAL_FILESYSTEM
14
15_LIBCPP_CONSTEXPR path::value_type path::preferred_separator;
Eric Fiselierc7979582016-06-17 19:46:40 +000016
Eric Fiselier0fdab5e2016-07-23 03:10:56 +000017
18using string_view_t = path::__string_view;
19
Eric Fiselierc7979582016-06-17 19:46:40 +000020namespace { namespace parser
21{
22
Eric Fiselierc7979582016-06-17 19:46:40 +000023using value_type = path::value_type;
Eric Fiselier0fdab5e2016-07-23 03:10:56 +000024using string_view_pair = pair<string_view_t, string_view_t>;
Eric Fiselierc7979582016-06-17 19:46:40 +000025
26// status reporting
27constexpr size_t npos = static_cast<size_t>(-1);
28
29inline bool good(size_t pos) { return pos != npos; }
30
31// lexical elements
32constexpr value_type preferred_separator = path::preferred_separator;
33constexpr value_type const * preferred_separator_str = "/";
34constexpr value_type const * dot = ".";
35
36// forward //
Eric Fiselier0fdab5e2016-07-23 03:10:56 +000037bool is_separator(string_view_t const &, size_t);
38bool is_root_name(const string_view_t&, size_t);
39bool is_root_directory(string_view_t const &, size_t);
40bool is_trailing_separator(string_view_t const &, size_t);
Eric Fiselierc7979582016-06-17 19:46:40 +000041
Eric Fiselier0fdab5e2016-07-23 03:10:56 +000042size_t start_of(string_view_t const &, size_t);
43size_t end_of(string_view_t const &, size_t);
Eric Fiselierc7979582016-06-17 19:46:40 +000044
Eric Fiselier0fdab5e2016-07-23 03:10:56 +000045size_t root_name_start(const string_view_t& s);
46size_t root_name_end(const string_view_t&);
Eric Fiselierc7979582016-06-17 19:46:40 +000047
Eric Fiselier0fdab5e2016-07-23 03:10:56 +000048size_t root_directory_start(string_view_t const &);
49size_t root_directory_end(string_view_t const &);
Eric Fiselierc7979582016-06-17 19:46:40 +000050
Eric Fiselier0fdab5e2016-07-23 03:10:56 +000051string_view_pair separate_filename(string_view_t const &);
52string_view extract_raw(string_view_t const &, size_t);
53string_view extract_preferred(string_view_t const &, size_t);
Eric Fiselierc7979582016-06-17 19:46:40 +000054
Eric Fiselier0fdab5e2016-07-23 03:10:56 +000055inline bool is_separator(const string_view_t& s, size_t pos) {
Eric Fiselierc7979582016-06-17 19:46:40 +000056 return (pos < s.size() && s[pos] == preferred_separator);
57}
58
Eric Fiselier0fdab5e2016-07-23 03:10:56 +000059inline bool is_root_name(const string_view_t& s, size_t pos) {
Eric Fiselierc7979582016-06-17 19:46:40 +000060 return good(pos) && pos == 0 ? root_name_start(s) == pos : false;
61}
62
Eric Fiselier0fdab5e2016-07-23 03:10:56 +000063inline bool is_root_directory(const string_view_t& s, size_t pos) {
Eric Fiselierc7979582016-06-17 19:46:40 +000064 return good(pos) ? root_directory_start(s) == pos : false;
65}
66
Eric Fiselier0fdab5e2016-07-23 03:10:56 +000067inline bool is_trailing_separator(const string_view_t& s, size_t pos) {
Eric Fiselierc7979582016-06-17 19:46:40 +000068 return (pos < s.size() && is_separator(s, pos) &&
69 end_of(s, pos) == s.size()-1 &&
70 !is_root_directory(s, pos) && !is_root_name(s, pos));
71}
72
Eric Fiselier0fdab5e2016-07-23 03:10:56 +000073size_t start_of(const string_view_t& s, size_t pos) {
Eric Fiselierc7979582016-06-17 19:46:40 +000074 if (pos >= s.size()) return npos;
75 bool in_sep = (s[pos] == preferred_separator);
76 while (pos - 1 < s.size() &&
77 (s[pos-1] == preferred_separator) == in_sep)
78 { --pos; }
79 if (pos == 2 && !in_sep && s[0] == preferred_separator &&
80 s[1] == preferred_separator)
81 { return 0; }
82 return pos;
83}
84
Eric Fiselier0fdab5e2016-07-23 03:10:56 +000085size_t end_of(const string_view_t& s, size_t pos) {
Eric Fiselierc7979582016-06-17 19:46:40 +000086 if (pos >= s.size()) return npos;
87 // special case for root name
88 if (pos == 0 && is_root_name(s, pos)) return root_name_end(s);
89 bool in_sep = (s[pos] == preferred_separator);
90 while (pos + 1 < s.size() && (s[pos+1] == preferred_separator) == in_sep)
91 { ++pos; }
92 return pos;
93}
94
Eric Fiselier0fdab5e2016-07-23 03:10:56 +000095inline size_t root_name_start(const string_view_t& s) {
Eric Fiselierc7979582016-06-17 19:46:40 +000096 return good(root_name_end(s)) ? 0 : npos;
97}
98
Eric Fiselier0fdab5e2016-07-23 03:10:56 +000099size_t root_name_end(const string_view_t& s) {
Eric Fiselierc7979582016-06-17 19:46:40 +0000100 if (s.size() < 2 || s[0] != preferred_separator
101 || s[1] != preferred_separator) {
102 return npos;
103 }
104 if (s.size() == 2) {
105 return 1;
106 }
107 size_t index = 2; // current position
108 if (s[index] == preferred_separator) {
109 return npos;
110 }
111 while (index + 1 < s.size() && s[index+1] != preferred_separator) {
112 ++index;
113 }
114 return index;
115}
116
Eric Fiselier0fdab5e2016-07-23 03:10:56 +0000117size_t root_directory_start(const string_view_t& s) {
Eric Fiselierc7979582016-06-17 19:46:40 +0000118 size_t e = root_name_end(s);
119 if (!good(e))
120 return is_separator(s, 0) ? 0 : npos;
121 return is_separator(s, e + 1) ? e + 1 : npos;
122}
123
Eric Fiselier0fdab5e2016-07-23 03:10:56 +0000124size_t root_directory_end(const string_view_t& s) {
Eric Fiselierc7979582016-06-17 19:46:40 +0000125 size_t st = root_directory_start(s);
126 if (!good(st)) return npos;
127 size_t index = st;
128 while (index + 1 < s.size() && s[index + 1] == preferred_separator)
129 { ++index; }
130 return index;
131}
132
Eric Fiselier0fdab5e2016-07-23 03:10:56 +0000133string_view_pair separate_filename(string_view_t const & s) {
Eric Fiselierc7979582016-06-17 19:46:40 +0000134 if (s == "." || s == ".." || s.empty()) return string_view_pair{s, ""};
135 auto pos = s.find_last_of('.');
Eric Fiselier0fdab5e2016-07-23 03:10:56 +0000136 if (pos == string_view_t::npos) return string_view_pair{s, string_view{}};
Eric Fiselierc7979582016-06-17 19:46:40 +0000137 return string_view_pair{s.substr(0, pos), s.substr(pos)};
138}
139
Eric Fiselier0fdab5e2016-07-23 03:10:56 +0000140inline string_view extract_raw(const string_view_t& s, size_t pos) {
Eric Fiselierc7979582016-06-17 19:46:40 +0000141 size_t end_i = end_of(s, pos);
142 if (!good(end_i)) return string_view{};
143 return string_view(s).substr(pos, end_i - pos + 1);
144}
145
Eric Fiselier0fdab5e2016-07-23 03:10:56 +0000146string_view extract_preferred(const string_view_t& s, size_t pos) {
Eric Fiselierc7979582016-06-17 19:46:40 +0000147 string_view raw = extract_raw(s, pos);
148 if (raw.empty())
149 return raw;
150 if (is_trailing_separator(s, pos))
151 return string_view{dot};
152 if (is_separator(s, pos) && !is_root_name(s, pos))
153 return string_view(preferred_separator_str);
154 return raw;
155}
156
157}} // namespace parser
158
159
160////////////////////////////////////////////////////////////////////////////////
161// path_view_iterator
162////////////////////////////////////////////////////////////////////////////////
163namespace {
164
165struct path_view_iterator {
166 const string_view __s_;
167 size_t __pos_;
168
169 explicit path_view_iterator(string_view const& __s) : __s_(__s), __pos_(__s_.empty() ? parser::npos : 0) {}
170 explicit path_view_iterator(string_view const& __s, size_t __p) : __s_(__s), __pos_(__p) {}
171
172 string_view operator*() const {
173 return parser::extract_preferred(__s_, __pos_);
174 }
175
176 path_view_iterator& operator++() {
177 increment();
178 return *this;
179 }
180
181 path_view_iterator& operator--() {
182 decrement();
183 return *this;
184 }
185
186 void increment() {
187 if (__pos_ == parser::npos) return;
188 while (! set_position(parser::end_of(__s_, __pos_)+1))
189 ;
190 return;
191 }
192
193 void decrement() {
194 if (__pos_ == 0) {
195 set_position(0);
196 }
197 else if (__pos_ == parser::npos) {
198 auto const str_size = __s_.size();
199 set_position(parser::start_of(
200 __s_, str_size != 0 ? str_size - 1 : str_size));
201 } else {
202 while (!set_position(parser::start_of(__s_, __pos_-1)))
203 ;
204 }
205 }
206
207 bool set_position(size_t pos) {
208 if (pos >= __s_.size()) {
209 __pos_ = parser::npos;
210 } else {
211 __pos_ = pos;
212 }
213 return valid_iterator_position();
214 }
215
216 bool valid_iterator_position() const {
217 if (__pos_ == parser::npos) return true; // end position is valid
218 return (!parser::is_separator (__s_, __pos_) ||
219 parser::is_root_directory (__s_, __pos_) ||
220 parser::is_trailing_separator(__s_, __pos_) ||
221 parser::is_root_name (__s_, __pos_));
222 }
223
224 bool is_end() const { return __pos_ == parser::npos; }
225
226 inline bool operator==(path_view_iterator const& __p) {
227 return __pos_ == __p.__pos_;
228 }
229};
230
231path_view_iterator pbegin(path const& p) {
232 return path_view_iterator(p.native());
233}
234
235path_view_iterator pend(path const& p) {
236 path_view_iterator __p(p.native());
237 __p.__pos_ = parser::npos;
238 return __p;
239}
240
241} // end namespace
242///////////////////////////////////////////////////////////////////////////////
243// path definitions
244///////////////////////////////////////////////////////////////////////////////
245
246path & path::replace_extension(path const & replacement)
247{
248 path p = extension();
249 if (not p.empty()) {
250 __pn_.erase(__pn_.size() - p.native().size());
251 }
252 if (!replacement.empty()) {
253 if (replacement.native()[0] != '.') {
254 __pn_ += ".";
255 }
256 __pn_.append(replacement.__pn_);
257 }
258 return *this;
259}
260
261///////////////////////////////////////////////////////////////////////////////
262// path.decompose
263
Eric Fiselier0fdab5e2016-07-23 03:10:56 +0000264string_view_t path::__root_name() const
Eric Fiselierc7979582016-06-17 19:46:40 +0000265{
266 return parser::is_root_name(__pn_, 0)
267 ? parser::extract_preferred(__pn_, 0)
Eric Fiselier0fdab5e2016-07-23 03:10:56 +0000268 : string_view_t{};
Eric Fiselierc7979582016-06-17 19:46:40 +0000269}
270
Eric Fiselier0fdab5e2016-07-23 03:10:56 +0000271string_view_t path::__root_directory() const
Eric Fiselierc7979582016-06-17 19:46:40 +0000272{
273 auto start_i = parser::root_directory_start(__pn_);
274 if(!parser::good(start_i)) {
275 return {};
276 }
277 return parser::extract_preferred(__pn_, start_i);
278}
279
Eric Fiselier0fdab5e2016-07-23 03:10:56 +0000280string_view_t path::__relative_path() const
Eric Fiselierc7979582016-06-17 19:46:40 +0000281{
282 if (empty()) {
Eric Fiselier0fdab5e2016-07-23 03:10:56 +0000283 return __pn_;
Eric Fiselierc7979582016-06-17 19:46:40 +0000284 }
285 auto end_i = parser::root_directory_end(__pn_);
286 if (not parser::good(end_i)) {
287 end_i = parser::root_name_end(__pn_);
288 }
289 if (not parser::good(end_i)) {
Eric Fiselier0fdab5e2016-07-23 03:10:56 +0000290 return __pn_;
Eric Fiselierc7979582016-06-17 19:46:40 +0000291 }
Eric Fiselier0fdab5e2016-07-23 03:10:56 +0000292 return string_view_t(__pn_).substr(end_i+1);
Eric Fiselierc7979582016-06-17 19:46:40 +0000293}
294
Eric Fiselier0fdab5e2016-07-23 03:10:56 +0000295string_view_t path::__parent_path() const
Eric Fiselierc7979582016-06-17 19:46:40 +0000296{
297 if (empty() || pbegin(*this) == --pend(*this)) {
298 return {};
299 }
300 auto end_it = --(--pend(*this));
301 auto end_i = parser::end_of(__pn_, end_it.__pos_);
Eric Fiselier0fdab5e2016-07-23 03:10:56 +0000302 return string_view_t(__pn_).substr(0, end_i+1);
Eric Fiselierc7979582016-06-17 19:46:40 +0000303}
304
Eric Fiselier0fdab5e2016-07-23 03:10:56 +0000305string_view_t path::__filename() const
Eric Fiselierc7979582016-06-17 19:46:40 +0000306{
Eric Fiselier0fdab5e2016-07-23 03:10:56 +0000307 return empty() ? string_view_t{} : *--pend(*this);
Eric Fiselierc7979582016-06-17 19:46:40 +0000308}
309
Eric Fiselier0fdab5e2016-07-23 03:10:56 +0000310string_view_t path::__stem() const
Eric Fiselierc7979582016-06-17 19:46:40 +0000311{
312 return parser::separate_filename(__filename()).first;
313}
314
Eric Fiselier0fdab5e2016-07-23 03:10:56 +0000315string_view_t path::__extension() const
Eric Fiselierc7979582016-06-17 19:46:40 +0000316{
317 return parser::separate_filename(__filename()).second;
318}
319
320////////////////////////////////////////////////////////////////////////////
321// path.comparisons
Eric Fiselier0fdab5e2016-07-23 03:10:56 +0000322int path::__compare(string_view_t __s) const {
Eric Fiselierc7979582016-06-17 19:46:40 +0000323 path_view_iterator thisIter(this->native());
324 path_view_iterator sIter(__s);
325 while (!thisIter.is_end() && !sIter.is_end()) {
326 int res = (*thisIter).compare(*sIter);
327 if (res != 0) return res;
328 ++thisIter; ++sIter;
329 }
330 if (thisIter.is_end() && sIter.is_end())
331 return 0;
332 if (thisIter.is_end())
333 return -1;
334 return 1;
335}
336
337////////////////////////////////////////////////////////////////////////////
338// path.nonmembers
339size_t hash_value(const path& __p) _NOEXCEPT {
340 path_view_iterator thisIter(__p.native());
341 struct HashPairT {
342 size_t first;
343 size_t second;
344 };
345 HashPairT hp = {0, 0};
346 std::hash<string_view> hasher;
347 std::__scalar_hash<decltype(hp)> pair_hasher;
348 while (!thisIter.is_end()) {
349 hp.second = hasher(*thisIter);
350 hp.first = pair_hasher(hp);
351 ++thisIter;
352 }
353 return hp.first;
354}
355
356////////////////////////////////////////////////////////////////////////////
357// path.itr
358path::iterator path::begin() const
359{
360 path_view_iterator pit = pbegin(*this);
361 iterator it;
362 it.__path_ptr_ = this;
363 it.__pos_ = pit.__pos_;
364 it.__elem_.__assign_view(*pit);
365 return it;
366}
367
368path::iterator path::end() const
369{
370 iterator it{};
371 it.__path_ptr_ = this;
372 it.__pos_ = parser::npos;
373 return it;
374}
375
376path::iterator& path::iterator::__increment() {
377 path_view_iterator it(__path_ptr_->native(), __pos_);
378 it.increment();
379 __pos_ = it.__pos_;
380 __elem_.__assign_view(*it);
381 return *this;
382}
383
384path::iterator& path::iterator::__decrement() {
385 path_view_iterator it(__path_ptr_->native(), __pos_);
386 it.decrement();
387 __pos_ = it.__pos_;
388 __elem_.__assign_view(*it);
389 return *this;
390}
391
392_LIBCPP_END_NAMESPACE_EXPERIMENTAL_FILESYSTEM