aboutsummaryrefslogtreecommitdiff
path: root/comp6771/2/src/filtered_string_view.h
blob: 9142f84b38b0e77789ce7bc1d9aa707783b54187 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#ifndef COMP6771_ASS2_FSV_H
#define COMP6771_ASS2_FSV_H

#include <algorithm>
#include <compare>
#include <cstring>
#include <functional>
#include <iterator>
#include <string>
#include <utility>
#include <vector>

namespace fsv {
	using filter = std::function<bool(const char&)>;

	class filtered_string_view {
	 private: // Private data members.
		const char* string;
		std::size_t length;
		filter pred;

	 public: // Iterator definition.
		class iterator {
		 public:
			using difference_type = std::size_t;
			using value_type = char;
			using pointer = void;
			using reference = const char&;
			using iterator_category = std::bidirectional_iterator_tag;

		 private:
			const value_type* const string = nullptr;
			const std::size_t* const length = nullptr;
			const filter* const predicate = nullptr;
			const value_type* it = nullptr;

		 public:
			iterator() noexcept = default;
			iterator(const value_type* const& str,
			         const std::size_t& len,
			         const filter& pred,
			         const value_type* const& it) noexcept;

			auto operator*() const noexcept -> reference;
			auto operator->() const noexcept -> reference;
			auto operator++() noexcept -> iterator&;
			auto operator++(int) noexcept -> iterator;
			auto operator--() noexcept -> iterator&;
			auto operator--(int) noexcept -> iterator;

			friend auto operator==(const iterator& a, const iterator& b) noexcept -> bool;
			friend auto operator!=(const iterator& a, const iterator& b) noexcept -> bool;
		};
		using const_iterator = iterator;
		using reverse_iterator = std::reverse_iterator<iterator>;
		using const_reverse_iterator = reverse_iterator;

		iterator begin() const noexcept;
		iterator end() const noexcept;
		const_iterator cbegin() const noexcept;
		const_iterator cend() const noexcept;
		reverse_iterator rbegin() const noexcept;
		reverse_iterator rend() const noexcept;
		const_reverse_iterator crbegin() const noexcept;
		const_reverse_iterator crend() const noexcept;

	 public: // Public functions.
		filtered_string_view() noexcept;
		filtered_string_view(const std::string& str) noexcept;
		explicit filtered_string_view(const std::string& str, filter pred) noexcept;
		filtered_string_view(const char* str) noexcept;
		explicit filtered_string_view(const char* str, filter pred) noexcept;
		filtered_string_view(const filtered_string_view& other) noexcept;
		filtered_string_view(filtered_string_view&& other) noexcept;
		~filtered_string_view() noexcept = default;

		auto operator=(const filtered_string_view& other) noexcept -> filtered_string_view&;
		auto operator=(filtered_string_view&& other) noexcept -> filtered_string_view&;

		auto operator[](const std::size_t& n) const noexcept -> const char&;
		auto operator[](const int& n) const noexcept -> const char&;

		auto at(const int index) const -> const char&;
		auto size() const noexcept -> std::size_t;
		auto empty() const noexcept -> bool;
		auto data() const noexcept -> const char*;
		auto predicate() const noexcept -> const filter&;

		friend auto operator==(const filtered_string_view& a, const filtered_string_view& b) -> bool;
		friend auto operator<=>(const filtered_string_view& a, const filtered_string_view& b) -> std::strong_ordering;
		friend auto operator<<(std::ostream& os, const filtered_string_view& fsv) -> std::ostream&;

		explicit operator std::string() const;

		static auto default_predicate(const char&) noexcept -> bool;
	};

	auto compose(const filtered_string_view& fsv, const std::vector<filter>& filts) -> filtered_string_view;
	auto split(const filtered_string_view& fsv, const filtered_string_view& tok) -> std::vector<filtered_string_view>;
	auto substr(const filtered_string_view& fsv, const int pos = 0, const int count = 0) -> filtered_string_view;

} // namespace fsv

#endif // COMP6771_ASS2_FSV_H