1
1
2
- // Copyright (c) 2015-2016 niXman (i dot nixman dog gmail dot com). All
2
+ // Copyright (c) 2015-2023 niXman (i dot nixman dog gmail dot com). All
3
3
// rights reserved.
4
4
//
5
5
// This file is part of CSPROT(https://github.com/niXman/csprot) project.
39
39
#include < cstdint>
40
40
41
41
#if __cplusplus < 201402L
42
- # error C++14 or greater is required
42
+ # error C++14 or greater is required
43
43
#endif
44
44
45
45
namespace csprot {
@@ -49,7 +49,7 @@ namespace csprot {
49
49
namespace {
50
50
51
51
constexpr std::uint32_t fnv1a (const char *s, std::uint32_t h = 0x811c9dc5 ) {
52
- return (*s == 0 ) ? h : fnv1a (s+1 , ((h ^ (*s)) * 0x01000193 ));
52
+ return (*s == 0 ) ? h : fnv1a (s+1 , ((h ^ (*s)) * 0x01000193 ));
53
53
}
54
54
55
55
} // anon ns
@@ -66,28 +66,27 @@ struct string_holder;
66
66
template <typename CharType, std::uint32_t XorVal, CharType FirstChar, CharType... SecondChars>
67
67
struct string_holder <true , CharType, XorVal, FirstChar, SecondChars...> {
68
68
protected:
69
- static constexpr CharType _data[] = { FirstChar, SecondChars..., ' \0 ' };
70
-
71
- constexpr const CharType* c_str () const { return _data; }
69
+ static constexpr CharType _data[] = { FirstChar, SecondChars..., ' \0 ' };
70
+ constexpr const CharType* c_str () const { return _data; }
72
71
};
73
72
template <typename CharType, std::uint32_t XorVal, CharType FirstChar, CharType... SecondChars>
74
73
constexpr const CharType string_holder<true , CharType, XorVal, FirstChar, SecondChars...>::_data[];
75
74
76
75
// spec for xor`ed strings
77
76
template <typename CharType, std::uint32_t XorVal, CharType FirstChar, CharType... SecondChars>
78
77
struct string_holder <false , CharType, XorVal, FirstChar, SecondChars...> {
79
- static constexpr CharType _data[] = { FirstChar^(char )(XorVal), SecondChars^(char )(XorVal) ..., ' \0 ' };
80
- mutable char _plain[sizeof (_data)];
81
-
82
- constexpr const CharType* c_str () const {
83
- std::size_t i = 0 ;
84
- for ( ; i < 1 +sizeof ...(SecondChars); ++i ) {
85
- _plain[i] = _data[i]^(char )(XorVal);
86
- }
87
- _plain[i]=0 ;
88
-
89
- return _plain;
90
- }
78
+ static constexpr CharType _data[] = { FirstChar^(char )(XorVal), SecondChars^(char )(XorVal) ..., ' \0 ' };
79
+ mutable char _plain[sizeof (_data)];
80
+
81
+ constexpr const CharType* c_str () const {
82
+ std::size_t i = 0 ;
83
+ for ( ; i < 1 +sizeof ...(SecondChars); ++i ) {
84
+ _plain[i] = _data[i]^(char )(XorVal);
85
+ }
86
+ _plain[i]=0 ;
87
+
88
+ return _plain;
89
+ }
91
90
};
92
91
template <typename CharType, std::uint32_t XorVal, CharType FirstChar, CharType... SecondChars>
93
92
constexpr const CharType string_holder<false , CharType, XorVal, FirstChar, SecondChars...>::_data[];
@@ -100,65 +99,65 @@ template<typename CharType, std::uint32_t XorVal, CharType... Chars>
100
99
class cstring ;
101
100
102
101
template <
103
- typename CharType
104
- ,std::uint32_t XorVal
105
- ,CharType FirstChar
106
- ,CharType... SecondChars
102
+ typename CharType
103
+ ,std::uint32_t XorVal
104
+ ,CharType FirstChar
105
+ ,CharType... SecondChars
107
106
>
108
107
struct cstring <CharType, XorVal, FirstChar, SecondChars...>
109
- :string_holder<XorVal==0 , CharType, XorVal, FirstChar, SecondChars...> {
108
+ :string_holder<XorVal==0 , CharType, XorVal, FirstChar, SecondChars...> {
110
109
private:
111
- using holder_type = string_holder<XorVal==0 , CharType, XorVal, FirstChar, SecondChars...>;
110
+ using holder_type = string_holder<XorVal==0 , CharType, XorVal, FirstChar, SecondChars...>;
112
111
113
112
public:
114
- constexpr cstring () = default;
115
-
116
- constexpr std::size_t size () const { return 1 +sizeof ...(SecondChars); }
117
- constexpr std::size_t length () const { return size (); }
118
- constexpr bool empty () const { return false ; }
119
- constexpr bool is_plain () const { return XorVal == 0 ; }
120
- constexpr bool is_xored () const { return !is_plain (); }
121
- constexpr const CharType* data () const { return holder_type::_data; }
122
- using holder_type::c_str;
123
-
124
- template <
125
- std::uint32_t XorVal2
126
- ,CharType... Chars
127
- >
128
- constexpr int compare (const cstring<CharType, XorVal2, Chars...>& other) const {
129
- for ( std::size_t i = 0 ; i < size () && i < other.size (); ++i ) {
130
- if (data ()[i] < other.data ()[i]) return -1 ;
131
- if (data ()[i] > other.data ()[i]) return 1 ;
132
- }
133
-
134
- if (size () < other.size ()) return -1 ;
135
- if (size () > other.size ()) return 1 ;
136
-
137
- return 0 ;
138
- }
113
+ constexpr cstring () = default;
114
+
115
+ constexpr std::size_t size () const { return 1 +sizeof ...(SecondChars); }
116
+ constexpr std::size_t length () const { return size (); }
117
+ constexpr bool empty () const { return false ; }
118
+ constexpr bool is_plain () const { return XorVal == 0 ; }
119
+ constexpr bool is_xored () const { return !is_plain (); }
120
+ constexpr const CharType* data () const { return holder_type::_data; }
121
+ using holder_type::c_str;
122
+
123
+ template <
124
+ std::uint32_t XorVal2
125
+ ,CharType... Chars
126
+ >
127
+ constexpr int compare (const cstring<CharType, XorVal2, Chars...>& other) const {
128
+ for ( std::size_t i = 0 ; i < size () && i < other.size (); ++i ) {
129
+ if (data ()[i] < other.data ()[i]) return -1 ;
130
+ if (data ()[i] > other.data ()[i]) return 1 ;
131
+ }
132
+
133
+ if (size () < other.size ()) return -1 ;
134
+ if (size () > other.size ()) return 1 ;
135
+
136
+ return 0 ;
137
+ }
139
138
};
140
139
141
140
// spec for empty strings
142
141
template <typename CharType, std::uint32_t XorVal>
143
142
struct cstring <CharType, XorVal> {
144
143
private:
145
- static constexpr CharType _data = ' \0 ' ;
144
+ static constexpr CharType _data = ' \0 ' ;
146
145
147
146
public:
148
- constexpr cstring () = default;
149
-
150
- constexpr std::size_t size () const { return 0 ; }
151
- constexpr std::size_t length () const { return 0 ; }
152
- constexpr bool empty () const { return true ; }
153
- constexpr bool is_plain () const { return XorVal == 0 ; }
154
- constexpr bool is_xored () const { return !is_plain (); }
155
- constexpr const CharType* data () const { return &_data; }
156
- constexpr const CharType* c_str () const { return &_data; }
157
-
158
- template <CharType... OtherChars>
159
- constexpr int compare (const cstring<CharType, XorVal, OtherChars...>& other) const {
160
- return other.empty () ? 0 : -1 ;
161
- }
147
+ constexpr cstring () = default;
148
+
149
+ constexpr std::size_t size () const { return 0 ; }
150
+ constexpr std::size_t length () const { return 0 ; }
151
+ constexpr bool empty () const { return true ; }
152
+ constexpr bool is_plain () const { return XorVal == 0 ; }
153
+ constexpr bool is_xored () const { return !is_plain (); }
154
+ constexpr const CharType* data () const { return &_data; }
155
+ constexpr const CharType* c_str () const { return &_data; }
156
+
157
+ template <CharType... OtherChars>
158
+ constexpr int compare (const cstring<CharType, XorVal, OtherChars...>& other) const {
159
+ return other.empty () ? 0 : -1 ;
160
+ }
162
161
};
163
162
template <typename CharType, std::uint32_t XorVal>
164
163
constexpr const CharType cstring<CharType, XorVal>::_data;
@@ -168,13 +167,13 @@ constexpr const CharType cstring<CharType, XorVal>::_data;
168
167
// for plain strings
169
168
template <typename CharType, CharType... Chars>
170
169
constexpr auto operator " " _S() {
171
- return cstring<CharType, 0 , Chars...>();
170
+ return cstring<CharType, 0 , Chars...>();
172
171
}
173
172
174
173
// for xor`ed strings
175
174
template <typename CharType, CharType... Chars>
176
175
constexpr auto operator " " _XS() {
177
- return cstring<CharType, fnv1a (__DATE__ __TIME__), Chars...>();
176
+ return cstring<CharType, fnv1a (__DATE__ __TIME__), Chars...>();
178
177
}
179
178
180
179
#define _CSPROT_CAT2 (x, y ) x##y
@@ -186,44 +185,44 @@ constexpr auto operator"" _XS() {
186
185
/* *************************************************************************/
187
186
188
187
template <
189
- typename CharType
190
- ,std::uint32_t XorVal
191
- ,CharType... LeftChars
192
- ,CharType... RightChars
188
+ typename CharType
189
+ ,std::uint32_t XorVal
190
+ ,CharType... LeftChars
191
+ ,CharType... RightChars
193
192
>
194
193
constexpr auto operator + (
195
- const cstring<CharType, XorVal, LeftChars...> &
196
- ,const cstring<CharType, XorVal, RightChars...> &)
194
+ const cstring<CharType, XorVal, LeftChars...> &
195
+ ,const cstring<CharType, XorVal, RightChars...> &)
197
196
{
198
- return cstring<CharType, XorVal, LeftChars..., RightChars...>();
197
+ return cstring<CharType, XorVal, LeftChars..., RightChars...>();
199
198
}
200
199
201
200
template <
202
- typename CharType
203
- ,std::uint32_t XorValLeft
204
- ,std::uint32_t XorValRight
205
- ,CharType... LeftChars
206
- ,CharType... RightChars
201
+ typename CharType
202
+ ,std::uint32_t XorValLeft
203
+ ,std::uint32_t XorValRight
204
+ ,CharType... LeftChars
205
+ ,CharType... RightChars
207
206
>
208
207
constexpr bool operator == (
209
- const cstring<CharType, XorValLeft, LeftChars...>& lhs
210
- ,const cstring<CharType, XorValRight, RightChars...>& rhs)
208
+ const cstring<CharType, XorValLeft, LeftChars...>& lhs
209
+ ,const cstring<CharType, XorValRight, RightChars...>& rhs)
211
210
{
212
- return (lhs.compare (rhs) == 0 );
211
+ return (lhs.compare (rhs) == 0 );
213
212
}
214
213
215
214
template <
216
- typename CharType
217
- ,std::uint32_t XorValLeft
218
- ,std::uint32_t XorValRight
219
- ,CharType... LeftChars
220
- ,CharType... RightChars
215
+ typename CharType
216
+ ,std::uint32_t XorValLeft
217
+ ,std::uint32_t XorValRight
218
+ ,CharType... LeftChars
219
+ ,CharType... RightChars
221
220
>
222
221
constexpr bool operator != (
223
- const cstring<CharType, XorValLeft, LeftChars...>& lhs
224
- ,const cstring<CharType, XorValRight, RightChars...>& rhs)
222
+ const cstring<CharType, XorValLeft, LeftChars...>& lhs
223
+ ,const cstring<CharType, XorValRight, RightChars...>& rhs)
225
224
{
226
- return !operator ==(lhs, rhs);
225
+ return !operator ==(lhs, rhs);
227
226
}
228
227
229
228
/* *************************************************************************/
0 commit comments