Left: | ||
Right: |
OLD | NEW |
---|---|
(Empty) | |
1 #pragma once | |
2 | |
3 #include <cstddef> | |
4 #include <cstring> | |
5 #include <algorithm> | |
6 | |
7 #include "debug.h" | |
8 | |
9 inline void String_assert_readonly(bool readOnly); | |
10 | |
11 class String | |
12 { | |
13 friend class DependentString; | |
14 friend class OwnedString; | |
15 | |
16 public: | |
17 typedef char16_t value_type; | |
18 typedef size_t size_type; | |
19 | |
20 // Type flags, stored in the top 2 bits of the mLen member | |
21 static constexpr size_type INVALID = 0xC0000000; | |
22 static constexpr size_type DELETED = 0x80000000; | |
23 static constexpr size_type READ_ONLY = 0x40000000; | |
24 static constexpr size_type READ_WRITE = 0x00000000; | |
25 | |
26 static constexpr size_type FLAGS_MASK = 0xC0000000; | |
27 static constexpr size_type LENGTH_MASK = 0x3FFFFFFF; | |
28 | |
29 static constexpr size_type npos = -1; | |
30 | |
31 protected: | |
32 value_type* mBuf; | |
33 size_type mLen; | |
34 | |
35 String(value_type* buf, size_type len, size_type flags) | |
36 : mBuf(buf), mLen((len & LENGTH_MASK) | flags) | |
37 { | |
38 } | |
39 | |
40 ~String() | |
41 { | |
42 } | |
43 | |
44 void reset(value_type* buf, size_type len, size_type flags) | |
45 { | |
46 mBuf = buf; | |
47 mLen = (len & LENGTH_MASK) | flags; | |
48 } | |
49 | |
50 public: | |
51 size_type length() const | |
52 { | |
53 return mLen & LENGTH_MASK; | |
54 } | |
55 | |
56 bool empty() const | |
57 { | |
58 return !(mLen & LENGTH_MASK); | |
59 } | |
60 | |
61 const value_type* data() const | |
62 { | |
63 return mBuf; | |
64 } | |
65 | |
66 value_type* data() | |
67 { | |
68 String_assert_readonly(is_readOnly()); | |
69 return mBuf; | |
70 } | |
71 | |
72 const value_type& operator[](size_type pos) const | |
73 { | |
74 return mBuf[pos]; | |
75 } | |
76 | |
77 value_type& operator[](size_type pos) | |
78 { | |
79 String_assert_readonly(is_readOnly()); | |
80 return mBuf[pos]; | |
81 } | |
82 | |
83 bool is_readOnly() const | |
84 { | |
85 return (mLen & FLAGS_MASK) != READ_WRITE; | |
86 } | |
87 | |
88 bool equals(const String& other) const | |
89 { | |
90 if (length() != other.length()) | |
91 return false; | |
92 | |
93 return std::memcmp(mBuf, other.mBuf, sizeof(value_type) * length()) == 0; | |
94 } | |
95 | |
96 size_type find(value_type c, size_type pos = 0) const | |
97 { | |
98 for (size_type i = pos; i < length(); ++i) | |
99 if (mBuf[i] == c) | |
100 return i; | |
101 return npos; | |
102 } | |
103 | |
104 size_type find(const String& str, size_type pos = 0) const | |
105 { | |
106 if (pos + str.length() > length()) | |
107 return npos; | |
108 | |
109 if (!str.length()) | |
110 return pos; | |
111 | |
112 for (; pos + str.length() <= length(); ++pos) | |
113 { | |
114 if (mBuf[pos] == str[0] && | |
115 std::memcmp(mBuf + pos, str.mBuf, sizeof(value_type) * str.length()) = = 0) | |
116 { | |
117 return pos; | |
118 } | |
119 } | |
120 | |
121 return npos; | |
122 } | |
123 | |
124 size_type rfind(value_type c, size_type pos = npos) const | |
125 { | |
126 if (length() == 0) | |
127 return npos; | |
128 | |
129 if (pos >= length()) | |
130 pos = length() - 1; | |
131 | |
132 for (int i = pos; i >= 0; --i) | |
133 if (mBuf[i] == c) | |
134 return i; | |
135 return npos; | |
136 } | |
137 | |
138 bool is_invalid() const | |
139 { | |
140 return (mLen & FLAGS_MASK) == INVALID; | |
141 } | |
142 | |
143 bool is_deleted() const | |
144 { | |
145 return (mLen & FLAGS_MASK) == DELETED; | |
146 } | |
147 }; | |
148 | |
149 class DependentString : public String | |
150 { | |
151 public: | |
152 DependentString() | |
153 : String(nullptr, 0, INVALID) | |
154 { | |
155 } | |
156 | |
157 DependentString(value_type* buf, size_type len) | |
158 : String(buf, len, READ_WRITE) | |
159 { | |
160 } | |
161 | |
162 DependentString(const value_type* buf, size_type len) | |
163 : String(const_cast<value_type*>(buf), len, READ_ONLY) | |
164 { | |
165 } | |
166 | |
167 DependentString(String& str, size_type pos = 0, size_type len = npos) | |
168 : String( | |
169 str.mBuf + std::min(pos, str.length()), | |
170 std::min(len, str.length() - std::min(pos, str.length())), | |
171 str.is_readOnly() ? READ_ONLY : READ_WRITE | |
172 ) | |
173 { | |
174 } | |
175 | |
176 DependentString(const String& str, size_type pos = 0, | |
177 size_type len = npos) | |
178 : String( | |
179 str.mBuf + std::min(pos, str.length()), | |
180 std::min(len, str.length() - std::min(pos, str.length())), | |
181 READ_ONLY | |
182 ) | |
183 { | |
184 } | |
185 | |
186 void reset(value_type* buf, size_type len) | |
187 { | |
188 *this = DependentString(buf, len); | |
189 } | |
190 | |
191 void reset(const value_type* buf, size_type len) | |
192 { | |
193 *this = DependentString(buf, len); | |
194 } | |
195 | |
196 void reset(String& str, size_type pos = 0, size_type len = npos) | |
197 { | |
198 *this = DependentString(str, pos, len); | |
199 } | |
200 | |
201 void reset(const String& str, size_type pos = 0, size_type len = npos) | |
202 { | |
203 *this = DependentString(str, pos, len); | |
204 } | |
205 }; | |
206 | |
207 inline DependentString operator "" _str(const String::value_type* str, | |
208 String::size_type len) | |
209 { | |
210 return DependentString(str, len); | |
211 } | |
212 | |
213 inline void String_assert_readonly(bool readOnly) | |
214 { | |
215 assert(!readOnly, u"Writing access to a read-only string"_str); | |
216 } | |
217 | |
218 class OwnedString : public String | |
219 { | |
220 private: | |
221 value_type* allocate(size_type len) | |
222 { | |
223 if (len) | |
224 return new value_type[len]; | |
225 else | |
226 return nullptr; | |
227 } | |
228 | |
229 void grow(size_type additionalSize) | |
230 { | |
231 size_type oldLength = length(); | |
232 size_type newLength = oldLength + additionalSize; | |
233 value_type* oldBuffer = mBuf; | |
234 | |
235 reset(nullptr, newLength, READ_WRITE); | |
236 newLength = length(); | |
237 mBuf = allocate(newLength); | |
238 annotate_address(mBuf, "String"); | |
239 | |
240 if (oldLength) | |
241 std::memcpy(mBuf, oldBuffer, sizeof(value_type) * oldLength); | |
242 if (oldBuffer) | |
243 delete[] oldBuffer; | |
244 } | |
245 | |
246 public: | |
247 OwnedString(size_type len = 0) | |
248 : String(nullptr, len, READ_WRITE) | |
249 { | |
250 mBuf = allocate(length()); | |
251 annotate_address(mBuf, "String"); | |
252 } | |
253 | |
254 OwnedString(const String& str) | |
255 : OwnedString(str.length()) | |
256 { | |
257 if (length()) | |
258 std::memcpy(mBuf, str.mBuf, sizeof(value_type) * length()); | |
259 } | |
260 | |
261 OwnedString(const OwnedString& str) | |
262 : OwnedString(static_cast<const String&>(str)) | |
263 { | |
264 } | |
265 | |
266 OwnedString(const value_type* str, size_type len) | |
267 : OwnedString(DependentString(str, len)) | |
268 { | |
269 } | |
270 | |
271 OwnedString(OwnedString&& str) | |
272 : OwnedString(0) | |
273 { | |
274 mBuf = str.mBuf; | |
275 mLen = str.mLen; | |
276 str.mBuf = nullptr; | |
277 str.mLen = READ_WRITE | 0; | |
sergei
2016/02/23 15:07:34
I would like to use `std::swap` here, but the curr
Wladimir Palant
2016/02/23 21:34:57
I don't see how that could be used in a meaningful
| |
278 } | |
279 | |
280 ~OwnedString() | |
281 { | |
282 if (mBuf) | |
283 delete[] mBuf; | |
284 } | |
285 | |
286 OwnedString& operator=(const String& str) | |
287 { | |
288 *this = std::move(OwnedString(str)); | |
289 return *this; | |
290 } | |
291 | |
292 OwnedString& operator=(const OwnedString& str) | |
293 { | |
294 *this = std::move(OwnedString(str)); | |
295 return *this; | |
296 } | |
297 | |
298 OwnedString& operator=(OwnedString&& str) | |
299 { | |
300 mBuf = str.mBuf; | |
301 mLen = str.mLen; | |
302 str.mBuf = nullptr; | |
303 str.mLen = READ_WRITE | 0; | |
304 return *this; | |
305 } | |
306 | |
307 void append(const value_type* source, size_type sourceLen) | |
308 { | |
309 if (!sourceLen) | |
310 return; | |
311 | |
312 assert(source, u"Null buffer passed to OwnedString.append()"_str); | |
313 size_t oldLength = length(); | |
314 grow(sourceLen); | |
315 std::memcpy(mBuf + oldLength, source, sizeof(value_type) * sourceLen); | |
316 } | |
317 | |
318 void append(const String& str) | |
319 { | |
320 append(str.mBuf, str.length()); | |
321 } | |
322 | |
323 void append(value_type c) | |
324 { | |
325 append(&c, 1); | |
326 } | |
327 }; | |
OLD | NEW |