2 * Copyright 2014 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_
24 fbstring's behavior can be configured via two macro definitions, as
25 follows. Normally, fbstring does not write a '\0' at the end of
26 each string whenever it changes the underlying characters. Instead,
27 it lazily writes the '\0' whenever either c_str() or data()
30 This is standard-compliant behavior and may save costs in some
31 circumstances. However, it may be surprising to some client code
32 because c_str() and data() are const member functions (fbstring
33 uses the "mutable" storage class for its own state).
35 In order to appease client code that expects fbstring to be
36 zero-terminated at all times, if the preprocessor symbol
37 FBSTRING_CONSERVATIVE is defined, fbstring does exactly that,
38 i.e. it goes the extra mile to guarantee a '\0' is always planted
39 at the end of its data.
41 On the contrary, if the desire is to debug faulty client code that
42 unduly assumes the '\0' is present, fbstring plants a '^' (i.e.,
43 emphatically NOT a zero) at the end of each string if
44 FBSTRING_PERVERSE is defined. (Calling c_str() or data() still
45 writes the '\0', of course.)
47 The preprocessor symbols FBSTRING_PERVERSE and
48 FBSTRING_CONSERVATIVE cannot be defined simultaneously. This is
49 enforced during preprocessing.
52 //#define FBSTRING_PERVERSE
53 //#define FBSTRING_CONSERVATIVE
55 #ifdef FBSTRING_PERVERSE
56 #ifdef FBSTRING_CONSERVATIVE
57 #error Cannot define both FBSTRING_PERVERSE and FBSTRING_CONSERVATIVE.
63 #include <type_traits>
66 #include "folly/Portability.h"
68 // libc++ doesn't provide this header, nor does msvc
69 #ifdef FOLLY_HAVE_BITS_CXXCONFIG_H
70 // This file appears in two locations: inside fbcode and in the
71 // libstdc++ source code (when embedding fbstring as std::string).
72 // To aid in this schizophrenic use, two macros are defined in
74 // _LIBSTDCXX_FBSTRING - Set inside libstdc++. This is useful to
75 // gate use inside fbcode v. libstdc++
76 #include <bits/c++config.h>
79 #ifdef _LIBSTDCXX_FBSTRING
81 #pragma GCC system_header
83 // Handle the cases where the fbcode version (folly/Malloc.h) is included
84 // either before or after this inclusion.
85 #ifdef FOLLY_MALLOC_H_
86 #undef FOLLY_MALLOC_H_
87 #include "basic_fbstring_malloc.h"
89 #include "basic_fbstring_malloc.h"
90 #undef FOLLY_MALLOC_H_
93 #else // !_LIBSTDCXX_FBSTRING
99 #include "folly/Traits.h"
100 #include "folly/Malloc.h"
101 #include "folly/Hash.h"
103 #if FOLLY_HAVE_DEPRECATED_ASSOC
104 #ifdef _GLIBCXX_SYMVER
105 #include <ext/hash_set>
106 #include <ext/hash_map>
112 // We defined these here rather than including Likely.h to avoid
113 // redefinition errors when fbstring is imported into libstdc++.
114 #if defined(__GNUC__) && __GNUC__ >= 4
115 #define FBSTRING_LIKELY(x) (__builtin_expect((x), 1))
116 #define FBSTRING_UNLIKELY(x) (__builtin_expect((x), 0))
118 #define FBSTRING_LIKELY(x) (x)
119 #define FBSTRING_UNLIKELY(x) (x)
122 // Ignore shadowing warnings within this file, so includers can use -Wshadow.
123 #pragma GCC diagnostic push
124 #pragma GCC diagnostic ignored "-Wshadow"
126 // FBString cannot use throw when replacing std::string, though it may still
127 // use std::__throw_*
128 #define throw FOLLY_FBSTRING_MAY_NOT_USE_THROW
130 #ifdef _LIBSTDCXX_FBSTRING
131 namespace std _GLIBCXX_VISIBILITY(default) {
132 _GLIBCXX_BEGIN_NAMESPACE_VERSION
137 // Different versions of gcc/clang support different versions of
138 // the address sanitizer attribute. Unfortunately, this attribute
139 // has issues when inlining is used, so disable that as well.
140 #if defined(__clang__)
141 # if __has_feature(address_sanitizer)
142 # if __has_attribute(__no_address_safety_analysis__)
143 # define FBSTRING_DISABLE_ADDRESS_SANITIZER \
144 __attribute__((__no_address_safety_analysis__, __noinline__))
145 # elif __has_attribute(__no_sanitize_address__)
146 # define FBSTRING_DISABLE_ADDRESS_SANITIZER \
147 __attribute__((__no_sanitize_address__, __noinline__))
150 #elif defined (__GNUC__) && \
152 (__GNUC_MINOR__ >= 8) && \
154 # define FBSTRING_DISABLE_ADDRESS_SANITIZER \
155 __attribute__((__no_address_safety_analysis__, __noinline__))
157 #ifndef FBSTRING_DISABLE_ADDRESS_SANITIZER
158 # define FBSTRING_DISABLE_ADDRESS_SANITIZER
161 namespace fbstring_detail {
163 template <class InIt, class OutIt>
166 typename std::iterator_traits<InIt>::difference_type n,
168 for (; n != 0; --n, ++b, ++d) {
174 template <class Pod, class T>
175 inline void pod_fill(Pod* b, Pod* e, T c) {
176 assert(b && e && b <= e);
177 /*static*/ if (sizeof(T) == 1) {
180 auto const ee = b + ((e - b) & ~7u);
181 for (; b != ee; b += 8) {
192 for (; b != e; ++b) {
199 * Lightly structured memcpy, simplifies copying PODs and introduces
200 * some asserts. Unfortunately using this function may cause
201 * measurable overhead (presumably because it adjusts from a begin/end
202 * convention to a pointer/size convention, so it does some extra
203 * arithmetic even though the caller might have done the inverse
204 * adaptation outside).
207 inline void pod_copy(const Pod* b, const Pod* e, Pod* d) {
209 assert(d >= e || d + (e - b) <= b);
210 memcpy(d, b, (e - b) * sizeof(Pod));
214 * Lightly structured memmove, simplifies copying PODs and introduces
218 inline void pod_move(const Pod* b, const Pod* e, Pod* d) {
220 memmove(d, b, (e - b) * sizeof(*b));
223 } // namespace fbstring_detail
226 * Defines a special acquisition method for constructing fbstring
227 * objects. AcquireMallocatedString means that the user passes a
228 * pointer to a malloc-allocated string that the fbstring object will
231 enum class AcquireMallocatedString {};
234 * fbstring_core_model is a mock-up type that defines all required
235 * signatures of a fbstring core. The fbstring class itself uses such
236 * a core object to implement all of the numerous member functions
237 * required by the standard.
239 * If you want to define a new core, copy the definition below and
240 * implement the primitives. Then plug the core into basic_fbstring as
241 * a template argument.
243 template <class Char>
244 class fbstring_core_model {
246 fbstring_core_model();
247 fbstring_core_model(const fbstring_core_model &);
248 ~fbstring_core_model();
249 // Returns a pointer to string's buffer (currently only contiguous
250 // strings are supported). The pointer is guaranteed to be valid
251 // until the next call to a non-const member function.
252 const Char * data() const;
253 // Much like data(), except the string is prepared to support
254 // character-level changes. This call is a signal for
255 // e.g. reference-counted implementation to fork the data. The
256 // pointer is guaranteed to be valid until the next call to a
257 // non-const member function.
258 Char * mutable_data();
259 // Returns a pointer to string's buffer and guarantees that a
260 // readable '\0' lies right after the buffer. The pointer is
261 // guaranteed to be valid until the next call to a non-const member
263 const Char * c_str() const;
264 // Shrinks the string by delta characters. Asserts that delta <=
266 void shrink(size_t delta);
267 // Expands the string by delta characters (i.e. after this call
268 // size() will report the old size() plus delta) but without
269 // initializing the expanded region. Returns a pointer to the memory
270 // to be initialized (the beginning of the expanded portion). The
271 // caller is expected to fill the expanded area appropriately.
272 Char* expand_noinit(size_t delta);
273 // Expands the string by one character and sets the last character
275 void push_back(Char c);
276 // Returns the string's size.
278 // Returns the string's capacity, i.e. maximum size that the string
279 // can grow to without reallocation. Note that for reference counted
280 // strings that's technically a lie - even assigning characters
281 // within the existing size would cause a reallocation.
282 size_t capacity() const;
283 // Returns true if the data underlying the string is actually shared
284 // across multiple strings (in a refcounted fashion).
285 bool isShared() const;
286 // Makes sure that at least minCapacity characters are available for
287 // the string without reallocation. For reference-counted strings,
288 // it should fork the data even if minCapacity < size().
289 void reserve(size_t minCapacity);
292 fbstring_core_model& operator=(const fbstring_core_model &);
297 * gcc-4.7 throws what appears to be some false positive uninitialized
298 * warnings for the members of the MediumLarge struct. So, mute them here.
300 #if defined(__GNUC__) && !defined(__clang__)
301 # pragma GCC diagnostic push
302 # pragma GCC diagnostic ignored "-Wuninitialized"
306 * This is the core of the string. The code should work on 32- and
307 * 64-bit architectures and with any Char size. Porting to big endian
308 * architectures would require some changes.
310 * The storage is selected as follows (assuming we store one-byte
311 * characters on a 64-bit machine): (a) "small" strings between 0 and
312 * 23 chars are stored in-situ without allocation (the rightmost byte
313 * stores the size); (b) "medium" strings from 24 through 254 chars
314 * are stored in malloc-allocated memory that is copied eagerly; (c)
315 * "large" strings of 255 chars and above are stored in a similar
316 * structure as medium arrays, except that the string is
317 * reference-counted and copied lazily. the reference count is
318 * allocated right before the character array.
320 * The discriminator between these three strategies sits in the two
321 * most significant bits of the rightmost char of the storage. If
322 * neither is set, then the string is small (and its length sits in
323 * the lower-order bits of that rightmost character). If the MSb is
324 * set, the string is medium width. If the second MSb is set, then the
327 template <class Char> class fbstring_core {
329 fbstring_core() noexcept {
330 // Only initialize the tag, will set the MSBs (i.e. the small
331 // string size) to zero too
332 ml_.capacity_ = maxSmallSize << (8 * (sizeof(size_t) - sizeof(Char)));
333 // or: setSmallSize(0);
335 assert(category() == isSmall && size() == 0);
338 fbstring_core(const fbstring_core & rhs) {
339 assert(&rhs != this);
340 // Simplest case first: small strings are bitblitted
341 if (rhs.category() == isSmall) {
342 assert(offsetof(MediumLarge, data_) == 0);
343 assert(offsetof(MediumLarge, size_) == sizeof(ml_.data_));
344 assert(offsetof(MediumLarge, capacity_) == 2 * sizeof(ml_.data_));
345 const size_t size = rhs.smallSize();
347 ml_.capacity_ = rhs.ml_.capacity_;
350 // Just write the whole thing, don't look at details. In
351 // particular we need to copy capacity anyway because we want
352 // to set the size (don't forget that the last character,
353 // which stores a short string's length, is shared with the
354 // ml_.capacity field).
357 assert(category() == isSmall && this->size() == rhs.size());
358 } else if (rhs.category() == isLarge) {
359 // Large strings are just refcounted
361 RefCounted::incrementRefs(ml_.data_);
362 assert(category() == isLarge && size() == rhs.size());
364 // Medium strings are copied eagerly. Don't forget to allocate
365 // one extra Char for the null terminator.
366 auto const allocSize =
367 goodMallocSize((1 + rhs.ml_.size_) * sizeof(Char));
368 ml_.data_ = static_cast<Char*>(checkedMalloc(allocSize));
369 fbstring_detail::pod_copy(rhs.ml_.data_,
371 rhs.ml_.data_ + rhs.ml_.size_ + 1,
373 // No need for writeTerminator() here, we copied one extra
374 // element just above.
375 ml_.size_ = rhs.ml_.size_;
376 ml_.capacity_ = (allocSize / sizeof(Char) - 1) | isMedium;
377 assert(category() == isMedium);
379 assert(size() == rhs.size());
380 assert(memcmp(data(), rhs.data(), size() * sizeof(Char)) == 0);
383 fbstring_core(fbstring_core&& goner) noexcept {
384 if (goner.category() == isSmall) {
385 // Just copy, leave the goner in peace
386 new(this) fbstring_core(goner.small_, goner.smallSize());
390 // Clean goner's carcass
391 goner.setSmallSize(0);
395 // NOTE(agallagher): The word-aligned copy path copies bytes which are
396 // outside the range of the string, and makes address sanitizer unhappy,
397 // so just disable it on this function.
398 fbstring_core(const Char *const data, const size_t size)
399 FBSTRING_DISABLE_ADDRESS_SANITIZER {
400 // Simplest case first: small strings are bitblitted
401 if (size <= maxSmallSize) {
402 // Layout is: Char* data_, size_t size_, size_t capacity_
403 /*static_*/assert(sizeof(*this) == sizeof(Char*) + 2 * sizeof(size_t));
404 /*static_*/assert(sizeof(Char*) == sizeof(size_t));
405 // sizeof(size_t) must be a power of 2
406 /*static_*/assert((sizeof(size_t) & (sizeof(size_t) - 1)) == 0);
408 // If data is aligned, use fast word-wise copying. Otherwise,
409 // use conservative memcpy.
410 if (reinterpret_cast<size_t>(data) & (sizeof(size_t) - 1)) {
411 fbstring_detail::pod_copy(data, data + size, small_);
413 // Copy one word (64 bits) at a time
414 const size_t byteSize = size * sizeof(Char);
415 if (byteSize > 2 * sizeof(size_t)) {
417 ml_.capacity_ = reinterpret_cast<const size_t*>(data)[2];
419 ml_.size_ = reinterpret_cast<const size_t*>(data)[1];
421 ml_.data_ = *reinterpret_cast<Char**>(const_cast<Char*>(data));
422 } else if (byteSize > sizeof(size_t)) {
425 } else if (size > 0) {
431 } else if (size <= maxMediumSize) {
432 // Medium strings are allocated normally. Don't forget to
433 // allocate one extra Char for the terminating null.
434 auto const allocSize = goodMallocSize((1 + size) * sizeof(Char));
435 ml_.data_ = static_cast<Char*>(checkedMalloc(allocSize));
436 fbstring_detail::pod_copy(data, data + size, ml_.data_);
438 ml_.capacity_ = (allocSize / sizeof(Char) - 1) | isMedium;
440 // Large strings are allocated differently
441 size_t effectiveCapacity = size;
442 auto const newRC = RefCounted::create(data, & effectiveCapacity);
443 ml_.data_ = newRC->data_;
445 ml_.capacity_ = effectiveCapacity | isLarge;
448 assert(this->size() == size);
449 assert(memcmp(this->data(), data, size * sizeof(Char)) == 0);
452 ~fbstring_core() noexcept {
453 auto const c = category();
461 RefCounted::decrementRefs(ml_.data_);
464 // Snatches a previously mallocated string. The parameter "size"
465 // is the size of the string, and the parameter "allocatedSize"
466 // is the size of the mallocated block. The string must be
467 // \0-terminated, so allocatedSize >= size + 1 and data[size] == '\0'.
469 // So if you want a 2-character string, pass malloc(3) as "data",
470 // pass 2 as "size", and pass 3 as "allocatedSize".
471 fbstring_core(Char * const data,
473 const size_t allocatedSize,
474 AcquireMallocatedString) {
476 assert(allocatedSize >= size + 1);
477 assert(data[size] == '\0');
478 // Use the medium string storage
481 // Don't forget about null terminator
482 ml_.capacity_ = (allocatedSize - 1) | isMedium;
484 // No need for the memory
490 // swap below doesn't test whether &rhs == this (and instead
491 // potentially does extra work) on the premise that the rarity of
492 // that situation actually makes the check more expensive than is
494 void swap(fbstring_core & rhs) {
500 // In C++11 data() and c_str() are 100% equivalent.
501 const Char * data() const {
505 Char * mutable_data() {
506 auto const c = category();
510 assert(c == isMedium || c == isLarge);
511 if (c == isLarge && RefCounted::refs(ml_.data_) > 1) {
513 size_t effectiveCapacity = ml_.capacity();
514 auto const newRC = RefCounted::create(& effectiveCapacity);
515 // If this fails, someone placed the wrong capacity in an
517 assert(effectiveCapacity >= ml_.capacity());
518 fbstring_detail::pod_copy(ml_.data_, ml_.data_ + ml_.size_ + 1,
520 RefCounted::decrementRefs(ml_.data_);
521 ml_.data_ = newRC->data_;
522 // No need to call writeTerminator(), we have + 1 above.
527 const Char * c_str() const {
528 auto const c = category();
529 #ifdef FBSTRING_PERVERSE
531 assert(small_[smallSize()] == TERMINATOR || smallSize() == maxSmallSize
532 || small_[smallSize()] == '\0');
533 small_[smallSize()] = '\0';
536 assert(c == isMedium || c == isLarge);
537 assert(ml_.data_[ml_.size_] == TERMINATOR || ml_.data_[ml_.size_] == '\0');
538 ml_.data_[ml_.size_] = '\0';
539 #elif defined(FBSTRING_CONSERVATIVE)
541 assert(small_[smallSize()] == '\0');
544 assert(c == isMedium || c == isLarge);
545 assert(ml_.data_[ml_.size_] == '\0');
548 small_[smallSize()] = '\0';
551 assert(c == isMedium || c == isLarge);
552 ml_.data_[ml_.size_] = '\0';
557 void shrink(const size_t delta) {
558 if (category() == isSmall) {
559 // Check for underflow
560 assert(delta <= smallSize());
561 setSmallSize(smallSize() - delta);
562 } else if (category() == isMedium || RefCounted::refs(ml_.data_) == 1) {
563 // Medium strings and unique large strings need no special
565 assert(ml_.size_ >= delta);
568 assert(ml_.size_ >= delta);
569 // Shared large string, must make unique. This is because of the
570 // durn terminator must be written, which may trample the shared
573 fbstring_core(ml_.data_, ml_.size_ - delta).swap(*this);
575 // No need to write the terminator.
581 void reserve(size_t minCapacity) {
582 if (category() == isLarge) {
584 if (RefCounted::refs(ml_.data_) > 1) {
585 // We must make it unique regardless; in-place reallocation is
586 // useless if the string is shared. In order to not surprise
587 // people, reserve the new block at current capacity or
588 // more. That way, a string's capacity never shrinks after a
590 minCapacity = std::max(minCapacity, ml_.capacity());
591 auto const newRC = RefCounted::create(& minCapacity);
592 fbstring_detail::pod_copy(ml_.data_, ml_.data_ + ml_.size_ + 1,
594 // Done with the old data. No need to call writeTerminator(),
595 // we have + 1 above.
596 RefCounted::decrementRefs(ml_.data_);
597 ml_.data_ = newRC->data_;
598 ml_.capacity_ = minCapacity | isLarge;
599 // size remains unchanged
601 // String is not shared, so let's try to realloc (if needed)
602 if (minCapacity > ml_.capacity()) {
603 // Asking for more memory
605 RefCounted::reallocate(ml_.data_, ml_.size_,
606 ml_.capacity(), minCapacity);
607 ml_.data_ = newRC->data_;
608 ml_.capacity_ = minCapacity | isLarge;
611 assert(capacity() >= minCapacity);
613 } else if (category() == isMedium) {
614 // String is not shared
615 if (minCapacity <= ml_.capacity()) {
616 return; // nothing to do, there's enough room
618 if (minCapacity <= maxMediumSize) {
619 // Keep the string at medium size. Don't forget to allocate
620 // one extra Char for the terminating null.
621 size_t capacityBytes = goodMallocSize((1 + minCapacity) * sizeof(Char));
622 ml_.data_ = static_cast<Char *>(
625 ml_.size_ * sizeof(Char),
626 (ml_.capacity() + 1) * sizeof(Char),
629 ml_.capacity_ = (capacityBytes / sizeof(Char) - 1) | isMedium;
631 // Conversion from medium to large string
632 fbstring_core nascent;
633 // Will recurse to another branch of this function
634 nascent.reserve(minCapacity);
635 nascent.ml_.size_ = ml_.size_;
636 fbstring_detail::pod_copy(ml_.data_, ml_.data_ + ml_.size_,
640 assert(capacity() >= minCapacity);
643 assert(category() == isSmall);
644 if (minCapacity > maxMediumSize) {
646 auto const newRC = RefCounted::create(& minCapacity);
647 auto const size = smallSize();
648 fbstring_detail::pod_copy(small_, small_ + size + 1, newRC->data_);
649 // No need for writeTerminator(), we wrote it above with + 1.
650 ml_.data_ = newRC->data_;
652 ml_.capacity_ = minCapacity | isLarge;
653 assert(capacity() >= minCapacity);
654 } else if (minCapacity > maxSmallSize) {
656 // Don't forget to allocate one extra Char for the terminating null
657 auto const allocSizeBytes =
658 goodMallocSize((1 + minCapacity) * sizeof(Char));
659 auto const data = static_cast<Char*>(checkedMalloc(allocSizeBytes));
660 auto const size = smallSize();
661 fbstring_detail::pod_copy(small_, small_ + size + 1, data);
662 // No need for writeTerminator(), we wrote it above with + 1.
665 ml_.capacity_ = (allocSizeBytes / sizeof(Char) - 1) | isMedium;
668 // Nothing to do, everything stays put
671 assert(capacity() >= minCapacity);
674 Char * expand_noinit(const size_t delta) {
675 // Strategy is simple: make room, then change size
676 assert(capacity() >= size());
678 if (category() == isSmall) {
681 if (newSz <= maxSmallSize) {
689 newSz = ml_.size_ + delta;
690 if (newSz > capacity()) {
694 assert(capacity() >= newSz);
695 // Category can't be small - we took care of that above
696 assert(category() == isMedium || category() == isLarge);
699 assert(size() == newSz);
700 return ml_.data_ + sz;
703 void push_back(Char c) {
704 assert(capacity() >= size());
706 if (category() == isSmall) {
708 if (sz < maxSmallSize) {
709 setSmallSize(sz + 1);
714 reserve(maxSmallSize * 2);
717 if (sz == capacity()) { // always true for isShared()
718 reserve(1 + sz * 3 / 2); // ensures not shared
722 assert(capacity() >= sz + 1);
723 // Category can't be small - we took care of that above
724 assert(category() == isMedium || category() == isLarge);
730 size_t size() const {
731 return category() == isSmall ? smallSize() : ml_.size_;
734 size_t capacity() const {
735 switch (category()) {
739 // For large-sized strings, a multi-referenced chunk has no
740 // available capacity. This is because any attempt to append
741 // data would trigger a new allocation.
742 if (RefCounted::refs(ml_.data_) > 1) return ml_.size_;
745 return ml_.capacity();
748 bool isShared() const {
749 return category() == isLarge && RefCounted::refs(ml_.data_) > 1;
752 #ifdef FBSTRING_PERVERSE
753 enum { TERMINATOR = '^' };
755 enum { TERMINATOR = '\0' };
758 void writeTerminator() {
759 #if defined(FBSTRING_PERVERSE) || defined(FBSTRING_CONSERVATIVE)
760 if (category() == isSmall) {
761 const auto s = smallSize();
762 if (s != maxSmallSize) {
763 small_[s] = TERMINATOR;
766 ml_.data_[ml_.size_] = TERMINATOR;
773 fbstring_core & operator=(const fbstring_core & rhs);
780 size_t capacity() const {
781 return capacity_ & capacityExtractMask;
786 std::atomic<size_t> refCount_;
789 static RefCounted * fromData(Char * p) {
790 return static_cast<RefCounted*>(
792 static_cast<unsigned char*>(static_cast<void*>(p))
793 - sizeof(refCount_)));
796 static size_t refs(Char * p) {
797 return fromData(p)->refCount_.load(std::memory_order_acquire);
800 static void incrementRefs(Char * p) {
801 fromData(p)->refCount_.fetch_add(1, std::memory_order_acq_rel);
804 static void decrementRefs(Char * p) {
805 auto const dis = fromData(p);
806 size_t oldcnt = dis->refCount_.fetch_sub(1, std::memory_order_acq_rel);
813 static RefCounted * create(size_t * size) {
814 // Don't forget to allocate one extra Char for the terminating
815 // null. In this case, however, one Char is already part of the
817 const size_t allocSize = goodMallocSize(
818 sizeof(RefCounted) + *size * sizeof(Char));
819 auto result = static_cast<RefCounted*>(checkedMalloc(allocSize));
820 result->refCount_.store(1, std::memory_order_release);
821 *size = (allocSize - sizeof(RefCounted)) / sizeof(Char);
825 static RefCounted * create(const Char * data, size_t * size) {
826 const size_t effectiveSize = *size;
827 auto result = create(size);
828 fbstring_detail::pod_copy(data, data + effectiveSize, result->data_);
832 static RefCounted * reallocate(Char *const data,
833 const size_t currentSize,
834 const size_t currentCapacity,
835 const size_t newCapacity) {
836 assert(newCapacity > 0 && newCapacity > currentSize);
837 auto const dis = fromData(data);
838 assert(dis->refCount_.load(std::memory_order_acquire) == 1);
839 // Don't forget to allocate one extra Char for the terminating
840 // null. In this case, however, one Char is already part of the
842 auto result = static_cast<RefCounted*>(
844 sizeof(RefCounted) + currentSize * sizeof(Char),
845 sizeof(RefCounted) + currentCapacity * sizeof(Char),
846 sizeof(RefCounted) + newCapacity * sizeof(Char)));
847 assert(result->refCount_.load(std::memory_order_acquire) == 1);
853 mutable Char small_[sizeof(MediumLarge) / sizeof(Char)];
854 mutable MediumLarge ml_;
858 lastChar = sizeof(MediumLarge) - 1,
859 maxSmallSize = lastChar / sizeof(Char),
860 maxMediumSize = 254 / sizeof(Char), // coincides with the small
861 // bin size in dlmalloc
862 categoryExtractMask = sizeof(size_t) == 4 ? 0xC0000000 : 0xC000000000000000,
863 capacityExtractMask = ~categoryExtractMask,
865 static_assert(!(sizeof(MediumLarge) % sizeof(Char)),
866 "Corrupt memory layout for fbstring.");
870 isMedium = sizeof(size_t) == 4 ? 0x80000000 : 0x8000000000000000,
871 isLarge = sizeof(size_t) == 4 ? 0x40000000 : 0x4000000000000000,
874 Category category() const {
875 // Assumes little endian
876 return static_cast<Category>(ml_.capacity_ & categoryExtractMask);
879 size_t smallSize() const {
880 assert(category() == isSmall &&
881 static_cast<size_t>(small_[maxSmallSize])
882 <= static_cast<size_t>(maxSmallSize));
883 return static_cast<size_t>(maxSmallSize)
884 - static_cast<size_t>(small_[maxSmallSize]);
887 void setSmallSize(size_t s) {
888 // Warning: this should work with uninitialized strings too,
889 // so don't assume anything about the previous value of
890 // small_[maxSmallSize].
891 assert(s <= maxSmallSize);
892 small_[maxSmallSize] = maxSmallSize - s;
896 #if defined(__GNUC__) && !defined(__clang__)
897 # pragma GCC diagnostic pop
900 #ifndef _LIBSTDCXX_FBSTRING
902 * Dummy fbstring core that uses an actual std::string. This doesn't
903 * make any sense - it's just for testing purposes.
905 template <class Char>
906 class dummy_fbstring_core {
908 dummy_fbstring_core() {
910 dummy_fbstring_core(const dummy_fbstring_core& another)
911 : backend_(another.backend_) {
913 dummy_fbstring_core(const Char * s, size_t n)
916 void swap(dummy_fbstring_core & rhs) {
917 backend_.swap(rhs.backend_);
919 const Char * data() const {
920 return backend_.data();
922 Char * mutable_data() {
923 //assert(!backend_.empty());
924 return &*backend_.begin();
926 void shrink(size_t delta) {
927 assert(delta <= size());
928 backend_.resize(size() - delta);
930 Char * expand_noinit(size_t delta) {
931 auto const sz = size();
932 backend_.resize(size() + delta);
933 return backend_.data() + sz;
935 void push_back(Char c) {
936 backend_.push_back(c);
938 size_t size() const {
939 return backend_.size();
941 size_t capacity() const {
942 return backend_.capacity();
944 bool isShared() const {
947 void reserve(size_t minCapacity) {
948 backend_.reserve(minCapacity);
952 std::basic_string<Char> backend_;
954 #endif // !_LIBSTDCXX_FBSTRING
957 * This is the basic_string replacement. For conformity,
958 * basic_fbstring takes the same template parameters, plus the last
959 * one which is the core.
961 #ifdef _LIBSTDCXX_FBSTRING
962 template <typename E, class T, class A, class Storage>
964 template <typename E,
965 class T = std::char_traits<E>,
966 class A = std::allocator<E>,
967 class Storage = fbstring_core<E> >
969 class basic_fbstring {
973 void (*throw_exc)(const char*),
975 if (!condition) throw_exc(msg);
978 bool isSane() const {
981 empty() == (size() == 0) &&
982 empty() == (begin() == end()) &&
983 size() <= max_size() &&
984 capacity() <= max_size() &&
985 size() <= capacity() &&
986 (begin()[size()] == Storage::TERMINATOR || begin()[size()] == '\0');
990 friend struct Invariant;
993 explicit Invariant(const basic_fbstring& s) : s_(s) {
1000 const basic_fbstring& s_;
1002 explicit Invariant(const basic_fbstring&) {}
1004 Invariant& operator=(const Invariant&);
1009 typedef T traits_type;
1010 typedef typename traits_type::char_type value_type;
1011 typedef A allocator_type;
1012 typedef typename A::size_type size_type;
1013 typedef typename A::difference_type difference_type;
1015 typedef typename A::reference reference;
1016 typedef typename A::const_reference const_reference;
1017 typedef typename A::pointer pointer;
1018 typedef typename A::const_pointer const_pointer;
1020 typedef E* iterator;
1021 typedef const E* const_iterator;
1022 typedef std::reverse_iterator<iterator
1023 #ifdef NO_ITERATOR_TRAITS
1027 typedef std::reverse_iterator<const_iterator
1028 #ifdef NO_ITERATOR_TRAITS
1031 > const_reverse_iterator;
1033 static const size_type npos; // = size_type(-1)
1036 static void procrustes(size_type& n, size_type nmax) {
1037 if (n > nmax) n = nmax;
1041 // C++11 21.4.2 construct/copy/destroy
1042 explicit basic_fbstring(const A& a = A()) noexcept {
1045 basic_fbstring(const basic_fbstring& str)
1046 : store_(str.store_) {
1050 basic_fbstring(basic_fbstring&& goner) noexcept
1051 : store_(std::move(goner.store_)) {
1054 #ifndef _LIBSTDCXX_FBSTRING
1055 // This is defined for compatibility with std::string
1056 /* implicit */ basic_fbstring(const std::string& str)
1057 : store_(str.data(), str.size()) {
1061 basic_fbstring(const basic_fbstring& str, size_type pos,
1062 size_type n = npos, const A& a = A()) {
1063 assign(str, pos, n);
1066 /* implicit */ basic_fbstring(const value_type* s, const A& a = A())
1068 ? traits_type::length(s)
1070 std::__throw_logic_error(
1071 "basic_fbstring: null pointer initializer not valid");
1076 basic_fbstring(const value_type* s, size_type n, const A& a = A())
1080 basic_fbstring(size_type n, value_type c, const A& a = A()) {
1081 auto const data = store_.expand_noinit(n);
1082 fbstring_detail::pod_fill(data, data + n, c);
1083 store_.writeTerminator();
1086 template <class InIt>
1087 basic_fbstring(InIt begin, InIt end,
1088 typename std::enable_if<
1089 !std::is_same<typename std::remove_const<InIt>::type,
1090 value_type*>::value, const A>::type & a = A()) {
1094 // Specialization for const char*, const char*
1095 basic_fbstring(const value_type* b, const value_type* e)
1096 : store_(b, e - b) {
1099 // Nonstandard constructor
1100 basic_fbstring(value_type *s, size_type n, size_type c,
1101 AcquireMallocatedString a)
1102 : store_(s, n, c, a) {
1105 // Construction from initialization list
1106 basic_fbstring(std::initializer_list<value_type> il) {
1107 assign(il.begin(), il.end());
1110 ~basic_fbstring() noexcept {
1113 basic_fbstring& operator=(const basic_fbstring& lhs) {
1114 if (FBSTRING_UNLIKELY(&lhs == this)) {
1117 auto const oldSize = size();
1118 auto const srcSize = lhs.size();
1119 if (capacity() >= srcSize && !store_.isShared()) {
1120 // great, just copy the contents
1121 if (oldSize < srcSize)
1122 store_.expand_noinit(srcSize - oldSize);
1124 store_.shrink(oldSize - srcSize);
1125 assert(size() == srcSize);
1126 fbstring_detail::pod_copy(lhs.begin(), lhs.end(), begin());
1127 store_.writeTerminator();
1129 // need to reallocate, so we may as well create a brand new string
1130 basic_fbstring(lhs).swap(*this);
1136 basic_fbstring& operator=(basic_fbstring&& goner) noexcept {
1137 if (FBSTRING_UNLIKELY(&goner == this)) {
1138 // Compatibility with std::basic_string<>,
1139 // C++11 21.4.2 [string.cons] / 23 requires self-move-assignment support.
1142 // No need of this anymore
1143 this->~basic_fbstring();
1144 // Move the goner into this
1145 new(&store_) fbstring_core<E>(std::move(goner.store_));
1149 #ifndef _LIBSTDCXX_FBSTRING
1150 // Compatibility with std::string
1151 basic_fbstring & operator=(const std::string & rhs) {
1152 return assign(rhs.data(), rhs.size());
1155 // Compatibility with std::string
1156 std::string toStdString() const {
1157 return std::string(data(), size());
1160 // A lot of code in fbcode still uses this method, so keep it here for now.
1161 const basic_fbstring& toStdString() const {
1166 basic_fbstring& operator=(const value_type* s) {
1170 basic_fbstring& operator=(value_type c) {
1172 store_.expand_noinit(1);
1173 } else if (store_.isShared()) {
1174 basic_fbstring(1, c).swap(*this);
1177 store_.shrink(size() - 1);
1179 *store_.mutable_data() = c;
1180 store_.writeTerminator();
1184 basic_fbstring& operator=(std::initializer_list<value_type> il) {
1185 return assign(il.begin(), il.end());
1188 // C++11 21.4.3 iterators:
1189 iterator begin() { return store_.mutable_data(); }
1191 const_iterator begin() const { return store_.data(); }
1193 const_iterator cbegin() const { return begin(); }
1196 return store_.mutable_data() + store_.size();
1199 const_iterator end() const {
1200 return store_.data() + store_.size();
1203 const_iterator cend() const { return end(); }
1205 reverse_iterator rbegin() {
1206 return reverse_iterator(end());
1209 const_reverse_iterator rbegin() const {
1210 return const_reverse_iterator(end());
1213 const_reverse_iterator crbegin() const { return rbegin(); }
1215 reverse_iterator rend() {
1216 return reverse_iterator(begin());
1219 const_reverse_iterator rend() const {
1220 return const_reverse_iterator(begin());
1223 const_reverse_iterator crend() const { return rend(); }
1226 // C++11 21.4.5, element access:
1227 const value_type& front() const { return *begin(); }
1228 const value_type& back() const {
1230 // Should be begin()[size() - 1], but that branches twice
1231 return *(end() - 1);
1233 value_type& front() { return *begin(); }
1234 value_type& back() {
1236 // Should be begin()[size() - 1], but that branches twice
1237 return *(end() - 1);
1244 // C++11 21.4.4 capacity:
1245 size_type size() const { return store_.size(); }
1247 size_type length() const { return size(); }
1249 size_type max_size() const {
1250 return std::numeric_limits<size_type>::max();
1253 void resize(const size_type n, const value_type c = value_type()) {
1254 auto size = this->size();
1256 store_.shrink(size - n);
1258 // Do this in two steps to minimize slack memory copied (see
1260 auto const capacity = this->capacity();
1261 assert(capacity >= size);
1262 if (size < capacity) {
1263 auto delta = std::min(n, capacity) - size;
1264 store_.expand_noinit(delta);
1265 fbstring_detail::pod_fill(begin() + size, end(), c);
1268 store_.writeTerminator();
1273 auto const delta = n - size;
1274 store_.expand_noinit(delta);
1275 fbstring_detail::pod_fill(end() - delta, end(), c);
1276 store_.writeTerminator();
1278 assert(this->size() == n);
1281 size_type capacity() const { return store_.capacity(); }
1283 void reserve(size_type res_arg = 0) {
1284 enforce(res_arg <= max_size(), std::__throw_length_error, "");
1285 store_.reserve(res_arg);
1288 void shrink_to_fit() {
1289 // Shrink only if slack memory is sufficiently large
1290 if (capacity() < size() * 3 / 2) {
1293 basic_fbstring(cbegin(), cend()).swap(*this);
1296 void clear() { resize(0); }
1298 bool empty() const { return size() == 0; }
1300 // C++11 21.4.5 element access:
1301 const_reference operator[](size_type pos) const {
1302 return *(c_str() + pos);
1305 reference operator[](size_type pos) {
1306 if (pos == size()) {
1307 // Just call c_str() to make sure '\0' is present
1310 return *(begin() + pos);
1313 const_reference at(size_type n) const {
1314 enforce(n <= size(), std::__throw_out_of_range, "");
1318 reference at(size_type n) {
1319 enforce(n < size(), std::__throw_out_of_range, "");
1323 // C++11 21.4.6 modifiers:
1324 basic_fbstring& operator+=(const basic_fbstring& str) {
1328 basic_fbstring& operator+=(const value_type* s) {
1332 basic_fbstring& operator+=(const value_type c) {
1337 basic_fbstring& operator+=(std::initializer_list<value_type> il) {
1342 basic_fbstring& append(const basic_fbstring& str) {
1344 auto desiredSize = size() + str.size();
1346 append(str.data(), str.size());
1347 assert(size() == desiredSize);
1351 basic_fbstring& append(const basic_fbstring& str, const size_type pos,
1353 const size_type sz = str.size();
1354 enforce(pos <= sz, std::__throw_out_of_range, "");
1355 procrustes(n, sz - pos);
1356 return append(str.data() + pos, n);
1359 basic_fbstring& append(const value_type* s, size_type n) {
1361 Invariant checker(*this);
1364 if (FBSTRING_UNLIKELY(!n)) {
1365 // Unlikely but must be done
1368 auto const oldSize = size();
1369 auto const oldData = data();
1370 // Check for aliasing (rare). We could use "<=" here but in theory
1371 // those do not work for pointers unless the pointers point to
1372 // elements in the same array. For that reason we use
1373 // std::less_equal, which is guaranteed to offer a total order
1374 // over pointers. See discussion at http://goo.gl/Cy2ya for more
1376 std::less_equal<const value_type*> le;
1377 if (FBSTRING_UNLIKELY(le(oldData, s) && !le(oldData + oldSize, s))) {
1378 assert(le(s + n, oldData + oldSize));
1379 const size_type offset = s - oldData;
1380 store_.reserve(oldSize + n);
1381 // Restore the source
1382 s = data() + offset;
1384 // Warning! Repeated appends with short strings may actually incur
1385 // practically quadratic performance. Avoid that by pushing back
1386 // the first character (which ensures exponential growth) and then
1387 // appending the rest normally. Worst case the append may incur a
1388 // second allocation but that will be rare.
1391 memcpy(store_.expand_noinit(n), s, n * sizeof(value_type));
1392 assert(size() == oldSize + n + 1);
1396 basic_fbstring& append(const value_type* s) {
1397 return append(s, traits_type::length(s));
1400 basic_fbstring& append(size_type n, value_type c) {
1401 resize(size() + n, c);
1405 template<class InputIterator>
1406 basic_fbstring& append(InputIterator first, InputIterator last) {
1407 insert(end(), first, last);
1411 basic_fbstring& append(std::initializer_list<value_type> il) {
1412 return append(il.begin(), il.end());
1415 void push_back(const value_type c) { // primitive
1416 store_.push_back(c);
1419 basic_fbstring& assign(const basic_fbstring& str) {
1420 if (&str == this) return *this;
1421 return assign(str.data(), str.size());
1424 basic_fbstring& assign(basic_fbstring&& str) {
1425 return *this = std::move(str);
1428 basic_fbstring& assign(const basic_fbstring& str, const size_type pos,
1430 const size_type sz = str.size();
1431 enforce(pos <= sz, std::__throw_out_of_range, "");
1432 procrustes(n, sz - pos);
1433 return assign(str.data() + pos, n);
1436 basic_fbstring& assign(const value_type* s, const size_type n) {
1437 Invariant checker(*this);
1440 std::copy(s, s + n, begin());
1442 assert(size() == n);
1444 const value_type *const s2 = s + size();
1445 std::copy(s, s2, begin());
1446 append(s2, n - size());
1447 assert(size() == n);
1449 store_.writeTerminator();
1450 assert(size() == n);
1454 basic_fbstring& assign(const value_type* s) {
1455 return assign(s, traits_type::length(s));
1458 basic_fbstring& assign(std::initializer_list<value_type> il) {
1459 return assign(il.begin(), il.end());
1462 template <class ItOrLength, class ItOrChar>
1463 basic_fbstring& assign(ItOrLength first_or_n, ItOrChar last_or_c) {
1464 return replace(begin(), end(), first_or_n, last_or_c);
1467 basic_fbstring& insert(size_type pos1, const basic_fbstring& str) {
1468 return insert(pos1, str.data(), str.size());
1471 basic_fbstring& insert(size_type pos1, const basic_fbstring& str,
1472 size_type pos2, size_type n) {
1473 enforce(pos2 <= str.length(), std::__throw_out_of_range, "");
1474 procrustes(n, str.length() - pos2);
1475 return insert(pos1, str.data() + pos2, n);
1478 basic_fbstring& insert(size_type pos, const value_type* s, size_type n) {
1479 enforce(pos <= length(), std::__throw_out_of_range, "");
1480 insert(begin() + pos, s, s + n);
1484 basic_fbstring& insert(size_type pos, const value_type* s) {
1485 return insert(pos, s, traits_type::length(s));
1488 basic_fbstring& insert(size_type pos, size_type n, value_type c) {
1489 enforce(pos <= length(), std::__throw_out_of_range, "");
1490 insert(begin() + pos, n, c);
1494 iterator insert(const_iterator p, const value_type c) {
1495 const size_type pos = p - begin();
1497 return begin() + pos;
1501 template <int i> class Selector {};
1503 iterator insertImplDiscr(const_iterator p,
1504 size_type n, value_type c, Selector<1>) {
1505 Invariant checker(*this);
1507 auto const pos = p - begin();
1508 assert(p >= begin() && p <= end());
1509 if (capacity() - size() < n) {
1510 const size_type sz = p - begin();
1511 reserve(size() + n);
1514 const iterator oldEnd = end();
1515 if (n < size_type(oldEnd - p)) {
1516 append(oldEnd - n, oldEnd);
1518 // reverse_iterator(oldEnd - n),
1519 // reverse_iterator(p),
1520 // reverse_iterator(oldEnd));
1521 fbstring_detail::pod_move(&*p, &*oldEnd - n,
1523 std::fill(begin() + pos, begin() + pos + n, c);
1525 append(n - (end() - p), c);
1526 append(iterator(p), oldEnd);
1527 std::fill(iterator(p), oldEnd, c);
1529 store_.writeTerminator();
1530 return begin() + pos;
1533 template<class InputIter>
1534 iterator insertImplDiscr(const_iterator i,
1535 InputIter b, InputIter e, Selector<0>) {
1536 return insertImpl(i, b, e,
1537 typename std::iterator_traits<InputIter>::iterator_category());
1540 template <class FwdIterator>
1541 iterator insertImpl(const_iterator i,
1542 FwdIterator s1, FwdIterator s2, std::forward_iterator_tag) {
1543 Invariant checker(*this);
1545 const size_type pos = i - begin();
1546 const typename std::iterator_traits<FwdIterator>::difference_type n2 =
1547 std::distance(s1, s2);
1549 using namespace fbstring_detail;
1550 assert(pos <= size());
1552 const typename std::iterator_traits<FwdIterator>::difference_type maxn2 =
1553 capacity() - size();
1555 // realloc the string
1556 reserve(size() + n2);
1559 if (pos + n2 <= size()) {
1560 const iterator tailBegin = end() - n2;
1561 store_.expand_noinit(n2);
1562 fbstring_detail::pod_copy(tailBegin, tailBegin + n2, end() - n2);
1563 std::copy(const_reverse_iterator(tailBegin), const_reverse_iterator(i),
1564 reverse_iterator(tailBegin + n2));
1565 std::copy(s1, s2, begin() + pos);
1568 const size_type old_size = size();
1569 std::advance(t, old_size - pos);
1570 const size_t newElems = std::distance(t, s2);
1571 store_.expand_noinit(n2);
1572 std::copy(t, s2, begin() + old_size);
1573 fbstring_detail::pod_copy(data() + pos, data() + old_size,
1574 begin() + old_size + newElems);
1575 std::copy(s1, t, begin() + pos);
1577 store_.writeTerminator();
1578 return begin() + pos;
1581 template <class InputIterator>
1582 iterator insertImpl(const_iterator i,
1583 InputIterator b, InputIterator e,
1584 std::input_iterator_tag) {
1585 const auto pos = i - begin();
1586 basic_fbstring temp(begin(), i);
1587 for (; b != e; ++b) {
1590 temp.append(i, cend());
1592 return begin() + pos;
1596 template <class ItOrLength, class ItOrChar>
1597 iterator insert(const_iterator p, ItOrLength first_or_n, ItOrChar last_or_c) {
1598 Selector<std::numeric_limits<ItOrLength>::is_specialized> sel;
1599 return insertImplDiscr(p, first_or_n, last_or_c, sel);
1602 iterator insert(const_iterator p, std::initializer_list<value_type> il) {
1603 return insert(p, il.begin(), il.end());
1606 basic_fbstring& erase(size_type pos = 0, size_type n = npos) {
1607 Invariant checker(*this);
1609 enforce(pos <= length(), std::__throw_out_of_range, "");
1610 procrustes(n, length() - pos);
1611 std::copy(begin() + pos + n, end(), begin() + pos);
1612 resize(length() - n);
1616 iterator erase(iterator position) {
1617 const size_type pos(position - begin());
1618 enforce(pos <= size(), std::__throw_out_of_range, "");
1620 return begin() + pos;
1623 iterator erase(iterator first, iterator last) {
1624 const size_type pos(first - begin());
1625 erase(pos, last - first);
1626 return begin() + pos;
1629 // Replaces at most n1 chars of *this, starting with pos1 with the
1631 basic_fbstring& replace(size_type pos1, size_type n1,
1632 const basic_fbstring& str) {
1633 return replace(pos1, n1, str.data(), str.size());
1636 // Replaces at most n1 chars of *this, starting with pos1,
1637 // with at most n2 chars of str starting with pos2
1638 basic_fbstring& replace(size_type pos1, size_type n1,
1639 const basic_fbstring& str,
1640 size_type pos2, size_type n2) {
1641 enforce(pos2 <= str.length(), std::__throw_out_of_range, "");
1642 return replace(pos1, n1, str.data() + pos2,
1643 std::min(n2, str.size() - pos2));
1646 // Replaces at most n1 chars of *this, starting with pos, with chars from s
1647 basic_fbstring& replace(size_type pos, size_type n1, const value_type* s) {
1648 return replace(pos, n1, s, traits_type::length(s));
1651 // Replaces at most n1 chars of *this, starting with pos, with n2
1654 // consolidated with
1656 // Replaces at most n1 chars of *this, starting with pos, with at
1657 // most n2 chars of str. str must have at least n2 chars.
1658 template <class StrOrLength, class NumOrChar>
1659 basic_fbstring& replace(size_type pos, size_type n1,
1660 StrOrLength s_or_n2, NumOrChar n_or_c) {
1661 Invariant checker(*this);
1663 enforce(pos <= size(), std::__throw_out_of_range, "");
1664 procrustes(n1, length() - pos);
1665 const iterator b = begin() + pos;
1666 return replace(b, b + n1, s_or_n2, n_or_c);
1669 basic_fbstring& replace(iterator i1, iterator i2, const basic_fbstring& str) {
1670 return replace(i1, i2, str.data(), str.length());
1673 basic_fbstring& replace(iterator i1, iterator i2, const value_type* s) {
1674 return replace(i1, i2, s, traits_type::length(s));
1678 basic_fbstring& replaceImplDiscr(iterator i1, iterator i2,
1679 const value_type* s, size_type n,
1682 assert(begin() <= i1 && i1 <= end());
1683 assert(begin() <= i2 && i2 <= end());
1684 return replace(i1, i2, s, s + n);
1687 basic_fbstring& replaceImplDiscr(iterator i1, iterator i2,
1688 size_type n2, value_type c, Selector<1>) {
1689 const size_type n1 = i2 - i1;
1691 std::fill(i1, i1 + n2, c);
1694 std::fill(i1, i2, c);
1695 insert(i2, n2 - n1, c);
1701 template <class InputIter>
1702 basic_fbstring& replaceImplDiscr(iterator i1, iterator i2,
1703 InputIter b, InputIter e,
1705 replaceImpl(i1, i2, b, e,
1706 typename std::iterator_traits<InputIter>::iterator_category());
1711 template <class FwdIterator>
1712 bool replaceAliased(iterator i1, iterator i2,
1713 FwdIterator s1, FwdIterator s2, std::false_type) {
1717 template <class FwdIterator>
1718 bool replaceAliased(iterator i1, iterator i2,
1719 FwdIterator s1, FwdIterator s2, std::true_type) {
1720 static const std::less_equal<const value_type*> le =
1721 std::less_equal<const value_type*>();
1722 const bool aliased = le(&*begin(), &*s1) && le(&*s1, &*end());
1726 // Aliased replace, copy to new string
1727 basic_fbstring temp;
1728 temp.reserve(size() - (i2 - i1) + std::distance(s1, s2));
1729 temp.append(begin(), i1).append(s1, s2).append(i2, end());
1734 template <class FwdIterator>
1735 void replaceImpl(iterator i1, iterator i2,
1736 FwdIterator s1, FwdIterator s2, std::forward_iterator_tag) {
1737 Invariant checker(*this);
1740 // Handle aliased replace
1741 if (replaceAliased(i1, i2, s1, s2,
1742 std::integral_constant<bool,
1743 std::is_same<FwdIterator, iterator>::value ||
1744 std::is_same<FwdIterator, const_iterator>::value>())) {
1748 auto const n1 = i2 - i1;
1750 auto const n2 = std::distance(s1, s2);
1755 std::copy(s1, s2, i1);
1759 fbstring_detail::copy_n(s1, n1, i1);
1760 std::advance(s1, n1);
1766 template <class InputIterator>
1767 void replaceImpl(iterator i1, iterator i2,
1768 InputIterator b, InputIterator e, std::input_iterator_tag) {
1769 basic_fbstring temp(begin(), i1);
1770 temp.append(b, e).append(i2, end());
1775 template <class T1, class T2>
1776 basic_fbstring& replace(iterator i1, iterator i2,
1777 T1 first_or_n_or_s, T2 last_or_c_or_n) {
1779 num1 = std::numeric_limits<T1>::is_specialized,
1780 num2 = std::numeric_limits<T2>::is_specialized;
1781 return replaceImplDiscr(
1782 i1, i2, first_or_n_or_s, last_or_c_or_n,
1783 Selector<num1 ? (num2 ? 1 : -1) : (num2 ? 2 : 0)>());
1786 size_type copy(value_type* s, size_type n, size_type pos = 0) const {
1787 enforce(pos <= size(), std::__throw_out_of_range, "");
1788 procrustes(n, size() - pos);
1790 fbstring_detail::pod_copy(
1797 void swap(basic_fbstring& rhs) {
1798 store_.swap(rhs.store_);
1801 const value_type* c_str() const {
1802 return store_.c_str();
1805 const value_type* data() const { return c_str(); }
1807 allocator_type get_allocator() const {
1808 return allocator_type();
1811 size_type find(const basic_fbstring& str, size_type pos = 0) const {
1812 return find(str.data(), pos, str.length());
1815 size_type find(const value_type* needle, const size_type pos,
1816 const size_type nsize) const {
1817 if (!nsize) return pos;
1818 auto const size = this->size();
1819 // nsize + pos can overflow (eg pos == npos), guard against that by checking
1820 // that nsize + pos does not wrap around.
1821 if (nsize + pos > size || nsize + pos < pos) return npos;
1822 // Don't use std::search, use a Boyer-Moore-like trick by comparing
1823 // the last characters first
1824 auto const haystack = data();
1825 auto const nsize_1 = nsize - 1;
1826 auto const lastNeedle = needle[nsize_1];
1828 // Boyer-Moore skip value for the last char in the needle. Zero is
1829 // not a valid value; skip will be computed the first time it's
1833 const E * i = haystack + pos;
1834 auto iEnd = haystack + size - nsize_1;
1837 // Boyer-Moore: match the last element in the needle
1838 while (i[nsize_1] != lastNeedle) {
1844 // Here we know that the last char matches
1845 // Continue in pedestrian mode
1846 for (size_t j = 0; ; ) {
1848 if (i[j] != needle[j]) {
1849 // Not found, we can skip
1850 // Compute the skip value lazily
1853 while (skip <= nsize_1 && needle[nsize_1 - skip] != lastNeedle) {
1860 // Check if done searching
1863 return i - haystack;
1870 size_type find(const value_type* s, size_type pos = 0) const {
1871 return find(s, pos, traits_type::length(s));
1874 size_type find (value_type c, size_type pos = 0) const {
1875 return find(&c, pos, 1);
1878 size_type rfind(const basic_fbstring& str, size_type pos = npos) const {
1879 return rfind(str.data(), pos, str.length());
1882 size_type rfind(const value_type* s, size_type pos, size_type n) const {
1883 if (n > length()) return npos;
1884 pos = std::min(pos, length() - n);
1885 if (n == 0) return pos;
1887 const_iterator i(begin() + pos);
1889 if (traits_type::eq(*i, *s)
1890 && traits_type::compare(&*i, s, n) == 0) {
1893 if (i == begin()) break;
1898 size_type rfind(const value_type* s, size_type pos = npos) const {
1899 return rfind(s, pos, traits_type::length(s));
1902 size_type rfind(value_type c, size_type pos = npos) const {
1903 return rfind(&c, pos, 1);
1906 size_type find_first_of(const basic_fbstring& str, size_type pos = 0) const {
1907 return find_first_of(str.data(), pos, str.length());
1910 size_type find_first_of(const value_type* s,
1911 size_type pos, size_type n) const {
1912 if (pos > length() || n == 0) return npos;
1913 const_iterator i(begin() + pos),
1915 for (; i != finish; ++i) {
1916 if (traits_type::find(s, n, *i) != 0) {
1923 size_type find_first_of(const value_type* s, size_type pos = 0) const {
1924 return find_first_of(s, pos, traits_type::length(s));
1927 size_type find_first_of(value_type c, size_type pos = 0) const {
1928 return find_first_of(&c, pos, 1);
1931 size_type find_last_of (const basic_fbstring& str,
1932 size_type pos = npos) const {
1933 return find_last_of(str.data(), pos, str.length());
1936 size_type find_last_of (const value_type* s, size_type pos,
1937 size_type n) const {
1938 if (!empty() && n > 0) {
1939 pos = std::min(pos, length() - 1);
1940 const_iterator i(begin() + pos);
1942 if (traits_type::find(s, n, *i) != 0) {
1945 if (i == begin()) break;
1951 size_type find_last_of (const value_type* s,
1952 size_type pos = npos) const {
1953 return find_last_of(s, pos, traits_type::length(s));
1956 size_type find_last_of (value_type c, size_type pos = npos) const {
1957 return find_last_of(&c, pos, 1);
1960 size_type find_first_not_of(const basic_fbstring& str,
1961 size_type pos = 0) const {
1962 return find_first_not_of(str.data(), pos, str.size());
1965 size_type find_first_not_of(const value_type* s, size_type pos,
1966 size_type n) const {
1967 if (pos < length()) {
1971 for (; i != finish; ++i) {
1972 if (traits_type::find(s, n, *i) == 0) {
1980 size_type find_first_not_of(const value_type* s,
1981 size_type pos = 0) const {
1982 return find_first_not_of(s, pos, traits_type::length(s));
1985 size_type find_first_not_of(value_type c, size_type pos = 0) const {
1986 return find_first_not_of(&c, pos, 1);
1989 size_type find_last_not_of(const basic_fbstring& str,
1990 size_type pos = npos) const {
1991 return find_last_not_of(str.data(), pos, str.length());
1994 size_type find_last_not_of(const value_type* s, size_type pos,
1995 size_type n) const {
1996 if (!this->empty()) {
1997 pos = std::min(pos, size() - 1);
1998 const_iterator i(begin() + pos);
2000 if (traits_type::find(s, n, *i) == 0) {
2003 if (i == begin()) break;
2009 size_type find_last_not_of(const value_type* s,
2010 size_type pos = npos) const {
2011 return find_last_not_of(s, pos, traits_type::length(s));
2014 size_type find_last_not_of (value_type c, size_type pos = npos) const {
2015 return find_last_not_of(&c, pos, 1);
2018 basic_fbstring substr(size_type pos = 0, size_type n = npos) const {
2019 enforce(pos <= size(), std::__throw_out_of_range, "");
2020 return basic_fbstring(data() + pos, std::min(n, size() - pos));
2023 int compare(const basic_fbstring& str) const {
2024 // FIX due to Goncalo N M de Carvalho July 18, 2005
2025 return compare(0, size(), str);
2028 int compare(size_type pos1, size_type n1,
2029 const basic_fbstring& str) const {
2030 return compare(pos1, n1, str.data(), str.size());
2033 int compare(size_type pos1, size_type n1,
2034 const value_type* s) const {
2035 return compare(pos1, n1, s, traits_type::length(s));
2038 int compare(size_type pos1, size_type n1,
2039 const value_type* s, size_type n2) const {
2040 enforce(pos1 <= size(), std::__throw_out_of_range, "");
2041 procrustes(n1, size() - pos1);
2042 // The line below fixed by Jean-Francois Bastien, 04-23-2007. Thanks!
2043 const int r = traits_type::compare(pos1 + data(), s, std::min(n1, n2));
2044 return r != 0 ? r : n1 > n2 ? 1 : n1 < n2 ? -1 : 0;
2047 int compare(size_type pos1, size_type n1,
2048 const basic_fbstring& str,
2049 size_type pos2, size_type n2) const {
2050 enforce(pos2 <= str.size(), std::__throw_out_of_range, "");
2051 return compare(pos1, n1, str.data() + pos2,
2052 std::min(n2, str.size() - pos2));
2055 // Code from Jean-Francois Bastien (03/26/2007)
2056 int compare(const value_type* s) const {
2057 // Could forward to compare(0, size(), s, traits_type::length(s))
2058 // but that does two extra checks
2059 const size_type n1(size()), n2(traits_type::length(s));
2060 const int r = traits_type::compare(data(), s, std::min(n1, n2));
2061 return r != 0 ? r : n1 > n2 ? 1 : n1 < n2 ? -1 : 0;
2069 // non-member functions
2071 template <typename E, class T, class A, class S>
2073 basic_fbstring<E, T, A, S> operator+(const basic_fbstring<E, T, A, S>& lhs,
2074 const basic_fbstring<E, T, A, S>& rhs) {
2076 basic_fbstring<E, T, A, S> result;
2077 result.reserve(lhs.size() + rhs.size());
2078 result.append(lhs).append(rhs);
2079 return std::move(result);
2083 template <typename E, class T, class A, class S>
2085 basic_fbstring<E, T, A, S> operator+(basic_fbstring<E, T, A, S>&& lhs,
2086 const basic_fbstring<E, T, A, S>& rhs) {
2087 return std::move(lhs.append(rhs));
2091 template <typename E, class T, class A, class S>
2093 basic_fbstring<E, T, A, S> operator+(const basic_fbstring<E, T, A, S>& lhs,
2094 basic_fbstring<E, T, A, S>&& rhs) {
2095 if (rhs.capacity() >= lhs.size() + rhs.size()) {
2096 // Good, at least we don't need to reallocate
2097 return std::move(rhs.insert(0, lhs));
2099 // Meh, no go. Forward to operator+(const&, const&).
2100 auto const& rhsC = rhs;
2105 template <typename E, class T, class A, class S>
2107 basic_fbstring<E, T, A, S> operator+(basic_fbstring<E, T, A, S>&& lhs,
2108 basic_fbstring<E, T, A, S>&& rhs) {
2109 return std::move(lhs.append(rhs));
2112 template <typename E, class T, class A, class S>
2114 basic_fbstring<E, T, A, S> operator+(
2115 const typename basic_fbstring<E, T, A, S>::value_type* lhs,
2116 const basic_fbstring<E, T, A, S>& rhs) {
2118 basic_fbstring<E, T, A, S> result;
2119 const typename basic_fbstring<E, T, A, S>::size_type len =
2120 basic_fbstring<E, T, A, S>::traits_type::length(lhs);
2121 result.reserve(len + rhs.size());
2122 result.append(lhs, len).append(rhs);
2126 template <typename E, class T, class A, class S>
2128 basic_fbstring<E, T, A, S> operator+(
2129 typename basic_fbstring<E, T, A, S>::value_type lhs,
2130 const basic_fbstring<E, T, A, S>& rhs) {
2132 basic_fbstring<E, T, A, S> result;
2133 result.reserve(1 + rhs.size());
2134 result.push_back(lhs);
2139 template <typename E, class T, class A, class S>
2141 basic_fbstring<E, T, A, S> operator+(
2142 const basic_fbstring<E, T, A, S>& lhs,
2143 const typename basic_fbstring<E, T, A, S>::value_type* rhs) {
2145 typedef typename basic_fbstring<E, T, A, S>::size_type size_type;
2146 typedef typename basic_fbstring<E, T, A, S>::traits_type traits_type;
2148 basic_fbstring<E, T, A, S> result;
2149 const size_type len = traits_type::length(rhs);
2150 result.reserve(lhs.size() + len);
2151 result.append(lhs).append(rhs, len);
2155 template <typename E, class T, class A, class S>
2157 basic_fbstring<E, T, A, S> operator+(
2158 const basic_fbstring<E, T, A, S>& lhs,
2159 typename basic_fbstring<E, T, A, S>::value_type rhs) {
2161 basic_fbstring<E, T, A, S> result;
2162 result.reserve(lhs.size() + 1);
2164 result.push_back(rhs);
2168 template <typename E, class T, class A, class S>
2170 bool operator==(const basic_fbstring<E, T, A, S>& lhs,
2171 const basic_fbstring<E, T, A, S>& rhs) {
2172 return lhs.size() == rhs.size() && lhs.compare(rhs) == 0; }
2174 template <typename E, class T, class A, class S>
2176 bool operator==(const typename basic_fbstring<E, T, A, S>::value_type* lhs,
2177 const basic_fbstring<E, T, A, S>& rhs) {
2178 return rhs == lhs; }
2180 template <typename E, class T, class A, class S>
2182 bool operator==(const basic_fbstring<E, T, A, S>& lhs,
2183 const typename basic_fbstring<E, T, A, S>::value_type* rhs) {
2184 return lhs.compare(rhs) == 0; }
2186 template <typename E, class T, class A, class S>
2188 bool operator!=(const basic_fbstring<E, T, A, S>& lhs,
2189 const basic_fbstring<E, T, A, S>& rhs) {
2190 return !(lhs == rhs); }
2192 template <typename E, class T, class A, class S>
2194 bool operator!=(const typename basic_fbstring<E, T, A, S>::value_type* lhs,
2195 const basic_fbstring<E, T, A, S>& rhs) {
2196 return !(lhs == rhs); }
2198 template <typename E, class T, class A, class S>
2200 bool operator!=(const basic_fbstring<E, T, A, S>& lhs,
2201 const typename basic_fbstring<E, T, A, S>::value_type* rhs) {
2202 return !(lhs == rhs); }
2204 template <typename E, class T, class A, class S>
2206 bool operator<(const basic_fbstring<E, T, A, S>& lhs,
2207 const basic_fbstring<E, T, A, S>& rhs) {
2208 return lhs.compare(rhs) < 0; }
2210 template <typename E, class T, class A, class S>
2212 bool operator<(const basic_fbstring<E, T, A, S>& lhs,
2213 const typename basic_fbstring<E, T, A, S>::value_type* rhs) {
2214 return lhs.compare(rhs) < 0; }
2216 template <typename E, class T, class A, class S>
2218 bool operator<(const typename basic_fbstring<E, T, A, S>::value_type* lhs,
2219 const basic_fbstring<E, T, A, S>& rhs) {
2220 return rhs.compare(lhs) > 0; }
2222 template <typename E, class T, class A, class S>
2224 bool operator>(const basic_fbstring<E, T, A, S>& lhs,
2225 const basic_fbstring<E, T, A, S>& rhs) {
2228 template <typename E, class T, class A, class S>
2230 bool operator>(const basic_fbstring<E, T, A, S>& lhs,
2231 const typename basic_fbstring<E, T, A, S>::value_type* rhs) {
2234 template <typename E, class T, class A, class S>
2236 bool operator>(const typename basic_fbstring<E, T, A, S>::value_type* lhs,
2237 const basic_fbstring<E, T, A, S>& rhs) {
2240 template <typename E, class T, class A, class S>
2242 bool operator<=(const basic_fbstring<E, T, A, S>& lhs,
2243 const basic_fbstring<E, T, A, S>& rhs) {
2244 return !(rhs < lhs); }
2246 template <typename E, class T, class A, class S>
2248 bool operator<=(const basic_fbstring<E, T, A, S>& lhs,
2249 const typename basic_fbstring<E, T, A, S>::value_type* rhs) {
2250 return !(rhs < lhs); }
2252 template <typename E, class T, class A, class S>
2254 bool operator<=(const typename basic_fbstring<E, T, A, S>::value_type* lhs,
2255 const basic_fbstring<E, T, A, S>& rhs) {
2256 return !(rhs < lhs); }
2258 template <typename E, class T, class A, class S>
2260 bool operator>=(const basic_fbstring<E, T, A, S>& lhs,
2261 const basic_fbstring<E, T, A, S>& rhs) {
2262 return !(lhs < rhs); }
2264 template <typename E, class T, class A, class S>
2266 bool operator>=(const basic_fbstring<E, T, A, S>& lhs,
2267 const typename basic_fbstring<E, T, A, S>::value_type* rhs) {
2268 return !(lhs < rhs); }
2270 template <typename E, class T, class A, class S>
2272 bool operator>=(const typename basic_fbstring<E, T, A, S>::value_type* lhs,
2273 const basic_fbstring<E, T, A, S>& rhs) {
2274 return !(lhs < rhs);
2278 template <typename E, class T, class A, class S>
2279 void swap(basic_fbstring<E, T, A, S>& lhs, basic_fbstring<E, T, A, S>& rhs) {
2283 // TODO: make this faster.
2284 template <typename E, class T, class A, class S>
2287 typename basic_fbstring<E, T, A, S>::value_type,
2288 typename basic_fbstring<E, T, A, S>::traits_type>&
2290 std::basic_istream<typename basic_fbstring<E, T, A, S>::value_type,
2291 typename basic_fbstring<E, T, A, S>::traits_type>& is,
2292 basic_fbstring<E, T, A, S>& str) {
2293 typename std::basic_istream<E, T>::sentry sentry(is);
2294 typedef std::basic_istream<typename basic_fbstring<E, T, A, S>::value_type,
2295 typename basic_fbstring<E, T, A, S>::traits_type>
2297 typedef typename __istream_type::ios_base __ios_base;
2298 size_t extracted = 0;
2299 auto err = __ios_base::goodbit;
2301 auto n = is.width();
2306 auto got = is.rdbuf()->sgetc();
2307 for (; extracted != n && got != T::eof() && !isspace(got); ++extracted) {
2308 // Whew. We get to store this guy
2310 got = is.rdbuf()->snextc();
2312 if (got == T::eof()) {
2313 err |= __ios_base::eofbit;
2318 err |= __ios_base::failbit;
2326 template <typename E, class T, class A, class S>
2328 std::basic_ostream<typename basic_fbstring<E, T, A, S>::value_type,
2329 typename basic_fbstring<E, T, A, S>::traits_type>&
2331 std::basic_ostream<typename basic_fbstring<E, T, A, S>::value_type,
2332 typename basic_fbstring<E, T, A, S>::traits_type>& os,
2333 const basic_fbstring<E, T, A, S>& str) {
2335 typename std::basic_ostream<
2336 typename basic_fbstring<E, T, A, S>::value_type,
2337 typename basic_fbstring<E, T, A, S>::traits_type>::sentry __s(os);
2339 typedef std::ostreambuf_iterator<
2340 typename basic_fbstring<E, T, A, S>::value_type,
2341 typename basic_fbstring<E, T, A, S>::traits_type> _Ip;
2342 size_t __len = str.size();
2344 (os.flags() & std::ios_base::adjustfield) == std::ios_base::left;
2345 if (__pad_and_output(_Ip(os),
2347 __left ? str.data() + __len : str.data(),
2350 os.fill()).failed()) {
2351 os.setstate(std::ios_base::badbit | std::ios_base::failbit);
2355 std::__ostream_insert(os, str.data(), str.size());
2360 #ifndef _LIBSTDCXX_FBSTRING
2362 template <typename E, class T, class A, class S>
2364 std::basic_istream<typename basic_fbstring<E, T, A, S>::value_type,
2365 typename basic_fbstring<E, T, A, S>::traits_type>&
2367 std::basic_istream<typename basic_fbstring<E, T, A, S>::value_type,
2368 typename basic_fbstring<E, T, A, S>::traits_type>& is,
2369 basic_fbstring<E, T, A, S>& str,
2370 typename basic_fbstring<E, T, A, S>::value_type delim) {
2371 // Use the nonstandard getdelim()
2372 char * buf = nullptr;
2375 // This looks quadratic but it really depends on realloc
2376 auto const newSize = size + 128;
2377 buf = static_cast<char*>(checkedRealloc(buf, newSize));
2378 is.getline(buf + size, newSize - size, delim);
2379 if (is.bad() || is.eof() || !is.fail()) {
2380 // done by either failure, end of file, or normal read
2381 size += std::strlen(buf + size);
2384 // Here we have failed due to too short a buffer
2385 // Minus one to discount the terminating '\0'
2387 assert(buf[size] == 0);
2388 // Clear the error so we can continue reading
2391 basic_fbstring<E, T, A, S> result(buf, size, size + 1,
2392 AcquireMallocatedString());
2397 template <typename E, class T, class A, class S>
2399 std::basic_istream<typename basic_fbstring<E, T, A, S>::value_type,
2400 typename basic_fbstring<E, T, A, S>::traits_type>&
2402 std::basic_istream<typename basic_fbstring<E, T, A, S>::value_type,
2403 typename basic_fbstring<E, T, A, S>::traits_type>& is,
2404 basic_fbstring<E, T, A, S>& str) {
2405 // Just forward to the version with a delimiter
2406 return getline(is, str, '\n');
2411 template <typename E1, class T, class A, class S>
2412 const typename basic_fbstring<E1, T, A, S>::size_type
2413 basic_fbstring<E1, T, A, S>::npos =
2414 static_cast<typename basic_fbstring<E1, T, A, S>::size_type>(-1);
2416 #ifndef _LIBSTDCXX_FBSTRING
2417 // basic_string compatibility routines
2419 template <typename E, class T, class A, class S>
2421 bool operator==(const basic_fbstring<E, T, A, S>& lhs,
2422 const std::string& rhs) {
2423 return lhs.compare(0, lhs.size(), rhs.data(), rhs.size()) == 0;
2426 template <typename E, class T, class A, class S>
2428 bool operator==(const std::string& lhs,
2429 const basic_fbstring<E, T, A, S>& rhs) {
2433 template <typename E, class T, class A, class S>
2435 bool operator!=(const basic_fbstring<E, T, A, S>& lhs,
2436 const std::string& rhs) {
2437 return !(lhs == rhs);
2440 template <typename E, class T, class A, class S>
2442 bool operator!=(const std::string& lhs,
2443 const basic_fbstring<E, T, A, S>& rhs) {
2444 return !(lhs == rhs);
2447 #if !defined(_LIBSTDCXX_FBSTRING)
2448 typedef basic_fbstring<char> fbstring;
2451 // fbstring is relocatable
2452 template <class T, class R, class A, class S>
2453 FOLLY_ASSUME_RELOCATABLE(basic_fbstring<T, R, A, S>);
2456 _GLIBCXX_END_NAMESPACE_VERSION
2459 } // namespace folly
2461 #ifndef _LIBSTDCXX_FBSTRING
2463 // Hash functions to make fbstring usable with e.g. hash_map
2465 // Handle interaction with different C++ standard libraries, which
2466 // expect these types to be in different namespaces.
2470 struct hash<folly::basic_fbstring<C> > : private hash<const C*> {
2471 size_t operator()(const folly::basic_fbstring<C> & s) const {
2472 return hash<const C*>::operator()(s.c_str());
2477 struct hash< ::folly::fbstring> {
2478 size_t operator()(const ::folly::fbstring& s) const {
2479 return ::folly::hash::fnv32_buf(s.data(), s.size());
2485 #if FOLLY_HAVE_DEPRECATED_ASSOC
2486 #if defined(_GLIBCXX_SYMVER) && !defined(__BIONIC__)
2487 namespace __gnu_cxx {
2490 struct hash<folly::basic_fbstring<C> > : private hash<const C*> {
2491 size_t operator()(const folly::basic_fbstring<C> & s) const {
2492 return hash<const C*>::operator()(s.c_str());
2497 struct hash< ::folly::fbstring> {
2498 size_t operator()(const ::folly::fbstring& s) const {
2499 return ::folly::hash::fnv32_buf(s.data(), s.size());
2504 #endif // _GLIBCXX_SYMVER && !__BIONIC__
2505 #endif // FOLLY_HAVE_DEPRECATED_ASSOC
2507 #endif // _LIBSTDCXX_FBSTRING
2509 #pragma GCC diagnostic pop
2511 #undef FBSTRING_DISABLE_ADDRESS_SANITIZER
2513 #undef FBSTRING_LIKELY
2514 #undef FBSTRING_UNLIKELY
2516 #endif // FOLLY_BASE_FBSTRING_H_