summaryrefslogtreecommitdiff
path: root/lib/nes/common/containers/string-view.hh
blob: 858a8585f6fba856c9e7958862244b62cb79e986 (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
#pragma once

#include "nes/common/containers/span.hh"
#include "nes/common/types.hh"

namespace nes
{
	enum class case_sensitive
	{
		yes,
		no,
	};

	/// Non-owning view into a (not necessarily null-terminated) string.
	class string_view
	{
		span<char const> data_;

	public:
		explicit string_view() = default;
		explicit string_view(span<char const>);
		explicit string_view(char const* data, u32 length);
		string_view(char const* c_string);

		using iterator = span<char const>::iterator;

		auto operator[](u32 const index) const -> char { return data_[index]; }
		auto get_span() const -> span<char const> { return data_; }
		auto get_data() const -> char const* { return data_.get_data(); }
		auto get_length() const -> u32 { return data_.get_length(); }
		auto is_empty() const -> bool { return data_.is_empty(); }
		auto begin() const -> iterator { return data_.begin(); }
		auto end() const -> iterator { return data_.end(); }

		// String helpers

		auto get_first() const -> char;
		auto get_last() const -> char;
		auto compare(string_view, case_sensitive = case_sensitive::yes) const -> bool;
		auto contains(char) const -> bool;
		auto substring(u32 first) const -> string_view;
		auto substring(u32 first, u32 length) const -> string_view;
		auto has_prefix(string_view, case_sensitive = case_sensitive::yes) const -> bool;
		auto has_suffix(string_view, case_sensitive = case_sensitive::yes) const -> bool;
	};

	auto operator==(string_view, string_view) -> bool;
	auto operator!=(string_view, string_view) -> bool;
} // namespace nes