MSTL 1.4.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
path_iterator.hpp
1#ifndef MSTL_CORE_ITERATOR_PATH_ITERATOR_HPP__
2#define MSTL_CORE_ITERATOR_PATH_ITERATOR_HPP__
3#include "../string/string.hpp"
4#include "../file/file_constants.hpp"
6
7class path_iterator {
8public:
9 using value_type = string_view;
10 using reference = value_type;
11 using pointer = void;
12 using iterator_category = forward_iterator_tag;
13 using difference_type = ptrdiff_t;
14
15private:
16 const string* p_ = nullptr;
17 size_t start_ = 0;
18 size_t end_ = 0;
19 bool done_ = true;
20 string current_part_;
21
22 void find_next() {
23 const size_t sz = p_->size();
24 const size_t pos = start_;
25
26#ifdef MSTL_PLATFORM_WINDOWS__
27 if (pos == 0 && sz > 1 && (*p_)[1] == ':') {
28 current_part_ = p_->substr(0, 2);
29 start_ = 2;
30 while (start_ < sz && ((*p_)[start_] == '/' || (*p_)[start_] == '\\'))
31 ++start_;
32 end_ = start_ - 1;
33 done_ = false;
34 return;
35 }
36#endif
37
38 const size_t sep_pos = p_->find_first_of(FILE_SPLITER, pos);
39 if (sep_pos == string::npos) {
40 current_part_ = p_->substr(pos);
41 end_ = sz;
42 } else {
43 current_part_ = p_->substr(pos, sep_pos - pos);
44 end_ = sep_pos;
45 }
46 }
47
48public:
49 path_iterator() noexcept = default;
50
51 explicit path_iterator(const string* path, const size_t pos = 0) noexcept
52 : p_(path), start_(pos), done_(false) {
53 if (!p_ || p_->empty() || start_ >= p_->size()) {
54 done_ = true;
55 return;
56 }
57 find_next();
58 }
59
60 reference operator *() const noexcept {
61 return current_part_.view();
62 }
63
64 path_iterator& operator ++() {
65 if (done_) return *this;
66 start_ = end_ + 1;
67 if (start_ >= p_->size()) {
68 done_ = true;
69 current_part_ = {};
70 } else {
71 find_next();
72 }
73 return *this;
74 }
75
76 path_iterator operator ++(int) {
77 path_iterator tmp = *this;
78 ++*this;
79 return tmp;
80 }
81
82 MSTL_NODISCARD bool operator ==(const path_iterator& b) const noexcept {
83 if (done_ && b.done_) return true;
84 if (p_ != b.p_) return false;
85 return start_ == b.start_;
86 }
87
88 MSTL_NODISCARD bool operator !=(const path_iterator& b) const noexcept {
89 return !(*this == b);
90 }
91};
92
94#endif // MSTL_CORE_ITERATOR_PATH_ITERATOR_HPP__
#define MSTL_END_NAMESPACE__
结束全局命名空间MSTL
#define MSTL_BEGIN_NAMESPACE__
开始全局命名空间MSTL
int64_t ptrdiff_t
指针差类型