2 * Copyright 2016 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)
20 #ifndef FOLLY_BASE_FBSTRING_H_
21 #define FOLLY_BASE_FBSTRING_H_
25 #include <type_traits>
27 // This file appears in two locations: inside fbcode and in the
28 // libstdc++ source code (when embedding fbstring as std::string).
29 // To aid in this schizophrenic use, _LIBSTDCXX_FBSTRING is defined in
30 // libstdc++'s c++config.h, to gate use inside fbcode v. libstdc++.
31 #ifdef _LIBSTDCXX_FBSTRING
33 #pragma GCC system_header
35 // When used as std::string replacement always disable assertions.
38 #define FOLLY_DEFINED_NDEBUG_FOR_FBSTRING
41 // Handle the cases where the fbcode version (folly/Malloc.h) is included
42 // either before or after this inclusion.
43 #ifdef FOLLY_MALLOC_H_
44 #undef FOLLY_MALLOC_H_
45 #include "basic_fbstring_malloc.h" // nolint
47 #include "basic_fbstring_malloc.h" // nolint
48 #undef FOLLY_MALLOC_H_
51 #else // !_LIBSTDCXX_FBSTRING
53 #include <folly/Portability.h>
55 // libc++ doesn't provide this header, nor does msvc
56 #ifdef FOLLY_HAVE_BITS_CXXCONFIG_H
57 #include <bits/c++config.h>
65 #include <folly/Traits.h>
66 #include <folly/Malloc.h>
67 #include <folly/Hash.h>
68 #include <folly/ScopeGuard.h>
70 #if FOLLY_HAVE_DEPRECATED_ASSOC
71 #ifdef _GLIBCXX_SYMVER
72 #include <ext/hash_set>
73 #include <ext/hash_map>
79 // We defined these here rather than including Likely.h to avoid
80 // redefinition errors when fbstring is imported into libstdc++.
81 #if defined(__GNUC__) && __GNUC__ >= 4
82 #define FBSTRING_LIKELY(x) (__builtin_expect((x), 1))
83 #define FBSTRING_UNLIKELY(x) (__builtin_expect((x), 0))
85 #define FBSTRING_LIKELY(x) (x)
86 #define FBSTRING_UNLIKELY(x) (x)
89 #pragma GCC diagnostic push
90 // Ignore shadowing warnings within this file, so includers can use -Wshadow.
91 #pragma GCC diagnostic ignored "-Wshadow"
92 // GCC 4.9 has a false positive in setSmallSize (probably
93 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59124), disable
94 // compile-time array bound checking.
95 #pragma GCC diagnostic ignored "-Warray-bounds"
97 // FBString cannot use throw when replacing std::string, though it may still
100 #define throw FOLLY_FBSTRING_MAY_NOT_USE_THROW
102 #ifdef _LIBSTDCXX_FBSTRING
103 namespace std _GLIBCXX_VISIBILITY(default) {
104 _GLIBCXX_BEGIN_NAMESPACE_VERSION
109 #if defined(__clang__)
110 # if __has_feature(address_sanitizer)
111 # define FBSTRING_SANITIZE_ADDRESS
113 #elif defined (__GNUC__) && \
114 (((__GNUC__ == 4) && (__GNUC_MINOR__ >= 8)) || (__GNUC__ >= 5)) && \
116 # define FBSTRING_SANITIZE_ADDRESS
119 // When compiling with ASan, always heap-allocate the string even if
120 // it would fit in-situ, so that ASan can detect access to the string
121 // buffer after it has been invalidated (destroyed, resized, etc.).
122 // Note that this flag doesn't remove support for in-situ strings, as
123 // that would break ABI-compatibility and wouldn't allow linking code
124 // compiled with this flag with code compiled without.
125 #ifdef FBSTRING_SANITIZE_ADDRESS
126 # define FBSTRING_DISABLE_SMALL_STRING_OPTIMIZATION
129 namespace fbstring_detail {
131 template <class InIt, class OutIt>
134 typename std::iterator_traits<InIt>::difference_type n,
136 for (; n != 0; --n, ++b, ++d) {
142 template <class Pod, class T>
143 inline void pod_fill(Pod* b, Pod* e, T c) {
144 assert(b && e && b <= e);
145 /*static*/ if (sizeof(T) == 1) {
148 auto const ee = b + ((e - b) & ~7u);
149 for (; b != ee; b += 8) {
160 for (; b != e; ++b) {
167 * Lightly structured memcpy, simplifies copying PODs and introduces
168 * some asserts. Unfortunately using this function may cause
169 * measurable overhead (presumably because it adjusts from a begin/end
170 * convention to a pointer/size convention, so it does some extra
171 * arithmetic even though the caller might have done the inverse
172 * adaptation outside).
175 inline void pod_copy(const Pod* b, const Pod* e, Pod* d) {
177 assert(d >= e || d + (e - b) <= b);
178 memcpy(d, b, (e - b) * sizeof(Pod));
182 * Lightly structured memmove, simplifies copying PODs and introduces
186 inline void pod_move(const Pod* b, const Pod* e, Pod* d) {
188 memmove(d, b, (e - b) * sizeof(*b));
191 } // namespace fbstring_detail
194 * Defines a special acquisition method for constructing fbstring
195 * objects. AcquireMallocatedString means that the user passes a
196 * pointer to a malloc-allocated string that the fbstring object will
199 enum class AcquireMallocatedString {};
202 * fbstring_core_model is a mock-up type that defines all required
203 * signatures of a fbstring core. The fbstring class itself uses such
204 * a core object to implement all of the numerous member functions
205 * required by the standard.
207 * If you want to define a new core, copy the definition below and
208 * implement the primitives. Then plug the core into basic_fbstring as
209 * a template argument.
211 template <class Char>
212 class fbstring_core_model {
214 fbstring_core_model();
215 fbstring_core_model(const fbstring_core_model &);
216 ~fbstring_core_model();
217 // Returns a pointer to string's buffer (currently only contiguous
218 // strings are supported). The pointer is guaranteed to be valid
219 // until the next call to a non-const member function.
220 const Char * data() const;
221 // Much like data(), except the string is prepared to support
222 // character-level changes. This call is a signal for
223 // e.g. reference-counted implementation to fork the data. The
224 // pointer is guaranteed to be valid until the next call to a
225 // non-const member function.
226 Char * mutable_data();
227 // Returns a pointer to string's buffer and guarantees that a
228 // readable '\0' lies right after the buffer. The pointer is
229 // guaranteed to be valid until the next call to a non-const member
231 const Char * c_str() const;
232 // Shrinks the string by delta characters. Asserts that delta <=
234 void shrink(size_t delta);
235 // Expands the string by delta characters (i.e. after this call
236 // size() will report the old size() plus delta) but without
237 // initializing the expanded region. The expanded region is
238 // zero-terminated. Returns a pointer to the memory to be
239 // initialized (the beginning of the expanded portion). The caller
240 // is expected to fill the expanded area appropriately.
241 // If expGrowth is true, exponential growth is guaranteed.
242 Char* expand_noinit(size_t delta, bool expGrowth);
243 // Expands the string by one character and sets the last character
245 void push_back(Char c);
246 // Returns the string's size.
248 // Returns the string's capacity, i.e. maximum size that the string
249 // can grow to without reallocation. Note that for reference counted
250 // strings that's technically a lie - even assigning characters
251 // within the existing size would cause a reallocation.
252 size_t capacity() const;
253 // Returns true if the data underlying the string is actually shared
254 // across multiple strings (in a refcounted fashion).
255 bool isShared() const;
256 // Makes sure that at least minCapacity characters are available for
257 // the string without reallocation. For reference-counted strings,
258 // it should fork the data even if minCapacity < size().
259 void reserve(size_t minCapacity);
262 fbstring_core_model& operator=(const fbstring_core_model &);
267 * This is the core of the string. The code should work on 32- and
268 * 64-bit and both big- and little-endianan architectures with any
271 * The storage is selected as follows (assuming we store one-byte
272 * characters on a 64-bit machine): (a) "small" strings between 0 and
273 * 23 chars are stored in-situ without allocation (the rightmost byte
274 * stores the size); (b) "medium" strings from 24 through 254 chars
275 * are stored in malloc-allocated memory that is copied eagerly; (c)
276 * "large" strings of 255 chars and above are stored in a similar
277 * structure as medium arrays, except that the string is
278 * reference-counted and copied lazily. the reference count is
279 * allocated right before the character array.
281 * The discriminator between these three strategies sits in two
282 * bits of the rightmost char of the storage. If neither is set, then the
283 * string is small (and its length sits in the lower-order bits on
284 * little-endian or the high-order bits on big-endian of that
285 * rightmost character). If the MSb is set, the string is medium width.
286 * If the second MSb is set, then the string is large. On little-endian,
287 * these 2 bits are the 2 MSbs of MediumLarge::capacity_, while on
288 * big-endian, these 2 bits are the 2 LSbs. This keeps both little-endian
289 * and big-endian fbstring_core equivalent with merely different ops used
290 * to extract capacity/category.
292 template <class Char> class fbstring_core {
294 fbstring_core() noexcept { reset(); }
296 fbstring_core(const fbstring_core & rhs) {
297 assert(&rhs != this);
298 // Simplest case first: small strings are bitblitted
299 if (rhs.category() == Category::isSmall) {
300 static_assert(offsetof(MediumLarge, data_) == 0,
301 "fbstring layout failure");
302 static_assert(offsetof(MediumLarge, size_) == sizeof(ml_.data_),
303 "fbstring layout failure");
304 static_assert(offsetof(MediumLarge, capacity_) == 2 * sizeof(ml_.data_),
305 "fbstring layout failure");
306 // Just write the whole thing, don't look at details. In
307 // particular we need to copy capacity anyway because we want
308 // to set the size (don't forget that the last character,
309 // which stores a short string's length, is shared with the
310 // ml_.capacity field).
312 assert(category() == Category::isSmall && this->size() == rhs.size());
313 } else if (rhs.category() == Category::isLarge) {
314 // Large strings are just refcounted
316 RefCounted::incrementRefs(ml_.data_);
317 assert(category() == Category::isLarge && size() == rhs.size());
319 // Medium strings are copied eagerly. Don't forget to allocate
320 // one extra Char for the null terminator.
321 auto const allocSize =
322 goodMallocSize((1 + rhs.ml_.size_) * sizeof(Char));
323 ml_.data_ = static_cast<Char*>(checkedMalloc(allocSize));
324 // Also copies terminator.
325 fbstring_detail::pod_copy(rhs.ml_.data_,
326 rhs.ml_.data_ + rhs.ml_.size_ + 1,
328 ml_.size_ = rhs.ml_.size_;
329 ml_.setCapacity(allocSize / sizeof(Char) - 1, Category::isMedium);
330 assert(category() == Category::isMedium);
332 assert(size() == rhs.size());
333 assert(memcmp(data(), rhs.data(), size() * sizeof(Char)) == 0);
336 fbstring_core(fbstring_core&& goner) noexcept {
339 // Clean goner's carcass
343 fbstring_core(const Char *const data, const size_t size) {
345 #ifndef _LIBSTDCXX_FBSTRING
347 assert(this->size() == size);
348 assert(memcmp(this->data(), data, size * sizeof(Char)) == 0);
353 #ifndef FBSTRING_DISABLE_SMALL_STRING_OPTIMIZATION
354 // Simplest case first: small strings are bitblitted
355 if (size <= maxSmallSize) {
356 // Layout is: Char* data_, size_t size_, size_t capacity_
357 static_assert(sizeof(*this) == sizeof(Char*) + 2 * sizeof(size_t),
358 "fbstring has unexpected size");
359 static_assert(sizeof(Char*) == sizeof(size_t),
360 "fbstring size assumption violation");
361 // sizeof(size_t) must be a power of 2
362 static_assert((sizeof(size_t) & (sizeof(size_t) - 1)) == 0,
363 "fbstring size assumption violation");
365 // If data is aligned, use fast word-wise copying. Otherwise,
366 // use conservative memcpy.
367 if (reinterpret_cast<size_t>(data) & (sizeof(size_t) - 1)) {
368 fbstring_detail::pod_copy(data, data + size, small_);
370 // Copy one word at a time.
371 // NOTE: This reads bytes which are outside the range of the
372 // string, and makes ASan unhappy, but the small case is
373 // disabled under ASan.
375 const size_t byteSize = size * sizeof(Char);
376 constexpr size_t wordWidth = sizeof(size_t);
377 switch ((byteSize + wordWidth - 1) / wordWidth) { // Number of words.
379 ml_.capacity_ = reinterpret_cast<const size_t*>(data)[2];
381 ml_.size_ = reinterpret_cast<const size_t*>(data)[1];
383 ml_.data_ = *reinterpret_cast<Char**>(const_cast<Char*>(data));
390 #endif // FBSTRING_DISABLE_SMALL_STRING_OPTIMIZATION
392 if (size <= maxMediumSize) {
393 // Medium strings are allocated normally. Don't forget to
394 // allocate one extra Char for the terminating null.
395 auto const allocSize = goodMallocSize((1 + size) * sizeof(Char));
396 ml_.data_ = static_cast<Char*>(checkedMalloc(allocSize));
397 fbstring_detail::pod_copy(data, data + size, ml_.data_);
399 ml_.setCapacity(allocSize / sizeof(Char) - 1, Category::isMedium);
401 // Large strings are allocated differently
402 size_t effectiveCapacity = size;
403 auto const newRC = RefCounted::create(data, & effectiveCapacity);
404 ml_.data_ = newRC->data_;
406 ml_.setCapacity(effectiveCapacity, Category::isLarge);
408 ml_.data_[size] = '\0';
412 ~fbstring_core() noexcept {
413 auto const c = category();
414 if (c == Category::isSmall) {
417 if (c == Category::isMedium) {
421 RefCounted::decrementRefs(ml_.data_);
424 // Snatches a previously mallocated string. The parameter "size"
425 // is the size of the string, and the parameter "allocatedSize"
426 // is the size of the mallocated block. The string must be
427 // \0-terminated, so allocatedSize >= size + 1 and data[size] == '\0'.
429 // So if you want a 2-character string, pass malloc(3) as "data",
430 // pass 2 as "size", and pass 3 as "allocatedSize".
431 fbstring_core(Char * const data,
433 const size_t allocatedSize,
434 AcquireMallocatedString) {
436 assert(allocatedSize >= size + 1);
437 assert(data[size] == '\0');
438 // Use the medium string storage
441 // Don't forget about null terminator
442 ml_.setCapacity(allocatedSize - 1, Category::isMedium);
444 // No need for the memory
450 // swap below doesn't test whether &rhs == this (and instead
451 // potentially does extra work) on the premise that the rarity of
452 // that situation actually makes the check more expensive than is
454 void swap(fbstring_core & rhs) {
460 // In C++11 data() and c_str() are 100% equivalent.
461 const Char * data() const {
465 Char * mutable_data() {
466 auto const c = category();
467 if (c == Category::isSmall) {
470 assert(c == Category::isMedium || c == Category::isLarge);
471 if (c == Category::isLarge && RefCounted::refs(ml_.data_) > 1) {
473 size_t effectiveCapacity = ml_.capacity();
474 auto const newRC = RefCounted::create(& effectiveCapacity);
475 // If this fails, someone placed the wrong capacity in an
477 assert(effectiveCapacity >= ml_.capacity());
478 // Also copies terminator.
479 fbstring_detail::pod_copy(ml_.data_, ml_.data_ + ml_.size_ + 1,
481 RefCounted::decrementRefs(ml_.data_);
482 ml_.data_ = newRC->data_;
487 const Char * c_str() const {
488 auto const c = category();
489 if (c == Category::isSmall) {
490 assert(small_[smallSize()] == '\0');
493 assert(c == Category::isMedium || c == Category::isLarge);
494 assert(ml_.data_[ml_.size_] == '\0');
498 void shrink(const size_t delta) {
499 if (category() == Category::isSmall) {
500 // Check for underflow
501 assert(delta <= smallSize());
502 setSmallSize(smallSize() - delta);
503 } else if (category() == Category::isMedium ||
504 RefCounted::refs(ml_.data_) == 1) {
505 // Medium strings and unique large strings need no special
507 assert(ml_.size_ >= delta);
509 ml_.data_[ml_.size_] = '\0';
511 assert(ml_.size_ >= delta);
512 // Shared large string, must make unique. This is because of the
513 // durn terminator must be written, which may trample the shared
516 fbstring_core(ml_.data_, ml_.size_ - delta).swap(*this);
518 // No need to write the terminator.
522 void reserve(size_t minCapacity) {
523 if (category() == Category::isLarge) {
525 if (RefCounted::refs(ml_.data_) > 1) {
526 // We must make it unique regardless; in-place reallocation is
527 // useless if the string is shared. In order to not surprise
528 // people, reserve the new block at current capacity or
529 // more. That way, a string's capacity never shrinks after a
531 minCapacity = std::max(minCapacity, ml_.capacity());
532 auto const newRC = RefCounted::create(& minCapacity);
533 // Also copies terminator.
534 fbstring_detail::pod_copy(ml_.data_, ml_.data_ + ml_.size_ + 1,
536 RefCounted::decrementRefs(ml_.data_);
537 ml_.data_ = newRC->data_;
538 ml_.setCapacity(minCapacity, Category::isLarge);
539 // size remains unchanged
541 // String is not shared, so let's try to realloc (if needed)
542 if (minCapacity > ml_.capacity()) {
543 // Asking for more memory
545 RefCounted::reallocate(ml_.data_, ml_.size_,
546 ml_.capacity(), minCapacity);
547 ml_.data_ = newRC->data_;
548 ml_.setCapacity(minCapacity, Category::isLarge);
550 assert(capacity() >= minCapacity);
552 } else if (category() == Category::isMedium) {
553 // String is not shared
554 if (minCapacity <= ml_.capacity()) {
555 return; // nothing to do, there's enough room
557 if (minCapacity <= maxMediumSize) {
558 // Keep the string at medium size. Don't forget to allocate
559 // one extra Char for the terminating null.
560 size_t capacityBytes = goodMallocSize((1 + minCapacity) * sizeof(Char));
561 // Also copies terminator.
562 ml_.data_ = static_cast<Char *>(
565 (ml_.size_ + 1) * sizeof(Char),
566 (ml_.capacity() + 1) * sizeof(Char),
568 ml_.setCapacity(capacityBytes / sizeof(Char) - 1, Category::isMedium);
570 // Conversion from medium to large string
571 fbstring_core nascent;
572 // Will recurse to another branch of this function
573 nascent.reserve(minCapacity);
574 nascent.ml_.size_ = ml_.size_;
575 // Also copies terminator.
576 fbstring_detail::pod_copy(ml_.data_, ml_.data_ + ml_.size_ + 1,
579 assert(capacity() >= minCapacity);
582 assert(category() == Category::isSmall);
583 #ifndef FBSTRING_DISABLE_SMALL_STRING_OPTIMIZATION
584 if (minCapacity <= maxSmallSize) {
586 // Nothing to do, everything stays put
588 #endif // FBSTRING_DISABLE_SMALL_STRING_OPTIMIZATION
589 if (minCapacity <= maxMediumSize) {
591 // Don't forget to allocate one extra Char for the terminating null
592 auto const allocSizeBytes =
593 goodMallocSize((1 + minCapacity) * sizeof(Char));
594 auto const pData = static_cast<Char*>(checkedMalloc(allocSizeBytes));
595 auto const size = smallSize();
596 // Also copies terminator.
597 fbstring_detail::pod_copy(small_, small_ + size + 1, pData);
600 ml_.setCapacity(allocSizeBytes / sizeof(Char) - 1, Category::isMedium);
603 auto const newRC = RefCounted::create(& minCapacity);
604 auto const size = smallSize();
605 // Also copies terminator.
606 fbstring_detail::pod_copy(small_, small_ + size + 1, newRC->data_);
607 ml_.data_ = newRC->data_;
609 ml_.setCapacity(minCapacity, Category::isLarge);
610 assert(capacity() >= minCapacity);
613 assert(capacity() >= minCapacity);
616 Char * expand_noinit(const size_t delta, bool expGrowth = false) {
617 // Strategy is simple: make room, then change size
618 assert(capacity() >= size());
620 if (category() == Category::isSmall) {
623 #ifndef FBSTRING_DISABLE_SMALL_STRING_OPTIMIZATION
624 if (FBSTRING_LIKELY(newSz <= maxSmallSize)) {
628 #endif // FBSTRING_DISABLE_SMALL_STRING_OPTIMIZATION
629 reserve(expGrowth ? std::max(newSz, 2 * maxSmallSize) : newSz);
633 if (FBSTRING_UNLIKELY(newSz > capacity())) {
634 // ensures not shared
635 reserve(expGrowth ? std::max(newSz, 1 + capacity() * 3 / 2) : newSz);
638 assert(capacity() >= newSz);
639 // Category can't be small - we took care of that above
640 assert(category() == Category::isMedium || category() == Category::isLarge);
642 ml_.data_[newSz] = '\0';
643 assert(size() == newSz);
644 return ml_.data_ + sz;
647 void push_back(Char c) {
648 *expand_noinit(1, /* expGrowth */ true) = c;
651 size_t size() const {
652 return category() == Category::isSmall ? smallSize() : ml_.size_;
655 size_t capacity() const {
656 switch (category()) {
657 case Category::isSmall:
659 case Category::isLarge:
660 // For large-sized strings, a multi-referenced chunk has no
661 // available capacity. This is because any attempt to append
662 // data would trigger a new allocation.
663 if (RefCounted::refs(ml_.data_) > 1) return ml_.size_;
666 return ml_.capacity();
669 bool isShared() const {
670 return category() == Category::isLarge && RefCounted::refs(ml_.data_) > 1;
675 fbstring_core & operator=(const fbstring_core & rhs);
677 // Equivalent to setSmallSize(0) but a few ns faster in
680 ml_.capacity_ = kIsLittleEndian
681 ? maxSmallSize << (8 * (sizeof(size_t) - sizeof(Char)))
684 assert(category() == Category::isSmall && size() == 0);
688 std::atomic<size_t> refCount_;
691 static RefCounted * fromData(Char * p) {
692 return static_cast<RefCounted*>(
694 static_cast<unsigned char*>(static_cast<void*>(p))
695 - sizeof(refCount_)));
698 static size_t refs(Char * p) {
699 return fromData(p)->refCount_.load(std::memory_order_acquire);
702 static void incrementRefs(Char * p) {
703 fromData(p)->refCount_.fetch_add(1, std::memory_order_acq_rel);
706 static void decrementRefs(Char * p) {
707 auto const dis = fromData(p);
708 size_t oldcnt = dis->refCount_.fetch_sub(1, std::memory_order_acq_rel);
715 static RefCounted * create(size_t * size) {
716 // Don't forget to allocate one extra Char for the terminating
717 // null. In this case, however, one Char is already part of the
719 const size_t allocSize = goodMallocSize(
720 sizeof(RefCounted) + *size * sizeof(Char));
721 auto result = static_cast<RefCounted*>(checkedMalloc(allocSize));
722 result->refCount_.store(1, std::memory_order_release);
723 *size = (allocSize - sizeof(RefCounted)) / sizeof(Char);
727 static RefCounted * create(const Char * data, size_t * size) {
728 const size_t effectiveSize = *size;
729 auto result = create(size);
730 fbstring_detail::pod_copy(data, data + effectiveSize, result->data_);
734 static RefCounted * reallocate(Char *const data,
735 const size_t currentSize,
736 const size_t currentCapacity,
737 const size_t newCapacity) {
738 assert(newCapacity > 0 && newCapacity > currentSize);
739 auto const dis = fromData(data);
740 assert(dis->refCount_.load(std::memory_order_acquire) == 1);
741 // Don't forget to allocate one extra Char for the terminating
742 // null. In this case, however, one Char is already part of the
744 auto result = static_cast<RefCounted*>(
746 sizeof(RefCounted) + currentSize * sizeof(Char),
747 sizeof(RefCounted) + currentCapacity * sizeof(Char),
748 sizeof(RefCounted) + newCapacity * sizeof(Char)));
749 assert(result->refCount_.load(std::memory_order_acquire) == 1);
754 typedef std::conditional<sizeof(size_t) == 4, uint32_t, uint64_t>::type
757 enum class Category : category_type {
759 isMedium = kIsLittleEndian
760 ? sizeof(size_t) == 4 ? 0x80000000 : 0x8000000000000000
762 isLarge = kIsLittleEndian
763 ? sizeof(size_t) == 4 ? 0x40000000 : 0x4000000000000000
767 Category category() const {
768 // works for both big-endian and little-endian
769 return static_cast<Category>(ml_.capacity_ & categoryExtractMask);
777 size_t capacity() const {
778 return kIsLittleEndian
779 ? capacity_ & capacityExtractMask
783 void setCapacity(size_t cap, Category cat) {
784 capacity_ = kIsLittleEndian
785 ? cap | static_cast<category_type>(cat)
786 : (cap << 2) | static_cast<category_type>(cat);
791 Char small_[sizeof(MediumLarge) / sizeof(Char)];
796 lastChar = sizeof(MediumLarge) - 1,
797 maxSmallSize = lastChar / sizeof(Char),
798 maxMediumSize = 254 / sizeof(Char), // coincides with the small
799 // bin size in dlmalloc
800 categoryExtractMask = kIsLittleEndian
801 ? sizeof(size_t) == 4 ? 0xC0000000 : 0xC000000000000000
803 capacityExtractMask = kIsLittleEndian
804 ? ~categoryExtractMask
807 static_assert(!(sizeof(MediumLarge) % sizeof(Char)),
808 "Corrupt memory layout for fbstring.");
810 size_t smallSize() const {
811 assert(category() == Category::isSmall);
812 constexpr auto shift = kIsLittleEndian ? 0 : 2;
813 auto smallShifted = static_cast<size_t>(small_[maxSmallSize]) >> shift;
814 assert(static_cast<size_t>(maxSmallSize) >= smallShifted);
815 return static_cast<size_t>(maxSmallSize) - smallShifted;
818 void setSmallSize(size_t s) {
819 // Warning: this should work with uninitialized strings too,
820 // so don't assume anything about the previous value of
821 // small_[maxSmallSize].
822 assert(s <= maxSmallSize);
823 constexpr auto shift = kIsLittleEndian ? 0 : 2;
824 small_[maxSmallSize] = (maxSmallSize - s) << shift;
826 assert(category() == Category::isSmall && size() == s);
830 #ifndef _LIBSTDCXX_FBSTRING
832 * Dummy fbstring core that uses an actual std::string. This doesn't
833 * make any sense - it's just for testing purposes.
835 template <class Char>
836 class dummy_fbstring_core {
838 dummy_fbstring_core() {
840 dummy_fbstring_core(const dummy_fbstring_core& another)
841 : backend_(another.backend_) {
843 dummy_fbstring_core(const Char * s, size_t n)
846 void swap(dummy_fbstring_core & rhs) {
847 backend_.swap(rhs.backend_);
849 const Char * data() const {
850 return backend_.data();
852 Char * mutable_data() {
853 //assert(!backend_.empty());
854 return &*backend_.begin();
856 void shrink(size_t delta) {
857 assert(delta <= size());
858 backend_.resize(size() - delta);
860 Char * expand_noinit(size_t delta) {
861 auto const sz = size();
862 backend_.resize(size() + delta);
863 return backend_.data() + sz;
865 void push_back(Char c) {
866 backend_.push_back(c);
868 size_t size() const {
869 return backend_.size();
871 size_t capacity() const {
872 return backend_.capacity();
874 bool isShared() const {
877 void reserve(size_t minCapacity) {
878 backend_.reserve(minCapacity);
882 std::basic_string<Char> backend_;
884 #endif // !_LIBSTDCXX_FBSTRING
887 * This is the basic_string replacement. For conformity,
888 * basic_fbstring takes the same template parameters, plus the last
889 * one which is the core.
891 #ifdef _LIBSTDCXX_FBSTRING
892 template <typename E, class T, class A, class Storage>
894 template <typename E,
895 class T = std::char_traits<E>,
896 class A = std::allocator<E>,
897 class Storage = fbstring_core<E> >
899 class basic_fbstring {
903 void (*throw_exc)(const char*),
905 if (!condition) throw_exc(msg);
908 bool isSane() const {
911 empty() == (size() == 0) &&
912 empty() == (begin() == end()) &&
913 size() <= max_size() &&
914 capacity() <= max_size() &&
915 size() <= capacity() &&
916 begin()[size()] == '\0';
920 friend struct Invariant;
923 explicit Invariant(const basic_fbstring& s) : s_(s) {
930 const basic_fbstring& s_;
932 explicit Invariant(const basic_fbstring&) {}
934 Invariant& operator=(const Invariant&);
939 typedef T traits_type;
940 typedef typename traits_type::char_type value_type;
941 typedef A allocator_type;
942 typedef typename A::size_type size_type;
943 typedef typename A::difference_type difference_type;
945 typedef typename A::reference reference;
946 typedef typename A::const_reference const_reference;
947 typedef typename A::pointer pointer;
948 typedef typename A::const_pointer const_pointer;
951 typedef const E* const_iterator;
952 typedef std::reverse_iterator<iterator
953 #ifdef NO_ITERATOR_TRAITS
957 typedef std::reverse_iterator<const_iterator
958 #ifdef NO_ITERATOR_TRAITS
961 > const_reverse_iterator;
963 static const size_type npos; // = size_type(-1)
966 static void procrustes(size_type& n, size_type nmax) {
967 if (n > nmax) n = nmax;
971 // C++11 21.4.2 construct/copy/destroy
973 // Note: while the following two constructors can be (and previously were)
974 // collapsed into one constructor written this way:
976 // explicit basic_fbstring(const A& a = A()) noexcept { }
978 // This can cause Clang (at least version 3.7) to fail with the error:
979 // "chosen constructor is explicit in copy-initialization ...
980 // in implicit initialization of field '(x)' with omitted initializer"
982 // if used in a struct which is default-initialized. Hence the split into
983 // these two separate constructors.
985 basic_fbstring() noexcept : basic_fbstring(A()) {
988 explicit basic_fbstring(const A&) noexcept {
991 basic_fbstring(const basic_fbstring& str)
992 : store_(str.store_) {
996 basic_fbstring(basic_fbstring&& goner) noexcept
997 : store_(std::move(goner.store_)) {
1000 #ifndef _LIBSTDCXX_FBSTRING
1001 // This is defined for compatibility with std::string
1002 /* implicit */ basic_fbstring(const std::string& str)
1003 : store_(str.data(), str.size()) {
1007 basic_fbstring(const basic_fbstring& str,
1010 const A& /* a */ = A()) {
1011 assign(str, pos, n);
1014 /* implicit */ basic_fbstring(const value_type* s, const A& /*a*/ = A())
1016 ? traits_type::length(s)
1017 : (std::__throw_logic_error(
1018 "basic_fbstring: null pointer initializer not valid"),
1022 basic_fbstring(const value_type* s, size_type n, const A& /*a*/ = A())
1026 basic_fbstring(size_type n, value_type c, const A& /*a*/ = A()) {
1027 auto const pData = store_.expand_noinit(n);
1028 fbstring_detail::pod_fill(pData, pData + n, c);
1031 template <class InIt>
1032 basic_fbstring(InIt begin, InIt end,
1033 typename std::enable_if<
1034 !std::is_same<typename std::remove_const<InIt>::type,
1035 value_type*>::value, const A>::type & /*a*/ = A()) {
1039 // Specialization for const char*, const char*
1040 basic_fbstring(const value_type* b, const value_type* e)
1041 : store_(b, e - b) {
1044 // Nonstandard constructor
1045 basic_fbstring(value_type *s, size_type n, size_type c,
1046 AcquireMallocatedString a)
1047 : store_(s, n, c, a) {
1050 // Construction from initialization list
1051 basic_fbstring(std::initializer_list<value_type> il) {
1052 assign(il.begin(), il.end());
1055 ~basic_fbstring() noexcept {
1058 basic_fbstring& operator=(const basic_fbstring& lhs) {
1059 Invariant checker(*this);
1061 if (FBSTRING_UNLIKELY(&lhs == this)) {
1064 auto const oldSize = size();
1065 auto const srcSize = lhs.size();
1066 if (capacity() >= srcSize && !store_.isShared()) {
1067 // great, just copy the contents
1068 if (oldSize < srcSize) {
1069 store_.expand_noinit(srcSize - oldSize);
1071 store_.shrink(oldSize - srcSize);
1073 assert(size() == srcSize);
1074 auto srcData = lhs.data();
1075 fbstring_detail::pod_copy(
1076 srcData, srcData + srcSize, store_.mutable_data());
1078 // need to reallocate, so we may as well create a brand new string
1079 basic_fbstring(lhs).swap(*this);
1085 basic_fbstring& operator=(basic_fbstring&& goner) noexcept {
1086 if (FBSTRING_UNLIKELY(&goner == this)) {
1087 // Compatibility with std::basic_string<>,
1088 // C++11 21.4.2 [string.cons] / 23 requires self-move-assignment support.
1091 // No need of this anymore
1092 this->~basic_fbstring();
1093 // Move the goner into this
1094 new(&store_) fbstring_core<E>(std::move(goner.store_));
1098 #ifndef _LIBSTDCXX_FBSTRING
1099 // Compatibility with std::string
1100 basic_fbstring & operator=(const std::string & rhs) {
1101 return assign(rhs.data(), rhs.size());
1104 // Compatibility with std::string
1105 std::string toStdString() const {
1106 return std::string(data(), size());
1109 // A lot of code in fbcode still uses this method, so keep it here for now.
1110 const basic_fbstring& toStdString() const {
1115 basic_fbstring& operator=(const value_type* s) {
1119 basic_fbstring& operator=(value_type c) {
1120 Invariant checker(*this);
1123 store_.expand_noinit(1);
1124 } else if (store_.isShared()) {
1125 basic_fbstring(1, c).swap(*this);
1128 store_.shrink(size() - 1);
1134 basic_fbstring& operator=(std::initializer_list<value_type> il) {
1135 return assign(il.begin(), il.end());
1138 // C++11 21.4.3 iterators:
1139 iterator begin() { return store_.mutable_data(); }
1141 const_iterator begin() const { return store_.data(); }
1143 const_iterator cbegin() const { return begin(); }
1146 return store_.mutable_data() + store_.size();
1149 const_iterator end() const {
1150 return store_.data() + store_.size();
1153 const_iterator cend() const { return end(); }
1155 reverse_iterator rbegin() {
1156 return reverse_iterator(end());
1159 const_reverse_iterator rbegin() const {
1160 return const_reverse_iterator(end());
1163 const_reverse_iterator crbegin() const { return rbegin(); }
1165 reverse_iterator rend() {
1166 return reverse_iterator(begin());
1169 const_reverse_iterator rend() const {
1170 return const_reverse_iterator(begin());
1173 const_reverse_iterator crend() const { return rend(); }
1176 // C++11 21.4.5, element access:
1177 const value_type& front() const { return *begin(); }
1178 const value_type& back() const {
1180 // Should be begin()[size() - 1], but that branches twice
1181 return *(end() - 1);
1183 value_type& front() { return *begin(); }
1184 value_type& back() {
1186 // Should be begin()[size() - 1], but that branches twice
1187 return *(end() - 1);
1194 // C++11 21.4.4 capacity:
1195 size_type size() const { return store_.size(); }
1197 size_type length() const { return size(); }
1199 size_type max_size() const {
1200 return std::numeric_limits<size_type>::max();
1203 void resize(const size_type n, const value_type c = value_type()) {
1204 Invariant checker(*this);
1206 auto size = this->size();
1208 store_.shrink(size - n);
1210 auto const delta = n - size;
1211 auto pData = store_.expand_noinit(delta);
1212 fbstring_detail::pod_fill(pData, pData + delta, c);
1214 assert(this->size() == n);
1217 size_type capacity() const { return store_.capacity(); }
1219 void reserve(size_type res_arg = 0) {
1220 enforce(res_arg <= max_size(), std::__throw_length_error, "");
1221 store_.reserve(res_arg);
1224 void shrink_to_fit() {
1225 // Shrink only if slack memory is sufficiently large
1226 if (capacity() < size() * 3 / 2) {
1229 basic_fbstring(cbegin(), cend()).swap(*this);
1232 void clear() { resize(0); }
1234 bool empty() const { return size() == 0; }
1236 // C++11 21.4.5 element access:
1237 const_reference operator[](size_type pos) const {
1238 return *(begin() + pos);
1241 reference operator[](size_type pos) {
1242 return *(begin() + pos);
1245 const_reference at(size_type n) const {
1246 enforce(n <= size(), std::__throw_out_of_range, "");
1250 reference at(size_type n) {
1251 enforce(n < size(), std::__throw_out_of_range, "");
1255 // C++11 21.4.6 modifiers:
1256 basic_fbstring& operator+=(const basic_fbstring& str) {
1260 basic_fbstring& operator+=(const value_type* s) {
1264 basic_fbstring& operator+=(const value_type c) {
1269 basic_fbstring& operator+=(std::initializer_list<value_type> il) {
1274 basic_fbstring& append(const basic_fbstring& str) {
1276 auto desiredSize = size() + str.size();
1278 append(str.data(), str.size());
1279 assert(size() == desiredSize);
1283 basic_fbstring& append(const basic_fbstring& str, const size_type pos,
1285 const size_type sz = str.size();
1286 enforce(pos <= sz, std::__throw_out_of_range, "");
1287 procrustes(n, sz - pos);
1288 return append(str.data() + pos, n);
1291 basic_fbstring& append(const value_type* s, size_type n) {
1292 Invariant checker(*this);
1294 if (FBSTRING_UNLIKELY(!n)) {
1295 // Unlikely but must be done
1298 auto const oldSize = size();
1299 auto const oldData = data();
1300 // Check for aliasing (rare). We could use "<=" here but in theory
1301 // those do not work for pointers unless the pointers point to
1302 // elements in the same array. For that reason we use
1303 // std::less_equal, which is guaranteed to offer a total order
1304 // over pointers. See discussion at http://goo.gl/Cy2ya for more
1306 std::less_equal<const value_type*> le;
1307 if (FBSTRING_UNLIKELY(le(oldData, s) && !le(oldData + oldSize, s))) {
1308 assert(le(s + n, oldData + oldSize));
1309 const size_type offset = s - oldData;
1310 store_.reserve(oldSize + n);
1311 // Restore the source
1312 s = data() + offset;
1315 fbstring_detail::pod_copy(
1316 s, s + n, store_.expand_noinit(n, /* expGrowth */ true));
1317 assert(size() == oldSize + n);
1321 basic_fbstring& append(const value_type* s) {
1322 return append(s, traits_type::length(s));
1325 basic_fbstring& append(size_type n, value_type c) {
1326 resize(size() + n, c);
1330 template<class InputIterator>
1331 basic_fbstring& append(InputIterator first, InputIterator last) {
1332 insert(end(), first, last);
1336 basic_fbstring& append(std::initializer_list<value_type> il) {
1337 return append(il.begin(), il.end());
1340 void push_back(const value_type c) { // primitive
1341 store_.push_back(c);
1344 basic_fbstring& assign(const basic_fbstring& str) {
1345 if (&str == this) return *this;
1346 return assign(str.data(), str.size());
1349 basic_fbstring& assign(basic_fbstring&& str) {
1350 return *this = std::move(str);
1353 basic_fbstring& assign(const basic_fbstring& str, const size_type pos,
1355 const size_type sz = str.size();
1356 enforce(pos <= sz, std::__throw_out_of_range, "");
1357 procrustes(n, sz - pos);
1358 return assign(str.data() + pos, n);
1361 basic_fbstring& assign(const value_type* s, const size_type n) {
1362 Invariant checker(*this);
1364 // s can alias this, we need to use pod_move.
1366 fbstring_detail::pod_move(s, s + n, store_.mutable_data());
1368 assert(size() == n);
1370 const value_type *const s2 = s + size();
1371 fbstring_detail::pod_move(s, s2, store_.mutable_data());
1372 append(s2, n - size());
1373 assert(size() == n);
1375 assert(size() == n);
1379 basic_fbstring& assign(const value_type* s) {
1380 return assign(s, traits_type::length(s));
1383 basic_fbstring& assign(std::initializer_list<value_type> il) {
1384 return assign(il.begin(), il.end());
1387 template <class ItOrLength, class ItOrChar>
1388 basic_fbstring& assign(ItOrLength first_or_n, ItOrChar last_or_c) {
1389 return replace(begin(), end(), first_or_n, last_or_c);
1392 basic_fbstring& insert(size_type pos1, const basic_fbstring& str) {
1393 return insert(pos1, str.data(), str.size());
1396 basic_fbstring& insert(size_type pos1, const basic_fbstring& str,
1397 size_type pos2, size_type n) {
1398 enforce(pos2 <= str.length(), std::__throw_out_of_range, "");
1399 procrustes(n, str.length() - pos2);
1400 return insert(pos1, str.data() + pos2, n);
1403 basic_fbstring& insert(size_type pos, const value_type* s, size_type n) {
1404 enforce(pos <= length(), std::__throw_out_of_range, "");
1405 insert(begin() + pos, s, s + n);
1409 basic_fbstring& insert(size_type pos, const value_type* s) {
1410 return insert(pos, s, traits_type::length(s));
1413 basic_fbstring& insert(size_type pos, size_type n, value_type c) {
1414 enforce(pos <= length(), std::__throw_out_of_range, "");
1415 insert(begin() + pos, n, c);
1419 iterator insert(const_iterator p, const value_type c) {
1420 const size_type pos = p - begin();
1422 return begin() + pos;
1425 #ifndef _LIBSTDCXX_FBSTRING
1427 typedef std::basic_istream<value_type, traits_type> istream_type;
1430 friend inline istream_type& getline(istream_type& is,
1431 basic_fbstring& str,
1433 Invariant checker(str);
1438 size_t avail = str.capacity() - size;
1439 // fbstring has 1 byte extra capacity for the null terminator,
1440 // and getline null-terminates the read string.
1441 is.getline(str.store_.expand_noinit(avail), avail + 1, delim);
1442 size += is.gcount();
1444 if (is.bad() || is.eof() || !is.fail()) {
1445 // Done by either failure, end of file, or normal read.
1446 if (!is.bad() && !is.eof()) {
1447 --size; // gcount() also accounts for the delimiter.
1453 assert(size == str.size());
1454 assert(size == str.capacity());
1455 // Start at minimum allocation 63 + terminator = 64.
1456 str.reserve(std::max<size_t>(63, 3 * size / 2));
1457 // Clear the error so we can continue reading.
1463 friend inline istream_type& getline(istream_type& is, basic_fbstring& str) {
1464 return getline(is, str, '\n');
1469 template <int i> class Selector {};
1471 iterator insertImplDiscr(const_iterator p,
1472 size_type n, value_type c, Selector<1>) {
1473 Invariant checker(*this);
1475 auto const pos = p - begin();
1476 assert(p >= begin() && p <= end());
1477 if (capacity() - size() < n) {
1478 const size_type sz = p - begin();
1479 reserve(size() + n);
1482 const iterator oldEnd = end();
1483 if (n < size_type(oldEnd - p)) {
1484 append(oldEnd - n, oldEnd);
1485 // Also copies terminator.
1486 fbstring_detail::pod_move(&*p, &*oldEnd - n + 1, begin() + pos + n);
1487 std::fill(begin() + pos, begin() + pos + n, c);
1489 append(n - (end() - p), c);
1490 append(iterator(p), oldEnd);
1491 std::fill(iterator(p), oldEnd, c);
1493 return begin() + pos;
1496 template<class InputIter>
1497 iterator insertImplDiscr(const_iterator i,
1498 InputIter b, InputIter e, Selector<0>) {
1499 return insertImpl(i, b, e,
1500 typename std::iterator_traits<InputIter>::iterator_category());
1503 template <class FwdIterator>
1504 iterator insertImpl(const_iterator i,
1505 FwdIterator s1, FwdIterator s2, std::forward_iterator_tag) {
1506 Invariant checker(*this);
1508 const size_type pos = i - begin();
1509 const typename std::iterator_traits<FwdIterator>::difference_type n2 =
1510 std::distance(s1, s2);
1512 using namespace fbstring_detail;
1513 assert(pos <= size());
1515 const typename std::iterator_traits<FwdIterator>::difference_type maxn2 =
1516 capacity() - size();
1518 // realloc the string
1519 reserve(size() + n2);
1522 if (pos + n2 <= size()) {
1523 const iterator tailBegin = end() - n2;
1524 store_.expand_noinit(n2);
1525 fbstring_detail::pod_copy(tailBegin, tailBegin + n2, end() - n2);
1526 std::copy(const_reverse_iterator(tailBegin), const_reverse_iterator(i),
1527 reverse_iterator(tailBegin + n2));
1528 std::copy(s1, s2, begin() + pos);
1531 const size_type old_size = size();
1532 std::advance(t, old_size - pos);
1533 const size_t newElems = std::distance(t, s2);
1534 store_.expand_noinit(n2);
1535 std::copy(t, s2, begin() + old_size);
1536 fbstring_detail::pod_copy(data() + pos, data() + old_size,
1537 begin() + old_size + newElems);
1538 std::copy(s1, t, begin() + pos);
1540 return begin() + pos;
1543 template <class InputIterator>
1544 iterator insertImpl(const_iterator i,
1545 InputIterator b, InputIterator e,
1546 std::input_iterator_tag) {
1547 const auto pos = i - begin();
1548 basic_fbstring temp(begin(), i);
1549 for (; b != e; ++b) {
1552 temp.append(i, cend());
1554 return begin() + pos;
1558 template <class ItOrLength, class ItOrChar>
1559 iterator insert(const_iterator p, ItOrLength first_or_n, ItOrChar last_or_c) {
1560 Selector<std::numeric_limits<ItOrLength>::is_specialized> sel;
1561 return insertImplDiscr(p, first_or_n, last_or_c, sel);
1564 iterator insert(const_iterator p, std::initializer_list<value_type> il) {
1565 return insert(p, il.begin(), il.end());
1568 basic_fbstring& erase(size_type pos = 0, size_type n = npos) {
1569 Invariant checker(*this);
1571 enforce(pos <= length(), std::__throw_out_of_range, "");
1572 procrustes(n, length() - pos);
1573 std::copy(begin() + pos + n, end(), begin() + pos);
1574 resize(length() - n);
1578 iterator erase(iterator position) {
1579 const size_type pos(position - begin());
1580 enforce(pos <= size(), std::__throw_out_of_range, "");
1582 return begin() + pos;
1585 iterator erase(iterator first, iterator last) {
1586 const size_type pos(first - begin());
1587 erase(pos, last - first);
1588 return begin() + pos;
1591 // Replaces at most n1 chars of *this, starting with pos1 with the
1593 basic_fbstring& replace(size_type pos1, size_type n1,
1594 const basic_fbstring& str) {
1595 return replace(pos1, n1, str.data(), str.size());
1598 // Replaces at most n1 chars of *this, starting with pos1,
1599 // with at most n2 chars of str starting with pos2
1600 basic_fbstring& replace(size_type pos1, size_type n1,
1601 const basic_fbstring& str,
1602 size_type pos2, size_type n2) {
1603 enforce(pos2 <= str.length(), std::__throw_out_of_range, "");
1604 return replace(pos1, n1, str.data() + pos2,
1605 std::min(n2, str.size() - pos2));
1608 // Replaces at most n1 chars of *this, starting with pos, with chars from s
1609 basic_fbstring& replace(size_type pos, size_type n1, const value_type* s) {
1610 return replace(pos, n1, s, traits_type::length(s));
1613 // Replaces at most n1 chars of *this, starting with pos, with n2
1616 // consolidated with
1618 // Replaces at most n1 chars of *this, starting with pos, with at
1619 // most n2 chars of str. str must have at least n2 chars.
1620 template <class StrOrLength, class NumOrChar>
1621 basic_fbstring& replace(size_type pos, size_type n1,
1622 StrOrLength s_or_n2, NumOrChar n_or_c) {
1623 Invariant checker(*this);
1625 enforce(pos <= size(), std::__throw_out_of_range, "");
1626 procrustes(n1, length() - pos);
1627 const iterator b = begin() + pos;
1628 return replace(b, b + n1, s_or_n2, n_or_c);
1631 basic_fbstring& replace(iterator i1, iterator i2, const basic_fbstring& str) {
1632 return replace(i1, i2, str.data(), str.length());
1635 basic_fbstring& replace(iterator i1, iterator i2, const value_type* s) {
1636 return replace(i1, i2, s, traits_type::length(s));
1640 basic_fbstring& replaceImplDiscr(iterator i1, iterator i2,
1641 const value_type* s, size_type n,
1644 assert(begin() <= i1 && i1 <= end());
1645 assert(begin() <= i2 && i2 <= end());
1646 return replace(i1, i2, s, s + n);
1649 basic_fbstring& replaceImplDiscr(iterator i1, iterator i2,
1650 size_type n2, value_type c, Selector<1>) {
1651 const size_type n1 = i2 - i1;
1653 std::fill(i1, i1 + n2, c);
1656 std::fill(i1, i2, c);
1657 insert(i2, n2 - n1, c);
1663 template <class InputIter>
1664 basic_fbstring& replaceImplDiscr(iterator i1, iterator i2,
1665 InputIter b, InputIter e,
1667 replaceImpl(i1, i2, b, e,
1668 typename std::iterator_traits<InputIter>::iterator_category());
1673 template <class FwdIterator>
1674 bool replaceAliased(iterator /* i1 */,
1676 FwdIterator /* s1 */,
1677 FwdIterator /* s2 */,
1682 template <class FwdIterator>
1683 bool replaceAliased(iterator i1, iterator i2,
1684 FwdIterator s1, FwdIterator s2, std::true_type) {
1685 static const std::less_equal<const value_type*> le =
1686 std::less_equal<const value_type*>();
1687 const bool aliased = le(&*begin(), &*s1) && le(&*s1, &*end());
1691 // Aliased replace, copy to new string
1692 basic_fbstring temp;
1693 temp.reserve(size() - (i2 - i1) + std::distance(s1, s2));
1694 temp.append(begin(), i1).append(s1, s2).append(i2, end());
1699 template <class FwdIterator>
1700 void replaceImpl(iterator i1, iterator i2,
1701 FwdIterator s1, FwdIterator s2, std::forward_iterator_tag) {
1702 Invariant checker(*this);
1704 // Handle aliased replace
1705 if (replaceAliased(i1, i2, s1, s2,
1706 std::integral_constant<bool,
1707 std::is_same<FwdIterator, iterator>::value ||
1708 std::is_same<FwdIterator, const_iterator>::value>())) {
1712 auto const n1 = i2 - i1;
1714 auto const n2 = std::distance(s1, s2);
1719 std::copy(s1, s2, i1);
1723 fbstring_detail::copy_n(s1, n1, i1);
1724 std::advance(s1, n1);
1730 template <class InputIterator>
1731 void replaceImpl(iterator i1, iterator i2,
1732 InputIterator b, InputIterator e, std::input_iterator_tag) {
1733 basic_fbstring temp(begin(), i1);
1734 temp.append(b, e).append(i2, end());
1739 template <class T1, class T2>
1740 basic_fbstring& replace(iterator i1, iterator i2,
1741 T1 first_or_n_or_s, T2 last_or_c_or_n) {
1743 num1 = std::numeric_limits<T1>::is_specialized,
1744 num2 = std::numeric_limits<T2>::is_specialized;
1745 return replaceImplDiscr(
1746 i1, i2, first_or_n_or_s, last_or_c_or_n,
1747 Selector<num1 ? (num2 ? 1 : -1) : (num2 ? 2 : 0)>());
1750 size_type copy(value_type* s, size_type n, size_type pos = 0) const {
1751 enforce(pos <= size(), std::__throw_out_of_range, "");
1752 procrustes(n, size() - pos);
1754 fbstring_detail::pod_copy(
1761 void swap(basic_fbstring& rhs) {
1762 store_.swap(rhs.store_);
1765 const value_type* c_str() const {
1766 return store_.c_str();
1769 const value_type* data() const { return c_str(); }
1771 allocator_type get_allocator() const {
1772 return allocator_type();
1775 size_type find(const basic_fbstring& str, size_type pos = 0) const {
1776 return find(str.data(), pos, str.length());
1779 size_type find(const value_type* needle, const size_type pos,
1780 const size_type nsize) const {
1781 if (!nsize) return pos;
1782 auto const size = this->size();
1783 // nsize + pos can overflow (eg pos == npos), guard against that by checking
1784 // that nsize + pos does not wrap around.
1785 if (nsize + pos > size || nsize + pos < pos) return npos;
1786 // Don't use std::search, use a Boyer-Moore-like trick by comparing
1787 // the last characters first
1788 auto const haystack = data();
1789 auto const nsize_1 = nsize - 1;
1790 auto const lastNeedle = needle[nsize_1];
1792 // Boyer-Moore skip value for the last char in the needle. Zero is
1793 // not a valid value; skip will be computed the first time it's
1797 const E * i = haystack + pos;
1798 auto iEnd = haystack + size - nsize_1;
1801 // Boyer-Moore: match the last element in the needle
1802 while (i[nsize_1] != lastNeedle) {
1808 // Here we know that the last char matches
1809 // Continue in pedestrian mode
1810 for (size_t j = 0; ; ) {
1812 if (i[j] != needle[j]) {
1813 // Not found, we can skip
1814 // Compute the skip value lazily
1817 while (skip <= nsize_1 && needle[nsize_1 - skip] != lastNeedle) {
1824 // Check if done searching
1827 return i - haystack;
1834 size_type find(const value_type* s, size_type pos = 0) const {
1835 return find(s, pos, traits_type::length(s));
1838 size_type find (value_type c, size_type pos = 0) const {
1839 return find(&c, pos, 1);
1842 size_type rfind(const basic_fbstring& str, size_type pos = npos) const {
1843 return rfind(str.data(), pos, str.length());
1846 size_type rfind(const value_type* s, size_type pos, size_type n) const {
1847 if (n > length()) return npos;
1848 pos = std::min(pos, length() - n);
1849 if (n == 0) return pos;
1851 const_iterator i(begin() + pos);
1853 if (traits_type::eq(*i, *s)
1854 && traits_type::compare(&*i, s, n) == 0) {
1857 if (i == begin()) break;
1862 size_type rfind(const value_type* s, size_type pos = npos) const {
1863 return rfind(s, pos, traits_type::length(s));
1866 size_type rfind(value_type c, size_type pos = npos) const {
1867 return rfind(&c, pos, 1);
1870 size_type find_first_of(const basic_fbstring& str, size_type pos = 0) const {
1871 return find_first_of(str.data(), pos, str.length());
1874 size_type find_first_of(const value_type* s,
1875 size_type pos, size_type n) const {
1876 if (pos > length() || n == 0) return npos;
1877 const_iterator i(begin() + pos),
1879 for (; i != finish; ++i) {
1880 if (traits_type::find(s, n, *i) != 0) {
1887 size_type find_first_of(const value_type* s, size_type pos = 0) const {
1888 return find_first_of(s, pos, traits_type::length(s));
1891 size_type find_first_of(value_type c, size_type pos = 0) const {
1892 return find_first_of(&c, pos, 1);
1895 size_type find_last_of (const basic_fbstring& str,
1896 size_type pos = npos) const {
1897 return find_last_of(str.data(), pos, str.length());
1900 size_type find_last_of (const value_type* s, size_type pos,
1901 size_type n) const {
1902 if (!empty() && n > 0) {
1903 pos = std::min(pos, length() - 1);
1904 const_iterator i(begin() + pos);
1906 if (traits_type::find(s, n, *i) != 0) {
1909 if (i == begin()) break;
1915 size_type find_last_of (const value_type* s,
1916 size_type pos = npos) const {
1917 return find_last_of(s, pos, traits_type::length(s));
1920 size_type find_last_of (value_type c, size_type pos = npos) const {
1921 return find_last_of(&c, pos, 1);
1924 size_type find_first_not_of(const basic_fbstring& str,
1925 size_type pos = 0) const {
1926 return find_first_not_of(str.data(), pos, str.size());
1929 size_type find_first_not_of(const value_type* s, size_type pos,
1930 size_type n) const {
1931 if (pos < length()) {
1935 for (; i != finish; ++i) {
1936 if (traits_type::find(s, n, *i) == 0) {
1944 size_type find_first_not_of(const value_type* s,
1945 size_type pos = 0) const {
1946 return find_first_not_of(s, pos, traits_type::length(s));
1949 size_type find_first_not_of(value_type c, size_type pos = 0) const {
1950 return find_first_not_of(&c, pos, 1);
1953 size_type find_last_not_of(const basic_fbstring& str,
1954 size_type pos = npos) const {
1955 return find_last_not_of(str.data(), pos, str.length());
1958 size_type find_last_not_of(const value_type* s, size_type pos,
1959 size_type n) const {
1960 if (!this->empty()) {
1961 pos = std::min(pos, size() - 1);
1962 const_iterator i(begin() + pos);
1964 if (traits_type::find(s, n, *i) == 0) {
1967 if (i == begin()) break;
1973 size_type find_last_not_of(const value_type* s,
1974 size_type pos = npos) const {
1975 return find_last_not_of(s, pos, traits_type::length(s));
1978 size_type find_last_not_of (value_type c, size_type pos = npos) const {
1979 return find_last_not_of(&c, pos, 1);
1982 basic_fbstring substr(size_type pos = 0, size_type n = npos) const& {
1983 enforce(pos <= size(), std::__throw_out_of_range, "");
1984 return basic_fbstring(data() + pos, std::min(n, size() - pos));
1987 basic_fbstring substr(size_type pos = 0, size_type n = npos) && {
1988 enforce(pos <= size(), std::__throw_out_of_range, "");
1990 if (n < size()) resize(n);
1991 return std::move(*this);
1994 int compare(const basic_fbstring& str) const {
1995 // FIX due to Goncalo N M de Carvalho July 18, 2005
1996 return compare(0, size(), str);
1999 int compare(size_type pos1, size_type n1,
2000 const basic_fbstring& str) const {
2001 return compare(pos1, n1, str.data(), str.size());
2004 int compare(size_type pos1, size_type n1,
2005 const value_type* s) const {
2006 return compare(pos1, n1, s, traits_type::length(s));
2009 int compare(size_type pos1, size_type n1,
2010 const value_type* s, size_type n2) const {
2011 enforce(pos1 <= size(), std::__throw_out_of_range, "");
2012 procrustes(n1, size() - pos1);
2013 // The line below fixed by Jean-Francois Bastien, 04-23-2007. Thanks!
2014 const int r = traits_type::compare(pos1 + data(), s, std::min(n1, n2));
2015 return r != 0 ? r : n1 > n2 ? 1 : n1 < n2 ? -1 : 0;
2018 int compare(size_type pos1, size_type n1,
2019 const basic_fbstring& str,
2020 size_type pos2, size_type n2) const {
2021 enforce(pos2 <= str.size(), std::__throw_out_of_range, "");
2022 return compare(pos1, n1, str.data() + pos2,
2023 std::min(n2, str.size() - pos2));
2026 // Code from Jean-Francois Bastien (03/26/2007)
2027 int compare(const value_type* s) const {
2028 // Could forward to compare(0, size(), s, traits_type::length(s))
2029 // but that does two extra checks
2030 const size_type n1(size()), n2(traits_type::length(s));
2031 const int r = traits_type::compare(data(), s, std::min(n1, n2));
2032 return r != 0 ? r : n1 > n2 ? 1 : n1 < n2 ? -1 : 0;
2040 // non-member functions
2042 template <typename E, class T, class A, class S>
2044 basic_fbstring<E, T, A, S> operator+(const basic_fbstring<E, T, A, S>& lhs,
2045 const basic_fbstring<E, T, A, S>& rhs) {
2047 basic_fbstring<E, T, A, S> result;
2048 result.reserve(lhs.size() + rhs.size());
2049 result.append(lhs).append(rhs);
2050 return std::move(result);
2054 template <typename E, class T, class A, class S>
2056 basic_fbstring<E, T, A, S> operator+(basic_fbstring<E, T, A, S>&& lhs,
2057 const basic_fbstring<E, T, A, S>& rhs) {
2058 return std::move(lhs.append(rhs));
2062 template <typename E, class T, class A, class S>
2064 basic_fbstring<E, T, A, S> operator+(const basic_fbstring<E, T, A, S>& lhs,
2065 basic_fbstring<E, T, A, S>&& rhs) {
2066 if (rhs.capacity() >= lhs.size() + rhs.size()) {
2067 // Good, at least we don't need to reallocate
2068 return std::move(rhs.insert(0, lhs));
2070 // Meh, no go. Forward to operator+(const&, const&).
2071 auto const& rhsC = rhs;
2076 template <typename E, class T, class A, class S>
2078 basic_fbstring<E, T, A, S> operator+(basic_fbstring<E, T, A, S>&& lhs,
2079 basic_fbstring<E, T, A, S>&& rhs) {
2080 return std::move(lhs.append(rhs));
2084 template <typename E, class T, class A, class S>
2086 basic_fbstring<E, T, A, S> operator+(
2088 const basic_fbstring<E, T, A, S>& rhs) {
2090 basic_fbstring<E, T, A, S> result;
2091 const auto len = basic_fbstring<E, T, A, S>::traits_type::length(lhs);
2092 result.reserve(len + rhs.size());
2093 result.append(lhs, len).append(rhs);
2098 template <typename E, class T, class A, class S>
2100 basic_fbstring<E, T, A, S> operator+(
2102 basic_fbstring<E, T, A, S>&& rhs) {
2104 const auto len = basic_fbstring<E, T, A, S>::traits_type::length(lhs);
2105 if (rhs.capacity() >= len + rhs.size()) {
2106 // Good, at least we don't need to reallocate
2107 rhs.insert(rhs.begin(), lhs, lhs + len);
2110 // Meh, no go. Do it by hand since we have len already.
2111 basic_fbstring<E, T, A, S> result;
2112 result.reserve(len + rhs.size());
2113 result.append(lhs, len).append(rhs);
2118 template <typename E, class T, class A, class S>
2120 basic_fbstring<E, T, A, S> operator+(
2122 const basic_fbstring<E, T, A, S>& rhs) {
2124 basic_fbstring<E, T, A, S> result;
2125 result.reserve(1 + rhs.size());
2126 result.push_back(lhs);
2132 template <typename E, class T, class A, class S>
2134 basic_fbstring<E, T, A, S> operator+(
2136 basic_fbstring<E, T, A, S>&& rhs) {
2138 if (rhs.capacity() > rhs.size()) {
2139 // Good, at least we don't need to reallocate
2140 rhs.insert(rhs.begin(), lhs);
2143 // Meh, no go. Forward to operator+(E, const&).
2144 auto const& rhsC = rhs;
2149 template <typename E, class T, class A, class S>
2151 basic_fbstring<E, T, A, S> operator+(
2152 const basic_fbstring<E, T, A, S>& lhs,
2155 typedef typename basic_fbstring<E, T, A, S>::size_type size_type;
2156 typedef typename basic_fbstring<E, T, A, S>::traits_type traits_type;
2158 basic_fbstring<E, T, A, S> result;
2159 const size_type len = traits_type::length(rhs);
2160 result.reserve(lhs.size() + len);
2161 result.append(lhs).append(rhs, len);
2165 // C++11 21.4.8.1/10
2166 template <typename E, class T, class A, class S>
2168 basic_fbstring<E, T, A, S> operator+(
2169 basic_fbstring<E, T, A, S>&& lhs,
2172 return std::move(lhs += rhs);
2175 // C++11 21.4.8.1/11
2176 template <typename E, class T, class A, class S>
2178 basic_fbstring<E, T, A, S> operator+(
2179 const basic_fbstring<E, T, A, S>& lhs,
2182 basic_fbstring<E, T, A, S> result;
2183 result.reserve(lhs.size() + 1);
2185 result.push_back(rhs);
2189 // C++11 21.4.8.1/12
2190 template <typename E, class T, class A, class S>
2192 basic_fbstring<E, T, A, S> operator+(
2193 basic_fbstring<E, T, A, S>&& lhs,
2196 return std::move(lhs += rhs);
2199 template <typename E, class T, class A, class S>
2201 bool operator==(const basic_fbstring<E, T, A, S>& lhs,
2202 const basic_fbstring<E, T, A, S>& rhs) {
2203 return lhs.size() == rhs.size() && lhs.compare(rhs) == 0; }
2205 template <typename E, class T, class A, class S>
2207 bool operator==(const typename basic_fbstring<E, T, A, S>::value_type* lhs,
2208 const basic_fbstring<E, T, A, S>& rhs) {
2209 return rhs == lhs; }
2211 template <typename E, class T, class A, class S>
2213 bool operator==(const basic_fbstring<E, T, A, S>& lhs,
2214 const typename basic_fbstring<E, T, A, S>::value_type* rhs) {
2215 return lhs.compare(rhs) == 0; }
2217 template <typename E, class T, class A, class S>
2219 bool operator!=(const basic_fbstring<E, T, A, S>& lhs,
2220 const basic_fbstring<E, T, A, S>& rhs) {
2221 return !(lhs == rhs); }
2223 template <typename E, class T, class A, class S>
2225 bool operator!=(const typename basic_fbstring<E, T, A, S>::value_type* lhs,
2226 const basic_fbstring<E, T, A, S>& rhs) {
2227 return !(lhs == rhs); }
2229 template <typename E, class T, class A, class S>
2231 bool operator!=(const basic_fbstring<E, T, A, S>& lhs,
2232 const typename basic_fbstring<E, T, A, S>::value_type* rhs) {
2233 return !(lhs == rhs); }
2235 template <typename E, class T, class A, class S>
2237 bool operator<(const basic_fbstring<E, T, A, S>& lhs,
2238 const basic_fbstring<E, T, A, S>& rhs) {
2239 return lhs.compare(rhs) < 0; }
2241 template <typename E, class T, class A, class S>
2243 bool operator<(const basic_fbstring<E, T, A, S>& lhs,
2244 const typename basic_fbstring<E, T, A, S>::value_type* rhs) {
2245 return lhs.compare(rhs) < 0; }
2247 template <typename E, class T, class A, class S>
2249 bool operator<(const typename basic_fbstring<E, T, A, S>::value_type* lhs,
2250 const basic_fbstring<E, T, A, S>& rhs) {
2251 return rhs.compare(lhs) > 0; }
2253 template <typename E, class T, class A, class S>
2255 bool operator>(const basic_fbstring<E, T, A, S>& lhs,
2256 const basic_fbstring<E, T, A, S>& rhs) {
2259 template <typename E, class T, class A, class S>
2261 bool operator>(const basic_fbstring<E, T, A, S>& lhs,
2262 const typename basic_fbstring<E, T, A, S>::value_type* rhs) {
2265 template <typename E, class T, class A, class S>
2267 bool operator>(const typename basic_fbstring<E, T, A, S>::value_type* lhs,
2268 const basic_fbstring<E, T, A, S>& rhs) {
2271 template <typename E, class T, class A, class S>
2273 bool operator<=(const basic_fbstring<E, T, A, S>& lhs,
2274 const basic_fbstring<E, T, A, S>& rhs) {
2275 return !(rhs < lhs); }
2277 template <typename E, class T, class A, class S>
2279 bool operator<=(const basic_fbstring<E, T, A, S>& lhs,
2280 const typename basic_fbstring<E, T, A, S>::value_type* rhs) {
2281 return !(rhs < lhs); }
2283 template <typename E, class T, class A, class S>
2285 bool operator<=(const typename basic_fbstring<E, T, A, S>::value_type* lhs,
2286 const basic_fbstring<E, T, A, S>& rhs) {
2287 return !(rhs < lhs); }
2289 template <typename E, class T, class A, class S>
2291 bool operator>=(const basic_fbstring<E, T, A, S>& lhs,
2292 const basic_fbstring<E, T, A, S>& rhs) {
2293 return !(lhs < rhs); }
2295 template <typename E, class T, class A, class S>
2297 bool operator>=(const basic_fbstring<E, T, A, S>& lhs,
2298 const typename basic_fbstring<E, T, A, S>::value_type* rhs) {
2299 return !(lhs < rhs); }
2301 template <typename E, class T, class A, class S>
2303 bool operator>=(const typename basic_fbstring<E, T, A, S>::value_type* lhs,
2304 const basic_fbstring<E, T, A, S>& rhs) {
2305 return !(lhs < rhs);
2309 template <typename E, class T, class A, class S>
2310 void swap(basic_fbstring<E, T, A, S>& lhs, basic_fbstring<E, T, A, S>& rhs) {
2314 // TODO: make this faster.
2315 template <typename E, class T, class A, class S>
2318 typename basic_fbstring<E, T, A, S>::value_type,
2319 typename basic_fbstring<E, T, A, S>::traits_type>&
2321 std::basic_istream<typename basic_fbstring<E, T, A, S>::value_type,
2322 typename basic_fbstring<E, T, A, S>::traits_type>& is,
2323 basic_fbstring<E, T, A, S>& str) {
2324 typename std::basic_istream<E, T>::sentry sentry(is);
2325 typedef std::basic_istream<typename basic_fbstring<E, T, A, S>::value_type,
2326 typename basic_fbstring<E, T, A, S>::traits_type>
2328 typedef typename __istream_type::ios_base __ios_base;
2329 size_t extracted = 0;
2330 auto err = __ios_base::goodbit;
2332 auto n = is.width();
2337 for (auto got = is.rdbuf()->sgetc(); extracted != size_t(n); ++extracted) {
2338 if (got == T::eof()) {
2339 err |= __ios_base::eofbit;
2343 if (isspace(got)) break;
2345 got = is.rdbuf()->snextc();
2349 err |= __ios_base::failbit;
2357 template <typename E, class T, class A, class S>
2359 std::basic_ostream<typename basic_fbstring<E, T, A, S>::value_type,
2360 typename basic_fbstring<E, T, A, S>::traits_type>&
2362 std::basic_ostream<typename basic_fbstring<E, T, A, S>::value_type,
2363 typename basic_fbstring<E, T, A, S>::traits_type>& os,
2364 const basic_fbstring<E, T, A, S>& str) {
2366 typename std::basic_ostream<
2367 typename basic_fbstring<E, T, A, S>::value_type,
2368 typename basic_fbstring<E, T, A, S>::traits_type>::sentry __s(os);
2370 typedef std::ostreambuf_iterator<
2371 typename basic_fbstring<E, T, A, S>::value_type,
2372 typename basic_fbstring<E, T, A, S>::traits_type> _Ip;
2373 size_t __len = str.size();
2375 (os.flags() & std::ios_base::adjustfield) == std::ios_base::left;
2376 if (__pad_and_output(_Ip(os),
2378 __left ? str.data() + __len : str.data(),
2381 os.fill()).failed()) {
2382 os.setstate(std::ios_base::badbit | std::ios_base::failbit);
2385 #elif defined(_MSC_VER)
2386 // MSVC doesn't define __ostream_insert
2387 os.write(str.data(), str.size());
2389 std::__ostream_insert(os, str.data(), str.size());
2394 template <typename E1, class T, class A, class S>
2395 const typename basic_fbstring<E1, T, A, S>::size_type
2396 basic_fbstring<E1, T, A, S>::npos =
2397 static_cast<typename basic_fbstring<E1, T, A, S>::size_type>(-1);
2399 #ifndef _LIBSTDCXX_FBSTRING
2400 // basic_string compatibility routines
2402 template <typename E, class T, class A, class S>
2404 bool operator==(const basic_fbstring<E, T, A, S>& lhs,
2405 const std::string& rhs) {
2406 return lhs.compare(0, lhs.size(), rhs.data(), rhs.size()) == 0;
2409 template <typename E, class T, class A, class S>
2411 bool operator==(const std::string& lhs,
2412 const basic_fbstring<E, T, A, S>& rhs) {
2416 template <typename E, class T, class A, class S>
2418 bool operator!=(const basic_fbstring<E, T, A, S>& lhs,
2419 const std::string& rhs) {
2420 return !(lhs == rhs);
2423 template <typename E, class T, class A, class S>
2425 bool operator!=(const std::string& lhs,
2426 const basic_fbstring<E, T, A, S>& rhs) {
2427 return !(lhs == rhs);
2430 #if !defined(_LIBSTDCXX_FBSTRING)
2431 typedef basic_fbstring<char> fbstring;
2434 // fbstring is relocatable
2435 template <class T, class R, class A, class S>
2436 FOLLY_ASSUME_RELOCATABLE(basic_fbstring<T, R, A, S>);
2439 _GLIBCXX_END_NAMESPACE_VERSION
2442 } // namespace folly
2444 #ifndef _LIBSTDCXX_FBSTRING
2446 // Hash functions to make fbstring usable with e.g. hash_map
2448 // Handle interaction with different C++ standard libraries, which
2449 // expect these types to be in different namespaces.
2451 #define FOLLY_FBSTRING_HASH1(T) \
2453 struct hash< ::folly::basic_fbstring<T> > { \
2454 size_t operator()(const ::folly::fbstring& s) const { \
2455 return ::folly::hash::fnv32_buf(s.data(), s.size()); \
2459 // The C++11 standard says that these four are defined
2460 #define FOLLY_FBSTRING_HASH \
2461 FOLLY_FBSTRING_HASH1(char) \
2462 FOLLY_FBSTRING_HASH1(char16_t) \
2463 FOLLY_FBSTRING_HASH1(char32_t) \
2464 FOLLY_FBSTRING_HASH1(wchar_t)
2472 #if FOLLY_HAVE_DEPRECATED_ASSOC
2473 #if defined(_GLIBCXX_SYMVER) && !defined(__BIONIC__)
2474 namespace __gnu_cxx {
2478 } // namespace __gnu_cxx
2479 #endif // _GLIBCXX_SYMVER && !__BIONIC__
2480 #endif // FOLLY_HAVE_DEPRECATED_ASSOC
2482 #undef FOLLY_FBSTRING_HASH
2483 #undef FOLLY_FBSTRING_HASH1
2485 #endif // _LIBSTDCXX_FBSTRING
2487 #pragma GCC diagnostic pop
2489 #undef FBSTRING_DISABLE_SMALL_STRING_OPTIMIZATION
2490 #undef FBSTRING_SANITIZE_ADDRESS
2492 #undef FBSTRING_LIKELY
2493 #undef FBSTRING_UNLIKELY
2495 #ifdef FOLLY_DEFINED_NDEBUG_FOR_FBSTRING
2497 #undef FOLLY_DEFINED_NDEBUG_FOR_FBSTRING
2498 #endif // FOLLY_DEFINED_NDEBUG_FOR_FBSTRING
2500 #endif // FOLLY_BASE_FBSTRING_H_