-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatic_string.hpp
More file actions
85 lines (71 loc) · 2 KB
/
static_string.hpp
File metadata and controls
85 lines (71 loc) · 2 KB
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
#ifndef STATIC_STRING_HPP_INCLUDED
#define STATIC_STRING_HPP_INCLUDED
#include <algorithm>
#include <cstdint>
#include <string_view>
#include <utility>
#include <ostream>
template <std::size_t N>
struct static_string {
char m_str[N] {};
template <std::size_t... Is>
constexpr static_string(const char(&str)[N], std::index_sequence<Is...>)
: m_str{ str[Is]... }
{}
constexpr static_string(const char(&str)[N])
: static_string(str, std::make_index_sequence<N>{})
{}
constexpr static_string(std::string_view str) {
std::ranges::copy(str, m_str);
}
constexpr bool operator==(const static_string& other) const noexcept {
return std::ranges::equal(m_str, m_str + N, other.m_str, other.m_str + N);
}
template <std::size_t M>
constexpr bool operator==(const static_string<M>&) const noexcept {
return false;
}
constexpr bool operator==(const std::string_view& other) const noexcept {
return std::string_view(*this) == other;
}
constexpr bool operator==(const char* other) const noexcept {
return std::string_view(*this) == other;
}
constexpr bool operator==(char other) const noexcept {
if constexpr (N == 1)
return m_str[0] == other;
else if constexpr (N == 2)
return m_str[0] == other and m_str[1] == '\0';
else
return false;
}
constexpr operator std::string_view() const noexcept {
return std::string_view(m_str, N - 1);
}
// container interface
constexpr auto begin() noexcept {
return m_str;
}
constexpr auto begin() const noexcept {
return m_str;
}
constexpr auto end() noexcept {
return m_str + N;
}
constexpr auto end() const noexcept {
return m_str + N;
}
constexpr std::size_t size() const noexcept {
return N;
}
constexpr char* data() noexcept {
return m_str;
}
constexpr const char* data() const noexcept {
return m_str;
}
friend std::ostream& operator<<(std::ostream& os, const static_string& s) {
return os << s.m_str;
}
};
#endif // STATIC_STRING_HPP_INCLUDED