-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathstring.hpp
159 lines (132 loc) · 3.03 KB
/
string.hpp
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#pragma once
#include <cstddef>
#include <string>
#include <string_view>
namespace cexpr
{
template <typename Char, std::size_t N>
class string
{
public:
using char_type = Char;
constexpr string() noexcept : size_{ 0 }, string_{ 0 }
{}
constexpr string(const Char(&s)[N]) noexcept : string{}
{
for(; s[size_]; ++size_)
{
string_[size_] = s[size_];
}
}
constexpr string(cexpr::string<Char, N> const& s) noexcept : string{}
{
for (; s[size_]; ++size_)
{
string_[size_] = s[size_];
}
}
constexpr string(std::basic_string_view<Char> const& s) noexcept : string{}
{
if (s.length() < N)
{
for (; size_ < s.length(); ++size_)
{
string_[size_] = s[size_];
}
}
}
constexpr void fill(const Char* begin, const Char* end) noexcept
{
fill_from(begin, end, begin());
}
constexpr void fill_from(const Char* begin, const Char* end, Char* start) noexcept
{
if (end - begin < N)
{
for (auto curr{ start }; begin != end; ++begin, ++curr)
{
*curr = *begin;
}
}
}
inline constexpr std::size_t capacity() const noexcept
{
return N - 1;
}
inline constexpr std::size_t size() const noexcept
{
return size_;
}
inline constexpr Char* begin() noexcept
{
return string_;
}
inline constexpr const Char* cbegin() const noexcept
{
return string_;
}
inline constexpr Char* end() noexcept
{
return &string_[size_];
}
inline constexpr const Char* cend() const noexcept
{
return &string_[size_];
}
inline constexpr Char& operator[](std::size_t i)
{
return string_[i];
}
inline constexpr Char const& operator[](std::size_t i) const
{
return string_[i];
}
template <typename OtherChar, std::size_t OtherN>
constexpr bool operator==(string<OtherChar, OtherN> const& other) const noexcept
{
if constexpr (N != OtherN)
{
return false;
}
std::size_t i{};
for (; i < N && string_[i] == other[i]; ++i);
return i == N;
}
template <typename OtherChar, std::size_t OtherN>
constexpr bool operator==(const OtherChar(&other)[OtherN]) const noexcept
{
if constexpr (N != OtherN)
{
return false;
}
std::size_t i{};
for (; i < N && string_[i] == other[i]; ++i);
return i == N;
}
template <typename OtherChar>
inline bool operator==(std::basic_string<OtherChar> const& other) const noexcept
{
return other == string_;
}
template <typename OtherChar>
inline bool operator!=(std::basic_string<OtherChar> const& other) const noexcept
{
return !(other == string_);
}
private:
std::size_t size_;
Char string_[N];
};
template <typename Char, std::size_t N>
string(const Char[N]) -> string<Char, N>;
template <typename Char, std::size_t N>
inline bool operator==(std::basic_string<Char> const& str, string<Char, N> const& cstr) noexcept
{
return cstr == str;
}
template <typename Char, std::size_t N>
inline bool operator!=(std::basic_string<Char> const& str, string<Char, N> const& cstr) noexcept
{
return cstr != str;
}
} // namespace cexpr