-
Notifications
You must be signed in to change notification settings - Fork 2
/
ks_basic_mutable_string.inl
482 lines (405 loc) · 19.4 KB
/
ks_basic_mutable_string.inl
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/* Copyright 2024 The Kingsoft's modern-string Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#pragma once
#include "base.h"
#include "ks_basic_xmutable_string_base.inl"
#include <istream>
template <class ELEM>
class ks_basic_mutable_string : public ks_basic_xmutable_string_base<ELEM> {
using __my_string_base = ks_basic_xmutable_string_base<ELEM>;
using __my_string_base::__to_basic_string_view;
public:
using typename __my_string_base::size_type;
using typename __my_string_base::difference_type;
using typename __my_string_base::value_type;
using typename __my_string_base::reference;
using typename __my_string_base::const_reference;
using typename __my_string_base::pointer;
using typename __my_string_base::const_pointer;
using typename __my_string_base::iterator;
using typename __my_string_base::const_iterator;
using typename __my_string_base::reverse_iterator;
using typename __my_string_base::const_reverse_iterator;
using __my_string_base::npos;
public:
//normal ctor
ks_basic_mutable_string() : __my_string_base() { ASSERT(this->do_check_end_ch0()); }
ks_basic_mutable_string(const ELEM* p) : __my_string_base(p) { ASSERT(this->do_check_end_ch0()); }
ks_basic_mutable_string(const ELEM* p, size_t count) : __my_string_base(p, count) { ASSERT(this->do_check_end_ch0()); }
ks_basic_mutable_string(const ks_basic_string_view<ELEM>& str_view) : __my_string_base(str_view) { ASSERT(this->do_check_end_ch0()); }
ks_basic_mutable_string(const ks_basic_string_view<ELEM>& str_view, size_t offset, size_t count = -1)
: __my_string_base(str_view.substr(offset, count)) { ASSERT(this->do_check_end_ch0()); }
ks_basic_mutable_string(size_t count, ELEM ch) : __my_string_base(count, ch) { ASSERT(this->do_check_end_ch0()); }
//copy & move ctor
ks_basic_mutable_string(const ks_basic_mutable_string& other) = default;
ks_basic_mutable_string& operator=(const ks_basic_mutable_string& other) = default;
ks_basic_mutable_string(ks_basic_mutable_string&& other) noexcept = default;
ks_basic_mutable_string& operator=(ks_basic_mutable_string&& other) noexcept = default;
//copy & move ctor (from xmutable)
ks_basic_mutable_string(const ks_basic_xmutable_string_base<ELEM>& other)
: __my_string_base(other) { this->do_ensure_end_ch0(true); }
ks_basic_mutable_string(const ks_basic_xmutable_string_base<ELEM>& other, size_t offset, size_t count = -1)
: __my_string_base(other.do_substr(offset, count)) { this->do_ensure_end_ch0(true); }
ks_basic_mutable_string(ks_basic_xmutable_string_base<ELEM>&& other)
: __my_string_base(other.do_detach()) { ASSERT(other.is_detached_empty()); this->do_ensure_end_ch0(true); }
ks_basic_mutable_string(ks_basic_xmutable_string_base<ELEM>&& other, size_t offset, size_t count = -1)
: __my_string_base(other.do_detach().do_substr(offset, count)) { ASSERT(other.is_detached_empty()); this->do_ensure_end_ch0(true); }
//copy & move ctor (from std::basic_string)
template <class AllocType>
ks_basic_mutable_string(const std::basic_string<ELEM, std::char_traits<ELEM>, AllocType>& str)
: __my_string_base(__to_basic_string_view(str)) { ASSERT(this->do_check_end_ch0()); }
template <class AllocType>
ks_basic_mutable_string(const std::basic_string<ELEM, std::char_traits<ELEM>, AllocType>& str, size_t offset, size_t count = -1)
: __my_string_base(__to_basic_string_view(str, offset, count)) { ASSERT(this->do_check_end_ch0()); }
ks_basic_mutable_string(std::basic_string<ELEM, std::char_traits<ELEM>, ks_basic_string_allocator<ELEM>>&& str_rvref)
: __my_string_base(std::move(str_rvref)) { ASSERT(this->do_check_end_ch0()); }
ks_basic_mutable_string(std::basic_string<ELEM, std::char_traits<ELEM>, ks_basic_string_allocator<ELEM>>&& str_rvref, size_t offset, size_t count = -1)
: __my_string_base(__my_string_base(std::move(str_rvref)).substr(offset, count)) { ASSERT(this->do_check_end_ch0()); }
public:
//assign...
ks_basic_mutable_string& assign(const ELEM* p) {
this->do_assign(__to_basic_string_view(p), true);
return *this;
}
ks_basic_mutable_string& assign(const ELEM* p, size_t count) {
this->do_assign(__to_basic_string_view(p, count), true);
return *this;
}
ks_basic_mutable_string& assign(const ks_basic_string_view<ELEM>& str_view) {
this->do_assign(str_view, true);
return *this;
}
ks_basic_mutable_string& assign(const ks_basic_string_view<ELEM>& str_view, size_t offset, size_t count = -1) {
this->do_assign(str_view.substr(offset, count), true);
return *this;
}
ks_basic_mutable_string& assign(size_t count, ELEM ch) {
this->do_assign(count, ch, true, true);
return *this;
}
template <class RIGHT, class _ = std::enable_if_t<std::is_convertible_v<RIGHT, ks_basic_string_view<ELEM>>>>
ks_basic_mutable_string& assign(RIGHT&& right) {
//if right is a xmutable-string,do assign directly, so this will ref right.data
if (std::is_base_of_v<ks_basic_xmutable_string_base<ELEM>, std::remove_cv_t<std::remove_reference_t<RIGHT>>>)
*this = ks_basic_mutable_string(std::forward<RIGHT>(right));
else if (std::is_same_v<RIGHT, std::basic_string<ELEM, std::char_traits<ELEM>, ks_basic_string_allocator<ELEM>>&&>)
*this = ks_basic_mutable_string(std::forward<RIGHT>(right));
else
this->do_assign(__to_basic_string_view(right), true);
return *this;
}
template <class RIGHT, class _ = std::enable_if_t<std::is_convertible_v<RIGHT, ks_basic_string_view<ELEM>>>>
ks_basic_mutable_string& assign(RIGHT&& right, size_t offset, size_t count = -1) {
//if right is a xmutable-string,do assign directly, so this will ref right.data
if (std::is_base_of_v<ks_basic_xmutable_string_base<ELEM>, std::remove_cv_t<std::remove_reference_t<RIGHT>>>)
*this = ks_basic_mutable_string(std::forward<RIGHT>(right), offset, count);
else if (std::is_same_v<RIGHT, std::basic_string<ELEM, std::char_traits<ELEM>, ks_basic_string_allocator<ELEM>>&&>)
*this = ks_basic_mutable_string(std::forward<RIGHT>(right), offset, count);
else
this->do_assign(__to_basic_string_view(right, offset, count), true);
return *this;
}
//append...
ks_basic_mutable_string& append(const ELEM* p) {
this->do_append(__to_basic_string_view(p), true);
return *this;
}
ks_basic_mutable_string& append(const ELEM* p, size_t count) {
this->do_append(__to_basic_string_view(p, count), true);
return *this;
}
ks_basic_mutable_string& append(const ks_basic_string_view<ELEM>& str_view) {
this->do_append(str_view, true);
return *this;
}
ks_basic_mutable_string& append(const ks_basic_string_view<ELEM>& str_view, size_t offset, size_t count = -1) {
this->do_append(str_view.substr(offset, count), true);
return *this;
}
ks_basic_mutable_string& append(size_t count, ELEM ch) {
this->do_append(count, ch, true, true);
return *this;
}
//insert... (by pos)
ks_basic_mutable_string& insert(size_t pos, const ELEM* p) {
this->do_insert(pos, __to_basic_string_view(p), true);
return *this;
}
ks_basic_mutable_string& insert(size_t pos, const ELEM* p, size_t count) {
this->do_insert(pos, __to_basic_string_view(p, count), true);
return *this;
}
ks_basic_mutable_string& insert(size_t pos, const ks_basic_string_view<ELEM>& str_view) {
this->do_insert(pos, str_view, true);
return *this;
}
ks_basic_mutable_string& insert(size_t pos, const ks_basic_string_view<ELEM>& str_view, size_t offset, size_t count = -1) {
this->do_insert(pos, str_view.substr(offset, count), true);
return *this;
}
ks_basic_mutable_string& insert(size_t pos, size_t count, ELEM ch) {
this->do_insert(pos, count, ch, true, true);
return *this;
}
//insert... (by iter)
iterator insert(const_iterator iter, const ELEM* p) {
size_t pos = iter - this->cbegin();
this->insert(pos, p);
return this->begin() + pos;
}
iterator insert(const_iterator iter, const ELEM* p, size_t count) {
size_t pos = iter - this->cbegin();
this->insert(pos, p, count);
return this->begin() + pos;
}
iterator insert(const_iterator iter, const ks_basic_string_view<ELEM>& str_view) {
size_t pos = iter - this->cbegin();
this->insert(pos, str_view);
return this->begin() + pos;
}
iterator insert(const_iterator iter, const ks_basic_string_view<ELEM>& str_view, size_t offset, size_t count = -1) {
size_t pos = iter - this->cbegin();
this->insert(pos, str_view, offset, count);
return this->begin() + pos;
}
iterator insert(const_iterator iter, size_t count, ELEM ch) {
size_t pos = iter - this->cbegin();
this->insert(pos, count, ch);
return this->begin() + pos;
}
//replace... (by pos)
ks_basic_mutable_string& replace(size_t pos, size_t number, const ELEM* p) {
this->do_replace(pos, number, __to_basic_string_view(p), true);
return *this;
}
ks_basic_mutable_string& replace(size_t pos, size_t number, const ELEM* p, size_t count) {
this->do_replace(pos, number, __to_basic_string_view(p, count), true);
return *this;
}
ks_basic_mutable_string& replace(size_t pos, size_t number, const ks_basic_string_view<ELEM>& str_view) {
this->do_replace(pos, number, str_view, true);
return *this;
}
ks_basic_mutable_string& replace(size_t pos, size_t number, const ks_basic_string_view<ELEM>& str_view, size_t offset, size_t count = -1) {
this->do_replace(pos, number, str_view.substr(offset, count), true);
return *this;
}
ks_basic_mutable_string& replace(size_t pos, size_t number, size_t count, ELEM ch) {
this->do_replace(pos, number, count, ch, true, true);
return *this;
}
//replace... (by iter)
iterator replace(const_iterator first, const_iterator last, const ELEM* p) {
size_t pos = first - this->cbegin();
size_t number = last - first;
this->replace(pos, number, p);
return this->begin() + pos;
}
iterator replace(const_iterator first, const_iterator last, const ELEM* p, size_t count) {
size_t pos = first - this->cbegin();
size_t number = last - first;
this->replace(pos, number, p, count);
return this->begin() + pos;
}
iterator replace(const_iterator first, const_iterator last, const ks_basic_string_view<ELEM>& str_view) {
size_t pos = first - this->cbegin();
size_t number = last - first;
this->replace(pos, number, str_view);
return this->begin() + pos;
}
iterator replace(const_iterator first, const_iterator last, const ks_basic_string_view<ELEM>& str_view, size_t offset, size_t count = -1) {
size_t pos = first - this->cbegin();
size_t number = last - first;
this->replace(pos, number, str_view, offset, count);
return this->begin() + pos;
}
iterator replace(const_iterator first, const_iterator last, size_t count, ELEM ch) {
size_t pos = first - this->cbegin();
size_t number = last - first;
this->replace(pos, number, count, ch, true, true);
return this->begin() + pos;
}
//substitute...
ks_basic_mutable_string& substitute(const ks_basic_string_view<ELEM>& old_str, const ks_basic_string_view<ELEM>& new_str, size_t n = -1) {
this->do_substitute(old_str, new_str, n, true);
return *this;
}
ks_basic_mutable_string& substitute(ELEM old_ch, const ELEM new_ch, size_t n = -1) {
this->do_substitute(__to_basic_string_view(&old_ch, 1), __to_basic_string_view(&new_ch, 1), n, true);
return *this;
}
//fill...
ks_basic_mutable_string& fill(size_t pos, size_t number, ELEM ch) {
const size_t this_length = this->length();
if (pos > this_length)
throw std::out_of_range("ks_basic_mutable_string::fill(pos, number, ch) out-of-range exception");
if (ptrdiff_t(number) < 0)
number = this_length - pos;
if (number == 1) {
this->set_at(pos, ch);
}
else if (number != 0) {
if (pos + number > this_length || number > this_length)
throw std::out_of_range("ks_basic_mutable_string::fill(pos, number, ch) out-of-range exception");
if (this->view().unsafe_subview(pos, number).find_first_not_of(ch) != -1) {
this->do_ensure_exclusive();
std::fill_n(this->unsafe_data(), number, ch);
}
}
return *this;
}
iterator fill(const_iterator first, const_iterator last, ELEM ch) {
size_t pos = first - this->cbegin();
size_t number = last - first;
this->fill(pos, number, ch);
return this->begin() + pos;
}
//erase...
ks_basic_mutable_string& erase(size_t pos, size_t number) {
this->do_erase(pos, number, true);
return *this;
}
iterator erase(const_iterator first, const_iterator last) {
size_t pos = first - this->cbegin();
size_t number = last - first;
this->erase(pos, number);
return this->begin() + pos;
}
iterator erase(const_iterator iter) {
size_t pos = iter - this->cbegin();
this->erase(pos, 1);
return this->begin() + pos;
}
//push-back, pop-back, set-at
void push_back(ELEM ch) {
this->do_append(1, ch, true, true);
}
void pop_back() {
this->do_erase(this->length() - 1, this->length(), true);
}
void set_at(size_t pos, ELEM ch) {
const size_t this_length = this->length();
if (pos >= this_length)
throw std::out_of_range("ks_basic_mutable_string::set_at(pos, ch) out-of-range exception");
if (this->data()[pos] != ch) {
this->do_ensure_exclusive();
this->unsafe_data()[pos] = ch;
}
}
//clear
void clear() {
this->do_clear(true);
}
//resize & reserve
void resize(size_t count, ELEM ch = ELEM{}) {
this->do_resize(count, ch, true, true);
this->do_ensure_exclusive(); //for compatibility,ensure exclusive!
}
void reserve(size_t capa) {
this->do_reserve(capa);
this->do_ensure_exclusive(); //for compatibility,ensure exclusive!
}
//exclusive
ELEM* __begin_exclusive_writing(size_t capa) {
this->do_resize(capa, ELEM{}, false, false);
this->do_ensure_exclusive();
return this->unsafe_data();
}
void __end_exclusive_writing(ELEM* p, size_t count) {
if (!this->is_exclusive())
throw std::logic_error("ks_basic_mutable_string::end_exclusive_writing(p, count) non-exclusive exception");
if (p != this->data() || count > this->length())
throw std::out_of_range("ks_basic_mutable_string::end_exclusive_writing(p, count) out-of-range exception");
this->do_resize(count, ELEM{}, true, false);
this->do_ensure_end_ch0(true);
}
void __ensure_exclusive_writable_directly(size_t count) {
this->do_resize(count, ELEM{}, true, true);
this->do_ensure_exclusive();
}
public:
void trim() {
this->do_trim(true);
}
void trim_left() {
this->do_trim_left(true);
}
void trim_right() {
this->do_trim_right(true);
}
void shrink_to_fit() {
this->do_shrink();
ASSERT(this->do_check_end_ch0());
}
public:
//注:for optimization, use immutable-string as return-type
std::vector<ks_basic_immutable_string<ELEM>> split(const ks_basic_string_view<ELEM>& sep, size_t n = -1) const {
return this->template do_split<ks_basic_immutable_string<ELEM>>(sep, n);
}
public:
//注:for optimization, use immutable-string as return-type
ks_basic_immutable_string<ELEM> slice(size_t from, size_t to = size_t(-1)) const& { return this->to_immutable().slice(from, to); }
ks_basic_immutable_string<ELEM> slice(size_t from, size_t to = size_t(-1))&& { return this->detach_to_immutable().slice(from, to); }
ks_basic_immutable_string<ELEM> substr(size_t offset, size_t count = size_t(-1)) const& { return this->to_immutable().substr(offset, count); }
ks_basic_immutable_string<ELEM> substr(size_t offset, size_t count = size_t(-1))&& { return this->detach_to_immutable().substr(offset, count); }
ks_basic_immutable_string<ELEM> slice(const_iterator from, const_iterator to) const& { size_t from_pos = from - this->cbegin(), to_pos = to - this->cbegin(); return this->to_immutable().slice(from_pos, to_pos); }
ks_basic_immutable_string<ELEM> slice(const_iterator from, const_iterator to)&& { size_t from_pos = from - this->cbegin(), to_pos = to - this->cbegin(); return this->detach_to_immutable().slice(from_pos, to_pos); }
ks_basic_immutable_string<ELEM> substr(const_iterator from, const_iterator to) const& { size_t offset = from - this->cbegin(), count = to - from; return this->to_immutable().substr(offset, count); }
ks_basic_immutable_string<ELEM> substr(const_iterator from, const_iterator to)&& { size_t offset = from - this->cbegin(), count = to - from; return this->detach_to_immutable().substr(offset, count); }
ks_basic_immutable_string<ELEM> trimmed() const& { return this->to_immutable().trimmed(); }
ks_basic_immutable_string<ELEM> trimmed()&& { return this->detach_to_immutable().trimmed(); }
ks_basic_immutable_string<ELEM> shrunk() const& { return this->to_immutable().shrunk(); }
ks_basic_immutable_string<ELEM> shrunk()&& { return this->detach_to_immutable().shrunk(); }
const ELEM* c_str() const {
ASSERT(this->do_check_end_ch0());
return this->data();
}
public:
ks_basic_immutable_string<ELEM> to_immutable() const& { return ks_basic_immutable_string<ELEM>(*this); }
ks_basic_immutable_string<ELEM> to_immutable()&& { return ks_basic_immutable_string<ELEM>(this->detach()); }
ks_basic_mutable_string detach() { return ks_basic_mutable_string(std::move(*this)); }
ks_basic_immutable_string<ELEM> detach_to_immutable() { return ks_basic_immutable_string<ELEM>(this->detach()); }
public:
template <class RIGHT, class _ = std::enable_if_t<std::is_convertible_v<RIGHT, ks_basic_string_view<ELEM>>>>
ks_basic_mutable_string& operator+=(RIGHT&& right) {
this->do_self_add(std::forward<RIGHT>(right), false, true);
return *this;
}
template <class RIGHT, class _ = std::enable_if_t<std::is_convertible_v<RIGHT, ks_basic_string_view<ELEM>>>>
ks_basic_immutable_string<ELEM> operator+(RIGHT&& right) const& {
return this->to_immutable() + right;
}
template <class RIGHT, class _ = std::enable_if_t<std::is_convertible_v<RIGHT, ks_basic_string_view<ELEM>>>>
ks_basic_immutable_string<ELEM> operator+(RIGHT&& right)&& {
return this->detach_to_immutable() + right;
}
};
template <class ELEM, class LEFT, class _ = std::enable_if_t<std::is_convertible_v<LEFT, ks_basic_string_view<ELEM>>>>
inline ks_basic_immutable_string<ELEM> operator+(const LEFT& left, const ks_basic_mutable_string<ELEM>& right) {
return left + right.to_immutable();
}
template <class ELEM, class LEFT, class _ = std::enable_if_t<std::is_convertible_v<LEFT, ks_basic_string_view<ELEM>>>>
inline ks_basic_immutable_string<ELEM> operator+(const LEFT& left, ks_basic_mutable_string<ELEM>&& right) {
return left + right.detach_to_immutable();
}
namespace std {
template <class ELEM>
struct hash<ks_basic_mutable_string<ELEM>> : hash<ks_basic_xmutable_string_base<ELEM>> {
};
}
template <class ELEM>
std::basic_istream<ELEM, std::char_traits<ELEM>>& operator>>(std::basic_istream<ELEM, std::char_traits<ELEM>>& strm, ks_basic_mutable_string<ELEM>& str) {
std::basic_string<ELEM, std::char_traits<ELEM>, ks_basic_string_allocator<ELEM>> std_str;
strm >> std_str;
str = ks_basic_mutable_string<ELEM>(std::move(std_str));
return strm;
}