-
Notifications
You must be signed in to change notification settings - Fork 1
/
constexpr_string.hpp
172 lines (153 loc) · 6.33 KB
/
constexpr_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
160
161
162
163
164
165
166
167
168
169
170
171
172
#pragma once
/*
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <string>
#include <utility>
#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1900)
namespace compiletime {
template <typename CharT, std::size_t N> struct basic_constexpr_string {
typedef CharT value_type;
typedef CharT *pointer;
typedef const CharT *const_pointer;
typedef CharT &reference;
typedef const CharT &const_reference;
typedef std::size_t size_type;
constexpr const_pointer c_str() const { return buffer; }
std::basic_string<CharT> to_string() const {
return std::basic_string<CharT>(buffer);
}
constexpr CharT operator[](size_type pos) const { return buffer[pos]; }
constexpr int compare(const basic_constexpr_string<CharT, N> &v) const;
constexpr bool operator==(const basic_constexpr_string &rhs) const {
return compare(rhs) == 0;
}
constexpr bool operator!=(const basic_constexpr_string &rhs) const {
return compare(rhs) != 0;
}
constexpr bool operator<(const basic_constexpr_string &rhs) const {
return compare(rhs) < 0;
}
constexpr bool operator>(const basic_constexpr_string &rhs) const {
return rhs < *this;
}
constexpr bool operator<=(const basic_constexpr_string &rhs) const {
return !(*this > rhs);
}
constexpr bool operator>=(const basic_constexpr_string &rhs) const {
return !(*this < rhs);
}
constexpr size_type size() const { return N; }
CharT buffer[N + 1];
};
#if __cplusplus >= 201402L || (defined(_MSC_VER) && _MSC_VER >= 1900)
using std::index_sequence;
using std::make_index_sequence;
#else
template <std::size_t... Indices> struct index_sequence {};
template <std::size_t first, std::size_t last, class result = index_sequence<>,
bool flag = first >= last>
struct make_index_sequence_t {
using type = result;
};
template <std::size_t step, std::size_t last, std::size_t... Indices>
struct make_index_sequence_t<step, last, index_sequence<Indices...>, false>
: make_index_sequence_t<step + 1, last, index_sequence<Indices..., step>> {
};
template <std::size_t N>
using make_index_sequence = typename make_index_sequence_t<0, N>::type;
#endif
namespace detail {
template <typename CharT, std::size_t... Indices>
constexpr basic_constexpr_string<CharT, sizeof...(Indices)>
build_string(const CharT *str, index_sequence<Indices...>) {
return basic_constexpr_string<CharT, sizeof...(Indices)>{str[Indices]...};
}
template <typename CharT, std::size_t N1, std::size_t N2,
std::size_t... Indices1, std::size_t... Indices2>
constexpr basic_constexpr_string<CharT,
sizeof...(Indices1) + sizeof...(Indices2)>
concat(const CharT (&str1)[N1], index_sequence<Indices1...>,
const CharT (&str2)[N2], index_sequence<Indices2...>) {
return basic_constexpr_string<CharT,
sizeof...(Indices1) + sizeof...(Indices2)>{
str1[Indices1]..., str2[Indices2]...};
}
template <typename CharT, std::size_t N1, std::size_t N2>
constexpr int compare(const CharT (&str1)[N1], const CharT (&str2)[N2],
std::size_t pos, std::size_t len) {
return len == pos
? 0
: str1[pos] != str2[pos] ? str1[pos] - str2[pos]
: compare(str1, str2, pos + 1, len);
}
} // namespace detail
template <typename CharT, std::size_t N>
constexpr int basic_constexpr_string<CharT, N>::compare(
const basic_constexpr_string<CharT, N> &n) const {
return detail::compare(buffer, n.buffer, 0, N);
}
template <typename CharT, std::size_t N1, std::size_t N2>
constexpr basic_constexpr_string<CharT, N1 + N2>
operator+(const basic_constexpr_string<CharT, N1> &cstr1,
const basic_constexpr_string<CharT, N2> &cstr2) {
return detail::concat(cstr1.buffer, make_index_sequence<N1>(), cstr2.buffer,
make_index_sequence<N2>());
}
template <typename CharT, std::size_t N>
std::basic_string<CharT>
operator+(const std::basic_string<CharT> &str,
const basic_constexpr_string<CharT, N> &cstr) {
std::string ret;
ret.reserve(str.size() + N);
ret.append(str);
ret.append(cstr.buffer);
return ret;
}
template <typename CharT, std::size_t N>
std::basic_string<CharT> operator+(const basic_constexpr_string<CharT, N> &cstr,
const std::basic_string<CharT> &str) {
std::string ret;
ret.reserve(str.size() + N);
ret.append(cstr.buffer);
ret.append(str);
return ret;
}
template <typename CharT, std::size_t N>
constexpr basic_constexpr_string<CharT, N - 1>
to_constexpr_string(const CharT (&str)[N]) {
return detail::build_string(str, make_index_sequence<N - 1>());
}
template <std::size_t N>
using constexpr_string = basic_constexpr_string<char, N>;
template <std::size_t N>
using constexpr_wstring = basic_constexpr_string<wchar_t, N>;
#if defined(_HAS_CONSTEXPR) || __cplusplus >= 201103L || \
(defined(_MSC_VER) && _MSC_VER >= 1800)
template <std::size_t N>
using constexpr_u16string = basic_constexpr_string<char16_t, N>;
template <std::size_t N>
using constexpr_u32string = basic_constexpr_string<char32_t, N>;
#endif
} // namespace compiletime
#else
#error Needs at least a C++11 compiler
#endif