summaryrefslogtreecommitdiff
path: root/lib/fixedString.h
blob: df0529af017d54cbd6e31c40a915a74899ab3711 (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
#ifndef MYGRATE_SUPPORT_FIXEDSTRING_H
#define MYGRATE_SUPPORT_FIXEDSTRING_H

#include <array>
#include <cstddef>
#include <string_view>

namespace MyGrate::Support {
	template<typename CharT, std::size_t N> class basic_fixed_string {
	public:
		// cppcheck-suppress noExplicitConstructor
		constexpr basic_fixed_string(const CharT (&str)[N + 1])
		{
			for (decltype(N) x = 0; x < N; x++) {
				arr.at(x) = str[x];
			}
			arr.at(N) = '\0';
		}
		constexpr basic_fixed_string(const CharT * str, decltype(N) len)
		{
			for (decltype(N) x = 0; x < len; x++) {
				arr.at(x) = str[x];
			}
			arr.at(N) = '\0';
		}
		constexpr const char *
		s() const
		{
			return arr.data();
		}
		constexpr operator const char *() const
		{
			return s();
		}
		constexpr std::string_view
		v() const
		{
			return {arr.data(), arr.size() - 1};
		}
		constexpr auto &
		operator[](std::size_t n) const
		{
			return arr[n];
		}
		constexpr auto
		size() const
		{
			return arr.size() - 1;
		}

		std::array<CharT, N + 1> arr;
	};

	template<typename CharT, std::size_t N>
	basic_fixed_string(const CharT (&str)[N]) -> basic_fixed_string<CharT, N - 1>;

}

#endif