]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/libc++/src/experimental/filesystem/path.cpp
Merge ^/head r308842 through r308867.
[FreeBSD/FreeBSD.git] / contrib / libc++ / src / experimental / filesystem / path.cpp
1 //===--------------------- 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"
10 #include "experimental/string_view"
11 #include "utility"
12
13 _LIBCPP_BEGIN_NAMESPACE_EXPERIMENTAL_FILESYSTEM
14
15 _LIBCPP_CONSTEXPR path::value_type path::preferred_separator;
16
17 namespace { namespace parser
18 {
19
20 using string_type = string_view;
21 using value_type = path::value_type;
22
23 using string_view_pair = pair<string_view, string_view>;
24
25 // status reporting
26 constexpr size_t npos = static_cast<size_t>(-1);
27
28 inline bool good(size_t pos) { return pos != npos; }
29
30 // lexical elements
31 constexpr value_type preferred_separator = path::preferred_separator;
32 constexpr value_type const * preferred_separator_str = "/";
33 constexpr value_type const * dot = ".";
34
35 // forward //
36 bool is_separator(string_type const &, size_t);
37 bool is_root_name(const string_type&, size_t);
38 bool is_root_directory(string_type const &, size_t);
39 bool is_trailing_separator(string_type const &, size_t);
40
41 size_t start_of(string_type const &, size_t);
42 size_t end_of(string_type const &, size_t);
43
44 size_t root_name_start(const string_type& s);
45 size_t root_name_end(const string_type&);
46
47 size_t root_directory_start(string_type const &);
48 size_t root_directory_end(string_type const &);
49
50 string_view_pair separate_filename(string_type const &);
51 string_view extract_raw(string_type const &, size_t);
52 string_view extract_preferred(string_type const &, size_t);
53
54 inline bool is_separator(const string_type& s, size_t pos) {
55     return (pos < s.size() && s[pos] == preferred_separator);
56 }
57
58 inline bool is_root_name(const string_type& s, size_t pos) {
59   return good(pos) && pos == 0 ? root_name_start(s) == pos : false;
60 }
61
62 inline bool is_root_directory(const string_type& s, size_t pos) {
63     return good(pos) ? root_directory_start(s) == pos : false;
64 }
65
66 inline bool is_trailing_separator(const string_type& s, size_t pos) {
67     return (pos < s.size() && is_separator(s, pos) &&
68             end_of(s, pos) == s.size()-1 &&
69             !is_root_directory(s, pos) && !is_root_name(s, pos));
70 }
71
72 size_t start_of(const string_type& s, size_t pos) {
73     if (pos >= s.size()) return npos;
74     bool in_sep = (s[pos] == preferred_separator);
75     while (pos - 1 < s.size() &&
76         (s[pos-1] == preferred_separator) == in_sep)
77     { --pos; }
78     if (pos == 2 && !in_sep && s[0] == preferred_separator &&
79         s[1] == preferred_separator)
80     { return 0; }
81     return pos;
82 }
83
84 size_t end_of(const string_type& s, size_t pos) {
85     if (pos >= s.size()) return npos;
86     // special case for root name
87     if (pos == 0 && is_root_name(s, pos)) return root_name_end(s);
88     bool in_sep = (s[pos] == preferred_separator);
89     while (pos + 1 < s.size() && (s[pos+1] == preferred_separator) == in_sep)
90     { ++pos; }
91     return pos;
92 }
93
94 inline size_t root_name_start(const string_type& s) {
95     return good(root_name_end(s)) ? 0 : npos;
96 }
97
98 size_t root_name_end(const string_type& s) {
99     if (s.size() < 2 || s[0] != preferred_separator
100         || s[1] != preferred_separator) {
101         return npos;
102     }
103     if (s.size() == 2) {
104         return 1;
105     }
106     size_t index = 2; // current position
107     if (s[index] == preferred_separator) {
108         return npos;
109     }
110     while (index + 1 < s.size() && s[index+1] != preferred_separator) {
111         ++index;
112     }
113     return index;
114 }
115
116 size_t root_directory_start(const string_type& s) {
117     size_t e = root_name_end(s);
118     if (!good(e))
119     return is_separator(s, 0) ? 0 : npos;
120     return is_separator(s, e + 1) ? e + 1 : npos;
121 }
122
123 size_t root_directory_end(const string_type& s) {
124     size_t st = root_directory_start(s);
125     if (!good(st)) return npos;
126     size_t index = st;
127     while (index + 1 < s.size() && s[index + 1] == preferred_separator)
128       { ++index; }
129     return index;
130 }
131
132 string_view_pair separate_filename(string_type const & s) {
133     if (s == "." || s == ".." || s.empty()) return string_view_pair{s, ""};
134     auto pos = s.find_last_of('.');
135     if (pos == string_type::npos) return string_view_pair{s, string_view{}};
136     return string_view_pair{s.substr(0, pos), s.substr(pos)};
137 }
138
139 inline string_view extract_raw(const string_type& s, size_t pos) {
140     size_t end_i = end_of(s, pos);
141     if (!good(end_i)) return string_view{};
142     return string_view(s).substr(pos, end_i - pos + 1);
143 }
144
145 string_view extract_preferred(const string_type& s, size_t pos) {
146     string_view raw = extract_raw(s, pos);
147     if (raw.empty())
148         return raw;
149     if (is_trailing_separator(s, pos))
150         return string_view{dot};
151     if (is_separator(s, pos) && !is_root_name(s, pos))
152         return string_view(preferred_separator_str);
153     return raw;
154 }
155
156 }} // namespace parser
157
158
159 ////////////////////////////////////////////////////////////////////////////////
160 //                            path_view_iterator
161 ////////////////////////////////////////////////////////////////////////////////
162 namespace {
163
164 struct path_view_iterator {
165   const string_view __s_;
166   size_t __pos_;
167
168   explicit path_view_iterator(string_view const& __s) : __s_(__s), __pos_(__s_.empty() ? parser::npos : 0) {}
169   explicit path_view_iterator(string_view const& __s, size_t __p) : __s_(__s), __pos_(__p) {}
170
171   string_view operator*() const {
172     return parser::extract_preferred(__s_, __pos_);
173   }
174
175   path_view_iterator& operator++() {
176     increment();
177     return *this;
178   }
179
180   path_view_iterator& operator--() {
181     decrement();
182     return *this;
183   }
184
185   void increment() {
186     if (__pos_ == parser::npos) return;
187     while (! set_position(parser::end_of(__s_, __pos_)+1))
188         ;
189     return;
190   }
191
192   void decrement() {
193     if (__pos_ == 0) {
194       set_position(0);
195     }
196     else if (__pos_ == parser::npos) {
197       auto const str_size = __s_.size();
198       set_position(parser::start_of(
199           __s_, str_size != 0 ? str_size - 1 : str_size));
200     } else {
201       while (!set_position(parser::start_of(__s_, __pos_-1)))
202         ;
203     }
204   }
205
206   bool set_position(size_t pos) {
207     if (pos >= __s_.size()) {
208       __pos_ = parser::npos;
209     } else {
210       __pos_ = pos;
211     }
212     return valid_iterator_position();
213   }
214
215   bool valid_iterator_position() const {
216     if (__pos_ == parser::npos) return true; // end position is valid
217     return (!parser::is_separator      (__s_, __pos_) ||
218           parser::is_root_directory    (__s_, __pos_) ||
219           parser::is_trailing_separator(__s_, __pos_) ||
220           parser::is_root_name         (__s_, __pos_));
221   }
222
223   bool is_end() const { return __pos_ == parser::npos; }
224
225   inline bool operator==(path_view_iterator const& __p) {
226       return __pos_ == __p.__pos_;
227   }
228 };
229
230 path_view_iterator pbegin(path const& p) {
231     return path_view_iterator(p.native());
232 }
233
234 path_view_iterator pend(path const& p) {
235     path_view_iterator __p(p.native());
236     __p.__pos_ = parser::npos;
237     return __p;
238 }
239
240 } // end namespace
241 ///////////////////////////////////////////////////////////////////////////////
242 //                            path definitions
243 ///////////////////////////////////////////////////////////////////////////////
244
245 path & path::replace_extension(path const & replacement)
246 {
247     path p = extension();
248     if (not p.empty()) {
249       __pn_.erase(__pn_.size() - p.native().size());
250     }
251     if (!replacement.empty()) {
252         if (replacement.native()[0] != '.') {
253             __pn_ += ".";
254         }
255         __pn_.append(replacement.__pn_);
256     }
257     return *this;
258 }
259
260 ///////////////////////////////////////////////////////////////////////////////
261 // path.decompose
262
263 string_view path::__root_name() const
264 {
265     return parser::is_root_name(__pn_, 0)
266       ? parser::extract_preferred(__pn_, 0)
267       : string_view{};
268 }
269
270 string_view path::__root_directory() const
271 {
272     auto start_i = parser::root_directory_start(__pn_);
273     if(!parser::good(start_i)) {
274         return {};
275     }
276     return parser::extract_preferred(__pn_, start_i);
277 }
278
279 string_view path::__relative_path() const
280 {
281     if (empty()) {
282         return {__pn_};
283     }
284     auto end_i = parser::root_directory_end(__pn_);
285     if (not parser::good(end_i)) {
286         end_i = parser::root_name_end(__pn_);
287     }
288     if (not parser::good(end_i)) {
289         return {__pn_};
290     }
291     return string_view(__pn_).substr(end_i+1);
292 }
293
294 string_view path::__parent_path() const
295 {
296     if (empty() || pbegin(*this) == --pend(*this)) {
297         return {};
298     }
299     auto end_it = --(--pend(*this));
300     auto end_i = parser::end_of(__pn_, end_it.__pos_);
301     return string_view(__pn_).substr(0, end_i+1);
302 }
303
304 string_view path::__filename() const
305 {
306     return empty() ? string_view{} : *--pend(*this);
307 }
308
309 string_view path::__stem() const
310 {
311     return parser::separate_filename(__filename()).first;
312 }
313
314 string_view path::__extension() const
315 {
316     return parser::separate_filename(__filename()).second;
317 }
318
319 ////////////////////////////////////////////////////////////////////////////
320 // path.comparisons
321 int path::__compare(const value_type* __s) const {
322     path_view_iterator thisIter(this->native());
323     path_view_iterator sIter(__s);
324     while (!thisIter.is_end() && !sIter.is_end()) {
325         int res = (*thisIter).compare(*sIter);
326         if (res != 0) return res;
327         ++thisIter; ++sIter;
328     }
329     if (thisIter.is_end() && sIter.is_end())
330         return 0;
331     if (thisIter.is_end())
332         return -1;
333     return 1;
334 }
335
336 ////////////////////////////////////////////////////////////////////////////
337 // path.nonmembers
338 size_t hash_value(const path& __p) _NOEXCEPT {
339   path_view_iterator thisIter(__p.native());
340   struct HashPairT {
341     size_t first;
342     size_t second;
343   };
344   HashPairT hp = {0, 0};
345   std::hash<string_view> hasher;
346   std::__scalar_hash<decltype(hp)> pair_hasher;
347   while (!thisIter.is_end()) {
348     hp.second = hasher(*thisIter);
349     hp.first = pair_hasher(hp);
350     ++thisIter;
351   }
352   return hp.first;
353 }
354
355 ////////////////////////////////////////////////////////////////////////////
356 // path.itr
357 path::iterator path::begin() const
358 {
359     path_view_iterator pit = pbegin(*this);
360     iterator it;
361     it.__path_ptr_ = this;
362     it.__pos_ = pit.__pos_;
363     it.__elem_.__assign_view(*pit);
364     return it;
365 }
366
367 path::iterator path::end() const
368 {
369     iterator it{};
370     it.__path_ptr_ = this;
371     it.__pos_ = parser::npos;
372     return it;
373 }
374
375 path::iterator& path::iterator::__increment() {
376   path_view_iterator it(__path_ptr_->native(), __pos_);
377   it.increment();
378   __pos_ = it.__pos_;
379   __elem_.__assign_view(*it);
380   return *this;
381 }
382
383 path::iterator& path::iterator::__decrement() {
384   path_view_iterator it(__path_ptr_->native(), __pos_);
385   it.decrement();
386   __pos_ = it.__pos_;
387   __elem_.__assign_view(*it);
388   return *this;
389 }
390
391 _LIBCPP_END_NAMESPACE_EXPERIMENTAL_FILESYSTEM