1 //===--------------------- filesystem/path.cpp ----------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
9 #include "experimental/filesystem"
10 #include "experimental/string_view"
13 _LIBCPP_BEGIN_NAMESPACE_EXPERIMENTAL_FILESYSTEM
15 _LIBCPP_CONSTEXPR path::value_type path::preferred_separator;
17 namespace { namespace parser
20 using string_type = string_view;
21 using value_type = path::value_type;
23 using string_view_pair = pair<string_view, string_view>;
26 constexpr size_t npos = static_cast<size_t>(-1);
28 inline bool good(size_t pos) { return pos != npos; }
31 constexpr value_type preferred_separator = path::preferred_separator;
32 constexpr value_type const * preferred_separator_str = "/";
33 constexpr value_type const * dot = ".";
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);
41 size_t start_of(string_type const &, size_t);
42 size_t end_of(string_type const &, size_t);
44 size_t root_name_start(const string_type& s);
45 size_t root_name_end(const string_type&);
47 size_t root_directory_start(string_type const &);
48 size_t root_directory_end(string_type const &);
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);
54 inline bool is_separator(const string_type& s, size_t pos) {
55 return (pos < s.size() && s[pos] == preferred_separator);
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;
62 inline bool is_root_directory(const string_type& s, size_t pos) {
63 return good(pos) ? root_directory_start(s) == pos : false;
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));
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)
78 if (pos == 2 && !in_sep && s[0] == preferred_separator &&
79 s[1] == preferred_separator)
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)
94 inline size_t root_name_start(const string_type& s) {
95 return good(root_name_end(s)) ? 0 : npos;
98 size_t root_name_end(const string_type& s) {
99 if (s.size() < 2 || s[0] != preferred_separator
100 || s[1] != preferred_separator) {
106 size_t index = 2; // current position
107 if (s[index] == preferred_separator) {
110 while (index + 1 < s.size() && s[index+1] != preferred_separator) {
116 size_t root_directory_start(const string_type& s) {
117 size_t e = root_name_end(s);
119 return is_separator(s, 0) ? 0 : npos;
120 return is_separator(s, e + 1) ? e + 1 : npos;
123 size_t root_directory_end(const string_type& s) {
124 size_t st = root_directory_start(s);
125 if (!good(st)) return npos;
127 while (index + 1 < s.size() && s[index + 1] == preferred_separator)
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)};
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);
145 string_view extract_preferred(const string_type& s, size_t pos) {
146 string_view raw = extract_raw(s, pos);
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);
156 }} // namespace parser
159 ////////////////////////////////////////////////////////////////////////////////
160 // path_view_iterator
161 ////////////////////////////////////////////////////////////////////////////////
164 struct path_view_iterator {
165 const string_view __s_;
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) {}
171 string_view operator*() const {
172 return parser::extract_preferred(__s_, __pos_);
175 path_view_iterator& operator++() {
180 path_view_iterator& operator--() {
186 if (__pos_ == parser::npos) return;
187 while (! set_position(parser::end_of(__s_, __pos_)+1))
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));
201 while (!set_position(parser::start_of(__s_, __pos_-1)))
206 bool set_position(size_t pos) {
207 if (pos >= __s_.size()) {
208 __pos_ = parser::npos;
212 return valid_iterator_position();
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_));
223 bool is_end() const { return __pos_ == parser::npos; }
225 inline bool operator==(path_view_iterator const& __p) {
226 return __pos_ == __p.__pos_;
230 path_view_iterator pbegin(path const& p) {
231 return path_view_iterator(p.native());
234 path_view_iterator pend(path const& p) {
235 path_view_iterator __p(p.native());
236 __p.__pos_ = parser::npos;
241 ///////////////////////////////////////////////////////////////////////////////
243 ///////////////////////////////////////////////////////////////////////////////
245 path & path::replace_extension(path const & replacement)
247 path p = extension();
249 __pn_.erase(__pn_.size() - p.native().size());
251 if (!replacement.empty()) {
252 if (replacement.native()[0] != '.') {
255 __pn_.append(replacement.__pn_);
260 ///////////////////////////////////////////////////////////////////////////////
263 string_view path::__root_name() const
265 return parser::is_root_name(__pn_, 0)
266 ? parser::extract_preferred(__pn_, 0)
270 string_view path::__root_directory() const
272 auto start_i = parser::root_directory_start(__pn_);
273 if(!parser::good(start_i)) {
276 return parser::extract_preferred(__pn_, start_i);
279 string_view path::__relative_path() const
284 auto end_i = parser::root_directory_end(__pn_);
285 if (not parser::good(end_i)) {
286 end_i = parser::root_name_end(__pn_);
288 if (not parser::good(end_i)) {
291 return string_view(__pn_).substr(end_i+1);
294 string_view path::__parent_path() const
296 if (empty() || pbegin(*this) == --pend(*this)) {
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);
304 string_view path::__filename() const
306 return empty() ? string_view{} : *--pend(*this);
309 string_view path::__stem() const
311 return parser::separate_filename(__filename()).first;
314 string_view path::__extension() const
316 return parser::separate_filename(__filename()).second;
319 ////////////////////////////////////////////////////////////////////////////
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;
329 if (thisIter.is_end() && sIter.is_end())
331 if (thisIter.is_end())
336 ////////////////////////////////////////////////////////////////////////////
338 size_t hash_value(const path& __p) _NOEXCEPT {
339 path_view_iterator thisIter(__p.native());
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);
355 ////////////////////////////////////////////////////////////////////////////
357 path::iterator path::begin() const
359 path_view_iterator pit = pbegin(*this);
361 it.__path_ptr_ = this;
362 it.__pos_ = pit.__pos_;
363 it.__elem_.__assign_view(*pit);
367 path::iterator path::end() const
370 it.__path_ptr_ = this;
371 it.__pos_ = parser::npos;
375 path::iterator& path::iterator::__increment() {
376 path_view_iterator it(__path_ptr_->native(), __pos_);
379 __elem_.__assign_view(*it);
383 path::iterator& path::iterator::__decrement() {
384 path_view_iterator it(__path_ptr_->native(), __pos_);
387 __elem_.__assign_view(*it);
391 _LIBCPP_END_NAMESPACE_EXPERIMENTAL_FILESYSTEM