summaryrefslogtreecommitdiff
path: root/lib/nes/common/containers/string-view.cc
blob: 8265d09abdec29297f70975093335f4483cdb209 (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include "nes/common/containers/string-view.hh"
#include "nes/common/debug.hh"

namespace nes
{
	namespace
	{
		auto compute_length(char const* c_string) -> u32
		{
			auto res = u32{ 0 };
			while (*c_string)
			{
				res += 1;
				c_string++;
			}
			return res;
		}
	} // namespace

	string_view::string_view(span<char const> const data)
		: data_{ data }
	{
	}

	string_view::string_view(char const* data, u32 length)
		: data_{ data, length }
	{
	}

	string_view::string_view(char const* c_string)
		: string_view{ c_string, compute_length(c_string) }
	{
	}

	auto string_view::get_first() const -> char
	{
		NES_ASSERT(!is_empty() && "unable to get character of empty string");
		return data_[0];
	}

	auto string_view::get_last() const -> char
	{
		NES_ASSERT(!is_empty() && "unable to get character of empty string");
		return data_[data_.get_length() - 1];
	}

	auto string_view::contains(char const needle) const -> bool
	{
		for (const auto c : *this)
		{
			if (c == needle) { return true; }
		}
		return false;
	}

	auto string_view::substring(u32 const first) const -> string_view
	{
		return string_view{ data_.subspan(first) };
	}

	auto string_view::substring(u32 const first, u32 const length) const -> string_view
	{
		return string_view{ data_.subspan(first, length) };
	}

	auto operator==(string_view const lhs, string_view const rhs) -> bool
	{
		if (lhs.get_length() != rhs.get_length()) { return false; }

		for (auto i = u32{ 0 }; i < lhs.get_length(); ++i)
		{
			if (lhs[i] != rhs[i]) { return false; }
		}

		return true;
	}
	auto operator!=(string_view const lhs, string_view const rhs) -> bool
	{
		return !(lhs == rhs);
	}
} // namespace nes