2 * Copyright 2017 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 // @author: Andrei Alexandrescu (aalexandre)
26 #include <type_traits>
28 // This file appears in two locations: inside fbcode and in the
29 // libstdc++ source code (when embedding fbstring as std::string).
30 // To aid in this schizophrenic use, _LIBSTDCXX_FBSTRING is defined in
31 // libstdc++'s c++config.h, to gate use inside fbcode v. libstdc++.
32 #ifdef _LIBSTDCXX_FBSTRING
34 #pragma GCC system_header
36 #include "basic_fbstring_malloc.h"
38 // When used as std::string replacement always disable assertions.
39 #define FBSTRING_ASSERT(expr) /* empty */
41 #else // !_LIBSTDCXX_FBSTRING
43 #include <folly/Portability.h>
45 // libc++ doesn't provide this header, nor does msvc
46 #ifdef FOLLY_HAVE_BITS_CXXCONFIG_H
47 #include <bits/c++config.h>
56 #include <folly/Hash.h>
57 #include <folly/Malloc.h>
58 #include <folly/Traits.h>
60 #if FOLLY_HAVE_DEPRECATED_ASSOC
61 #ifdef _GLIBCXX_SYMVER
62 #include <ext/hash_set>
63 #include <ext/hash_map>
67 // When used in folly, assertions are not disabled.
68 #define FBSTRING_ASSERT(expr) assert(expr)
72 // We defined these here rather than including Likely.h to avoid
73 // redefinition errors when fbstring is imported into libstdc++.
74 #if defined(__GNUC__) && __GNUC__ >= 4
75 #define FBSTRING_LIKELY(x) (__builtin_expect((x), 1))
76 #define FBSTRING_UNLIKELY(x) (__builtin_expect((x), 0))
78 #define FBSTRING_LIKELY(x) (x)
79 #define FBSTRING_UNLIKELY(x) (x)
82 #pragma GCC diagnostic push
83 // Ignore shadowing warnings within this file, so includers can use -Wshadow.
84 #pragma GCC diagnostic ignored "-Wshadow"
85 // GCC 4.9 has a false positive in setSmallSize (probably
86 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59124), disable
87 // compile-time array bound checking.
88 #pragma GCC diagnostic ignored "-Warray-bounds"
90 // FBString cannot use throw when replacing std::string, though it may still
93 #define throw FOLLY_FBSTRING_MAY_NOT_USE_THROW
95 #ifdef _LIBSTDCXX_FBSTRING
96 namespace std _GLIBCXX_VISIBILITY(default) {
97 _GLIBCXX_BEGIN_NAMESPACE_VERSION
102 #if defined(__clang__)
103 # if __has_feature(address_sanitizer)
104 # define FBSTRING_SANITIZE_ADDRESS
106 #elif defined (__GNUC__) && \
107 (((__GNUC__ == 4) && (__GNUC_MINOR__ >= 8)) || (__GNUC__ >= 5)) && \
109 # define FBSTRING_SANITIZE_ADDRESS
112 // When compiling with ASan, always heap-allocate the string even if
113 // it would fit in-situ, so that ASan can detect access to the string
114 // buffer after it has been invalidated (destroyed, resized, etc.).
115 // Note that this flag doesn't remove support for in-situ strings, as
116 // that would break ABI-compatibility and wouldn't allow linking code
117 // compiled with this flag with code compiled without.
118 #ifdef FBSTRING_SANITIZE_ADDRESS
119 # define FBSTRING_DISABLE_SSO true
121 # define FBSTRING_DISABLE_SSO false
124 namespace fbstring_detail {
126 template <class InIt, class OutIt>
127 inline std::pair<InIt, OutIt> copy_n(
129 typename std::iterator_traits<InIt>::difference_type n,
131 for (; n != 0; --n, ++b, ++d) {
134 return std::make_pair(b, d);
137 template <class Pod, class T>
138 inline void podFill(Pod* b, Pod* e, T c) {
139 FBSTRING_ASSERT(b && e && b <= e);
140 constexpr auto kUseMemset = sizeof(T) == 1;
141 /* static */ if (kUseMemset) {
142 memset(b, c, size_t(e - b));
144 auto const ee = b + ((e - b) & ~7u);
145 for (; b != ee; b += 8) {
156 for (; b != e; ++b) {
163 * Lightly structured memcpy, simplifies copying PODs and introduces
164 * some asserts. Unfortunately using this function may cause
165 * measurable overhead (presumably because it adjusts from a begin/end
166 * convention to a pointer/size convention, so it does some extra
167 * arithmetic even though the caller might have done the inverse
168 * adaptation outside).
171 inline void podCopy(const Pod* b, const Pod* e, Pod* d) {
172 FBSTRING_ASSERT(b != nullptr);
173 FBSTRING_ASSERT(e != nullptr);
174 FBSTRING_ASSERT(d != nullptr);
175 FBSTRING_ASSERT(e >= b);
176 FBSTRING_ASSERT(d >= e || d + (e - b) <= b);
177 memcpy(d, b, (e - b) * sizeof(Pod));
181 * Lightly structured memmove, simplifies copying PODs and introduces
185 inline void podMove(const Pod* b, const Pod* e, Pod* d) {
186 FBSTRING_ASSERT(e >= b);
187 memmove(d, b, (e - b) * sizeof(*b));
191 #if defined(__GNUC__) // Clang also defines __GNUC__
192 # define FBSTRING_ALWAYS_INLINE inline __attribute__((__always_inline__))
193 #elif defined(_MSC_VER)
194 # define FBSTRING_ALWAYS_INLINE __forceinline
196 # define FBSTRING_ALWAYS_INLINE inline
199 [[noreturn]] FBSTRING_ALWAYS_INLINE void assume_unreachable() {
200 #if defined(__GNUC__) // Clang also defines __GNUC__
201 __builtin_unreachable();
202 #elif defined(_MSC_VER)
205 // Well, it's better than nothing.
210 } // namespace fbstring_detail
213 * Defines a special acquisition method for constructing fbstring
214 * objects. AcquireMallocatedString means that the user passes a
215 * pointer to a malloc-allocated string that the fbstring object will
218 enum class AcquireMallocatedString {};
221 * fbstring_core_model is a mock-up type that defines all required
222 * signatures of a fbstring core. The fbstring class itself uses such
223 * a core object to implement all of the numerous member functions
224 * required by the standard.
226 * If you want to define a new core, copy the definition below and
227 * implement the primitives. Then plug the core into basic_fbstring as
228 * a template argument.
230 template <class Char>
231 class fbstring_core_model {
233 fbstring_core_model();
234 fbstring_core_model(const fbstring_core_model &);
235 ~fbstring_core_model();
236 // Returns a pointer to string's buffer (currently only contiguous
237 // strings are supported). The pointer is guaranteed to be valid
238 // until the next call to a non-const member function.
239 const Char * data() const;
240 // Much like data(), except the string is prepared to support
241 // character-level changes. This call is a signal for
242 // e.g. reference-counted implementation to fork the data. The
243 // pointer is guaranteed to be valid until the next call to a
244 // non-const member function.
246 // Returns a pointer to string's buffer and guarantees that a
247 // readable '\0' lies right after the buffer. The pointer is
248 // guaranteed to be valid until the next call to a non-const member
250 const Char * c_str() const;
251 // Shrinks the string by delta characters. Asserts that delta <=
253 void shrink(size_t delta);
254 // Expands the string by delta characters (i.e. after this call
255 // size() will report the old size() plus delta) but without
256 // initializing the expanded region. The expanded region is
257 // zero-terminated. Returns a pointer to the memory to be
258 // initialized (the beginning of the expanded portion). The caller
259 // is expected to fill the expanded area appropriately.
260 // If expGrowth is true, exponential growth is guaranteed.
261 // It is not guaranteed not to reallocate even if size() + delta <
262 // capacity(), so all references to the buffer are invalidated.
263 Char* expandNoinit(size_t delta, bool expGrowth);
264 // Expands the string by one character and sets the last character
266 void push_back(Char c);
267 // Returns the string's size.
269 // Returns the string's capacity, i.e. maximum size that the string
270 // can grow to without reallocation. Note that for reference counted
271 // strings that's technically a lie - even assigning characters
272 // within the existing size would cause a reallocation.
273 size_t capacity() const;
274 // Returns true if the data underlying the string is actually shared
275 // across multiple strings (in a refcounted fashion).
276 bool isShared() const;
277 // Makes sure that at least minCapacity characters are available for
278 // the string without reallocation. For reference-counted strings,
279 // it should fork the data even if minCapacity < size().
280 void reserve(size_t minCapacity);
283 fbstring_core_model& operator=(const fbstring_core_model &);
288 * This is the core of the string. The code should work on 32- and
289 * 64-bit and both big- and little-endianan architectures with any
292 * The storage is selected as follows (assuming we store one-byte
293 * characters on a 64-bit machine): (a) "small" strings between 0 and
294 * 23 chars are stored in-situ without allocation (the rightmost byte
295 * stores the size); (b) "medium" strings from 24 through 254 chars
296 * are stored in malloc-allocated memory that is copied eagerly; (c)
297 * "large" strings of 255 chars and above are stored in a similar
298 * structure as medium arrays, except that the string is
299 * reference-counted and copied lazily. the reference count is
300 * allocated right before the character array.
302 * The discriminator between these three strategies sits in two
303 * bits of the rightmost char of the storage. If neither is set, then the
304 * string is small (and its length sits in the lower-order bits on
305 * little-endian or the high-order bits on big-endian of that
306 * rightmost character). If the MSb is set, the string is medium width.
307 * If the second MSb is set, then the string is large. On little-endian,
308 * these 2 bits are the 2 MSbs of MediumLarge::capacity_, while on
309 * big-endian, these 2 bits are the 2 LSbs. This keeps both little-endian
310 * and big-endian fbstring_core equivalent with merely different ops used
311 * to extract capacity/category.
313 template <class Char> class fbstring_core {
315 // It's MSVC, so we just have to guess ... and allow an override
317 # ifdef FOLLY_ENDIAN_BE
318 static constexpr auto kIsLittleEndian = false;
320 static constexpr auto kIsLittleEndian = true;
323 static constexpr auto kIsLittleEndian =
324 __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__;
327 fbstring_core() noexcept { reset(); }
329 fbstring_core(const fbstring_core & rhs) {
330 FBSTRING_ASSERT(&rhs != this);
331 switch (rhs.category()) {
332 case Category::isSmall:
335 case Category::isMedium:
338 case Category::isLarge:
342 fbstring_detail::assume_unreachable();
344 FBSTRING_ASSERT(size() == rhs.size());
345 FBSTRING_ASSERT(memcmp(data(), rhs.data(), size() * sizeof(Char)) == 0);
348 fbstring_core(fbstring_core&& goner) noexcept {
351 // Clean goner's carcass
355 fbstring_core(const Char *const data,
357 bool disableSSO = FBSTRING_DISABLE_SSO) {
358 if (!disableSSO && size <= maxSmallSize) {
359 initSmall(data, size);
360 } else if (size <= maxMediumSize) {
361 initMedium(data, size);
363 initLarge(data, size);
365 FBSTRING_ASSERT(this->size() == size);
367 size == 0 || memcmp(this->data(), data, size * sizeof(Char)) == 0);
370 ~fbstring_core() noexcept {
371 if (category() == Category::isSmall) {
374 destroyMediumLarge();
377 // Snatches a previously mallocated string. The parameter "size"
378 // is the size of the string, and the parameter "allocatedSize"
379 // is the size of the mallocated block. The string must be
380 // \0-terminated, so allocatedSize >= size + 1 and data[size] == '\0'.
382 // So if you want a 2-character string, pass malloc(3) as "data",
383 // pass 2 as "size", and pass 3 as "allocatedSize".
384 fbstring_core(Char * const data,
386 const size_t allocatedSize,
387 AcquireMallocatedString) {
389 FBSTRING_ASSERT(allocatedSize >= size + 1);
390 FBSTRING_ASSERT(data[size] == '\0');
391 // Use the medium string storage
394 // Don't forget about null terminator
395 ml_.setCapacity(allocatedSize - 1, Category::isMedium);
397 // No need for the memory
403 // swap below doesn't test whether &rhs == this (and instead
404 // potentially does extra work) on the premise that the rarity of
405 // that situation actually makes the check more expensive than is
407 void swap(fbstring_core & rhs) {
413 // In C++11 data() and c_str() are 100% equivalent.
414 const Char * data() const {
418 Char* mutableData() {
419 switch (category()) {
420 case Category::isSmall:
422 case Category::isMedium:
424 case Category::isLarge:
425 return mutableDataLarge();
427 fbstring_detail::assume_unreachable();
430 const Char* c_str() const {
431 const Char* ptr = ml_.data_;
432 // With this syntax, GCC and Clang generate a CMOV instead of a branch.
433 ptr = (category() == Category::isSmall) ? small_ : ptr;
437 void shrink(const size_t delta) {
438 if (category() == Category::isSmall) {
440 } else if (category() == Category::isMedium ||
441 RefCounted::refs(ml_.data_) == 1) {
448 FOLLY_MALLOC_NOINLINE
449 void reserve(size_t minCapacity, bool disableSSO = FBSTRING_DISABLE_SSO) {
450 switch (category()) {
451 case Category::isSmall:
452 reserveSmall(minCapacity, disableSSO);
454 case Category::isMedium:
455 reserveMedium(minCapacity);
457 case Category::isLarge:
458 reserveLarge(minCapacity);
461 fbstring_detail::assume_unreachable();
463 FBSTRING_ASSERT(capacity() >= minCapacity);
468 bool expGrowth = false,
469 bool disableSSO = FBSTRING_DISABLE_SSO);
471 void push_back(Char c) {
472 *expandNoinit(1, /* expGrowth = */ true) = c;
475 size_t size() const {
476 size_t ret = ml_.size_;
477 /* static */ if (kIsLittleEndian) {
478 // We can save a couple instructions, because the category is
479 // small iff the last char, as unsigned, is <= maxSmallSize.
480 typedef typename std::make_unsigned<Char>::type UChar;
481 auto maybeSmallSize = size_t(maxSmallSize) -
482 size_t(static_cast<UChar>(small_[maxSmallSize]));
483 // With this syntax, GCC and Clang generate a CMOV instead of a branch.
484 ret = (static_cast<ssize_t>(maybeSmallSize) >= 0) ? maybeSmallSize : ret;
486 ret = (category() == Category::isSmall) ? smallSize() : ret;
491 size_t capacity() const {
492 switch (category()) {
493 case Category::isSmall:
495 case Category::isLarge:
496 // For large-sized strings, a multi-referenced chunk has no
497 // available capacity. This is because any attempt to append
498 // data would trigger a new allocation.
499 if (RefCounted::refs(ml_.data_) > 1) {
504 return ml_.capacity();
507 bool isShared() const {
508 return category() == Category::isLarge && RefCounted::refs(ml_.data_) > 1;
513 fbstring_core & operator=(const fbstring_core & rhs);
519 FOLLY_MALLOC_NOINLINE void destroyMediumLarge() noexcept {
520 auto const c = category();
521 FBSTRING_ASSERT(c != Category::isSmall);
522 if (c == Category::isMedium) {
525 RefCounted::decrementRefs(ml_.data_);
530 std::atomic<size_t> refCount_;
533 constexpr static size_t getDataOffset() {
534 return offsetof(RefCounted, data_);
537 static RefCounted * fromData(Char * p) {
538 return static_cast<RefCounted*>(static_cast<void*>(
539 static_cast<unsigned char*>(static_cast<void*>(p)) -
543 static size_t refs(Char * p) {
544 return fromData(p)->refCount_.load(std::memory_order_acquire);
547 static void incrementRefs(Char * p) {
548 fromData(p)->refCount_.fetch_add(1, std::memory_order_acq_rel);
551 static void decrementRefs(Char * p) {
552 auto const dis = fromData(p);
553 size_t oldcnt = dis->refCount_.fetch_sub(1, std::memory_order_acq_rel);
554 FBSTRING_ASSERT(oldcnt > 0);
560 static RefCounted * create(size_t * size) {
561 const size_t allocSize =
562 goodMallocSize(getDataOffset() + (*size + 1) * sizeof(Char));
563 auto result = static_cast<RefCounted*>(checkedMalloc(allocSize));
564 result->refCount_.store(1, std::memory_order_release);
565 *size = (allocSize - getDataOffset()) / sizeof(Char) - 1;
569 static RefCounted * create(const Char * data, size_t * size) {
570 const size_t effectiveSize = *size;
571 auto result = create(size);
572 if (FBSTRING_LIKELY(effectiveSize > 0)) {
573 fbstring_detail::podCopy(data, data + effectiveSize, result->data_);
578 static RefCounted * reallocate(Char *const data,
579 const size_t currentSize,
580 const size_t currentCapacity,
581 size_t * newCapacity) {
582 FBSTRING_ASSERT(*newCapacity > 0 && *newCapacity > currentSize);
583 const size_t allocNewCapacity =
584 goodMallocSize(getDataOffset() + (*newCapacity + 1) * sizeof(Char));
585 auto const dis = fromData(data);
586 FBSTRING_ASSERT(dis->refCount_.load(std::memory_order_acquire) == 1);
587 auto result = static_cast<RefCounted*>(smartRealloc(
589 getDataOffset() + (currentSize + 1) * sizeof(Char),
590 getDataOffset() + (currentCapacity + 1) * sizeof(Char),
592 FBSTRING_ASSERT(result->refCount_.load(std::memory_order_acquire) == 1);
593 *newCapacity = (allocNewCapacity - getDataOffset()) / sizeof(Char) - 1;
598 typedef uint8_t category_type;
600 enum class Category : category_type {
602 isMedium = kIsLittleEndian ? 0x80 : 0x2,
603 isLarge = kIsLittleEndian ? 0x40 : 0x1,
606 Category category() const {
607 // works for both big-endian and little-endian
608 return static_cast<Category>(bytes_[lastChar] & categoryExtractMask);
616 size_t capacity() const {
617 return kIsLittleEndian
618 ? capacity_ & capacityExtractMask
622 void setCapacity(size_t cap, Category cat) {
623 capacity_ = kIsLittleEndian
624 ? cap | (static_cast<size_t>(cat) << kCategoryShift)
625 : (cap << 2) | static_cast<size_t>(cat);
630 uint8_t bytes_[sizeof(MediumLarge)]; // For accessing the last byte.
631 Char small_[sizeof(MediumLarge) / sizeof(Char)];
635 constexpr static size_t lastChar = sizeof(MediumLarge) - 1;
636 constexpr static size_t maxSmallSize = lastChar / sizeof(Char);
637 constexpr static size_t maxMediumSize = 254 / sizeof(Char);
638 constexpr static uint8_t categoryExtractMask = kIsLittleEndian ? 0xC0 : 0x3;
639 constexpr static size_t kCategoryShift = (sizeof(size_t) - 1) * 8;
640 constexpr static size_t capacityExtractMask = kIsLittleEndian
641 ? ~(size_t(categoryExtractMask) << kCategoryShift)
644 static_assert(!(sizeof(MediumLarge) % sizeof(Char)),
645 "Corrupt memory layout for fbstring.");
647 size_t smallSize() const {
648 FBSTRING_ASSERT(category() == Category::isSmall);
649 constexpr auto shift = kIsLittleEndian ? 0 : 2;
650 auto smallShifted = static_cast<size_t>(small_[maxSmallSize]) >> shift;
651 FBSTRING_ASSERT(static_cast<size_t>(maxSmallSize) >= smallShifted);
652 return static_cast<size_t>(maxSmallSize) - smallShifted;
655 void setSmallSize(size_t s) {
656 // Warning: this should work with uninitialized strings too,
657 // so don't assume anything about the previous value of
658 // small_[maxSmallSize].
659 FBSTRING_ASSERT(s <= maxSmallSize);
660 constexpr auto shift = kIsLittleEndian ? 0 : 2;
661 small_[maxSmallSize] = char((maxSmallSize - s) << shift);
663 FBSTRING_ASSERT(category() == Category::isSmall && size() == s);
666 void copySmall(const fbstring_core&);
667 void copyMedium(const fbstring_core&);
668 void copyLarge(const fbstring_core&);
670 void initSmall(const Char* data, size_t size);
671 void initMedium(const Char* data, size_t size);
672 void initLarge(const Char* data, size_t size);
674 void reserveSmall(size_t minCapacity, bool disableSSO);
675 void reserveMedium(size_t minCapacity);
676 void reserveLarge(size_t minCapacity);
678 void shrinkSmall(size_t delta);
679 void shrinkMedium(size_t delta);
680 void shrinkLarge(size_t delta);
682 void unshare(size_t minCapacity = 0);
683 Char* mutableDataLarge();
686 template <class Char>
687 inline void fbstring_core<Char>::copySmall(const fbstring_core& rhs) {
688 static_assert(offsetof(MediumLarge, data_) == 0, "fbstring layout failure");
690 offsetof(MediumLarge, size_) == sizeof(ml_.data_),
691 "fbstring layout failure");
693 offsetof(MediumLarge, capacity_) == 2 * sizeof(ml_.data_),
694 "fbstring layout failure");
695 // Just write the whole thing, don't look at details. In
696 // particular we need to copy capacity anyway because we want
697 // to set the size (don't forget that the last character,
698 // which stores a short string's length, is shared with the
699 // ml_.capacity field).
702 category() == Category::isSmall && this->size() == rhs.size());
705 template <class Char>
706 FOLLY_MALLOC_NOINLINE inline void fbstring_core<Char>::copyMedium(
707 const fbstring_core& rhs) {
708 // Medium strings are copied eagerly. Don't forget to allocate
709 // one extra Char for the null terminator.
710 auto const allocSize = goodMallocSize((1 + rhs.ml_.size_) * sizeof(Char));
711 ml_.data_ = static_cast<Char*>(checkedMalloc(allocSize));
712 // Also copies terminator.
713 fbstring_detail::podCopy(
714 rhs.ml_.data_, rhs.ml_.data_ + rhs.ml_.size_ + 1, ml_.data_);
715 ml_.size_ = rhs.ml_.size_;
716 ml_.setCapacity(allocSize / sizeof(Char) - 1, Category::isMedium);
717 FBSTRING_ASSERT(category() == Category::isMedium);
720 template <class Char>
721 FOLLY_MALLOC_NOINLINE inline void fbstring_core<Char>::copyLarge(
722 const fbstring_core& rhs) {
723 // Large strings are just refcounted
725 RefCounted::incrementRefs(ml_.data_);
726 FBSTRING_ASSERT(category() == Category::isLarge && size() == rhs.size());
729 // Small strings are bitblitted
730 template <class Char>
731 inline void fbstring_core<Char>::initSmall(
732 const Char* const data, const size_t size) {
733 // Layout is: Char* data_, size_t size_, size_t capacity_
735 sizeof(*this) == sizeof(Char*) + 2 * sizeof(size_t),
736 "fbstring has unexpected size");
738 sizeof(Char*) == sizeof(size_t), "fbstring size assumption violation");
739 // sizeof(size_t) must be a power of 2
741 (sizeof(size_t) & (sizeof(size_t) - 1)) == 0,
742 "fbstring size assumption violation");
744 // If data is aligned, use fast word-wise copying. Otherwise,
745 // use conservative memcpy.
746 // The word-wise path reads bytes which are outside the range of
747 // the string, and makes ASan unhappy, so we disable it when
748 // compiling with ASan.
749 #ifndef FBSTRING_SANITIZE_ADDRESS
750 if ((reinterpret_cast<size_t>(data) & (sizeof(size_t) - 1)) == 0) {
751 const size_t byteSize = size * sizeof(Char);
752 constexpr size_t wordWidth = sizeof(size_t);
753 switch ((byteSize + wordWidth - 1) / wordWidth) { // Number of words.
755 ml_.capacity_ = reinterpret_cast<const size_t*>(data)[2];
757 ml_.size_ = reinterpret_cast<const size_t*>(data)[1];
759 ml_.data_ = *reinterpret_cast<Char**>(const_cast<Char*>(data));
767 fbstring_detail::podCopy(data, data + size, small_);
773 template <class Char>
774 FOLLY_MALLOC_NOINLINE inline void fbstring_core<Char>::initMedium(
775 const Char* const data, const size_t size) {
776 // Medium strings are allocated normally. Don't forget to
777 // allocate one extra Char for the terminating null.
778 auto const allocSize = goodMallocSize((1 + size) * sizeof(Char));
779 ml_.data_ = static_cast<Char*>(checkedMalloc(allocSize));
780 if (FBSTRING_LIKELY(size > 0)) {
781 fbstring_detail::podCopy(data, data + size, ml_.data_);
784 ml_.setCapacity(allocSize / sizeof(Char) - 1, Category::isMedium);
785 ml_.data_[size] = '\0';
788 template <class Char>
789 FOLLY_MALLOC_NOINLINE inline void fbstring_core<Char>::initLarge(
790 const Char* const data, const size_t size) {
791 // Large strings are allocated differently
792 size_t effectiveCapacity = size;
793 auto const newRC = RefCounted::create(data, &effectiveCapacity);
794 ml_.data_ = newRC->data_;
796 ml_.setCapacity(effectiveCapacity, Category::isLarge);
797 ml_.data_[size] = '\0';
800 template <class Char>
801 FOLLY_MALLOC_NOINLINE inline void fbstring_core<Char>::unshare(
802 size_t minCapacity) {
803 FBSTRING_ASSERT(category() == Category::isLarge);
804 size_t effectiveCapacity = std::max(minCapacity, ml_.capacity());
805 auto const newRC = RefCounted::create(&effectiveCapacity);
806 // If this fails, someone placed the wrong capacity in an
808 FBSTRING_ASSERT(effectiveCapacity >= ml_.capacity());
809 // Also copies terminator.
810 fbstring_detail::podCopy(ml_.data_, ml_.data_ + ml_.size_ + 1, newRC->data_);
811 RefCounted::decrementRefs(ml_.data_);
812 ml_.data_ = newRC->data_;
813 ml_.setCapacity(effectiveCapacity, Category::isLarge);
814 // size_ remains unchanged.
817 template <class Char>
818 inline Char* fbstring_core<Char>::mutableDataLarge() {
819 FBSTRING_ASSERT(category() == Category::isLarge);
820 if (RefCounted::refs(ml_.data_) > 1) { // Ensure unique.
826 template <class Char>
827 FOLLY_MALLOC_NOINLINE inline void fbstring_core<Char>::reserveLarge(
828 size_t minCapacity) {
829 FBSTRING_ASSERT(category() == Category::isLarge);
830 if (RefCounted::refs(ml_.data_) > 1) { // Ensure unique
831 // We must make it unique regardless; in-place reallocation is
832 // useless if the string is shared. In order to not surprise
833 // people, reserve the new block at current capacity or
834 // more. That way, a string's capacity never shrinks after a
836 unshare(minCapacity);
838 // String is not shared, so let's try to realloc (if needed)
839 if (minCapacity > ml_.capacity()) {
840 // Asking for more memory
841 auto const newRC = RefCounted::reallocate(
842 ml_.data_, ml_.size_, ml_.capacity(), &minCapacity);
843 ml_.data_ = newRC->data_;
844 ml_.setCapacity(minCapacity, Category::isLarge);
846 FBSTRING_ASSERT(capacity() >= minCapacity);
850 template <class Char>
851 FOLLY_MALLOC_NOINLINE inline void fbstring_core<Char>::reserveMedium(
852 const size_t minCapacity) {
853 FBSTRING_ASSERT(category() == Category::isMedium);
854 // String is not shared
855 if (minCapacity <= ml_.capacity()) {
856 return; // nothing to do, there's enough room
858 if (minCapacity <= maxMediumSize) {
859 // Keep the string at medium size. Don't forget to allocate
860 // one extra Char for the terminating null.
861 size_t capacityBytes = goodMallocSize((1 + minCapacity) * sizeof(Char));
862 // Also copies terminator.
863 ml_.data_ = static_cast<Char*>(smartRealloc(
865 (ml_.size_ + 1) * sizeof(Char),
866 (ml_.capacity() + 1) * sizeof(Char),
868 ml_.setCapacity(capacityBytes / sizeof(Char) - 1, Category::isMedium);
870 // Conversion from medium to large string
871 fbstring_core nascent;
872 // Will recurse to another branch of this function
873 nascent.reserve(minCapacity);
874 nascent.ml_.size_ = ml_.size_;
875 // Also copies terminator.
876 fbstring_detail::podCopy(
877 ml_.data_, ml_.data_ + ml_.size_ + 1, nascent.ml_.data_);
879 FBSTRING_ASSERT(capacity() >= minCapacity);
883 template <class Char>
884 FOLLY_MALLOC_NOINLINE inline void fbstring_core<Char>::reserveSmall(
885 size_t minCapacity, const bool disableSSO) {
886 FBSTRING_ASSERT(category() == Category::isSmall);
887 if (!disableSSO && minCapacity <= maxSmallSize) {
889 // Nothing to do, everything stays put
890 } else if (minCapacity <= maxMediumSize) {
892 // Don't forget to allocate one extra Char for the terminating null
893 auto const allocSizeBytes =
894 goodMallocSize((1 + minCapacity) * sizeof(Char));
895 auto const pData = static_cast<Char*>(checkedMalloc(allocSizeBytes));
896 auto const size = smallSize();
897 // Also copies terminator.
898 fbstring_detail::podCopy(small_, small_ + size + 1, pData);
901 ml_.setCapacity(allocSizeBytes / sizeof(Char) - 1, Category::isMedium);
904 auto const newRC = RefCounted::create(&minCapacity);
905 auto const size = smallSize();
906 // Also copies terminator.
907 fbstring_detail::podCopy(small_, small_ + size + 1, newRC->data_);
908 ml_.data_ = newRC->data_;
910 ml_.setCapacity(minCapacity, Category::isLarge);
911 FBSTRING_ASSERT(capacity() >= minCapacity);
915 template <class Char>
916 inline Char* fbstring_core<Char>::expandNoinit(
918 bool expGrowth, /* = false */
919 bool disableSSO /* = FBSTRING_DISABLE_SSO */) {
920 // Strategy is simple: make room, then change size
921 FBSTRING_ASSERT(capacity() >= size());
923 if (category() == Category::isSmall) {
926 if (!disableSSO && FBSTRING_LIKELY(newSz <= maxSmallSize)) {
931 expGrowth ? std::max(newSz, 2 * maxSmallSize) : newSz, disableSSO);
935 if (FBSTRING_UNLIKELY(newSz > capacity())) {
936 // ensures not shared
937 reserve(expGrowth ? std::max(newSz, 1 + capacity() * 3 / 2) : newSz);
940 FBSTRING_ASSERT(capacity() >= newSz);
941 // Category can't be small - we took care of that above
943 category() == Category::isMedium || category() == Category::isLarge);
945 ml_.data_[newSz] = '\0';
946 FBSTRING_ASSERT(size() == newSz);
947 return ml_.data_ + sz;
950 template <class Char>
951 inline void fbstring_core<Char>::shrinkSmall(const size_t delta) {
952 // Check for underflow
953 FBSTRING_ASSERT(delta <= smallSize());
954 setSmallSize(smallSize() - delta);
957 template <class Char>
958 inline void fbstring_core<Char>::shrinkMedium(const size_t delta) {
959 // Medium strings and unique large strings need no special
961 FBSTRING_ASSERT(ml_.size_ >= delta);
963 ml_.data_[ml_.size_] = '\0';
966 template <class Char>
967 inline void fbstring_core<Char>::shrinkLarge(const size_t delta) {
968 FBSTRING_ASSERT(ml_.size_ >= delta);
969 // Shared large string, must make unique. This is because of the
970 // durn terminator must be written, which may trample the shared
973 fbstring_core(ml_.data_, ml_.size_ - delta).swap(*this);
975 // No need to write the terminator.
978 #ifndef _LIBSTDCXX_FBSTRING
980 * Dummy fbstring core that uses an actual std::string. This doesn't
981 * make any sense - it's just for testing purposes.
983 template <class Char>
984 class dummy_fbstring_core {
986 dummy_fbstring_core() {
988 dummy_fbstring_core(const dummy_fbstring_core& another)
989 : backend_(another.backend_) {
991 dummy_fbstring_core(const Char * s, size_t n)
994 void swap(dummy_fbstring_core & rhs) {
995 backend_.swap(rhs.backend_);
997 const Char * data() const {
998 return backend_.data();
1000 Char* mutableData() {
1001 return const_cast<Char*>(backend_.data());
1003 void shrink(size_t delta) {
1004 FBSTRING_ASSERT(delta <= size());
1005 backend_.resize(size() - delta);
1007 Char* expandNoinit(size_t delta) {
1008 auto const sz = size();
1009 backend_.resize(size() + delta);
1010 return backend_.data() + sz;
1012 void push_back(Char c) {
1013 backend_.push_back(c);
1015 size_t size() const {
1016 return backend_.size();
1018 size_t capacity() const {
1019 return backend_.capacity();
1021 bool isShared() const {
1024 void reserve(size_t minCapacity) {
1025 backend_.reserve(minCapacity);
1029 std::basic_string<Char> backend_;
1031 #endif // !_LIBSTDCXX_FBSTRING
1034 * This is the basic_string replacement. For conformity,
1035 * basic_fbstring takes the same template parameters, plus the last
1036 * one which is the core.
1038 #ifdef _LIBSTDCXX_FBSTRING
1039 template <typename E, class T, class A, class Storage>
1041 template <typename E,
1042 class T = std::char_traits<E>,
1043 class A = std::allocator<E>,
1044 class Storage = fbstring_core<E> >
1046 class basic_fbstring {
1047 static void enforce(
1049 void (*throw_exc)(const char*),
1056 bool isSane() const {
1059 empty() == (size() == 0) &&
1060 empty() == (begin() == end()) &&
1061 size() <= max_size() &&
1062 capacity() <= max_size() &&
1063 size() <= capacity() &&
1064 begin()[size()] == '\0';
1068 Invariant& operator=(const Invariant&) = delete;
1069 explicit Invariant(const basic_fbstring& s) noexcept : s_(s) {
1070 FBSTRING_ASSERT(s_.isSane());
1072 ~Invariant() noexcept {
1073 FBSTRING_ASSERT(s_.isSane());
1077 const basic_fbstring& s_;
1082 typedef T traits_type;
1083 typedef typename traits_type::char_type value_type;
1084 typedef A allocator_type;
1085 typedef typename A::size_type size_type;
1086 typedef typename A::difference_type difference_type;
1088 typedef typename A::reference reference;
1089 typedef typename A::const_reference const_reference;
1090 typedef typename A::pointer pointer;
1091 typedef typename A::const_pointer const_pointer;
1093 typedef E* iterator;
1094 typedef const E* const_iterator;
1095 typedef std::reverse_iterator<iterator
1096 #ifdef NO_ITERATOR_TRAITS
1100 typedef std::reverse_iterator<const_iterator
1101 #ifdef NO_ITERATOR_TRAITS
1104 > const_reverse_iterator;
1106 static constexpr size_type npos = size_type(-1);
1107 typedef std::true_type IsRelocatable;
1110 static void procrustes(size_type& n, size_type nmax) {
1116 static size_type traitsLength(const value_type* s);
1119 // C++11 21.4.2 construct/copy/destroy
1121 // Note: while the following two constructors can be (and previously were)
1122 // collapsed into one constructor written this way:
1124 // explicit basic_fbstring(const A& a = A()) noexcept { }
1126 // This can cause Clang (at least version 3.7) to fail with the error:
1127 // "chosen constructor is explicit in copy-initialization ...
1128 // in implicit initialization of field '(x)' with omitted initializer"
1130 // if used in a struct which is default-initialized. Hence the split into
1131 // these two separate constructors.
1133 basic_fbstring() noexcept : basic_fbstring(A()) {
1136 explicit basic_fbstring(const A&) noexcept {
1139 basic_fbstring(const basic_fbstring& str)
1140 : store_(str.store_) {
1144 basic_fbstring(basic_fbstring&& goner) noexcept
1145 : store_(std::move(goner.store_)) {
1148 #ifndef _LIBSTDCXX_FBSTRING
1149 // This is defined for compatibility with std::string
1150 /* implicit */ basic_fbstring(const std::string& str)
1151 : store_(str.data(), str.size()) {
1155 basic_fbstring(const basic_fbstring& str,
1158 const A& /* a */ = A()) {
1159 assign(str, pos, n);
1162 FOLLY_MALLOC_NOINLINE
1163 /* implicit */ basic_fbstring(const value_type* s, const A& /*a*/ = A())
1164 : store_(s, traitsLength(s)) {}
1166 FOLLY_MALLOC_NOINLINE
1167 basic_fbstring(const value_type* s, size_type n, const A& /*a*/ = A())
1171 FOLLY_MALLOC_NOINLINE
1172 basic_fbstring(size_type n, value_type c, const A& /*a*/ = A()) {
1173 auto const pData = store_.expandNoinit(n);
1174 fbstring_detail::podFill(pData, pData + n, c);
1177 template <class InIt>
1178 FOLLY_MALLOC_NOINLINE basic_fbstring(
1181 typename std::enable_if<
1182 !std::is_same<InIt, value_type*>::value,
1183 const A>::type& /*a*/ = A()) {
1187 // Specialization for const char*, const char*
1188 FOLLY_MALLOC_NOINLINE
1189 basic_fbstring(const value_type* b, const value_type* e, const A& /*a*/ = A())
1190 : store_(b, size_type(e - b)) {
1193 // Nonstandard constructor
1194 basic_fbstring(value_type *s, size_type n, size_type c,
1195 AcquireMallocatedString a)
1196 : store_(s, n, c, a) {
1199 // Construction from initialization list
1200 FOLLY_MALLOC_NOINLINE
1201 basic_fbstring(std::initializer_list<value_type> il) {
1202 assign(il.begin(), il.end());
1205 ~basic_fbstring() noexcept {}
1207 basic_fbstring& operator=(const basic_fbstring& lhs);
1210 basic_fbstring& operator=(basic_fbstring&& goner) noexcept;
1212 #ifndef _LIBSTDCXX_FBSTRING
1213 // Compatibility with std::string
1214 basic_fbstring & operator=(const std::string & rhs) {
1215 return assign(rhs.data(), rhs.size());
1218 // Compatibility with std::string
1219 std::string toStdString() const {
1220 return std::string(data(), size());
1223 // A lot of code in fbcode still uses this method, so keep it here for now.
1224 const basic_fbstring& toStdString() const {
1229 basic_fbstring& operator=(const value_type* s) {
1233 // This actually goes directly against the C++ spec, but the
1234 // value_type overload is dangerous, so we're explicitly deleting
1235 // any overloads of operator= that could implicitly convert to
1237 template <typename TP>
1238 typename std::enable_if<
1239 std::is_same<typename std::decay<TP>::type, value_type>::value,
1240 basic_fbstring&>::type
1243 basic_fbstring& operator=(std::initializer_list<value_type> il) {
1244 return assign(il.begin(), il.end());
1247 // C++11 21.4.3 iterators:
1249 return store_.mutableData();
1252 const_iterator begin() const {
1253 return store_.data();
1256 const_iterator cbegin() const {
1261 return store_.mutableData() + store_.size();
1264 const_iterator end() const {
1265 return store_.data() + store_.size();
1268 const_iterator cend() const { return end(); }
1270 reverse_iterator rbegin() {
1271 return reverse_iterator(end());
1274 const_reverse_iterator rbegin() const {
1275 return const_reverse_iterator(end());
1278 const_reverse_iterator crbegin() const { return rbegin(); }
1280 reverse_iterator rend() {
1281 return reverse_iterator(begin());
1284 const_reverse_iterator rend() const {
1285 return const_reverse_iterator(begin());
1288 const_reverse_iterator crend() const { return rend(); }
1291 // C++11 21.4.5, element access:
1292 const value_type& front() const { return *begin(); }
1293 const value_type& back() const {
1294 FBSTRING_ASSERT(!empty());
1295 // Should be begin()[size() - 1], but that branches twice
1296 return *(end() - 1);
1298 value_type& front() { return *begin(); }
1299 value_type& back() {
1300 FBSTRING_ASSERT(!empty());
1301 // Should be begin()[size() - 1], but that branches twice
1302 return *(end() - 1);
1305 FBSTRING_ASSERT(!empty());
1309 // C++11 21.4.4 capacity:
1310 size_type size() const { return store_.size(); }
1312 size_type length() const { return size(); }
1314 size_type max_size() const {
1315 return std::numeric_limits<size_type>::max();
1318 void resize(size_type n, value_type c = value_type());
1320 size_type capacity() const { return store_.capacity(); }
1322 void reserve(size_type res_arg = 0) {
1323 enforce(res_arg <= max_size(), std::__throw_length_error, "");
1324 store_.reserve(res_arg);
1327 void shrink_to_fit() {
1328 // Shrink only if slack memory is sufficiently large
1329 if (capacity() < size() * 3 / 2) {
1332 basic_fbstring(cbegin(), cend()).swap(*this);
1335 void clear() { resize(0); }
1337 bool empty() const { return size() == 0; }
1339 // C++11 21.4.5 element access:
1340 const_reference operator[](size_type pos) const {
1341 return *(begin() + pos);
1344 reference operator[](size_type pos) {
1345 return *(begin() + pos);
1348 const_reference at(size_type n) const {
1349 enforce(n <= size(), std::__throw_out_of_range, "");
1353 reference at(size_type n) {
1354 enforce(n < size(), std::__throw_out_of_range, "");
1358 // C++11 21.4.6 modifiers:
1359 basic_fbstring& operator+=(const basic_fbstring& str) {
1363 basic_fbstring& operator+=(const value_type* s) {
1367 basic_fbstring& operator+=(const value_type c) {
1372 basic_fbstring& operator+=(std::initializer_list<value_type> il) {
1377 basic_fbstring& append(const basic_fbstring& str);
1380 append(const basic_fbstring& str, const size_type pos, size_type n);
1382 basic_fbstring& append(const value_type* s, size_type n);
1384 basic_fbstring& append(const value_type* s) {
1385 return append(s, traitsLength(s));
1388 basic_fbstring& append(size_type n, value_type c);
1390 template<class InputIterator>
1391 basic_fbstring& append(InputIterator first, InputIterator last) {
1392 insert(end(), first, last);
1396 basic_fbstring& append(std::initializer_list<value_type> il) {
1397 return append(il.begin(), il.end());
1400 void push_back(const value_type c) { // primitive
1401 store_.push_back(c);
1404 basic_fbstring& assign(const basic_fbstring& str) {
1405 if (&str == this) return *this;
1406 return assign(str.data(), str.size());
1409 basic_fbstring& assign(basic_fbstring&& str) {
1410 return *this = std::move(str);
1414 assign(const basic_fbstring& str, const size_type pos, size_type n);
1416 basic_fbstring& assign(const value_type* s, const size_type n);
1418 basic_fbstring& assign(const value_type* s) {
1419 return assign(s, traitsLength(s));
1422 basic_fbstring& assign(std::initializer_list<value_type> il) {
1423 return assign(il.begin(), il.end());
1426 template <class ItOrLength, class ItOrChar>
1427 basic_fbstring& assign(ItOrLength first_or_n, ItOrChar last_or_c) {
1428 return replace(begin(), end(), first_or_n, last_or_c);
1431 basic_fbstring& insert(size_type pos1, const basic_fbstring& str) {
1432 return insert(pos1, str.data(), str.size());
1435 basic_fbstring& insert(size_type pos1, const basic_fbstring& str,
1436 size_type pos2, size_type n) {
1437 enforce(pos2 <= str.length(), std::__throw_out_of_range, "");
1438 procrustes(n, str.length() - pos2);
1439 return insert(pos1, str.data() + pos2, n);
1442 basic_fbstring& insert(size_type pos, const value_type* s, size_type n) {
1443 enforce(pos <= length(), std::__throw_out_of_range, "");
1444 insert(begin() + pos, s, s + n);
1448 basic_fbstring& insert(size_type pos, const value_type* s) {
1449 return insert(pos, s, traitsLength(s));
1452 basic_fbstring& insert(size_type pos, size_type n, value_type c) {
1453 enforce(pos <= length(), std::__throw_out_of_range, "");
1454 insert(begin() + pos, n, c);
1458 iterator insert(const_iterator p, const value_type c) {
1459 const size_type pos = p - cbegin();
1461 return begin() + pos;
1464 #ifndef _LIBSTDCXX_FBSTRING
1466 typedef std::basic_istream<value_type, traits_type> istream_type;
1467 istream_type& getlineImpl(istream_type& is, value_type delim);
1470 friend inline istream_type& getline(istream_type& is,
1471 basic_fbstring& str,
1473 return str.getlineImpl(is, delim);
1476 friend inline istream_type& getline(istream_type& is, basic_fbstring& str) {
1477 return getline(is, str, '\n');
1483 insertImplDiscr(const_iterator i, size_type n, value_type c, std::true_type);
1485 template <class InputIter>
1487 insertImplDiscr(const_iterator i, InputIter b, InputIter e, std::false_type);
1489 template <class FwdIterator>
1490 iterator insertImpl(
1494 std::forward_iterator_tag);
1496 template <class InputIterator>
1497 iterator insertImpl(
1501 std::input_iterator_tag);
1504 template <class ItOrLength, class ItOrChar>
1505 iterator insert(const_iterator p, ItOrLength first_or_n, ItOrChar last_or_c) {
1506 using Sel = std::integral_constant<
1508 std::numeric_limits<ItOrLength>::is_specialized>;
1509 return insertImplDiscr(p, first_or_n, last_or_c, Sel());
1512 iterator insert(const_iterator p, std::initializer_list<value_type> il) {
1513 return insert(p, il.begin(), il.end());
1516 basic_fbstring& erase(size_type pos = 0, size_type n = npos) {
1517 Invariant checker(*this);
1519 enforce(pos <= length(), std::__throw_out_of_range, "");
1520 procrustes(n, length() - pos);
1521 std::copy(begin() + pos + n, end(), begin() + pos);
1522 resize(length() - n);
1526 iterator erase(iterator position) {
1527 const size_type pos(position - begin());
1528 enforce(pos <= size(), std::__throw_out_of_range, "");
1530 return begin() + pos;
1533 iterator erase(iterator first, iterator last) {
1534 const size_type pos(first - begin());
1535 erase(pos, last - first);
1536 return begin() + pos;
1539 // Replaces at most n1 chars of *this, starting with pos1 with the
1541 basic_fbstring& replace(size_type pos1, size_type n1,
1542 const basic_fbstring& str) {
1543 return replace(pos1, n1, str.data(), str.size());
1546 // Replaces at most n1 chars of *this, starting with pos1,
1547 // with at most n2 chars of str starting with pos2
1548 basic_fbstring& replace(size_type pos1, size_type n1,
1549 const basic_fbstring& str,
1550 size_type pos2, size_type n2) {
1551 enforce(pos2 <= str.length(), std::__throw_out_of_range, "");
1552 return replace(pos1, n1, str.data() + pos2,
1553 std::min(n2, str.size() - pos2));
1556 // Replaces at most n1 chars of *this, starting with pos, with chars from s
1557 basic_fbstring& replace(size_type pos, size_type n1, const value_type* s) {
1558 return replace(pos, n1, s, traitsLength(s));
1561 // Replaces at most n1 chars of *this, starting with pos, with n2
1564 // consolidated with
1566 // Replaces at most n1 chars of *this, starting with pos, with at
1567 // most n2 chars of str. str must have at least n2 chars.
1568 template <class StrOrLength, class NumOrChar>
1569 basic_fbstring& replace(size_type pos, size_type n1,
1570 StrOrLength s_or_n2, NumOrChar n_or_c) {
1571 Invariant checker(*this);
1573 enforce(pos <= size(), std::__throw_out_of_range, "");
1574 procrustes(n1, length() - pos);
1575 const iterator b = begin() + pos;
1576 return replace(b, b + n1, s_or_n2, n_or_c);
1579 basic_fbstring& replace(iterator i1, iterator i2, const basic_fbstring& str) {
1580 return replace(i1, i2, str.data(), str.length());
1583 basic_fbstring& replace(iterator i1, iterator i2, const value_type* s) {
1584 return replace(i1, i2, s, traitsLength(s));
1588 basic_fbstring& replaceImplDiscr(
1591 const value_type* s,
1593 std::integral_constant<int, 2>);
1595 basic_fbstring& replaceImplDiscr(
1600 std::integral_constant<int, 1>);
1602 template <class InputIter>
1603 basic_fbstring& replaceImplDiscr(
1608 std::integral_constant<int, 0>);
1611 template <class FwdIterator>
1612 bool replaceAliased(iterator /* i1 */,
1614 FwdIterator /* s1 */,
1615 FwdIterator /* s2 */,
1620 template <class FwdIterator>
1621 bool replaceAliased(
1628 template <class FwdIterator>
1634 std::forward_iterator_tag);
1636 template <class InputIterator>
1642 std::input_iterator_tag);
1645 template <class T1, class T2>
1646 basic_fbstring& replace(iterator i1, iterator i2,
1647 T1 first_or_n_or_s, T2 last_or_c_or_n) {
1648 constexpr bool num1 = std::numeric_limits<T1>::is_specialized,
1649 num2 = std::numeric_limits<T2>::is_specialized;
1651 std::integral_constant<int, num1 ? (num2 ? 1 : -1) : (num2 ? 2 : 0)>;
1652 return replaceImplDiscr(i1, i2, first_or_n_or_s, last_or_c_or_n, Sel());
1655 size_type copy(value_type* s, size_type n, size_type pos = 0) const {
1656 enforce(pos <= size(), std::__throw_out_of_range, "");
1657 procrustes(n, size() - pos);
1660 fbstring_detail::podCopy(data() + pos, data() + pos + n, s);
1665 void swap(basic_fbstring& rhs) {
1666 store_.swap(rhs.store_);
1669 const value_type* c_str() const {
1670 return store_.c_str();
1673 const value_type* data() const { return c_str(); }
1675 allocator_type get_allocator() const {
1676 return allocator_type();
1679 size_type find(const basic_fbstring& str, size_type pos = 0) const {
1680 return find(str.data(), pos, str.length());
1683 size_type find(const value_type* needle, size_type pos, size_type nsize)
1686 size_type find(const value_type* s, size_type pos = 0) const {
1687 return find(s, pos, traitsLength(s));
1690 size_type find (value_type c, size_type pos = 0) const {
1691 return find(&c, pos, 1);
1694 size_type rfind(const basic_fbstring& str, size_type pos = npos) const {
1695 return rfind(str.data(), pos, str.length());
1698 size_type rfind(const value_type* s, size_type pos, size_type n) const;
1700 size_type rfind(const value_type* s, size_type pos = npos) const {
1701 return rfind(s, pos, traitsLength(s));
1704 size_type rfind(value_type c, size_type pos = npos) const {
1705 return rfind(&c, pos, 1);
1708 size_type find_first_of(const basic_fbstring& str, size_type pos = 0) const {
1709 return find_first_of(str.data(), pos, str.length());
1712 size_type find_first_of(const value_type* s, size_type pos, size_type n)
1715 size_type find_first_of(const value_type* s, size_type pos = 0) const {
1716 return find_first_of(s, pos, traitsLength(s));
1719 size_type find_first_of(value_type c, size_type pos = 0) const {
1720 return find_first_of(&c, pos, 1);
1723 size_type find_last_of(const basic_fbstring& str, size_type pos = npos)
1725 return find_last_of(str.data(), pos, str.length());
1728 size_type find_last_of(const value_type* s, size_type pos, size_type n) const;
1730 size_type find_last_of (const value_type* s,
1731 size_type pos = npos) const {
1732 return find_last_of(s, pos, traitsLength(s));
1735 size_type find_last_of (value_type c, size_type pos = npos) const {
1736 return find_last_of(&c, pos, 1);
1739 size_type find_first_not_of(const basic_fbstring& str,
1740 size_type pos = 0) const {
1741 return find_first_not_of(str.data(), pos, str.size());
1744 size_type find_first_not_of(const value_type* s, size_type pos, size_type n)
1747 size_type find_first_not_of(const value_type* s,
1748 size_type pos = 0) const {
1749 return find_first_not_of(s, pos, traitsLength(s));
1752 size_type find_first_not_of(value_type c, size_type pos = 0) const {
1753 return find_first_not_of(&c, pos, 1);
1756 size_type find_last_not_of(const basic_fbstring& str,
1757 size_type pos = npos) const {
1758 return find_last_not_of(str.data(), pos, str.length());
1761 size_type find_last_not_of(const value_type* s, size_type pos, size_type n)
1764 size_type find_last_not_of(const value_type* s,
1765 size_type pos = npos) const {
1766 return find_last_not_of(s, pos, traitsLength(s));
1769 size_type find_last_not_of (value_type c, size_type pos = npos) const {
1770 return find_last_not_of(&c, pos, 1);
1773 basic_fbstring substr(size_type pos = 0, size_type n = npos) const& {
1774 enforce(pos <= size(), std::__throw_out_of_range, "");
1775 return basic_fbstring(data() + pos, std::min(n, size() - pos));
1778 basic_fbstring substr(size_type pos = 0, size_type n = npos) && {
1779 enforce(pos <= size(), std::__throw_out_of_range, "");
1784 return std::move(*this);
1787 int compare(const basic_fbstring& str) const {
1788 // FIX due to Goncalo N M de Carvalho July 18, 2005
1789 return compare(0, size(), str);
1792 int compare(size_type pos1, size_type n1,
1793 const basic_fbstring& str) const {
1794 return compare(pos1, n1, str.data(), str.size());
1797 int compare(size_type pos1, size_type n1,
1798 const value_type* s) const {
1799 return compare(pos1, n1, s, traitsLength(s));
1802 int compare(size_type pos1, size_type n1,
1803 const value_type* s, size_type n2) const {
1804 enforce(pos1 <= size(), std::__throw_out_of_range, "");
1805 procrustes(n1, size() - pos1);
1806 // The line below fixed by Jean-Francois Bastien, 04-23-2007. Thanks!
1807 const int r = traits_type::compare(pos1 + data(), s, std::min(n1, n2));
1808 return r != 0 ? r : n1 > n2 ? 1 : n1 < n2 ? -1 : 0;
1811 int compare(size_type pos1, size_type n1,
1812 const basic_fbstring& str,
1813 size_type pos2, size_type n2) const {
1814 enforce(pos2 <= str.size(), std::__throw_out_of_range, "");
1815 return compare(pos1, n1, str.data() + pos2,
1816 std::min(n2, str.size() - pos2));
1819 // Code from Jean-Francois Bastien (03/26/2007)
1820 int compare(const value_type* s) const {
1821 // Could forward to compare(0, size(), s, traitsLength(s))
1822 // but that does two extra checks
1823 const size_type n1(size()), n2(traitsLength(s));
1824 const int r = traits_type::compare(data(), s, std::min(n1, n2));
1825 return r != 0 ? r : n1 > n2 ? 1 : n1 < n2 ? -1 : 0;
1833 template <typename E, class T, class A, class S>
1834 FOLLY_MALLOC_NOINLINE inline typename basic_fbstring<E, T, A, S>::size_type
1835 basic_fbstring<E, T, A, S>::traitsLength(const value_type* s) {
1836 return s ? traits_type::length(s)
1837 : (std::__throw_logic_error(
1838 "basic_fbstring: null pointer initializer not valid"),
1842 template <typename E, class T, class A, class S>
1843 inline basic_fbstring<E, T, A, S>& basic_fbstring<E, T, A, S>::operator=(
1844 const basic_fbstring& lhs) {
1845 Invariant checker(*this);
1847 if (FBSTRING_UNLIKELY(&lhs == this)) {
1851 return assign(lhs.data(), lhs.size());
1855 template <typename E, class T, class A, class S>
1856 inline basic_fbstring<E, T, A, S>& basic_fbstring<E, T, A, S>::operator=(
1857 basic_fbstring&& goner) noexcept {
1858 if (FBSTRING_UNLIKELY(&goner == this)) {
1859 // Compatibility with std::basic_string<>,
1860 // C++11 21.4.2 [string.cons] / 23 requires self-move-assignment support.
1863 // No need of this anymore
1864 this->~basic_fbstring();
1865 // Move the goner into this
1866 new (&store_) S(std::move(goner.store_));
1870 template <typename E, class T, class A, class S>
1871 template <typename TP>
1872 inline typename std::enable_if<
1874 typename std::decay<TP>::type,
1875 typename basic_fbstring<E, T, A, S>::value_type>::value,
1876 basic_fbstring<E, T, A, S>&>::type
1877 basic_fbstring<E, T, A, S>::operator=(TP c) {
1878 Invariant checker(*this);
1881 store_.expandNoinit(1);
1882 } else if (store_.isShared()) {
1883 basic_fbstring(1, c).swap(*this);
1886 store_.shrink(size() - 1);
1892 template <typename E, class T, class A, class S>
1893 inline void basic_fbstring<E, T, A, S>::resize(
1894 const size_type n, const value_type c /*= value_type()*/) {
1895 Invariant checker(*this);
1897 auto size = this->size();
1899 store_.shrink(size - n);
1901 auto const delta = n - size;
1902 auto pData = store_.expandNoinit(delta);
1903 fbstring_detail::podFill(pData, pData + delta, c);
1905 FBSTRING_ASSERT(this->size() == n);
1908 template <typename E, class T, class A, class S>
1909 inline basic_fbstring<E, T, A, S>& basic_fbstring<E, T, A, S>::append(
1910 const basic_fbstring& str) {
1912 auto desiredSize = size() + str.size();
1914 append(str.data(), str.size());
1915 FBSTRING_ASSERT(size() == desiredSize);
1919 template <typename E, class T, class A, class S>
1920 inline basic_fbstring<E, T, A, S>& basic_fbstring<E, T, A, S>::append(
1921 const basic_fbstring& str, const size_type pos, size_type n) {
1922 const size_type sz = str.size();
1923 enforce(pos <= sz, std::__throw_out_of_range, "");
1924 procrustes(n, sz - pos);
1925 return append(str.data() + pos, n);
1928 template <typename E, class T, class A, class S>
1929 FOLLY_MALLOC_NOINLINE inline basic_fbstring<E, T, A, S>&
1930 basic_fbstring<E, T, A, S>::append(const value_type* s, size_type n) {
1931 Invariant checker(*this);
1933 if (FBSTRING_UNLIKELY(!n)) {
1934 // Unlikely but must be done
1937 auto const oldSize = size();
1938 auto const oldData = data();
1939 auto pData = store_.expandNoinit(n, /* expGrowth = */ true);
1941 // Check for aliasing (rare). We could use "<=" here but in theory
1942 // those do not work for pointers unless the pointers point to
1943 // elements in the same array. For that reason we use
1944 // std::less_equal, which is guaranteed to offer a total order
1945 // over pointers. See discussion at http://goo.gl/Cy2ya for more
1947 std::less_equal<const value_type*> le;
1948 if (FBSTRING_UNLIKELY(le(oldData, s) && !le(oldData + oldSize, s))) {
1949 FBSTRING_ASSERT(le(s + n, oldData + oldSize));
1950 // expandNoinit() could have moved the storage, restore the source.
1951 s = data() + (s - oldData);
1952 fbstring_detail::podMove(s, s + n, pData);
1954 fbstring_detail::podCopy(s, s + n, pData);
1957 FBSTRING_ASSERT(size() == oldSize + n);
1961 template <typename E, class T, class A, class S>
1962 inline basic_fbstring<E, T, A, S>& basic_fbstring<E, T, A, S>::append(
1963 size_type n, value_type c) {
1964 Invariant checker(*this);
1965 auto pData = store_.expandNoinit(n, /* expGrowth = */ true);
1966 fbstring_detail::podFill(pData, pData + n, c);
1970 template <typename E, class T, class A, class S>
1971 inline basic_fbstring<E, T, A, S>& basic_fbstring<E, T, A, S>::assign(
1972 const basic_fbstring& str, const size_type pos, size_type n) {
1973 const size_type sz = str.size();
1974 enforce(pos <= sz, std::__throw_out_of_range, "");
1975 procrustes(n, sz - pos);
1976 return assign(str.data() + pos, n);
1979 template <typename E, class T, class A, class S>
1980 FOLLY_MALLOC_NOINLINE inline basic_fbstring<E, T, A, S>&
1981 basic_fbstring<E, T, A, S>::assign(const value_type* s, const size_type n) {
1982 Invariant checker(*this);
1986 } else if (size() >= n) {
1987 // s can alias this, we need to use podMove.
1988 fbstring_detail::podMove(s, s + n, store_.mutableData());
1989 store_.shrink(size() - n);
1990 FBSTRING_ASSERT(size() == n);
1992 // If n is larger than size(), s cannot alias this string's
1995 // Do not use exponential growth here: assign() should be tight,
1996 // to mirror the behavior of the equivalent constructor.
1997 fbstring_detail::podCopy(s, s + n, store_.expandNoinit(n));
2000 FBSTRING_ASSERT(size() == n);
2004 #ifndef _LIBSTDCXX_FBSTRING
2005 template <typename E, class T, class A, class S>
2006 inline typename basic_fbstring<E, T, A, S>::istream_type&
2007 basic_fbstring<E, T, A, S>::getlineImpl(istream_type & is, value_type delim) {
2008 Invariant checker(*this);
2013 size_t avail = capacity() - size;
2014 // fbstring has 1 byte extra capacity for the null terminator,
2015 // and getline null-terminates the read string.
2016 is.getline(store_.expandNoinit(avail), avail + 1, delim);
2017 size += is.gcount();
2019 if (is.bad() || is.eof() || !is.fail()) {
2020 // Done by either failure, end of file, or normal read.
2021 if (!is.bad() && !is.eof()) {
2022 --size; // gcount() also accounts for the delimiter.
2028 FBSTRING_ASSERT(size == this->size());
2029 FBSTRING_ASSERT(size == capacity());
2030 // Start at minimum allocation 63 + terminator = 64.
2031 reserve(std::max<size_t>(63, 3 * size / 2));
2032 // Clear the error so we can continue reading.
2039 template <typename E, class T, class A, class S>
2040 inline typename basic_fbstring<E, T, A, S>::size_type
2041 basic_fbstring<E, T, A, S>::find(
2042 const value_type* needle, const size_type pos, const size_type nsize)
2044 auto const size = this->size();
2045 // nsize + pos can overflow (eg pos == npos), guard against that by checking
2046 // that nsize + pos does not wrap around.
2047 if (nsize + pos > size || nsize + pos < pos) {
2054 // Don't use std::search, use a Boyer-Moore-like trick by comparing
2055 // the last characters first
2056 auto const haystack = data();
2057 auto const nsize_1 = nsize - 1;
2058 auto const lastNeedle = needle[nsize_1];
2060 // Boyer-Moore skip value for the last char in the needle. Zero is
2061 // not a valid value; skip will be computed the first time it's
2065 const E* i = haystack + pos;
2066 auto iEnd = haystack + size - nsize_1;
2069 // Boyer-Moore: match the last element in the needle
2070 while (i[nsize_1] != lastNeedle) {
2076 // Here we know that the last char matches
2077 // Continue in pedestrian mode
2078 for (size_t j = 0;;) {
2079 FBSTRING_ASSERT(j < nsize);
2080 if (i[j] != needle[j]) {
2081 // Not found, we can skip
2082 // Compute the skip value lazily
2085 while (skip <= nsize_1 && needle[nsize_1 - skip] != lastNeedle) {
2092 // Check if done searching
2095 return i - haystack;
2102 template <typename E, class T, class A, class S>
2103 inline typename basic_fbstring<E, T, A, S>::iterator
2104 basic_fbstring<E, T, A, S>::insertImplDiscr(
2105 const_iterator i, size_type n, value_type c, std::true_type) {
2106 Invariant checker(*this);
2108 FBSTRING_ASSERT(i >= cbegin() && i <= cend());
2109 const size_type pos = i - cbegin();
2111 auto oldSize = size();
2112 store_.expandNoinit(n, /* expGrowth = */ true);
2114 fbstring_detail::podMove(b + pos, b + oldSize, b + pos + n);
2115 fbstring_detail::podFill(b + pos, b + pos + n, c);
2120 template <typename E, class T, class A, class S>
2121 template <class InputIter>
2122 inline typename basic_fbstring<E, T, A, S>::iterator
2123 basic_fbstring<E, T, A, S>::insertImplDiscr(
2124 const_iterator i, InputIter b, InputIter e, std::false_type) {
2126 i, b, e, typename std::iterator_traits<InputIter>::iterator_category());
2129 template <typename E, class T, class A, class S>
2130 template <class FwdIterator>
2131 inline typename basic_fbstring<E, T, A, S>::iterator
2132 basic_fbstring<E, T, A, S>::insertImpl(
2136 std::forward_iterator_tag) {
2137 Invariant checker(*this);
2139 FBSTRING_ASSERT(i >= cbegin() && i <= cend());
2140 const size_type pos = i - cbegin();
2141 auto n = std::distance(s1, s2);
2142 FBSTRING_ASSERT(n >= 0);
2144 auto oldSize = size();
2145 store_.expandNoinit(n, /* expGrowth = */ true);
2147 fbstring_detail::podMove(b + pos, b + oldSize, b + pos + n);
2148 std::copy(s1, s2, b + pos);
2153 template <typename E, class T, class A, class S>
2154 template <class InputIterator>
2155 inline typename basic_fbstring<E, T, A, S>::iterator
2156 basic_fbstring<E, T, A, S>::insertImpl(
2160 std::input_iterator_tag) {
2161 const auto pos = i - cbegin();
2162 basic_fbstring temp(cbegin(), i);
2163 for (; b != e; ++b) {
2166 temp.append(i, cend());
2168 return begin() + pos;
2171 template <typename E, class T, class A, class S>
2172 inline basic_fbstring<E, T, A, S>& basic_fbstring<E, T, A, S>::replaceImplDiscr(
2175 const value_type* s,
2177 std::integral_constant<int, 2>) {
2178 FBSTRING_ASSERT(i1 <= i2);
2179 FBSTRING_ASSERT(begin() <= i1 && i1 <= end());
2180 FBSTRING_ASSERT(begin() <= i2 && i2 <= end());
2181 return replace(i1, i2, s, s + n);
2184 template <typename E, class T, class A, class S>
2185 inline basic_fbstring<E, T, A, S>& basic_fbstring<E, T, A, S>::replaceImplDiscr(
2190 std::integral_constant<int, 1>) {
2191 const size_type n1 = i2 - i1;
2193 std::fill(i1, i1 + n2, c);
2196 std::fill(i1, i2, c);
2197 insert(i2, n2 - n1, c);
2199 FBSTRING_ASSERT(isSane());
2203 template <typename E, class T, class A, class S>
2204 template <class InputIter>
2205 inline basic_fbstring<E, T, A, S>& basic_fbstring<E, T, A, S>::replaceImplDiscr(
2210 std::integral_constant<int, 0>) {
2211 using Cat = typename std::iterator_traits<InputIter>::iterator_category;
2212 replaceImpl(i1, i2, b, e, Cat());
2216 template <typename E, class T, class A, class S>
2217 template <class FwdIterator>
2218 inline bool basic_fbstring<E, T, A, S>::replaceAliased(
2219 iterator i1, iterator i2, FwdIterator s1, FwdIterator s2, std::true_type) {
2220 std::less_equal<const value_type*> le{};
2221 const bool aliased = le(&*begin(), &*s1) && le(&*s1, &*end());
2225 // Aliased replace, copy to new string
2226 basic_fbstring temp;
2227 temp.reserve(size() - (i2 - i1) + std::distance(s1, s2));
2228 temp.append(begin(), i1).append(s1, s2).append(i2, end());
2233 template <typename E, class T, class A, class S>
2234 template <class FwdIterator>
2235 inline void basic_fbstring<E, T, A, S>::replaceImpl(
2240 std::forward_iterator_tag) {
2241 Invariant checker(*this);
2243 // Handle aliased replace
2244 using Sel = std::integral_constant<
2246 std::is_same<FwdIterator, iterator>::value ||
2247 std::is_same<FwdIterator, const_iterator>::value>;
2248 if (replaceAliased(i1, i2, s1, s2, Sel())) {
2252 auto const n1 = i2 - i1;
2253 FBSTRING_ASSERT(n1 >= 0);
2254 auto const n2 = std::distance(s1, s2);
2255 FBSTRING_ASSERT(n2 >= 0);
2259 std::copy(s1, s2, i1);
2263 s1 = fbstring_detail::copy_n(s1, n1, i1).first;
2266 FBSTRING_ASSERT(isSane());
2269 template <typename E, class T, class A, class S>
2270 template <class InputIterator>
2271 inline void basic_fbstring<E, T, A, S>::replaceImpl(
2276 std::input_iterator_tag) {
2277 basic_fbstring temp(begin(), i1);
2278 temp.append(b, e).append(i2, end());
2282 template <typename E, class T, class A, class S>
2283 inline typename basic_fbstring<E, T, A, S>::size_type
2284 basic_fbstring<E, T, A, S>::rfind(
2285 const value_type* s, size_type pos, size_type n) const {
2289 pos = std::min(pos, length() - n);
2294 const_iterator i(begin() + pos);
2296 if (traits_type::eq(*i, *s) && traits_type::compare(&*i, s, n) == 0) {
2306 template <typename E, class T, class A, class S>
2307 inline typename basic_fbstring<E, T, A, S>::size_type
2308 basic_fbstring<E, T, A, S>::find_first_of(
2309 const value_type* s, size_type pos, size_type n) const {
2310 if (pos > length() || n == 0) {
2313 const_iterator i(begin() + pos), finish(end());
2314 for (; i != finish; ++i) {
2315 if (traits_type::find(s, n, *i) != 0) {
2322 template <typename E, class T, class A, class S>
2323 inline typename basic_fbstring<E, T, A, S>::size_type
2324 basic_fbstring<E, T, A, S>::find_last_of(
2325 const value_type* s, size_type pos, size_type n) const {
2326 if (!empty() && n > 0) {
2327 pos = std::min(pos, length() - 1);
2328 const_iterator i(begin() + pos);
2330 if (traits_type::find(s, n, *i) != 0) {
2341 template <typename E, class T, class A, class S>
2342 inline typename basic_fbstring<E, T, A, S>::size_type
2343 basic_fbstring<E, T, A, S>::find_first_not_of(
2344 const value_type* s, size_type pos, size_type n) const {
2345 if (pos < length()) {
2346 const_iterator i(begin() + pos), finish(end());
2347 for (; i != finish; ++i) {
2348 if (traits_type::find(s, n, *i) == 0) {
2356 template <typename E, class T, class A, class S>
2357 inline typename basic_fbstring<E, T, A, S>::size_type
2358 basic_fbstring<E, T, A, S>::find_last_not_of(
2359 const value_type* s, size_type pos, size_type n) const {
2360 if (!this->empty()) {
2361 pos = std::min(pos, size() - 1);
2362 const_iterator i(begin() + pos);
2364 if (traits_type::find(s, n, *i) == 0) {
2375 // non-member functions
2377 template <typename E, class T, class A, class S>
2379 basic_fbstring<E, T, A, S> operator+(const basic_fbstring<E, T, A, S>& lhs,
2380 const basic_fbstring<E, T, A, S>& rhs) {
2382 basic_fbstring<E, T, A, S> result;
2383 result.reserve(lhs.size() + rhs.size());
2384 result.append(lhs).append(rhs);
2385 return std::move(result);
2389 template <typename E, class T, class A, class S>
2391 basic_fbstring<E, T, A, S> operator+(basic_fbstring<E, T, A, S>&& lhs,
2392 const basic_fbstring<E, T, A, S>& rhs) {
2393 return std::move(lhs.append(rhs));
2397 template <typename E, class T, class A, class S>
2399 basic_fbstring<E, T, A, S> operator+(const basic_fbstring<E, T, A, S>& lhs,
2400 basic_fbstring<E, T, A, S>&& rhs) {
2401 if (rhs.capacity() >= lhs.size() + rhs.size()) {
2402 // Good, at least we don't need to reallocate
2403 return std::move(rhs.insert(0, lhs));
2405 // Meh, no go. Forward to operator+(const&, const&).
2406 auto const& rhsC = rhs;
2411 template <typename E, class T, class A, class S>
2413 basic_fbstring<E, T, A, S> operator+(basic_fbstring<E, T, A, S>&& lhs,
2414 basic_fbstring<E, T, A, S>&& rhs) {
2415 return std::move(lhs.append(rhs));
2419 template <typename E, class T, class A, class S>
2421 basic_fbstring<E, T, A, S> operator+(
2423 const basic_fbstring<E, T, A, S>& rhs) {
2425 basic_fbstring<E, T, A, S> result;
2426 const auto len = basic_fbstring<E, T, A, S>::traits_type::length(lhs);
2427 result.reserve(len + rhs.size());
2428 result.append(lhs, len).append(rhs);
2433 template <typename E, class T, class A, class S>
2435 basic_fbstring<E, T, A, S> operator+(
2437 basic_fbstring<E, T, A, S>&& rhs) {
2439 const auto len = basic_fbstring<E, T, A, S>::traits_type::length(lhs);
2440 if (rhs.capacity() >= len + rhs.size()) {
2441 // Good, at least we don't need to reallocate
2442 rhs.insert(rhs.begin(), lhs, lhs + len);
2445 // Meh, no go. Do it by hand since we have len already.
2446 basic_fbstring<E, T, A, S> result;
2447 result.reserve(len + rhs.size());
2448 result.append(lhs, len).append(rhs);
2453 template <typename E, class T, class A, class S>
2455 basic_fbstring<E, T, A, S> operator+(
2457 const basic_fbstring<E, T, A, S>& rhs) {
2459 basic_fbstring<E, T, A, S> result;
2460 result.reserve(1 + rhs.size());
2461 result.push_back(lhs);
2467 template <typename E, class T, class A, class S>
2469 basic_fbstring<E, T, A, S> operator+(
2471 basic_fbstring<E, T, A, S>&& rhs) {
2473 if (rhs.capacity() > rhs.size()) {
2474 // Good, at least we don't need to reallocate
2475 rhs.insert(rhs.begin(), lhs);
2478 // Meh, no go. Forward to operator+(E, const&).
2479 auto const& rhsC = rhs;
2484 template <typename E, class T, class A, class S>
2486 basic_fbstring<E, T, A, S> operator+(
2487 const basic_fbstring<E, T, A, S>& lhs,
2490 typedef typename basic_fbstring<E, T, A, S>::size_type size_type;
2491 typedef typename basic_fbstring<E, T, A, S>::traits_type traits_type;
2493 basic_fbstring<E, T, A, S> result;
2494 const size_type len = traits_type::length(rhs);
2495 result.reserve(lhs.size() + len);
2496 result.append(lhs).append(rhs, len);
2500 // C++11 21.4.8.1/10
2501 template <typename E, class T, class A, class S>
2503 basic_fbstring<E, T, A, S> operator+(
2504 basic_fbstring<E, T, A, S>&& lhs,
2507 return std::move(lhs += rhs);
2510 // C++11 21.4.8.1/11
2511 template <typename E, class T, class A, class S>
2513 basic_fbstring<E, T, A, S> operator+(
2514 const basic_fbstring<E, T, A, S>& lhs,
2517 basic_fbstring<E, T, A, S> result;
2518 result.reserve(lhs.size() + 1);
2520 result.push_back(rhs);
2524 // C++11 21.4.8.1/12
2525 template <typename E, class T, class A, class S>
2527 basic_fbstring<E, T, A, S> operator+(
2528 basic_fbstring<E, T, A, S>&& lhs,
2531 return std::move(lhs += rhs);
2534 template <typename E, class T, class A, class S>
2536 bool operator==(const basic_fbstring<E, T, A, S>& lhs,
2537 const basic_fbstring<E, T, A, S>& rhs) {
2538 return lhs.size() == rhs.size() && lhs.compare(rhs) == 0; }
2540 template <typename E, class T, class A, class S>
2542 bool operator==(const typename basic_fbstring<E, T, A, S>::value_type* lhs,
2543 const basic_fbstring<E, T, A, S>& rhs) {
2544 return rhs == lhs; }
2546 template <typename E, class T, class A, class S>
2548 bool operator==(const basic_fbstring<E, T, A, S>& lhs,
2549 const typename basic_fbstring<E, T, A, S>::value_type* rhs) {
2550 return lhs.compare(rhs) == 0; }
2552 template <typename E, class T, class A, class S>
2554 bool operator!=(const basic_fbstring<E, T, A, S>& lhs,
2555 const basic_fbstring<E, T, A, S>& rhs) {
2556 return !(lhs == rhs); }
2558 template <typename E, class T, class A, class S>
2560 bool operator!=(const typename basic_fbstring<E, T, A, S>::value_type* lhs,
2561 const basic_fbstring<E, T, A, S>& rhs) {
2562 return !(lhs == rhs); }
2564 template <typename E, class T, class A, class S>
2566 bool operator!=(const basic_fbstring<E, T, A, S>& lhs,
2567 const typename basic_fbstring<E, T, A, S>::value_type* rhs) {
2568 return !(lhs == rhs); }
2570 template <typename E, class T, class A, class S>
2572 bool operator<(const basic_fbstring<E, T, A, S>& lhs,
2573 const basic_fbstring<E, T, A, S>& rhs) {
2574 return lhs.compare(rhs) < 0; }
2576 template <typename E, class T, class A, class S>
2578 bool operator<(const basic_fbstring<E, T, A, S>& lhs,
2579 const typename basic_fbstring<E, T, A, S>::value_type* rhs) {
2580 return lhs.compare(rhs) < 0; }
2582 template <typename E, class T, class A, class S>
2584 bool operator<(const typename basic_fbstring<E, T, A, S>::value_type* lhs,
2585 const basic_fbstring<E, T, A, S>& rhs) {
2586 return rhs.compare(lhs) > 0; }
2588 template <typename E, class T, class A, class S>
2590 bool operator>(const basic_fbstring<E, T, A, S>& lhs,
2591 const basic_fbstring<E, T, A, S>& rhs) {
2594 template <typename E, class T, class A, class S>
2596 bool operator>(const basic_fbstring<E, T, A, S>& lhs,
2597 const typename basic_fbstring<E, T, A, S>::value_type* rhs) {
2600 template <typename E, class T, class A, class S>
2602 bool operator>(const typename basic_fbstring<E, T, A, S>::value_type* lhs,
2603 const basic_fbstring<E, T, A, S>& rhs) {
2606 template <typename E, class T, class A, class S>
2608 bool operator<=(const basic_fbstring<E, T, A, S>& lhs,
2609 const basic_fbstring<E, T, A, S>& rhs) {
2610 return !(rhs < lhs); }
2612 template <typename E, class T, class A, class S>
2614 bool operator<=(const basic_fbstring<E, T, A, S>& lhs,
2615 const typename basic_fbstring<E, T, A, S>::value_type* rhs) {
2616 return !(rhs < lhs); }
2618 template <typename E, class T, class A, class S>
2620 bool operator<=(const typename basic_fbstring<E, T, A, S>::value_type* lhs,
2621 const basic_fbstring<E, T, A, S>& rhs) {
2622 return !(rhs < lhs); }
2624 template <typename E, class T, class A, class S>
2626 bool operator>=(const basic_fbstring<E, T, A, S>& lhs,
2627 const basic_fbstring<E, T, A, S>& rhs) {
2628 return !(lhs < rhs); }
2630 template <typename E, class T, class A, class S>
2632 bool operator>=(const basic_fbstring<E, T, A, S>& lhs,
2633 const typename basic_fbstring<E, T, A, S>::value_type* rhs) {
2634 return !(lhs < rhs); }
2636 template <typename E, class T, class A, class S>
2638 bool operator>=(const typename basic_fbstring<E, T, A, S>::value_type* lhs,
2639 const basic_fbstring<E, T, A, S>& rhs) {
2640 return !(lhs < rhs);
2644 template <typename E, class T, class A, class S>
2645 void swap(basic_fbstring<E, T, A, S>& lhs, basic_fbstring<E, T, A, S>& rhs) {
2649 // TODO: make this faster.
2650 template <typename E, class T, class A, class S>
2653 typename basic_fbstring<E, T, A, S>::value_type,
2654 typename basic_fbstring<E, T, A, S>::traits_type>&
2656 std::basic_istream<typename basic_fbstring<E, T, A, S>::value_type,
2657 typename basic_fbstring<E, T, A, S>::traits_type>& is,
2658 basic_fbstring<E, T, A, S>& str) {
2659 typedef std::basic_istream<
2660 typename basic_fbstring<E, T, A, S>::value_type,
2661 typename basic_fbstring<E, T, A, S>::traits_type>
2663 typename _istream_type::sentry sentry(is);
2664 size_t extracted = 0;
2665 auto err = _istream_type::goodbit;
2667 auto n = is.width();
2672 for (auto got = is.rdbuf()->sgetc(); extracted != size_t(n); ++extracted) {
2673 if (got == T::eof()) {
2674 err |= _istream_type::eofbit;
2682 got = is.rdbuf()->snextc();
2686 err |= _istream_type::failbit;
2694 template <typename E, class T, class A, class S>
2696 std::basic_ostream<typename basic_fbstring<E, T, A, S>::value_type,
2697 typename basic_fbstring<E, T, A, S>::traits_type>&
2699 std::basic_ostream<typename basic_fbstring<E, T, A, S>::value_type,
2700 typename basic_fbstring<E, T, A, S>::traits_type>& os,
2701 const basic_fbstring<E, T, A, S>& str) {
2703 typedef std::basic_ostream<
2704 typename basic_fbstring<E, T, A, S>::value_type,
2705 typename basic_fbstring<E, T, A, S>::traits_type>
2707 typename _ostream_type::sentry _s(os);
2709 typedef std::ostreambuf_iterator<
2710 typename basic_fbstring<E, T, A, S>::value_type,
2711 typename basic_fbstring<E, T, A, S>::traits_type> _Ip;
2712 size_t __len = str.size();
2714 (os.flags() & _ostream_type::adjustfield) == _ostream_type::left;
2715 if (__pad_and_output(_Ip(os),
2717 __left ? str.data() + __len : str.data(),
2720 os.fill()).failed()) {
2721 os.setstate(_ostream_type::badbit | _ostream_type::failbit);
2724 #elif defined(_MSC_VER)
2725 typedef decltype(os.precision()) streamsize;
2726 // MSVC doesn't define __ostream_insert
2727 os.write(str.data(), static_cast<streamsize>(str.size()));
2729 std::__ostream_insert(os, str.data(), str.size());
2734 template <typename E1, class T, class A, class S>
2735 constexpr typename basic_fbstring<E1, T, A, S>::size_type
2736 basic_fbstring<E1, T, A, S>::npos;
2738 #ifndef _LIBSTDCXX_FBSTRING
2739 // basic_string compatibility routines
2741 template <typename E, class T, class A, class S, class A2>
2742 inline bool operator==(
2743 const basic_fbstring<E, T, A, S>& lhs,
2744 const std::basic_string<E, T, A2>& rhs) {
2745 return lhs.compare(0, lhs.size(), rhs.data(), rhs.size()) == 0;
2748 template <typename E, class T, class A, class S, class A2>
2749 inline bool operator==(
2750 const std::basic_string<E, T, A2>& lhs,
2751 const basic_fbstring<E, T, A, S>& rhs) {
2755 template <typename E, class T, class A, class S, class A2>
2756 inline bool operator!=(
2757 const basic_fbstring<E, T, A, S>& lhs,
2758 const std::basic_string<E, T, A2>& rhs) {
2759 return !(lhs == rhs);
2762 template <typename E, class T, class A, class S, class A2>
2763 inline bool operator!=(
2764 const std::basic_string<E, T, A2>& lhs,
2765 const basic_fbstring<E, T, A, S>& rhs) {
2766 return !(lhs == rhs);
2769 template <typename E, class T, class A, class S, class A2>
2770 inline bool operator<(
2771 const basic_fbstring<E, T, A, S>& lhs,
2772 const std::basic_string<E, T, A2>& rhs) {
2773 return lhs.compare(0, lhs.size(), rhs.data(), rhs.size()) < 0;
2776 template <typename E, class T, class A, class S, class A2>
2777 inline bool operator>(
2778 const basic_fbstring<E, T, A, S>& lhs,
2779 const std::basic_string<E, T, A2>& rhs) {
2780 return lhs.compare(0, lhs.size(), rhs.data(), rhs.size()) > 0;
2783 template <typename E, class T, class A, class S, class A2>
2784 inline bool operator<(
2785 const std::basic_string<E, T, A2>& lhs,
2786 const basic_fbstring<E, T, A, S>& rhs) {
2790 template <typename E, class T, class A, class S, class A2>
2791 inline bool operator>(
2792 const std::basic_string<E, T, A2>& lhs,
2793 const basic_fbstring<E, T, A, S>& rhs) {
2797 template <typename E, class T, class A, class S, class A2>
2798 inline bool operator<=(
2799 const basic_fbstring<E, T, A, S>& lhs,
2800 const std::basic_string<E, T, A2>& rhs) {
2801 return !(lhs > rhs);
2804 template <typename E, class T, class A, class S, class A2>
2805 inline bool operator>=(
2806 const basic_fbstring<E, T, A, S>& lhs,
2807 const std::basic_string<E, T, A2>& rhs) {
2808 return !(lhs < rhs);
2811 template <typename E, class T, class A, class S, class A2>
2812 inline bool operator<=(
2813 const std::basic_string<E, T, A2>& lhs,
2814 const basic_fbstring<E, T, A, S>& rhs) {
2815 return !(lhs > rhs);
2818 template <typename E, class T, class A, class S, class A2>
2819 inline bool operator>=(
2820 const std::basic_string<E, T, A2>& lhs,
2821 const basic_fbstring<E, T, A, S>& rhs) {
2822 return !(lhs < rhs);
2825 #if !defined(_LIBSTDCXX_FBSTRING)
2826 typedef basic_fbstring<char> fbstring;
2829 // fbstring is relocatable
2830 template <class T, class R, class A, class S>
2831 FOLLY_ASSUME_RELOCATABLE(basic_fbstring<T, R, A, S>);
2834 _GLIBCXX_END_NAMESPACE_VERSION
2837 } // namespace folly
2839 #ifndef _LIBSTDCXX_FBSTRING
2841 // Hash functions to make fbstring usable with e.g. hash_map
2843 // Handle interaction with different C++ standard libraries, which
2844 // expect these types to be in different namespaces.
2846 #define FOLLY_FBSTRING_HASH1(T) \
2848 struct hash< ::folly::basic_fbstring<T>> { \
2849 size_t operator()(const ::folly::basic_fbstring<T>& s) const { \
2850 return ::folly::hash::fnv32_buf(s.data(), s.size() * sizeof(T)); \
2854 // The C++11 standard says that these four are defined
2855 #define FOLLY_FBSTRING_HASH \
2856 FOLLY_FBSTRING_HASH1(char) \
2857 FOLLY_FBSTRING_HASH1(char16_t) \
2858 FOLLY_FBSTRING_HASH1(char32_t) \
2859 FOLLY_FBSTRING_HASH1(wchar_t)
2867 #if FOLLY_HAVE_DEPRECATED_ASSOC
2868 #if defined(_GLIBCXX_SYMVER) && !defined(__BIONIC__)
2869 namespace __gnu_cxx {
2873 } // namespace __gnu_cxx
2874 #endif // _GLIBCXX_SYMVER && !__BIONIC__
2875 #endif // FOLLY_HAVE_DEPRECATED_ASSOC
2877 #undef FOLLY_FBSTRING_HASH
2878 #undef FOLLY_FBSTRING_HASH1
2880 #endif // _LIBSTDCXX_FBSTRING
2882 #pragma GCC diagnostic pop
2884 #undef FBSTRING_DISABLE_SSO
2885 #undef FBSTRING_SANITIZE_ADDRESS
2887 #undef FBSTRING_LIKELY
2888 #undef FBSTRING_UNLIKELY
2889 #undef FBSTRING_ASSERT