diff options
| author | Hannes Schulze <haschu0103@outlook.de> | 2025-02-04 19:25:03 +0100 |
|---|---|---|
| committer | Hannes Schulze <haschu0103@outlook.de> | 2025-02-04 19:25:03 +0100 |
| commit | e791ac9a46095e0241181da89c35a7c702982c21 (patch) | |
| tree | d959c79c24ae03282f1b00ec9bcb2ccf86d9c96c /lib/nes/common/path-view.cc | |
| parent | 0f22ed2ef160eef2da6080782943b22c9539650d (diff) | |
clean up path logic a bit
Diffstat (limited to 'lib/nes/common/path-view.cc')
| -rw-r--r-- | lib/nes/common/path-view.cc | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/lib/nes/common/path-view.cc b/lib/nes/common/path-view.cc new file mode 100644 index 0000000..47f198b --- /dev/null +++ b/lib/nes/common/path-view.cc @@ -0,0 +1,49 @@ +#include "nes/common/path-view.hh" + +namespace nes +{ + auto path_view::iterator::operator*() const -> std::string_view + { + return current_ ? std::string_view{ current_, component_length_ } : ""; + } + + auto path_view::iterator::operator++() -> iterator& + { + // Move forward to next component + current_ += component_length_; + remaining_ -= component_length_; + + // Skip over '/'. + while (remaining_ > 0 && *current_ == '/') + { + current_ += 1; + remaining_ -= 1; + } + + // Scan until end of component to find its length. + component_length_ = 0; + while (component_length_ < remaining_ && current_[component_length_] != '/') + { + component_length_ += 1; + } + + return *this; + } + + auto path_view::iterator::operator++(int) -> iterator + { + auto const res = *this; + ++*this; + return res; + } + + auto path_view::begin() const -> iterator + { + return ++iterator{ path_.data(), 0, static_cast<u32>(path_.size()) }; + } + + auto path_view::end() const -> iterator + { + return iterator{ path_.data() + path_.size(), 0, 0 }; + } +} // namespace nes
\ No newline at end of file |
