NIM 跨平台 C++ SDK
载入中...
搜索中...
未找到
ne_stl_string.h
浏览该文件的文档.
1#ifndef NE_STL_INCLUDENE_STL_STRING_
2#define NE_STL_INCLUDENE_STL_STRING_
3
4#include <stdarg.h> // NOLINT
5#include <cstdlib>
6#include "ne_stl_build.h"
7#include "ne_stl_iterator.h"
8#include "ne_stl_memory.h"
9#include "ne_stl_tool.h"
10#include "ne_stl_value_def.h"
11
12#if !defined(NE_STL_OS_WIN)
13#include <errno.h>
14#endif
15
16namespace nstd {
17template <element_malloc_fun_t<char> _malloc = memory::safe_malloc_container_element<char>,
18 element_delete_fun_t<char> _delete = memory::safe_delete_container_element<char>>
19class _string {
20public:
21 using value_type = char;
22
23private:
29
30public:
32 : _str(nullptr)
33 , _str_len(0)
34 , _str_cap(0)
36 , malloc_fun_(_malloc)
37 , delete_fun_(_delete) {
38 _setCapacity(0);
39 _setLength(0);
40 }
41 _string(const _string& other)
42 : _str(nullptr)
43 , _str_len(0)
44 , _str_cap(0)
46 , malloc_fun_(_malloc)
47 , delete_fun_(_delete) {
48 _append(other._str, other._str_len);
49 }
50 _string(const _string& other, std::size_t pos, std::size_t len = container_npos::value)
51 : _str(nullptr)
52 , _str_len(0)
53 , _str_cap(0)
55 , malloc_fun_(_malloc)
56 , delete_fun_(_delete) {
57 append(other._str, pos, len);
58 }
59 _string(const char* copy)
60 : _str(nullptr)
61 , _str_len(0)
62 , _str_cap(0)
64 , malloc_fun_(_malloc)
65 , delete_fun_(_delete) {
67 }
68 _string(const char* s, std::size_t n)
69 : _str(nullptr)
70 , _str_len(0)
71 , _str_cap(0)
73 , malloc_fun_(_malloc)
74 , delete_fun_(_delete) {
75 append(s, 0, n);
76 }
77 _string(std::size_t n, char c)
78 : _str(nullptr)
79 , _str_len(0)
80 , _str_cap(0)
82 , malloc_fun_(_malloc)
83 , delete_fun_(_delete) {
84 append(n, c);
85 }
86 _string(const const_iterator first, const const_iterator second)
87 : _str(nullptr)
88 , _str_len(0)
89 , _str_cap(0)
91 , malloc_fun_(_malloc)
92 , delete_fun_(_delete) {
93 append(first, second);
94 }
95 _string(const std::string& other)
96 : _string(other.c_str(), other.length()) {}
97 _string(_string&& other) {
98 _str = other._str;
99 _str_len = other._str_len;
100 _str_cap = other._str_cap;
101 _increaseBy = other._increaseBy;
102 malloc_fun_ = other.malloc_fun_;
103 delete_fun_ = other.delete_fun_;
104 other._str = nullptr;
105 other._str_len = 0;
106 other._str_cap = 0;
107 other._increaseBy = container_increase_step::value;
108 other.malloc_fun_ = nullptr; // not set to _malloc because the implementation may be different across modules
109 other.delete_fun_ = nullptr; // same as above
110 }
111
112public:
113 _string& operator=(const char* other) {
114 if (other != nullptr) {
115 _setLength(0);
116 _append(other, strlen(other));
117 }
118 return *this;
119 }
120 _string& operator=(const _string& other) {
121 if (this != &other) {
122 _setLength(0);
123 _append(other._str, other._str_len);
124 }
125 return *this;
126 }
127 _string& operator=(const std::string& std_string) {
128 clear();
129 append(std_string.c_str());
130 return *this;
131 }
132 operator std::string() const {
133 std::string ret(c_str());
134 return ret;
135 }
136 // Destructor
137public:
140 _str_len = 0;
141 _str_cap = 0;
143 }
144
145public:
146 friend bool operator==(const _string& lhs, const _string& rhs) { return lhs._compare(rhs) == 0; }
147 friend bool operator!=(const _string& lhs, const _string& rhs) { return lhs._compare(rhs) != 0; }
148 friend bool operator<(const _string& lhs, const _string& rhs) { return lhs._compare(rhs) < 0; }
149 friend bool operator>(const _string& lhs, const _string& rhs) { return lhs._compare(rhs) > 0; }
150 friend bool operator<=(const _string& lhs, const _string& rhs) { return lhs._compare(rhs) <= 0; }
151 friend bool operator>=(const _string& lhs, const _string& rhs) { return lhs._compare(rhs) >= 0; }
152
153private:
154 int32_t _compare(const _string& rhs) const {
155 if (!_str && !rhs._str)
156 return 0;
157 if (_str && !rhs._str)
158 return 1;
159 if (!_str && rhs._str)
160 return -1;
161 if (_str_len < rhs._str_len)
162 return 1;
163 if (_str_len > rhs._str_len)
164 return -1;
165 return strcmp(_str, rhs._str);
166 }
167
168public:
169 const char& operator[](std::size_t i) const { return *(_str + i); }
170 char& operator[](std::size_t i) { return *(_str + i); }
171 const char& at(std::size_t i) const {
172 if (i < 0 || _str_len <= i)
173 throw;
174 return operator[](i);
175 }
176 char& at(std::size_t i) {
177 if (i < 0 || _str_len <= i)
178 throw;
179 return operator[](i);
180 }
181 const char& front() const { return operator[](0); }
182 char& front() { return operator[](0); }
183 const char& back() const { return operator[](_str_len - 1); }
184 char& back() { return operator[](_str_len - 1); }
185 // Iterators
186public:
187 iterator begin() { return _ibegin(); }
188 const_iterator begin() const { return _ibegin(); }
189 iterator end() { return _end(); }
190 const_iterator end() const { return _end(); }
191 const_iterator cbegin() const { return _ibegin(); }
192 const_iterator cend() const { return _end(); }
193
196 reverse_iterator rend() { return _end(); }
197 const_reverse_iterator rend() const { return _end(); }
199 const_reverse_iterator crend() const { return _end(); }
200
201private:
205 std::size_t _getLength(const const_iterator first, const const_iterator second) {
206 if (first.current().first_ != second.current().first_) {
207 throw;
208 }
209 if (first.current().pos_ > second.current().pos_) {
210 throw;
211 }
212 return second.current().pos_ - first.current().pos_;
213 }
214
215 // Modifiers
216public:
218 _append(s._str, s._str_len);
219 return *this;
220 }
221 _string& operator+=(const char* s) {
222 _append(s);
223 return *this;
224 }
226 _append(&c, 1);
227 return *this;
228 }
229 _string operator+(const _string& s) const {
230 _string ret(*this);
231 ret += s;
232 return ret;
233 }
234 _string operator+(const char* s) const {
235 _string ret(*this);
236 ret += s;
237 return ret;
238 }
239 _string operator+(char c) const {
240 _string ret(*this);
241 ret += c;
242 return ret;
243 }
244
245 _string& append(const _string& other) {
246 _append(other._str, other._str_len);
247 return *this;
248 }
249 _string& append(const _string& other, std::size_t subpos, std::size_t sublen = container_npos::value) {
250 sublen = _getLength(other, subpos, sublen);
251 char* buffer = nullptr;
252 _substr(buffer, other._str, subpos, sublen);
253 if (buffer == nullptr)
254 return *this;
255 _append(buffer, sublen);
256 _delete_data(buffer);
257 return *this;
258 }
259 _string& append(const char* other) {
260 _append(other);
261 return *this;
262 }
263 _string& append(const char* s, std::size_t n) {
264 if (s == nullptr || strlen(s) == 0)
265 return *this;
266 if (strlen(s) < n)
267 n = strlen(s);
268 char* buffer = nullptr;
269 _substr(buffer, s, 0, n);
270 if (buffer == nullptr)
271 return *this;
272 _append(buffer, n);
273 _delete_data(buffer);
274 return *this;
275 }
276 _string& append(std::size_t n, char c) {
277 char* buffer = nullptr;
278 _alloc_cstring(buffer, n, c);
279 if (buffer == nullptr)
280 return *this;
281 _append(buffer, n);
282 _delete_data(buffer);
283 return *this;
284 }
285 _string& append(const const_iterator first, const const_iterator second) {
286 const std::size_t n = _getLength(first, second);
287 if (n != 0) { // if length is not zero then _append
288 char* buffer = nullptr;
289 _alloc_cstring(buffer, n, first, second);
290 if (buffer == nullptr)
291 return *this;
292 _append(buffer, n);
293 _delete_data(buffer);
294 }
295 return *this;
296 }
297
299 _append(&c, 1);
300 return *this;
301 }
302
303 _string& insert(std::size_t pos, const _string& other) {
304 _insert_str(pos, other._str, other._str_len);
305 return *this;
306 }
307 _string& insert(std::size_t pos, const _string& other, std::size_t subpos, std::size_t sublen = container_npos::value) {
308 sublen = _getLength(other, subpos, sublen);
309 char* buffer = nullptr;
310 _substr(buffer, other._str, subpos, sublen);
311 _insert_str(pos, buffer, sublen);
312 _delete_data(buffer);
313 return *this;
314 }
315 _string& insert(std::size_t pos, const char* other) {
316 _insert_str(pos, other);
317 return *this;
318 }
319 _string& insert(std::size_t pos, const char* s, std::size_t n) {
320 char* buffer = nullptr;
321 _substr(buffer, s, 0, n);
322 _insert_str(pos, buffer, n);
323 _delete_data(buffer);
324 return *this;
325 }
326 _string& insert(std::size_t pos, std::size_t n, char c) {
327 char* buffer = nullptr;
328 _alloc_cstring(buffer, n, c);
329 _insert_str(pos, buffer, n);
330 _delete_data(buffer);
331 return *this;
332 }
333 void insert(iterator p, std::size_t n, char c) {
334 char* buffer = nullptr;
335 _alloc_cstring(buffer, n, c);
336 _insert_str(p.current().pos_, buffer, n);
337 _delete_data(buffer);
338 }
340 _insert_str(p.current().pos_, &c, 1);
341 return p;
342 }
343 void insert(iterator p, const const_iterator begin, const const_iterator last) {
344 const std::size_t n = _getLength(begin, last);
345 if (n == 0)
346 return;
347 char* buffer = nullptr;
348 _alloc_cstring(buffer, n, begin, last);
349 _insert_str(p.current().pos_, buffer, n);
350 _delete_data(buffer);
351 }
352
353 _string& erase(std::size_t pos = 0, std::size_t len = container_npos::value) {
354 _erase(pos, len);
355 return *this;
356 }
358 _erase(p.current().pos_, 1);
359 return p.current();
360 }
362 _erase(first.current().pos_, _getLength(first, last));
363 return first.current();
364 }
365
366 _string& replace(std::size_t pos, std::size_t len, const _string& other) {
367 _replace(pos, len, other._str, other._str_len);
368 return *this;
369 }
371 replace(i1.current().pos_, _getLength(i1, i2), other._str, other._str_len);
372 return *this;
373 }
374 _string& replace(std::size_t pos, std::size_t len, const _string& other, std::size_t subpos, std::size_t sublen = container_npos::value) {
375 sublen = _getLength(other, subpos, sublen);
376 char* buffer = nullptr;
377 _substr(buffer, other._str, subpos, sublen);
378 _replace(pos, len, buffer, strlen(buffer));
379 _delete_data(buffer);
380 return *this;
381 }
382 _string& replace(std::size_t pos, std::size_t len, const char* s) {
383 _replace(pos, len, s, strlen(s));
384 return *this;
385 }
386 _string& replace(const_iterator i1, const_iterator i2, const char* other) {
387 _replace(i1.current().pos_, _getLength(i1, i2), other, strlen(other));
388 return *this;
389 }
390 _string& replace(std::size_t pos, std::size_t len, const char* other, std::size_t n) {
391 _replace(pos, len, other, n);
392 return *this;
393 }
394 _string& replace(const_iterator i1, const_iterator i2, const char* other, std::size_t n) {
395 _replace(i1.current().pos_, _getLength(i1, i2), other, n);
396 return *this;
397 }
398 _string& replace(std::size_t pos, std::size_t len, std::size_t n, char c) {
399 char* buffer = nullptr;
400 _alloc_cstring(buffer, n, c);
401 _replace(pos, len, buffer, n);
402 _delete_data(buffer);
403 return *this;
404 }
405 _string& replace(const_iterator i1, const_iterator i2, std::size_t n, char c) {
406 char* buffer = nullptr;
407 _alloc_cstring(buffer, n, c);
408 _replace(i1.current().pos_, _getLength(i1, i2), buffer, n);
409 _delete_data(buffer);
410 return *this;
411 }
413 const std::size_t n = _getLength(first, second);
414 char* buffer = nullptr;
415 _alloc_cstring(buffer, n, first, second);
416 _replace(i1.current().pos_, _getLength(i1, i2), buffer, n);
417 _delete_data(buffer);
418 return *this;
419 }
420
421 void swap(_string& other) {
422 _string temp = std::move(*this);
423 *this = std::move(other);
424 other = std::move(temp);
425 }
427 _erase((end() - 1).current().pos_, 1);
428 return *this;
429 }
430
431private:
432 inline void _append(const char* other) {
433 if (other == nullptr)
434 return;
435 _append(other, strlen(other));
436 }
437 void _append(const char* other, std::size_t n) {
438 if (!other || n == 0)
439 return;
441 std::size_t i = 0;
442 for (; i < n; ++i)
443 operator[](_str_len + i) = other[i];
444 operator[](_str_len + i) = '\0';
445 _setLength(_str_len + n);
446 }
447 void _insert_str(std::size_t pos, const char* other) {
448 if (other != nullptr)
449 _insert_str(pos, other, strlen(other));
450 }
451 void _insert_str(std::size_t pos, const char* other, std::size_t n) {
452 if (pos == _str_len) {
453 return _append(other, n);
454 }
455 if (pos > _str_len) {
456 throw;
457 }
458 if (n == 0) {
459 return;
460 }
462 if (_str) {
463 char* buffer = nullptr;
464 _substr(buffer, _str, pos, _str_len);
465 _clear_str(pos);
466 _append(other, n);
467 _append(buffer, strlen(buffer));
468 _delete_data(buffer);
469 } else {
470 _append(other, n);
471 }
472 }
473 void _substr(char*& buffer, const char* other, std::size_t pos = 0, std::size_t len = container_npos::value) const {
474 if (other == nullptr) {
475 return;
476 }
477 _alloc_cstring(buffer, len);
478 for (std::size_t i = 0; i < len; ++i) {
479 buffer[i] = other[pos + i];
480 }
481 }
482 void _erase(std::size_t pos, std::size_t len) {
483 len = _getLength(*this, pos, len);
484 for (std::size_t i = pos + len; i < _str_len; ++i)
485 operator[](i - len) = operator[](i);
486 _clear_str(_str_len - len);
487 }
488 void _replace(std::size_t pos, std::size_t len, const char* other, std::size_t n) {
489 len = _getLength(*this, pos, len);
490 char* buffer = nullptr;
491 _substr(buffer, _str, pos + len, _str_len);
492 _clear_str(pos);
493 char* replace_buffer = nullptr;
494 _substr(replace_buffer, other, 0, n);
495 _append(replace_buffer);
496 _append(buffer);
497 _delete_data(replace_buffer);
498 _delete_data(buffer);
499 }
500 std::size_t _getLength(const _string& str, std::size_t pos, std::size_t len = container_npos::value) const {
501 if (len == container_npos::value)
502 len = _str_len - pos;
503
504 if (pos + len > str._str_len) {
505 return 0;
506 }
507 return len;
508 }
509
510public:
511 const char* c_str() const { return (_str_len > 0 && _str) ? _str : ""; }
512
513 std::size_t copy(char* s, std::size_t len, std::size_t pos = 0) const {
514 if (!s)
515 throw;
516 len = _getLength(*this, pos, len);
517 for (std::size_t i = 0; i < len; ++i)
518 *(s + i) = operator[](pos + i);
519 return len;
520 }
521
522 std::size_t find(const _string& other, std::size_t pos = 0) const { return _find(other._str, other._str_len, pos); }
523 std::size_t find(const char* s, std::size_t pos = 0) const { return _find(s, strlen(s), pos); }
524 std::size_t find(const char* s, std::size_t pos, std::size_t n) const { return _find(s, n, pos); }
525 std::size_t find(char c, std::size_t pos = 0) const { return _find(&c, 1, pos); }
526
527 std::size_t rfind(const _string& other, std::size_t pos = container_npos::value) const { return _rfind(other._str, other._str_len, pos); }
528 std::size_t rfind(const char* s, std::size_t pos = container_npos::value) const { return _rfind(s, strlen(s), pos); }
529 std::size_t rfind(const char* s, std::size_t pos, std::size_t n) const { return _rfind(s, n, pos); }
530 std::size_t rfind(char c, std::size_t pos = container_npos::value) const { return _rfind(&c, 1, pos); }
531
532 std::size_t find_first_of(const _string& other, std::size_t pos = 0) const { return _find_first_of(other._str, other._str_len, pos, true); }
533 std::size_t find_first_of(const char* other, std::size_t pos = 0) const { return _find_first_of(other, strlen(other), pos, true); }
534 std::size_t find_first_of(const char* other, std::size_t pos, std::size_t n) const { return _find_first_of(other, n, pos, true); }
535 std::size_t find_first_of(char c, std::size_t pos = 0) const { return _find_first_of(&c, 1, pos, true); }
536
537 std::size_t find_last_of(const _string& other, std::size_t pos = nstd::container_npos::value) const {
538 return _find_last_of(other._str, other._str_len, pos, true);
539 }
540 std::size_t find_last_of(const char* other, std::size_t pos = nstd::container_npos::value) const {
541 return _find_last_of(other, strlen(other), pos, true);
542 }
543 std::size_t find_last_of(const char* other, std::size_t pos, std::size_t n) const { return _find_last_of(other, n, pos, true); }
544 std::size_t find_last_of(char c, std::size_t pos = nstd::container_npos::value) const { return _find_last_of(&c, 1, pos, true); }
545
546 std::size_t find_first_not_of(const _string& other, std::size_t pos = 0) const { return _find_first_of(other._str, other._str_len, pos, false); }
547 std::size_t find_first_not_of(const char* other, std::size_t pos = 0) const { return _find_first_of(other, strlen(other), pos, false); }
548 std::size_t find_first_not_of(const char* other, std::size_t pos, std::size_t n) const { return _find_first_of(other, n, pos, false); }
549 std::size_t find_first_not_of(char c, std::size_t pos = 0) const { return _find_first_of(&c, 1, pos, false); }
550
551 std::size_t find_last_not_of(const _string& other, std::size_t pos = nstd::container_npos::value) const {
552 return _find_last_of(other._str, other._str_len, pos, false);
553 }
554 std::size_t find_last_not_of(const char* other, std::size_t pos = nstd::container_npos::value) const {
555 return _find_last_of(other, strlen(other), pos, false);
556 }
557 std::size_t find_last_not_of(const char* other, std::size_t pos, std::size_t n) const { return _find_last_of(other, n, pos, false); }
558 std::size_t find_last_not_of(char c, std::size_t pos = nstd::container_npos::value) const { return _find_last_of(&c, 1, pos, false); }
559
560 _string substr(std::size_t pos = 0, std::size_t len = container_npos::value) const {
561 len = _getLength(*this, pos, len);
562 char* buffer = nullptr;
563 _substr(buffer, _str, pos, len);
564 _string toReturn(buffer);
565 _delete_data(buffer);
566 return toReturn;
567 }
568
569private:
570 std::size_t _find(const char* other, std::size_t len, std::size_t pos) const {
571 std::size_t toReturn = container_npos::value;
572 if (pos == container_npos::value)
573 pos = _str_len - 1;
574 for (; pos <= _str_len - len; ++pos)
575 if (operator[](pos) == *other && _find_compare(other, len, pos)) {
576 toReturn = pos;
577 break;
578 }
579 return toReturn;
580 }
581 std::size_t _rfind(const char* other, std::size_t len, std::size_t pos) const {
582 std::size_t toReturn = container_npos::value;
583 if (pos == container_npos::value || pos + len > _str_len + 1)
584 pos = _str_len - len + 1;
585
586 for (std::size_t i = pos; 0 <= i; --i)
587 if (operator[](i) == *other && _find_compare(other, len, i)) {
588 toReturn = i;
589 break;
590 }
591 return toReturn;
592 }
593 bool _find_compare(const char* other, std::size_t len, std::size_t pos) const {
594 for (std::size_t i = 1; i < len; ++i)
595 if (operator[](pos + i) != *(other + i))
596 return false;
597 return true;
598 }
599 std::size_t _find_first_of(const char* other, std::size_t len, std::size_t pos, bool isTrue) const {
600 std::size_t toReturn = container_npos::value;
601 if (pos == container_npos::value)
602 pos = _str_len - 1;
603
604 for (; pos < _str_len; ++pos)
605 if (_find_of_compare(other, len, pos, isTrue)) {
606 toReturn = pos;
607 break;
608 }
609 return toReturn;
610 }
611 std::size_t _find_last_of(const char* other, std::size_t len, std::size_t pos, bool isTrue) const {
612 std::size_t toReturn = container_npos::value;
613 if (pos == container_npos::value)
614 pos = _str_len - 1;
615
616 for (std::size_t i = pos; 0 <= i; --i)
617 if (_find_of_compare(other, len, i, isTrue)) {
618 toReturn = i;
619 break;
620 }
621 return toReturn;
622 }
623 bool _find_of_compare(const char* other, std::size_t len, std::size_t pos, bool isTrue) const {
624 for (std::size_t i = 0; i < len; ++i)
625 if (operator[](pos) == *(other + i))
626 return isTrue;
627 return !isTrue;
628 }
629
630public:
631 std::size_t length() const { return _str_len; }
632 inline std::size_t size() const { return length(); }
633 std::size_t max_size() const { return nstd::container_npos::value - 1; }
634 void resize(std::size_t n) {
635 if (_str_len == n)
636 return;
637 if (_str_len < n) {
638 _setLength(n);
639 return;
640 }
641 _clear_str(n);
642 }
643 void resize(std::size_t n, char c) {
644 if (_str_len == n)
645 return;
646 if (_str_len < n) {
647 std::size_t pos = _str_len;
648 _setLength(n);
649 _fill_str(_str, _str_len, pos, c);
650 return;
651 }
652 _clear_str(n);
653 }
654 std::size_t capacity() const { return _str_cap; }
655 void reserve(std::size_t n = 0) {
656 if (_str_cap == n)
657 return;
658 if (_str_cap < n) {
659 _setCapacity(n);
660 return;
661 }
662 if (_str_cap / 2 > _str_len) {
664 }
665 }
666 void clear() { _clear_str(0); }
667 bool empty() const { return _str_len ? false : true; }
669
670private:
671 void _setLength(const std::size_t len) {
672 if (_str_len > len)
673 _clear_str(len);
674 else if (_str_cap < len)
676 _str_len = len;
677 }
678 void _setCapacity(const std::size_t cap) {
679 if (_str_cap == cap && _str)
680 return;
681 if (cap < _str_len)
682 return;
683 _str_cap = cap;
684
685 char* buffer = _str;
686 _str = nullptr;
688 if (buffer) {
689 for (std::size_t i = 0; i < _str_len; ++i)
690 operator[](i) = buffer[i];
691 operator[](_str_len) = '\0';
692 _delete_data(buffer);
693 }
694 }
695 void _increaseCapacity(const std::size_t cap) {
696 if (_str_cap > cap && _str)
697 return;
698 std::size_t _cap = _str_cap;
699 while (_cap <= cap)
700 _cap += _increaseBy;
701 _increaseBy++;
702 _setCapacity(_cap);
703 }
704 void _decreaseCapacity(const std::size_t cap) {
705 if (_str_cap < cap)
706 return;
708 --_increaseBy;
709 _setCapacity(cap);
710 }
711 void _alloc_cstring(char*& buffer, const std::size_t n) const {
712 if (buffer)
713 throw;
714 buffer = _new_data(n + 1);
715 }
716 void _alloc_cstring(char*& buffer, const std::size_t n, char c) const {
717 _alloc_cstring(buffer, n);
718 _fill_str(buffer, n, 0, c);
719 }
720 void _alloc_cstring(char*& buffer, const std::size_t n, const const_iterator i1, const const_iterator i2) const {
721 _alloc_cstring(buffer, n);
723 for (std::size_t i = 0; i < n; ++i)
724 buffer[i] = *begin++;
725 }
726 void _fill_str(char* other, const std::size_t len, std::size_t pos, char c) const {
727 if (other == nullptr)
728 return;
729 std::size_t begin = pos;
730 while (begin != len)
731 other[begin++] = c;
732 other[begin] = '\0';
733 }
734 void _clear_str(const std::size_t pos) {
735 _fill_str(_str, _str_len, pos, '\0');
736 _str_len = pos;
737 }
738 char* _new_data(std::size_t count) const {
739 if (malloc_fun_ != nullptr)
740 return malloc_fun_(count);
741 else
742 return memory::safe_malloc_container_element<char>(count);
743 }
744 void _delete_data(char*& data) const {
745 if (data == nullptr)
746 return;
747 if (delete_fun_ != nullptr) {
748 delete_fun_(data);
749 } else {
750 memory::safe_delete_container_element<char>(data);
751 }
752 }
753
754private:
755 char* _str;
756 std::size_t _str_len;
757 std::size_t _str_cap;
761};
763namespace {
764struct _ABS {
765 static uint32_t abs(uint32_t value) { return value; };
766 static uint64_t abs(uint64_t value) { return value; };
767 static int32_t abs(int32_t value) { return ::abs(value); };
768 static int64_t abs(int64_t value) {
769#ifdef NE_STL_OS_WIN
770 return ::_abs64(value);
771#else
772 return (int64_t)::fabs(value);
773#endif // NE_STL_OS_WIN
774 };
775};
776template <typename T>
777typename std::enable_if<std::numeric_limits<T>::is_signed, bool>::type is_valuenegative(T value) {
778 static_assert(std::numeric_limits<T>::is_specialized, "Argument must be numeric.");
779 return value < 0;
780}
781template <typename T>
782typename std::enable_if<!std::numeric_limits<T>::is_signed, bool>::type is_valuenegative(T) {
783 static_assert(std::numeric_limits<T>::is_specialized, "Argument must be numeric.");
784 return false;
785}
786template <typename TInt>
787nstd::string int2string(TInt value) {
788 const size_t kOutputBufSize = 3 * sizeof(TInt) + std::numeric_limits<TInt>::is_signed;
789 using chr_t = typename nstd::string::value_type;
790 chr_t outbuf[kOutputBufSize];
791 auto res = _ABS::abs(value);
792 chr_t* end = outbuf + kOutputBufSize;
793 chr_t* i = end;
794 do {
795 --i;
796 *i = static_cast<chr_t>((res % 10) + '0');
797 res /= 10;
798 } while (res != 0);
799 if (is_valuenegative(value)) {
800 --i;
801 *i = static_cast<chr_t>('-');
802 }
803 return nstd::string(i, end - i);
804}
805} // namespace
806} // namespace nstd
807
808template <>
809struct std::hash<nstd::string> {
810 std::size_t operator()(const nstd::string& ne_str) const noexcept { return std::hash<std::string>{}(ne_str.c_str()); }
811};
812
813#endif // !NE_STL_INCLUDENE_STL_STRING_
Definition: ne_stl_iterator.h:22
nstd::iterator_type< TElement > & current()
Definition: ne_stl_iterator.h:32
Definition: ne_stl_iterator.h:159
Definition: ne_stl_iterator.h:108
Definition: ne_stl_iterator.h:242
Definition: ne_stl_string.h:19
iterator begin()
Definition: ne_stl_string.h:187
char value_type
Definition: ne_stl_string.h:21
_string & operator+=(char c)
Definition: ne_stl_string.h:225
_string & replace(const_iterator i1, const_iterator i2, const char *other, std::size_t n)
Definition: ne_stl_string.h:394
std::size_t _str_len
Definition: ne_stl_string.h:756
_string & operator+=(const char *s)
Definition: ne_stl_string.h:221
~_string()
Definition: ne_stl_string.h:138
_string & operator+=(const _string &s)
Definition: ne_stl_string.h:217
std::size_t _getLength(const const_iterator first, const const_iterator second)
Definition: ne_stl_string.h:205
std::size_t find_last_of(const char *other, std::size_t pos=nstd::container_npos::value) const
Definition: ne_stl_string.h:540
friend bool operator<(const _string &lhs, const _string &rhs)
Definition: ne_stl_string.h:148
std::size_t copy(char *s, std::size_t len, std::size_t pos=0) const
Definition: ne_stl_string.h:513
bool _find_compare(const char *other, std::size_t len, std::size_t pos) const
Definition: ne_stl_string.h:593
friend bool operator!=(const _string &lhs, const _string &rhs)
Definition: ne_stl_string.h:147
const char & operator[](std::size_t i) const
Definition: ne_stl_string.h:169
void reserve(std::size_t n=0)
Definition: ne_stl_string.h:655
const_iterator end() const
Definition: ne_stl_string.h:190
char * _str
Definition: ne_stl_string.h:755
friend bool operator>=(const _string &lhs, const _string &rhs)
Definition: ne_stl_string.h:151
const_reverse_iterator rbegin() const
Definition: ne_stl_string.h:195
void insert(iterator p, std::size_t n, char c)
Definition: ne_stl_string.h:333
std::size_t _find_first_of(const char *other, std::size_t len, std::size_t pos, bool isTrue) const
Definition: ne_stl_string.h:599
std::size_t find_first_not_of(const char *other, std::size_t pos=0) const
Definition: ne_stl_string.h:547
_string & operator=(const std::string &std_string)
Definition: ne_stl_string.h:127
const char & back() const
Definition: ne_stl_string.h:183
std::size_t _find(const char *other, std::size_t len, std::size_t pos) const
Definition: ne_stl_string.h:570
_string(const char *copy)
Definition: ne_stl_string.h:59
const_iterator cbegin() const
Definition: ne_stl_string.h:191
char & back()
Definition: ne_stl_string.h:184
std::size_t rfind(const _string &other, std::size_t pos=container_npos::value) const
Definition: ne_stl_string.h:527
std::size_t find_last_not_of(const char *other, std::size_t pos=nstd::container_npos::value) const
Definition: ne_stl_string.h:554
const char & at(std::size_t i) const
Definition: ne_stl_string.h:171
std::size_t _rfind(const char *other, std::size_t len, std::size_t pos) const
Definition: ne_stl_string.h:581
friend bool operator>(const _string &lhs, const _string &rhs)
Definition: ne_stl_string.h:149
string_iterator_type _end() const
Definition: ne_stl_string.h:204
const_reverse_iterator rend() const
Definition: ne_stl_string.h:197
_string(std::size_t n, char c)
Definition: ne_stl_string.h:77
string_iterator_type _ibegin() const
Definition: ne_stl_string.h:202
std::size_t find_first_not_of(const char *other, std::size_t pos, std::size_t n) const
Definition: ne_stl_string.h:548
_string & replace(const_iterator i1, const_iterator i2, const _string &other)
Definition: ne_stl_string.h:370
void _append(const char *other)
Definition: ne_stl_string.h:432
std::size_t _getLength(const _string &str, std::size_t pos, std::size_t len=container_npos::value) const
Definition: ne_stl_string.h:500
std::size_t find_last_of(char c, std::size_t pos=nstd::container_npos::value) const
Definition: ne_stl_string.h:544
_string(const char *s, std::size_t n)
Definition: ne_stl_string.h:68
std::size_t find_first_of(const char *other, std::size_t pos=0) const
Definition: ne_stl_string.h:533
iterator insert(iterator p, char c)
Definition: ne_stl_string.h:339
nstd::iterator_type< char > string_iterator_type
Definition: ne_stl_string.h:24
_string(const const_iterator first, const const_iterator second)
Definition: ne_stl_string.h:86
const_iterator begin() const
Definition: ne_stl_string.h:188
void clear()
Definition: ne_stl_string.h:666
friend bool operator==(const _string &lhs, const _string &rhs)
Definition: ne_stl_string.h:146
bool empty() const
Definition: ne_stl_string.h:667
_string & append(const _string &other, std::size_t subpos, std::size_t sublen=container_npos::value)
Definition: ne_stl_string.h:249
void _substr(char *&buffer, const char *other, std::size_t pos=0, std::size_t len=container_npos::value) const
Definition: ne_stl_string.h:473
_string(const _string &other)
Definition: ne_stl_string.h:41
std::size_t find(const _string &other, std::size_t pos=0) const
Definition: ne_stl_string.h:522
std::size_t rfind(const char *s, std::size_t pos=container_npos::value) const
Definition: ne_stl_string.h:528
std::size_t find_first_of(const _string &other, std::size_t pos=0) const
Definition: ne_stl_string.h:532
void _alloc_cstring(char *&buffer, const std::size_t n, const const_iterator i1, const const_iterator i2) const
Definition: ne_stl_string.h:720
_string operator+(char c) const
Definition: ne_stl_string.h:239
void swap(_string &other)
Definition: ne_stl_string.h:421
std::size_t find_first_not_of(char c, std::size_t pos=0) const
Definition: ne_stl_string.h:549
void _increaseCapacity(const std::size_t cap)
Definition: ne_stl_string.h:695
char & at(std::size_t i)
Definition: ne_stl_string.h:176
void _replace(std::size_t pos, std::size_t len, const char *other, std::size_t n)
Definition: ne_stl_string.h:488
_string()
Definition: ne_stl_string.h:31
_string(const _string &other, std::size_t pos, std::size_t len=container_npos::value)
Definition: ne_stl_string.h:50
void resize(std::size_t n, char c)
Definition: ne_stl_string.h:643
string_iterator_type _rbegin() const
Definition: ne_stl_string.h:203
_string & replace(std::size_t pos, std::size_t len, const char *s)
Definition: ne_stl_string.h:382
void _insert_str(std::size_t pos, const char *other)
Definition: ne_stl_string.h:447
_string & insert(std::size_t pos, std::size_t n, char c)
Definition: ne_stl_string.h:326
void _append(const char *other, std::size_t n)
Definition: ne_stl_string.h:437
std::size_t find_last_of(const char *other, std::size_t pos, std::size_t n) const
Definition: ne_stl_string.h:543
_string & replace(const_iterator i1, const_iterator i2, const char *other)
Definition: ne_stl_string.h:386
std::size_t find(char c, std::size_t pos=0) const
Definition: ne_stl_string.h:525
_string & replace(std::size_t pos, std::size_t len, std::size_t n, char c)
Definition: ne_stl_string.h:398
void resize(std::size_t n)
Definition: ne_stl_string.h:634
void _delete_data(char *&data) const
Definition: ne_stl_string.h:744
std::size_t _increaseBy
Definition: ne_stl_string.h:758
_string & replace(std::size_t pos, std::size_t len, const _string &other, std::size_t subpos, std::size_t sublen=container_npos::value)
Definition: ne_stl_string.h:374
_string & replace(const_iterator i1, const_iterator i2, const_iterator first, const_iterator second)
Definition: ne_stl_string.h:412
void _fill_str(char *other, const std::size_t len, std::size_t pos, char c) const
Definition: ne_stl_string.h:726
void _alloc_cstring(char *&buffer, const std::size_t n, char c) const
Definition: ne_stl_string.h:716
reverse_iterator rbegin()
Definition: ne_stl_string.h:194
std::size_t rfind(char c, std::size_t pos=container_npos::value) const
Definition: ne_stl_string.h:530
bool _find_of_compare(const char *other, std::size_t len, std::size_t pos, bool isTrue) const
Definition: ne_stl_string.h:623
std::size_t find_first_of(char c, std::size_t pos=0) const
Definition: ne_stl_string.h:535
char * _new_data(std::size_t count) const
Definition: ne_stl_string.h:738
_string & insert(std::size_t pos, const char *s, std::size_t n)
Definition: ne_stl_string.h:319
_string & operator=(const _string &other)
Definition: ne_stl_string.h:120
const_iterator cend() const
Definition: ne_stl_string.h:192
iterator erase(const_iterator p)
Definition: ne_stl_string.h:357
std::size_t capacity() const
Definition: ne_stl_string.h:654
std::size_t find(const char *s, std::size_t pos=0) const
Definition: ne_stl_string.h:523
char & operator[](std::size_t i)
Definition: ne_stl_string.h:170
iterator end()
Definition: ne_stl_string.h:189
std::size_t length() const
Definition: ne_stl_string.h:631
_string & operator=(const char *other)
Definition: ne_stl_string.h:113
void shrink_to_fit()
Definition: ne_stl_string.h:668
const_reverse_iterator crend() const
Definition: ne_stl_string.h:199
std::size_t max_size() const
Definition: ne_stl_string.h:633
_string & append(std::size_t n, char c)
Definition: ne_stl_string.h:276
const_reverse_iterator crbegin() const
Definition: ne_stl_string.h:198
_string & replace(std::size_t pos, std::size_t len, const char *other, std::size_t n)
Definition: ne_stl_string.h:390
element_malloc_fun_t< char > malloc_fun_
Definition: ne_stl_string.h:759
void insert(iterator p, const const_iterator begin, const const_iterator last)
Definition: ne_stl_string.h:343
std::size_t find_first_of(const char *other, std::size_t pos, std::size_t n) const
Definition: ne_stl_string.h:534
std::size_t _str_cap
Definition: ne_stl_string.h:757
void _decreaseCapacity(const std::size_t cap)
Definition: ne_stl_string.h:704
_string & insert(std::size_t pos, const _string &other, std::size_t subpos, std::size_t sublen=container_npos::value)
Definition: ne_stl_string.h:307
std::size_t rfind(const char *s, std::size_t pos, std::size_t n) const
Definition: ne_stl_string.h:529
_string & append(const char *other)
Definition: ne_stl_string.h:259
_string(const std::string &other)
Definition: ne_stl_string.h:95
_string & replace(const_iterator i1, const_iterator i2, std::size_t n, char c)
Definition: ne_stl_string.h:405
char & front()
Definition: ne_stl_string.h:182
int32_t _compare(const _string &rhs) const
Definition: ne_stl_string.h:154
void _clear_str(const std::size_t pos)
Definition: ne_stl_string.h:734
element_delete_fun_t< char > delete_fun_
Definition: ne_stl_string.h:760
_string(_string &&other)
Definition: ne_stl_string.h:97
void _setCapacity(const std::size_t cap)
Definition: ne_stl_string.h:678
std::size_t find_last_of(const _string &other, std::size_t pos=nstd::container_npos::value) const
Definition: ne_stl_string.h:537
std::size_t find(const char *s, std::size_t pos, std::size_t n) const
Definition: ne_stl_string.h:524
void _erase(std::size_t pos, std::size_t len)
Definition: ne_stl_string.h:482
_string & insert(std::size_t pos, const _string &other)
Definition: ne_stl_string.h:303
friend bool operator<=(const _string &lhs, const _string &rhs)
Definition: ne_stl_string.h:150
std::size_t find_first_not_of(const _string &other, std::size_t pos=0) const
Definition: ne_stl_string.h:546
const char * c_str() const
Definition: ne_stl_string.h:511
reverse_iterator rend()
Definition: ne_stl_string.h:196
std::size_t find_last_not_of(const _string &other, std::size_t pos=nstd::container_npos::value) const
Definition: ne_stl_string.h:551
_string & append(const const_iterator first, const const_iterator second)
Definition: ne_stl_string.h:285
std::size_t find_last_not_of(char c, std::size_t pos=nstd::container_npos::value) const
Definition: ne_stl_string.h:558
void _insert_str(std::size_t pos, const char *other, std::size_t n)
Definition: ne_stl_string.h:451
_string & pop_back()
Definition: ne_stl_string.h:426
_string & append(const _string &other)
Definition: ne_stl_string.h:245
void _setLength(const std::size_t len)
Definition: ne_stl_string.h:671
_string operator+(const char *s) const
Definition: ne_stl_string.h:234
_string substr(std::size_t pos=0, std::size_t len=container_npos::value) const
Definition: ne_stl_string.h:560
void _alloc_cstring(char *&buffer, const std::size_t n) const
Definition: ne_stl_string.h:711
_string operator+(const _string &s) const
Definition: ne_stl_string.h:229
_string & erase(std::size_t pos=0, std::size_t len=container_npos::value)
Definition: ne_stl_string.h:353
iterator erase(const_iterator first, const_iterator last)
Definition: ne_stl_string.h:361
_string & push_back(char c)
Definition: ne_stl_string.h:298
std::size_t find_last_not_of(const char *other, std::size_t pos, std::size_t n) const
Definition: ne_stl_string.h:557
_string & append(const char *s, std::size_t n)
Definition: ne_stl_string.h:263
_string & insert(std::size_t pos, const char *other)
Definition: ne_stl_string.h:315
std::size_t _find_last_of(const char *other, std::size_t len, std::size_t pos, bool isTrue) const
Definition: ne_stl_string.h:611
_string & replace(std::size_t pos, std::size_t len, const _string &other)
Definition: ne_stl_string.h:366
const char & front() const
Definition: ne_stl_string.h:181
std::size_t size() const
Definition: ne_stl_string.h:632
Definition: ne_stl_any.h:7
void(*)(TElement *&) element_delete_fun_t
Definition: ne_stl_memory.h:13
TElement *(*)(std::size_t) element_malloc_fun_t
Definition: ne_stl_memory.h:10
_string<> string
Definition: ne_stl_string.h:762
#define false
Definition: stdbool.h:33
Definition: ne_stl_value_def.h:12
static const T value
Definition: ne_stl_type_traits.h:10
Definition: ne_stl_iterator.h:9
std::size_t operator()(const nstd::string &ne_str) const noexcept
Definition: ne_stl_string.h:810