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 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
|
#include "String.h"
#include "Array.h"
#include "StaticArray.h"
#include <cstring>
namespace Death { namespace Containers {
//###==##====#=====--==~--~=~- --- -- - - - -
namespace
{
enum : std::size_t {
SmallSizeMask = 0xc0,
LargeSizeMask = SmallSizeMask << (sizeof(std::size_t) - 1) * 8
};
}
static_assert(std::size_t(LargeSizeMask) == Implementation::StringViewSizeMask,
"Reserved bits should be the same in String and StringView");
static_assert(std::size_t(LargeSizeMask) == (std::size_t(StringViewFlags::Global) | (std::size_t(Implementation::SmallStringBit) << (sizeof(std::size_t) - 1) * 8)),
"Small string and global view bits should cover both reserved bits");
String String::nullTerminatedView(StringView view) {
if ((view.flags() & StringViewFlags::NullTerminated) == StringViewFlags::NullTerminated) {
String out{view.data(), view.size(), [](char*, std::size_t) {}};
out._large.size |= std::size_t(view.flags() & StringViewFlags::Global);
return out;
}
return String{view};
}
String String::nullTerminatedView(AllocatedInitT, StringView view) {
if ((view.flags() & StringViewFlags::NullTerminated) == StringViewFlags::NullTerminated) {
String out{view.data(), view.size(), [](char*, std::size_t) {}};
out._large.size |= std::size_t(view.flags() & StringViewFlags::Global);
return out;
}
return String{AllocatedInit, view};
}
String String::nullTerminatedGlobalView(StringView view) {
if ((view.flags() & (StringViewFlags::NullTerminated | StringViewFlags::Global)) == (StringViewFlags::NullTerminated | StringViewFlags::Global)) {
String out{view.data(), view.size(), [](char*, std::size_t) {}};
out._large.size |= std::size_t(StringViewFlags::Global);
return out;
}
return String{view};
}
String String::nullTerminatedGlobalView(AllocatedInitT, StringView view) {
if ((view.flags() & (StringViewFlags::NullTerminated | StringViewFlags::Global)) == (StringViewFlags::NullTerminated | StringViewFlags::Global)) {
String out{view.data(), view.size(), [](char*, std::size_t) {}};
out._large.size |= std::size_t(StringViewFlags::Global);
return out;
}
return String{AllocatedInit, view};
}
inline void String::construct(NoInitT, const std::size_t size) {
if (size < Implementation::SmallStringSize) {
_small.data[size] = '\0';
_small.size = (unsigned char)(size | Implementation::SmallStringBit);
} else {
_large.data = new char[size + 1];
_large.data[size] = '\0';
_large.size = size;
_large.deleter = nullptr;
}
}
inline void String::construct(const char* const data, const std::size_t size) {
construct(NoInit, size);
// If the size is small enough for SSO, use that. Not using <= because we need to store the null terminator as well.
if (size < Implementation::SmallStringSize) {
// Apparently memcpy() can't be called with null pointers, even if size is zero. I call that bullying.
if (size != 0) std::memcpy(_small.data, data, size);
} else {
// Otherwise allocate. Assuming the size is small enough -- this should have been checked in the caller already.
std::memcpy(_large.data, data, size);
}
}
inline void String::destruct() {
// If not SSO, delete the data
if (_small.size & Implementation::SmallStringBit) return;
// Instances created with a custom deleter either don't the Global bit set at all, or have it set but the deleter
// is a no-op passed from nullTerminatedView() / nullTerminatedGlobalView(). Thus *technically* it's not needed to clear
// the LargeSizeMask (which implies there's also no way to test that it got cleared), but do it for consistency.
if (_large.deleter) _large.deleter(_large.data, _large.size & ~LargeSizeMask);
else delete[] _large.data;
}
inline String::Data String::dataInternal() const {
if (_small.size & Implementation::SmallStringBit)
return {_small.data, _small.size & ~SmallSizeMask};
return {_large.data, _large.size & ~LargeSizeMask};
}
String::String() noexcept {
// Create a zero-size small string to fullfil the guarantee of data() being always non-null and null-terminated
_small.data[0] = '\0';
_small.size = Implementation::SmallStringBit;
}
String::String(const StringView view) : String{view._data, view._sizePlusFlags & ~Implementation::StringViewSizeMask} {}
String::String(const MutableStringView view) : String{view._data, view._sizePlusFlags & ~Implementation::StringViewSizeMask} {}
String::String(const ArrayView<const char> view) : String{view.data(), view.size()} {}
String::String(const ArrayView<char> view) : String{view.data(), view.size()} {}
String::String(std::nullptr_t, std::nullptr_t, std::nullptr_t, const char* const data) : String{data, data ? std::strlen(data) : 0} {}
String::String(const char* const data, const std::size_t size)
: _large{}
{
#if defined(DEATH_TARGET_32BIT)
// Compared to StringView construction which happens a lot this shouldn't, and the chance of strings > 1 GB on 32-bit
// is rare but possible and thus worth checking even in release
DEATH_ASSERT(size < std::size_t{1} << (sizeof(std::size_t) * 8 - 2), ("String expected to be smaller than 2^{} bytes, got {}", sizeof(std::size_t) * 8 - 2, size), );
#endif
DEATH_ASSERT(data != nullptr || size == 0, ("Received a null string of size {}", size), );
construct(data, size);
}
String::String(AllocatedInitT, const StringView view) : String{AllocatedInit, view._data, view._sizePlusFlags & ~Implementation::StringViewSizeMask} {}
String::String(AllocatedInitT, const MutableStringView view) : String{AllocatedInit, view._data, view._sizePlusFlags & ~Implementation::StringViewSizeMask} {}
String::String(AllocatedInitT, const ArrayView<const char> view) : String{AllocatedInit, view.data(), view.size()} {}
String::String(AllocatedInitT, const ArrayView<char> view) : String{AllocatedInit, view.data(), view.size()} {}
String::String(AllocatedInitT, const char* const data) : String{AllocatedInit, data, data ? std::strlen(data) : 0} {}
String::String(AllocatedInitT, const char* const data, const std::size_t size)
: _large{}
{
// Compared to StringView construction which happens a lot this shouldn't, and the chance of strings > 1 GB on 32-bit
// is rare but possible and thus worth checking even in release
DEATH_ASSERT(size < std::size_t{1} << (sizeof(std::size_t) * 8 - 2), ("String expected to be smaller than 2^{} bytes, got {}", sizeof(std::size_t) * 8 - 2, size), );
DEATH_ASSERT(data != nullptr || size == 0, ("Received a null string of size {}", size), );
_large.data = new char[size + 1];
// Apparently memcpy() can't be called with null pointers, even if size is zero. I call that bullying.
if (size != 0) std::memcpy(_large.data, data, size);
_large.data[size] = '\0';
_large.size = size;
_large.deleter = nullptr;
}
String::String(AllocatedInitT, String&& other)
{
// Allocate a copy if the other is a SSO
if (other.isSmall()) {
const std::size_t sizePlusOne = (other._small.size & ~SmallSizeMask) + 1;
_large.data = new char[sizePlusOne];
// Copies also the null terminator
std::memcpy(_large.data, other._small.data, sizePlusOne);
_large.size = other._small.size & ~SmallSizeMask;
_large.deleter = nullptr;
} else {
// Otherwise take over the data
_large.data = other._large.data;
_large.size = other._large.size; // including the potential Global bit
_large.deleter = other._large.deleter;
}
// Move-out the other instance in both cases
other._large.data = nullptr;
other._large.size = 0;
other._large.deleter = nullptr;
}
String::String(AllocatedInitT, const String& other)
{
const Data data = other.dataInternal();
const std::size_t sizePlusOne = data.size + 1;
_large.size = data.size;
_large.data = new char[sizePlusOne];
// Copies also the null terminator
std::memcpy(_large.data, data.data, sizePlusOne);
_large.deleter = nullptr;
}
String::String(char* const data, const std::size_t size, void(*deleter)(char*, std::size_t)) noexcept
: _large{}
{
// Compared to StringView construction which happens a lot this shouldn't, the chance of strings > 1 GB on 32-bit
// is rare but possible and thus worth checking even in release; but most importantly checking for null
// termination outweighs potential speed issues
DEATH_ASSERT(size < std::size_t{1} << (sizeof(std::size_t) * 8 - 2), ("String expected to be smaller than 2^{} bytes, got {}", sizeof(std::size_t) * 8 - 2, size), );
DEATH_ASSERT(data != nullptr && !data[size], "Can only take ownership of a non-null null-terminated array", );
_large.data = data;
_large.size = size;
_large.deleter = deleter;
}
String::String(void(*deleter)(char*, std::size_t), std::nullptr_t, char* const data) noexcept : String{
data,
// If data is null, strlen() would crash before reaching our assert inside the delegated-to constructor
data ? std::strlen(data) : 0,
deleter
} {}
String::String(ValueInitT, const std::size_t size)
: _large{}
{
// Compared to StringView construction which happens a lot this shouldn't, and the chance of strings > 1 GB on 32-bit
// is rare but possible and thus worth checking even in release
DEATH_ASSERT(size < std::size_t{1} << (sizeof(std::size_t) * 8 - 2), ("String expected to be smaller than 2^{} bytes, got {}", sizeof(std::size_t) * 8 - 2, size), );
if (size < Implementation::SmallStringSize) {
// Everything already zero-init'd in the constructor init list
_small.size = (unsigned char)(size | Implementation::SmallStringBit);
} else {
_large.data = new char[size + 1] {};
_large.size = size;
_large.deleter = nullptr;
}
}
String::String(NoInitT, const std::size_t size)
{
// Compared to StringView construction which happens a lot this shouldn't, and the chance of strings > 1 GB on 32-bit
// is rare but possible and thus worth checking even in release
DEATH_ASSERT(size < std::size_t{1} << (sizeof(std::size_t) * 8 - 2), ("String expected to be smaller than 2^{} bytes, got {}", sizeof(std::size_t) * 8 - 2, size), );
construct(NoInit, size);
}
String::String(DirectInitT, const std::size_t size, const char c) : String{NoInit, size} {
std::memset(size < Implementation::SmallStringSize ? _small.data : _large.data, c, size);
}
String::~String() {
destruct();
}
inline void String::copyConstruct(const String& other) {
// For predictability, in particular preserving the AllocatedInit aspect of a string in copies and not just in moves,
// if the original string is allocated, the copied one is as well, independently of the actual size.
if (other.isSmall()) {
std::memcpy(_small.data, other._small.data, Implementation::SmallStringSize);
_small.size = other._small.size;
} else {
// Excluding the potential Global bit
const std::size_t size = other._large.size & ~LargeSizeMask;
_large.data = new char[size + 1];
// Copies also the null terminator
std::memcpy(_large.data, other._large.data, size + 1);
_large.size = size;
_large.deleter = nullptr;
}
}
String::String(const String& other) {
copyConstruct(other);
}
String::String(String&& other) noexcept {
// Similarly as in operator=(String&&), the following works also in case of SSO, as for small string we would be
// doing a copy of _small.data and then also a copy of _small.size *including* the two highest bits
_large.data = other._large.data;
_large.size = other._large.size; // including the potential Global bit
_large.deleter = other._large.deleter;
other._large.data = nullptr;
other._large.size = 0;
other._large.deleter = nullptr;
}
String& String::operator=(const String& other) {
destruct();
copyConstruct(other);
return *this;
}
String& String::operator=(String&& other) noexcept {
using Death::swap;
swap(other._large.data, _large.data);
swap(other._large.size, _large.size); // including the potential Global bit
swap(other._large.deleter, _large.deleter);
return *this;
}
String::operator ArrayView<const char>() const noexcept {
const Data data = dataInternal();
return {data.data, data.size};
}
String::operator ArrayView<const void>() const noexcept {
const Data data = dataInternal();
return {data.data, data.size};
}
String::operator ArrayView<char>() noexcept {
const Data data = dataInternal();
return {const_cast<char*>(data.data), data.size};
}
String::operator ArrayView<void>() noexcept {
const Data data = dataInternal();
return {const_cast<char*>(data.data), data.size};
}
String::operator Array<char>() && {
Array<char> out;
if (_small.size & Implementation::SmallStringBit) {
const std::size_t size = _small.size & ~SmallSizeMask;
// Allocate the output including a null terminator at the end, but don't include it in the size
out = Array<char>{Array<char>{NoInit, size + 1}.release(), size};
out[size] = '\0';
std::memcpy(out.data(), _small.data, size);
} else {
out = Array<char>{_large.data, _large.size & ~LargeSizeMask, deleter()};
}
// Same as in release(). Create a zero-size small string to fullfil the guarantee of data() being always non-null
// and null-terminated. Since this makes the string switch to SSO, we also clear the deleter this way.
_small.data[0] = '\0';
_small.size = Implementation::SmallStringBit;
return out;
}
String::operator bool() const {
// The data pointer is guaranteed to be non-null, so no need to check it
if (_small.size & Implementation::SmallStringBit)
return _small.size & ~SmallSizeMask;
return _large.size & ~LargeSizeMask;
}
StringViewFlags String::viewFlags() const {
return StringViewFlags(_large.size & std::size_t(StringViewFlags::Global)) | StringViewFlags::NullTerminated;
}
const char* String::data() const {
if (_small.size & Implementation::SmallStringBit)
return _small.data;
return _large.data;
}
char* String::data() {
if (_small.size & Implementation::SmallStringBit)
return _small.data;
return _large.data;
}
bool String::empty() const {
if (_small.size & Implementation::SmallStringBit)
return !(_small.size & ~SmallSizeMask);
return !(_large.size & ~LargeSizeMask);
}
auto String::deleter() const -> Deleter {
DEATH_DEBUG_ASSERT(!(_small.size & Implementation::SmallStringBit),
"Cannot call on a SSO instance", {});
return _large.deleter;
}
std::size_t String::size() const {
if (_small.size & Implementation::SmallStringBit)
return _small.size & ~SmallSizeMask;
return _large.size & ~LargeSizeMask;
}
char* String::begin() {
if (_small.size & Implementation::SmallStringBit)
return _small.data;
return _large.data;
}
const char* String::begin() const {
if (_small.size & Implementation::SmallStringBit)
return _small.data;
return _large.data;
}
const char* String::cbegin() const {
if (_small.size & Implementation::SmallStringBit)
return _small.data;
return _large.data;
}
char* String::end() {
if (_small.size & Implementation::SmallStringBit)
return _small.data + (_small.size & ~SmallSizeMask);
return _large.data + (_large.size & ~LargeSizeMask);
}
const char* String::end() const {
if (_small.size & Implementation::SmallStringBit)
return _small.data + (_small.size & ~SmallSizeMask);
return _large.data + (_large.size & ~LargeSizeMask);
}
const char* String::cend() const {
if (_small.size & Implementation::SmallStringBit)
return _small.data + (_small.size & ~SmallSizeMask);
return _large.data + (_large.size & ~LargeSizeMask);
}
char& String::front() {
DEATH_DEBUG_ASSERT(size(), "String is empty", *begin());
return *begin();
}
char String::front() const {
return const_cast<String&>(*this).front();
}
char& String::back() {
DEATH_DEBUG_ASSERT(size(), "String is empty", *(end() - 1));
return *(end() - 1);
}
char String::back() const {
return const_cast<String&>(*this).back();
}
char& String::operator[](std::size_t i) {
// Accessing the null terminator is fine
DEATH_DEBUG_ASSERT(i < size() + 1, ("Index {} out of range for {} null-terminated bytes", i, size()), _small.data[0]);
if (_small.size & Implementation::SmallStringBit)
return _small.data[i];
return _large.data[i];
}
char String::operator[](std::size_t i) const {
// Accessing the null terminator is fine
DEATH_DEBUG_ASSERT(i < size() + 1, ("Index {} out of range for {} null-terminated bytes", i, size()), _small.data[0]);
if (_small.size & Implementation::SmallStringBit)
return _small.data[i];
return _large.data[i];
}
String String::operator+=(const StringView& other)
{
std::size_t otherSize = other.size();
if (otherSize == 0) {
return *this;
}
std::size_t currentSize = size();
std::size_t newSize = currentSize + otherSize;
if (newSize < Implementation::SmallStringSize) {
std::memcpy(_small.data + currentSize, other._data, otherSize);
_small.data[newSize] = '\0';
_small.size = (unsigned char)(newSize | Implementation::SmallStringBit);
} else if (isSmall()) {
char tmp[Implementation::SmallStringSize];
std::memcpy(tmp, _small.data, currentSize);
construct(NoInit, newSize);
std::memcpy(_large.data, tmp, currentSize);
std::memcpy(_large.data + currentSize, other._data, otherSize);
} else {
char* oldData = _large.data;
auto oldDeleter = _large.deleter;
construct(NoInit, newSize);
std::memcpy(_large.data, oldData, currentSize);
std::memcpy(_large.data + currentSize, other._data, otherSize);
if (oldDeleter) oldDeleter(oldData, currentSize);
else delete[] oldData;
}
return *this;
}
MutableStringView String::slice(char* const begin, char* const end) {
return MutableStringView{*this}.slice(begin, end);
}
StringView String::slice(const char* const begin, const char* const end) const {
return StringView{*this}.slice(begin, end);
}
MutableStringView String::slice(const std::size_t begin, const std::size_t end) {
return MutableStringView{*this}.slice(begin, end);
}
StringView String::slice(const std::size_t begin, const std::size_t end) const {
return StringView{*this}.slice(begin, end);
}
MutableStringView String::sliceSizePointerInternal(char* const begin, const std::size_t size) {
return MutableStringView{*this}.sliceSize(begin, size);
}
StringView String::sliceSizePointerInternal(const char* const begin, const std::size_t size) const {
return StringView{*this}.sliceSize(begin, size);
}
MutableStringView String::sliceSize(const std::size_t begin, const std::size_t size) {
return MutableStringView{*this}.sliceSize(begin, size);
}
StringView String::sliceSize(const std::size_t begin, const std::size_t size) const {
return StringView{*this}.sliceSize(begin, size);
}
MutableStringView String::prefixPointerInternal(char* const end) {
return MutableStringView{*this}.prefix(end);
}
StringView String::prefixPointerInternal(const char* const end) const {
return StringView{*this}.prefix(end);
}
MutableStringView String::suffix(char* const begin) {
return MutableStringView{*this}.suffix(begin);
}
StringView String::suffix(const char* const begin) const {
return StringView{*this}.suffix(begin);
}
MutableStringView String::prefix(const std::size_t count) {
return MutableStringView{*this}.prefix(count);
}
StringView String::prefix(const std::size_t count) const {
return StringView{*this}.prefix(count);
}
MutableStringView String::exceptPrefix(const std::size_t count) {
return MutableStringView{*this}.exceptPrefix(count);
}
StringView String::exceptPrefix(const std::size_t count) const {
return StringView{*this}.exceptPrefix(count);
}
MutableStringView String::exceptSuffix(const std::size_t count) {
return MutableStringView{*this}.exceptSuffix(count);
}
StringView String::exceptSuffix(const std::size_t count) const {
return StringView{*this}.exceptSuffix(count);
}
Array<MutableStringView> String::split(const char delimiter) {
return MutableStringView{*this}.split(delimiter);
}
Array<StringView> String::split(const char delimiter) const {
return StringView{*this}.split(delimiter);
}
Array<MutableStringView> String::split(const StringView delimiter) {
return MutableStringView{*this}.split(delimiter);
}
Array<StringView> String::split(const StringView delimiter) const {
return StringView{*this}.split(delimiter);
}
Array<MutableStringView> String::splitWithoutEmptyParts(const char delimiter) {
return MutableStringView{*this}.splitWithoutEmptyParts(delimiter);
}
Array<StringView> String::splitWithoutEmptyParts(const char delimiter) const {
return StringView{*this}.splitWithoutEmptyParts(delimiter);
}
Array<MutableStringView> String::splitOnAnyWithoutEmptyParts(const StringView delimiters) {
return MutableStringView{*this}.splitOnAnyWithoutEmptyParts(delimiters);
}
Array<StringView> String::splitOnAnyWithoutEmptyParts(const StringView delimiters) const {
return StringView{*this}.splitOnAnyWithoutEmptyParts(delimiters);
}
Array<MutableStringView> String::splitOnWhitespaceWithoutEmptyParts() {
return MutableStringView{*this}.splitOnWhitespaceWithoutEmptyParts();
}
Array<StringView> String::splitOnWhitespaceWithoutEmptyParts() const {
return StringView{*this}.splitOnWhitespaceWithoutEmptyParts();
}
StaticArray<3, MutableStringView> String::partition(const char separator) {
return MutableStringView{*this}.partition(separator);
}
StaticArray<3, StringView> String::partition(const char separator) const {
return StringView{*this}.partition(separator);
}
StaticArray<3, MutableStringView> String::partition(const StringView separator) {
return MutableStringView{*this}.partition(separator);
}
StaticArray<3, StringView> String::partition(const StringView separator) const {
return StringView{*this}.partition(separator);
}
String String::join(const ArrayView<const StringView> strings) const {
return StringView{*this}.join(strings);
}
String String::join(const std::initializer_list<StringView> strings) const {
// Doing it this way instead of calling directly into StringView to have the above overload implicitly covered
return join(arrayView(strings));
}
String String::joinWithoutEmptyParts(const ArrayView<const StringView> strings) const {
return StringView{*this}.joinWithoutEmptyParts(strings);
}
String String::joinWithoutEmptyParts(const std::initializer_list<StringView> strings) const {
// Doing it this way instead of calling directly into StringView to have the above overload implicitly covered
return joinWithoutEmptyParts(arrayView(strings));
}
bool String::hasPrefix(const StringView prefix) const {
return StringView{*this}.hasPrefix(prefix);
}
bool String::hasPrefix(const char prefix) const {
return StringView{*this}.hasPrefix(prefix);
}
bool String::hasSuffix(const StringView suffix) const {
return StringView{*this}.hasSuffix(suffix);
}
bool String::hasSuffix(const char suffix) const {
return StringView{*this}.hasSuffix(suffix);
}
MutableStringView String::exceptPrefix(const StringView prefix) {
return MutableStringView{*this}.exceptPrefix(prefix);
}
StringView String::exceptPrefix(const StringView prefix) const {
return StringView{*this}.exceptPrefix(prefix);
}
MutableStringView String::exceptSuffix(const StringView suffix) {
return MutableStringView{*this}.exceptSuffix(suffix);
}
StringView String::exceptSuffix(const StringView suffix) const {
return StringView{*this}.exceptSuffix(suffix);
}
MutableStringView String::trimmed(const StringView characters) {
return MutableStringView{*this}.trimmed(characters);
}
StringView String::trimmed(const StringView characters) const {
return StringView{*this}.trimmed(characters);
}
MutableStringView String::trimmed() {
return MutableStringView{*this}.trimmed();
}
StringView String::trimmed() const {
return StringView{*this}.trimmed();
}
MutableStringView String::trimmedPrefix(const StringView characters) {
return MutableStringView{*this}.trimmedPrefix(characters);
}
StringView String::trimmedPrefix(const StringView characters) const {
return StringView{*this}.trimmedPrefix(characters);
}
MutableStringView String::trimmedPrefix() {
return MutableStringView{*this}.trimmedPrefix();
}
StringView String::trimmedPrefix() const {
return StringView{*this}.trimmedPrefix();
}
MutableStringView String::trimmedSuffix(const StringView characters) {
return MutableStringView{*this}.trimmedSuffix(characters);
}
StringView String::trimmedSuffix(const StringView characters) const {
return StringView{*this}.trimmedSuffix(characters);
}
MutableStringView String::trimmedSuffix() {
return MutableStringView{*this}.trimmedSuffix();
}
StringView String::trimmedSuffix() const {
return StringView{*this}.trimmedSuffix();
}
MutableStringView String::find(const StringView substring) {
// Calling straight into the concrete implementation to reduce call stack depth
return MutableStringView{*this}.findOr(substring, nullptr);
}
StringView String::find(const StringView substring) const {
// Calling straight into the concrete implementation to reduce call stack depth
return StringView{*this}.findOr(substring, nullptr);
}
MutableStringView String::find(const char character) {
// Calling straight into the concrete implementation to reduce call stack depth
return MutableStringView{*this}.findOr(character, nullptr);
}
StringView String::find(const char character) const {
// Calling straight into the concrete implementation to reduce call stack depth
return StringView{*this}.findOr(character, nullptr);
}
MutableStringView String::findOr(const StringView substring, char* const fail) {
return MutableStringView{*this}.findOr(substring, fail);
}
StringView String::findOr(const StringView substring, const char* const fail) const {
return StringView{*this}.findOr(substring, fail);
}
MutableStringView String::findOr(const char character, char* const fail) {
return MutableStringView{*this}.findOr(character, fail);
}
StringView String::findOr(const char character, const char* const fail) const {
return StringView{*this}.findOr(character, fail);
}
MutableStringView String::findLast(const StringView substring) {
// Calling straight into the concrete implementation to reduce call stack depth
return MutableStringView{*this}.findLastOr(substring, nullptr);
}
StringView String::findLast(const StringView substring) const {
// Calling straight into the concrete implementation to reduce call stack depth
return StringView{*this}.findLastOr(substring, nullptr);
}
MutableStringView String::findLast(const char character) {
// Calling straight into the concrete implementation to reduce call stack depth
return MutableStringView{*this}.findLastOr(character, nullptr);
}
StringView String::findLast(const char character) const {
/* Calling straight into the concrete implementation to reduce call stack depth */
return StringView{*this}.findLastOr(character, nullptr);
}
MutableStringView String::findLastOr(const StringView substring, char* const fail) {
return MutableStringView{*this}.findLastOr(substring, fail);
}
StringView String::findLastOr(const StringView substring, const char* const fail) const {
return StringView{*this}.findLastOr(substring, fail);
}
MutableStringView String::findLastOr(const char character, char* const fail) {
return MutableStringView{*this}.findLastOr(character, fail);
}
StringView String::findLastOr(const char character, const char* const fail) const {
return StringView{*this}.findLastOr(character, fail);
}
bool String::contains(const StringView substring) const {
return StringView{*this}.contains(substring);
}
bool String::contains(const char character) const {
return StringView{*this}.contains(character);
}
MutableStringView String::findAny(const StringView characters) {
return MutableStringView{*this}.findAny(characters);
}
StringView String::findAny(const StringView characters) const {
return StringView{*this}.findAny(characters);
}
MutableStringView String::findAnyOr(const StringView characters, char* fail) {
return MutableStringView{*this}.findAnyOr(characters, fail);
}
StringView String::findAnyOr(const StringView characters, const char* fail) const {
return StringView{*this}.findAnyOr(characters, fail);
}
MutableStringView String::findLastAny(const StringView characters) {
return MutableStringView{*this}.findLastAny(characters);
}
StringView String::findLastAny(const StringView characters) const {
return StringView{*this}.findLastAny(characters);
}
MutableStringView String::findLastAnyOr(const StringView characters, char* fail) {
return MutableStringView{*this}.findLastAnyOr(characters, fail);
}
StringView String::findLastAnyOr(const StringView characters, const char* fail) const {
return StringView{*this}.findLastAnyOr(characters, fail);
}
bool String::containsAny(const StringView substring) const {
return StringView{*this}.containsAny(substring);
}
std::size_t String::count(const char character) const {
return StringView{*this}.count(character);
}
char* String::release() {
DEATH_DEBUG_ASSERT(!(_small.size & Implementation::SmallStringBit),
"Cannot call on a SSO instance", {});
char* data = _large.data;
// Create a zero-size small string to fullfil the guarantee of data() being always non-null and null-terminated.
// Since this makes the string switch to SSO, we also clear the deleter this way.
_small.data[0] = '\0';
_small.size = Implementation::SmallStringBit;
return data;
}
}}
|