Skip to content

Commit ad6141c

Browse files
authored
some changes
1 parent 4d355a3 commit ad6141c

File tree

1 file changed

+88
-89
lines changed

1 file changed

+88
-89
lines changed

csprot.hpp

+88-89
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

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
33
// rights reserved.
44
//
55
// This file is part of CSPROT(https://github.com/niXman/csprot) project.
@@ -39,7 +39,7 @@
3939
#include <cstdint>
4040

4141
#if __cplusplus < 201402L
42-
# error C++14 or greater is required
42+
# error C++14 or greater is required
4343
#endif
4444

4545
namespace csprot {
@@ -49,7 +49,7 @@ namespace csprot {
4949
namespace {
5050

5151
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));
5353
}
5454

5555
} // anon ns
@@ -66,28 +66,27 @@ struct string_holder;
6666
template<typename CharType, std::uint32_t XorVal, CharType FirstChar, CharType... SecondChars>
6767
struct string_holder<true, CharType, XorVal, FirstChar, SecondChars...> {
6868
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; }
7271
};
7372
template <typename CharType, std::uint32_t XorVal, CharType FirstChar, CharType... SecondChars>
7473
constexpr const CharType string_holder<true, CharType, XorVal, FirstChar, SecondChars...>::_data[];
7574

7675
// spec for xor`ed strings
7776
template<typename CharType, std::uint32_t XorVal, CharType FirstChar, CharType... SecondChars>
7877
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+
}
9190
};
9291
template <typename CharType, std::uint32_t XorVal, CharType FirstChar, CharType... SecondChars>
9392
constexpr const CharType string_holder<false, CharType, XorVal, FirstChar, SecondChars...>::_data[];
@@ -100,65 +99,65 @@ template<typename CharType, std::uint32_t XorVal, CharType... Chars>
10099
class cstring;
101100

102101
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
107106
>
108107
struct cstring<CharType, XorVal, FirstChar, SecondChars...>
109-
:string_holder<XorVal==0, CharType, XorVal, FirstChar, SecondChars...> {
108+
:string_holder<XorVal==0, CharType, XorVal, FirstChar, SecondChars...> {
110109
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...>;
112111

113112
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+
}
139138
};
140139

141140
// spec for empty strings
142141
template<typename CharType, std::uint32_t XorVal>
143142
struct cstring<CharType, XorVal> {
144143
private:
145-
static constexpr CharType _data = '\0';
144+
static constexpr CharType _data = '\0';
146145

147146
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+
}
162161
};
163162
template<typename CharType, std::uint32_t XorVal>
164163
constexpr const CharType cstring<CharType, XorVal>::_data;
@@ -168,13 +167,13 @@ constexpr const CharType cstring<CharType, XorVal>::_data;
168167
// for plain strings
169168
template<typename CharType, CharType... Chars>
170169
constexpr auto operator"" _S() {
171-
return cstring<CharType, 0, Chars...>();
170+
return cstring<CharType, 0, Chars...>();
172171
}
173172

174173
// for xor`ed strings
175174
template<typename CharType, CharType... Chars>
176175
constexpr auto operator"" _XS() {
177-
return cstring<CharType, fnv1a(__DATE__ __TIME__), Chars...>();
176+
return cstring<CharType, fnv1a(__DATE__ __TIME__), Chars...>();
178177
}
179178

180179
#define _CSPROT_CAT2(x, y) x##y
@@ -186,44 +185,44 @@ constexpr auto operator"" _XS() {
186185
/**************************************************************************/
187186

188187
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
193192
>
194193
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...> &)
197196
{
198-
return cstring<CharType, XorVal, LeftChars..., RightChars...>();
197+
return cstring<CharType, XorVal, LeftChars..., RightChars...>();
199198
}
200199

201200
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
207206
>
208207
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)
211210
{
212-
return (lhs.compare(rhs) == 0);
211+
return (lhs.compare(rhs) == 0);
213212
}
214213

215214
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
221220
>
222221
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)
225224
{
226-
return !operator==(lhs, rhs);
225+
return !operator==(lhs, rhs);
227226
}
228227

229228
/**************************************************************************/

0 commit comments

Comments
 (0)