summaryrefslogtreecommitdiff
path: root/lib/nes/common/path-view.cc
blob: 743c060c0bed99350da492f086e72aa506159a80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include "nes/common/path-view.hh"
#include "nes/common/debug.hh"

namespace nes
{
	auto path_view::iterator::operator*() const -> std::string_view
	{
		NES_ASSERT(current_ != nullptr && component_length_ > 0 && "invalid iterator");
		return 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