2 * Copyright 2015 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 // Ignore shadowing warnings within this file, so includers can use -Wshadow.
90 #pragma GCC diagnostic push
91 #pragma GCC diagnostic ignored "-Wshadow"
93 // FBString cannot use throw when replacing std::string, though it may still
96 #define throw FOLLY_FBSTRING_MAY_NOT_USE_THROW
98 #ifdef _LIBSTDCXX_FBSTRING
99 namespace std _GLIBCXX_VISIBILITY(default) {
100 _GLIBCXX_BEGIN_NAMESPACE_VERSION
105 // Different versions of gcc/clang support different versions of
106 // the address sanitizer attribute. Unfortunately, this attribute
107 // has issues when inlining is used, so disable that as well.
108 #if defined(__clang__)
109 # if __has_feature(address_sanitizer)
110 # if __has_attribute(__no_sanitize__)
111 # define FBSTRING_DISABLE_ADDRESS_SANITIZER \
112 __attribute__((__no_sanitize__("address"), __noinline__))
113 # elif __has_attribute(__no_address_safety_analysis__)
114 # define FBSTRING_DISABLE_ADDRESS_SANITIZER \
115 __attribute__((__no_address_safety_analysis__, __noinline__))
116 # elif __has_attribute(__no_sanitize_address__)
117 # define FBSTRING_DISABLE_ADDRESS_SANITIZER \
118 __attribute__((__no_sanitize_address__, __noinline__))
121 #elif defined (__GNUC__) && \
123 # define FBSTRING_DISABLE_ADDRESS_SANITIZER \
124 __attribute__((__no_address_safety_analysis__, __noinline__))
126 #ifndef FBSTRING_DISABLE_ADDRESS_SANITIZER
127 # define FBSTRING_DISABLE_ADDRESS_SANITIZER
130 namespace fbstring_detail {
132 template <class InIt, class OutIt>
135 typename std::iterator_traits<InIt>::difference_type n,
137 for (; n != 0; --n, ++b, ++d) {
143 template <class Pod, class T>
144 inline void pod_fill(Pod* b, Pod* e, T c) {
145 assert(b && e && b <= e);
146 /*static*/ if (sizeof(T) == 1) {
149 auto const ee = b + ((e - b) & ~7u);
150 for (; b != ee; b += 8) {
161 for (; b != e; ++b) {
168 * Lightly structured memcpy, simplifies copying PODs and introduces
169 * some asserts. Unfortunately using this function may cause
170 * measurable overhead (presumably because it adjusts from a begin/end
171 * convention to a pointer/size convention, so it does some extra
172 * arithmetic even though the caller might have done the inverse
173 * adaptation outside).
176 inline void pod_copy(const Pod* b, const Pod* e, Pod* d) {
178 assert(d >= e || d + (e - b) <= b);
179 memcpy(d, b, (e - b) * sizeof(Pod));
183 * Lightly structured memmove, simplifies copying PODs and introduces
187 inline void pod_move(const Pod* b, const Pod* e, Pod* d) {
189 memmove(d, b, (e - b) * sizeof(*b));
192 } // namespace fbstring_detail
195 * Defines a special acquisition method for constructing fbstring
196 * objects. AcquireMallocatedString means that the user passes a
197 * pointer to a malloc-allocated string that the fbstring object will
200 enum class AcquireMallocatedString {};
203 * fbstring_core_model is a mock-up type that defines all required
204 * signatures of a fbstring core. The fbstring class itself uses such
205 * a core object to implement all of the numerous member functions
206 * required by the standard.
208 * If you want to define a new core, copy the definition below and
209 * implement the primitives. Then plug the core into basic_fbstring as
210 * a template argument.
212 template <class Char>
213 class fbstring_core_model {
215 fbstring_core_model();
216 fbstring_core_model(const fbstring_core_model &);
217 ~fbstring_core_model();
218 // Returns a pointer to string's buffer (currently only contiguous
219 // strings are supported). The pointer is guaranteed to be valid
220 // until the next call to a non-const member function.
221 const Char * data() const;
222 // Much like data(), except the string is prepared to support
223 // character-level changes. This call is a signal for
224 // e.g. reference-counted implementation to fork the data. The
225 // pointer is guaranteed to be valid until the next call to a
226 // non-const member function.
227 Char * mutable_data();
228 // Returns a pointer to string's buffer and guarantees that a
229 // readable '\0' lies right after the buffer. The pointer is
230 // guaranteed to be valid until the next call to a non-const member
232 const Char * c_str() const;
233 // Shrinks the string by delta characters. Asserts that delta <=
235 void shrink(size_t delta);
236 // Expands the string by delta characters (i.e. after this call
237 // size() will report the old size() plus delta) but without
238 // initializing the expanded region. Returns a pointer to the memory
239 // to be initialized (the beginning of the expanded portion). The
240 // caller is expected to fill the expanded area appropriately.
241 Char* expand_noinit(size_t delta);
242 // Expands the string by one character and sets the last character
244 void push_back(Char c);
245 // Returns the string's size.
247 // Returns the string's capacity, i.e. maximum size that the string
248 // can grow to without reallocation. Note that for reference counted
249 // strings that's technically a lie - even assigning characters
250 // within the existing size would cause a reallocation.
251 size_t capacity() const;
252 // Returns true if the data underlying the string is actually shared
253 // across multiple strings (in a refcounted fashion).
254 bool isShared() const;
255 // Makes sure that at least minCapacity characters are available for
256 // the string without reallocation. For reference-counted strings,
257 // it should fork the data even if minCapacity < size().
258 void reserve(size_t minCapacity);
261 fbstring_core_model& operator=(const fbstring_core_model &);
266 * This is the core of the string. The code should work on 32- and
267 * 64-bit and both big- and little-endianan architectures with any
270 * The storage is selected as follows (assuming we store one-byte
271 * characters on a 64-bit machine): (a) "small" strings between 0 and
272 * 23 chars are stored in-situ without allocation (the rightmost byte
273 * stores the size); (b) "medium" strings from 24 through 254 chars
274 * are stored in malloc-allocated memory that is copied eagerly; (c)
275 * "large" strings of 255 chars and above are stored in a similar
276 * structure as medium arrays, except that the string is
277 * reference-counted and copied lazily. the reference count is
278 * allocated right before the character array.
280 * The discriminator between these three strategies sits in two
281 * bits of the rightmost char of the storage. If neither is set, then the
282 * string is small (and its length sits in the lower-order bits on
283 * little-endian or the high-order bits on big-endian of that
284 * rightmost character). If the MSb is set, the string is medium width.
285 * If the second MSb is set, then the string is large. On little-endian,
286 * these 2 bits are the 2 MSbs of MediumLarge::capacity_, while on
287 * big-endian, these 2 bits are the 2 LSbs. This keeps both little-endian
288 * and big-endian fbstring_core equivalent with merely different ops used
289 * to extract capacity/category.
291 template <class Char> class fbstring_core {
293 fbstring_core() noexcept { reset(); }
295 fbstring_core(const fbstring_core & rhs) {
296 assert(&rhs != this);
297 // Simplest case first: small strings are bitblitted
298 if (rhs.category() == Category::isSmall) {
299 static_assert(offsetof(MediumLarge, data_) == 0,
300 "fbstring layout failure");
301 static_assert(offsetof(MediumLarge, size_) == sizeof(ml_.data_),
302 "fbstring layout failure");
303 static_assert(offsetof(MediumLarge, capacity_) == 2 * sizeof(ml_.data_),
304 "fbstring layout failure");
305 // Just write the whole thing, don't look at details. In
306 // particular we need to copy capacity anyway because we want
307 // to set the size (don't forget that the last character,
308 // which stores a short string's length, is shared with the
309 // ml_.capacity field).
311 assert(category() == Category::isSmall && this->size() == rhs.size());
312 } else if (rhs.category() == Category::isLarge) {
313 // Large strings are just refcounted
315 RefCounted::incrementRefs(ml_.data_);
316 assert(category() == Category::isLarge && size() == rhs.size());
318 // Medium strings are copied eagerly. Don't forget to allocate
319 // one extra Char for the null terminator.
320 auto const allocSize =
321 goodMallocSize((1 + rhs.ml_.size_) * sizeof(Char));
322 ml_.data_ = static_cast<Char*>(checkedMalloc(allocSize));
323 fbstring_detail::pod_copy(rhs.ml_.data_,
325 rhs.ml_.data_ + rhs.ml_.size_ + 1,
327 // No need for writeTerminator() here, we copied one extra
328 // element just above.
329 ml_.size_ = rhs.ml_.size_;
330 ml_.setCapacity(allocSize / sizeof(Char) - 1, Category::isMedium);
331 assert(category() == Category::isMedium);
333 assert(size() == rhs.size());
334 assert(memcmp(data(), rhs.data(), size() * sizeof(Char)) == 0);
337 fbstring_core(fbstring_core&& goner) noexcept {
340 if (goner.category() != Category::isSmall) {
341 // Clean goner's carcass
346 // NOTE(agallagher): The word-aligned copy path copies bytes which are
347 // outside the range of the string, and makes address sanitizer unhappy,
348 // so just disable it on this function.
349 fbstring_core(const Char *const data, const size_t size)
350 FBSTRING_DISABLE_ADDRESS_SANITIZER {
352 #ifndef _LIBSTDCXX_FBSTRING
354 assert(this->size() == size);
355 assert(memcmp(this->data(), data, size * sizeof(Char)) == 0);
360 // Simplest case first: small strings are bitblitted
361 if (size <= maxSmallSize) {
362 // Layout is: Char* data_, size_t size_, size_t capacity_
363 static_assert(sizeof(*this) == sizeof(Char*) + 2 * sizeof(size_t),
364 "fbstring has unexpected size");
365 static_assert(sizeof(Char*) == sizeof(size_t),
366 "fbstring size assumption violation");
367 // sizeof(size_t) must be a power of 2
368 static_assert((sizeof(size_t) & (sizeof(size_t) - 1)) == 0,
369 "fbstring size assumption violation");
371 // If data is aligned, use fast word-wise copying. Otherwise,
372 // use conservative memcpy.
373 if (reinterpret_cast<size_t>(data) & (sizeof(size_t) - 1)) {
374 fbstring_detail::pod_copy(data, data + size, small_);
376 // Copy one word (64 bits) at a time
377 const size_t byteSize = size * sizeof(Char);
378 if (byteSize > 2 * sizeof(size_t)) {
380 ml_.capacity_ = reinterpret_cast<const size_t*>(data)[2];
382 ml_.size_ = reinterpret_cast<const size_t*>(data)[1];
384 ml_.data_ = *reinterpret_cast<Char**>(const_cast<Char*>(data));
385 } else if (byteSize > sizeof(size_t)) {
388 } else if (size > 0) {
395 } else if (size <= maxMediumSize) {
396 // Medium strings are allocated normally. Don't forget to
397 // allocate one extra Char for the terminating null.
398 auto const allocSize = goodMallocSize((1 + size) * sizeof(Char));
399 ml_.data_ = static_cast<Char*>(checkedMalloc(allocSize));
400 fbstring_detail::pod_copy(data, data + size, ml_.data_);
402 ml_.setCapacity(allocSize / sizeof(Char) - 1, Category::isMedium);
404 // Large strings are allocated differently
405 size_t effectiveCapacity = size;
406 auto const newRC = RefCounted::create(data, & effectiveCapacity);
407 ml_.data_ = newRC->data_;
409 ml_.setCapacity(effectiveCapacity, Category::isLarge);
414 ~fbstring_core() noexcept {
415 auto const c = category();
416 if (c == Category::isSmall) {
419 if (c == Category::isMedium) {
423 RefCounted::decrementRefs(ml_.data_);
426 // Snatches a previously mallocated string. The parameter "size"
427 // is the size of the string, and the parameter "allocatedSize"
428 // is the size of the mallocated block. The string must be
429 // \0-terminated, so allocatedSize >= size + 1 and data[size] == '\0'.
431 // So if you want a 2-character string, pass malloc(3) as "data",
432 // pass 2 as "size", and pass 3 as "allocatedSize".
433 fbstring_core(Char * const data,
435 const size_t allocatedSize,
436 AcquireMallocatedString) {
438 assert(allocatedSize >= size + 1);
439 assert(data[size] == '\0');
440 // Use the medium string storage
443 // Don't forget about null terminator
444 ml_.setCapacity(allocatedSize - 1, Category::isMedium);
446 // No need for the memory
452 // swap below doesn't test whether &rhs == this (and instead
453 // potentially does extra work) on the premise that the rarity of
454 // that situation actually makes the check more expensive than is
456 void swap(fbstring_core & rhs) {
462 // In C++11 data() and c_str() are 100% equivalent.
463 const Char * data() const {
467 Char * mutable_data() {
468 auto const c = category();
469 if (c == Category::isSmall) {
472 assert(c == Category::isMedium || c == Category::isLarge);
473 if (c == Category::isLarge && RefCounted::refs(ml_.data_) > 1) {
475 size_t effectiveCapacity = ml_.capacity();
476 auto const newRC = RefCounted::create(& effectiveCapacity);
477 // If this fails, someone placed the wrong capacity in an
479 assert(effectiveCapacity >= ml_.capacity());
480 fbstring_detail::pod_copy(ml_.data_, ml_.data_ + ml_.size_ + 1,
482 RefCounted::decrementRefs(ml_.data_);
483 ml_.data_ = newRC->data_;
484 // No need to call writeTerminator(), we have + 1 above.
489 const Char * c_str() const {
490 auto const c = category();
491 if (c == Category::isSmall) {
492 assert(small_[smallSize()] == '\0');
495 assert(c == Category::isMedium || c == Category::isLarge);
496 assert(ml_.data_[ml_.size_] == '\0');
500 void shrink(const size_t delta) {
501 if (category() == Category::isSmall) {
502 // Check for underflow
503 assert(delta <= smallSize());
504 setSmallSize(smallSize() - delta);
505 } else if (category() == Category::isMedium ||
506 RefCounted::refs(ml_.data_) == 1) {
507 // Medium strings and unique large strings need no special
509 assert(ml_.size_ >= delta);
513 assert(ml_.size_ >= delta);
514 // Shared large string, must make unique. This is because of the
515 // durn terminator must be written, which may trample the shared
518 fbstring_core(ml_.data_, ml_.size_ - delta).swap(*this);
520 // No need to write the terminator.
524 void reserve(size_t minCapacity) {
525 if (category() == Category::isLarge) {
527 if (RefCounted::refs(ml_.data_) > 1) {
528 // We must make it unique regardless; in-place reallocation is
529 // useless if the string is shared. In order to not surprise
530 // people, reserve the new block at current capacity or
531 // more. That way, a string's capacity never shrinks after a
533 minCapacity = std::max(minCapacity, ml_.capacity());
534 auto const newRC = RefCounted::create(& minCapacity);
535 fbstring_detail::pod_copy(ml_.data_, ml_.data_ + ml_.size_ + 1,
537 // Done with the old data. No need to call writeTerminator(),
538 // we have + 1 above.
539 RefCounted::decrementRefs(ml_.data_);
540 ml_.data_ = newRC->data_;
541 ml_.setCapacity(minCapacity, Category::isLarge);
542 // size remains unchanged
544 // String is not shared, so let's try to realloc (if needed)
545 if (minCapacity > ml_.capacity()) {
546 // Asking for more memory
548 RefCounted::reallocate(ml_.data_, ml_.size_,
549 ml_.capacity(), minCapacity);
550 ml_.data_ = newRC->data_;
551 ml_.setCapacity(minCapacity, Category::isLarge);
554 assert(capacity() >= minCapacity);
556 } else if (category() == Category::isMedium) {
557 // String is not shared
558 if (minCapacity <= ml_.capacity()) {
559 return; // nothing to do, there's enough room
561 if (minCapacity <= maxMediumSize) {
562 // Keep the string at medium size. Don't forget to allocate
563 // one extra Char for the terminating null.
564 size_t capacityBytes = goodMallocSize((1 + minCapacity) * sizeof(Char));
565 ml_.data_ = static_cast<Char *>(
568 ml_.size_ * sizeof(Char),
569 (ml_.capacity() + 1) * sizeof(Char),
572 ml_.setCapacity(capacityBytes / sizeof(Char) - 1, Category::isMedium);
574 // Conversion from medium to large string
575 fbstring_core nascent;
576 // Will recurse to another branch of this function
577 nascent.reserve(minCapacity);
578 nascent.ml_.size_ = ml_.size_;
579 fbstring_detail::pod_copy(ml_.data_, ml_.data_ + ml_.size_,
583 assert(capacity() >= minCapacity);
586 assert(category() == Category::isSmall);
587 if (minCapacity > maxMediumSize) {
589 auto const newRC = RefCounted::create(& minCapacity);
590 auto const size = smallSize();
591 fbstring_detail::pod_copy(small_, small_ + size + 1, newRC->data_);
592 // No need for writeTerminator(), we wrote it above with + 1.
593 ml_.data_ = newRC->data_;
595 ml_.setCapacity(minCapacity, Category::isLarge);
596 assert(capacity() >= minCapacity);
597 } else if (minCapacity > maxSmallSize) {
599 // Don't forget to allocate one extra Char for the terminating null
600 auto const allocSizeBytes =
601 goodMallocSize((1 + minCapacity) * sizeof(Char));
602 auto const data = static_cast<Char*>(checkedMalloc(allocSizeBytes));
603 auto const size = smallSize();
604 fbstring_detail::pod_copy(small_, small_ + size + 1, data);
605 // No need for writeTerminator(), we wrote it above with + 1.
608 ml_.setCapacity(allocSizeBytes / sizeof(Char) - 1, Category::isMedium);
611 // Nothing to do, everything stays put
614 assert(capacity() >= minCapacity);
617 Char * expand_noinit(const size_t delta) {
618 // Strategy is simple: make room, then change size
619 assert(capacity() >= size());
621 if (category() == Category::isSmall) {
624 if (newSz <= maxSmallSize) {
631 newSz = ml_.size_ + delta;
632 if (newSz > capacity()) {
636 assert(capacity() >= newSz);
637 // Category can't be small - we took care of that above
638 assert(category() == Category::isMedium || category() == Category::isLarge);
641 assert(size() == newSz);
642 return ml_.data_ + sz;
645 void push_back(Char c) {
646 assert(capacity() >= size());
648 if (category() == Category::isSmall) {
650 if (sz < maxSmallSize) {
652 setSmallSize(sz + 1);
655 reserve(maxSmallSize * 2);
658 if (sz == capacity()) { // always true for isShared()
659 reserve(1 + sz * 3 / 2); // ensures not shared
663 assert(capacity() >= sz + 1);
664 // Category can't be small - we took care of that above
665 assert(category() == Category::isMedium || category() == Category::isLarge);
671 size_t size() const {
672 return category() == Category::isSmall ? smallSize() : ml_.size_;
675 size_t capacity() const {
676 switch (category()) {
677 case Category::isSmall:
679 case Category::isLarge:
680 // For large-sized strings, a multi-referenced chunk has no
681 // available capacity. This is because any attempt to append
682 // data would trigger a new allocation.
683 if (RefCounted::refs(ml_.data_) > 1) return ml_.size_;
686 return ml_.capacity();
689 bool isShared() const {
690 return category() == Category::isLarge && RefCounted::refs(ml_.data_) > 1;
693 void writeTerminator() {
694 if (category() == Category::isSmall) {
695 const auto s = smallSize();
696 if (s != maxSmallSize) {
700 ml_.data_[ml_.size_] = '\0';
706 fbstring_core & operator=(const fbstring_core & rhs);
708 // Equivalent to setSmallSize(0), but with specialized
709 // writeTerminator which doesn't re-check the category after
710 // capacity_ is overwritten.
712 // Only initialize the tag, will set the MSBs (i.e. the small
713 // string size) to zero too.
714 ml_.capacity_ = kIsLittleEndian
715 ? maxSmallSize << (8 * (sizeof(size_t) - sizeof(Char)))
718 assert(category() == Category::isSmall && size() == 0);
722 std::atomic<size_t> refCount_;
725 static RefCounted * fromData(Char * p) {
726 return static_cast<RefCounted*>(
728 static_cast<unsigned char*>(static_cast<void*>(p))
729 - sizeof(refCount_)));
732 static size_t refs(Char * p) {
733 return fromData(p)->refCount_.load(std::memory_order_acquire);
736 static void incrementRefs(Char * p) {
737 fromData(p)->refCount_.fetch_add(1, std::memory_order_acq_rel);
740 static void decrementRefs(Char * p) {
741 auto const dis = fromData(p);
742 size_t oldcnt = dis->refCount_.fetch_sub(1, std::memory_order_acq_rel);
749 static RefCounted * create(size_t * size) {
750 // Don't forget to allocate one extra Char for the terminating
751 // null. In this case, however, one Char is already part of the
753 const size_t allocSize = goodMallocSize(
754 sizeof(RefCounted) + *size * sizeof(Char));
755 auto result = static_cast<RefCounted*>(checkedMalloc(allocSize));
756 result->refCount_.store(1, std::memory_order_release);
757 *size = (allocSize - sizeof(RefCounted)) / sizeof(Char);
761 static RefCounted * create(const Char * data, size_t * size) {
762 const size_t effectiveSize = *size;
763 auto result = create(size);
764 fbstring_detail::pod_copy(data, data + effectiveSize, result->data_);
768 static RefCounted * reallocate(Char *const data,
769 const size_t currentSize,
770 const size_t currentCapacity,
771 const size_t newCapacity) {
772 assert(newCapacity > 0 && newCapacity > currentSize);
773 auto const dis = fromData(data);
774 assert(dis->refCount_.load(std::memory_order_acquire) == 1);
775 // Don't forget to allocate one extra Char for the terminating
776 // null. In this case, however, one Char is already part of the
778 auto result = static_cast<RefCounted*>(
780 sizeof(RefCounted) + currentSize * sizeof(Char),
781 sizeof(RefCounted) + currentCapacity * sizeof(Char),
782 sizeof(RefCounted) + newCapacity * sizeof(Char)));
783 assert(result->refCount_.load(std::memory_order_acquire) == 1);
788 typedef std::conditional<sizeof(size_t) == 4, uint32_t, uint64_t>::type
791 enum class Category : category_type {
793 isMedium = kIsLittleEndian
794 ? sizeof(size_t) == 4 ? 0x80000000 : 0x8000000000000000
796 isLarge = kIsLittleEndian
797 ? sizeof(size_t) == 4 ? 0x40000000 : 0x4000000000000000
801 Category category() const {
802 // works for both big-endian and little-endian
803 return static_cast<Category>(ml_.capacity_ & categoryExtractMask);
811 size_t capacity() const {
812 return kIsLittleEndian
813 ? capacity_ & capacityExtractMask
817 void setCapacity(size_t cap, Category cat) {
818 capacity_ = kIsLittleEndian
819 ? cap | static_cast<category_type>(cat)
820 : (cap << 2) | static_cast<category_type>(cat);
825 Char small_[sizeof(MediumLarge) / sizeof(Char)];
830 lastChar = sizeof(MediumLarge) - 1,
831 maxSmallSize = lastChar / sizeof(Char),
832 maxMediumSize = 254 / sizeof(Char), // coincides with the small
833 // bin size in dlmalloc
834 categoryExtractMask = kIsLittleEndian
835 ? sizeof(size_t) == 4 ? 0xC0000000 : 0xC000000000000000
837 capacityExtractMask = kIsLittleEndian
838 ? ~categoryExtractMask
841 static_assert(!(sizeof(MediumLarge) % sizeof(Char)),
842 "Corrupt memory layout for fbstring.");
844 size_t smallSize() const {
845 assert(category() == Category::isSmall);
846 auto shift = kIsLittleEndian ? 0 : 2;
847 auto smallShifted = static_cast<size_t>(small_[maxSmallSize]) >> shift;
848 assert(static_cast<size_t>(maxSmallSize) >= smallShifted);
849 return static_cast<size_t>(maxSmallSize) - smallShifted;
852 void setSmallSize(size_t s) {
853 // Warning: this should work with uninitialized strings too,
854 // so don't assume anything about the previous value of
855 // small_[maxSmallSize].
856 assert(s <= maxSmallSize);
857 small_[maxSmallSize] = kIsLittleEndian
859 : (maxSmallSize - s) << 2;
864 #ifndef _LIBSTDCXX_FBSTRING
866 * Dummy fbstring core that uses an actual std::string. This doesn't
867 * make any sense - it's just for testing purposes.
869 template <class Char>
870 class dummy_fbstring_core {
872 dummy_fbstring_core() {
874 dummy_fbstring_core(const dummy_fbstring_core& another)
875 : backend_(another.backend_) {
877 dummy_fbstring_core(const Char * s, size_t n)
880 void swap(dummy_fbstring_core & rhs) {
881 backend_.swap(rhs.backend_);
883 const Char * data() const {
884 return backend_.data();
886 Char * mutable_data() {
887 //assert(!backend_.empty());
888 return &*backend_.begin();
890 void shrink(size_t delta) {
891 assert(delta <= size());
892 backend_.resize(size() - delta);
894 Char * expand_noinit(size_t delta) {
895 auto const sz = size();
896 backend_.resize(size() + delta);
897 return backend_.data() + sz;
899 void push_back(Char c) {
900 backend_.push_back(c);
902 size_t size() const {
903 return backend_.size();
905 size_t capacity() const {
906 return backend_.capacity();
908 bool isShared() const {
911 void reserve(size_t minCapacity) {
912 backend_.reserve(minCapacity);
916 std::basic_string<Char> backend_;
918 #endif // !_LIBSTDCXX_FBSTRING
921 * This is the basic_string replacement. For conformity,
922 * basic_fbstring takes the same template parameters, plus the last
923 * one which is the core.
925 #ifdef _LIBSTDCXX_FBSTRING
926 template <typename E, class T, class A, class Storage>
928 template <typename E,
929 class T = std::char_traits<E>,
930 class A = std::allocator<E>,
931 class Storage = fbstring_core<E> >
933 class basic_fbstring {
937 void (*throw_exc)(const char*),
939 if (!condition) throw_exc(msg);
942 bool isSane() const {
945 empty() == (size() == 0) &&
946 empty() == (begin() == end()) &&
947 size() <= max_size() &&
948 capacity() <= max_size() &&
949 size() <= capacity() &&
950 begin()[size()] == '\0';
954 friend struct Invariant;
957 explicit Invariant(const basic_fbstring& s) : s_(s) {
964 const basic_fbstring& s_;
966 explicit Invariant(const basic_fbstring&) {}
968 Invariant& operator=(const Invariant&);
973 typedef T traits_type;
974 typedef typename traits_type::char_type value_type;
975 typedef A allocator_type;
976 typedef typename A::size_type size_type;
977 typedef typename A::difference_type difference_type;
979 typedef typename A::reference reference;
980 typedef typename A::const_reference const_reference;
981 typedef typename A::pointer pointer;
982 typedef typename A::const_pointer const_pointer;
985 typedef const E* const_iterator;
986 typedef std::reverse_iterator<iterator
987 #ifdef NO_ITERATOR_TRAITS
991 typedef std::reverse_iterator<const_iterator
992 #ifdef NO_ITERATOR_TRAITS
995 > const_reverse_iterator;
997 static const size_type npos; // = size_type(-1)
1000 static void procrustes(size_type& n, size_type nmax) {
1001 if (n > nmax) n = nmax;
1005 // C++11 21.4.2 construct/copy/destroy
1007 // Note: while the following two constructors can be (and previously were)
1008 // collapsed into one constructor written this way:
1010 // explicit basic_fbstring(const A& a = A()) noexcept { }
1012 // This can cause Clang (at least version 3.7) to fail with the error:
1013 // "chosen constructor is explicit in copy-initialization ...
1014 // in implicit initialization of field '(x)' with omitted initializer"
1016 // if used in a struct which is default-initialized. Hence the split into
1017 // these two separate constructors.
1019 basic_fbstring() noexcept : basic_fbstring(A()) {
1022 explicit basic_fbstring(const A&) noexcept {
1025 basic_fbstring(const basic_fbstring& str)
1026 : store_(str.store_) {
1030 basic_fbstring(basic_fbstring&& goner) noexcept
1031 : store_(std::move(goner.store_)) {
1034 #ifndef _LIBSTDCXX_FBSTRING
1035 // This is defined for compatibility with std::string
1036 /* implicit */ basic_fbstring(const std::string& str)
1037 : store_(str.data(), str.size()) {
1041 basic_fbstring(const basic_fbstring& str, size_type pos,
1042 size_type n = npos, const A& a = A()) {
1043 assign(str, pos, n);
1046 /* implicit */ basic_fbstring(const value_type* s, const A& /*a*/ = A())
1048 ? traits_type::length(s)
1049 : (std::__throw_logic_error(
1050 "basic_fbstring: null pointer initializer not valid"),
1054 basic_fbstring(const value_type* s, size_type n, const A& /*a*/ = A())
1058 basic_fbstring(size_type n, value_type c, const A& /*a*/ = A()) {
1059 auto const data = store_.expand_noinit(n);
1060 fbstring_detail::pod_fill(data, data + n, c);
1061 store_.writeTerminator();
1064 template <class InIt>
1065 basic_fbstring(InIt begin, InIt end,
1066 typename std::enable_if<
1067 !std::is_same<typename std::remove_const<InIt>::type,
1068 value_type*>::value, const A>::type & /*a*/ = A()) {
1072 // Specialization for const char*, const char*
1073 basic_fbstring(const value_type* b, const value_type* e)
1074 : store_(b, e - b) {
1077 // Nonstandard constructor
1078 basic_fbstring(value_type *s, size_type n, size_type c,
1079 AcquireMallocatedString a)
1080 : store_(s, n, c, a) {
1083 // Construction from initialization list
1084 basic_fbstring(std::initializer_list<value_type> il) {
1085 assign(il.begin(), il.end());
1088 ~basic_fbstring() noexcept {
1091 basic_fbstring& operator=(const basic_fbstring& lhs) {
1092 if (FBSTRING_UNLIKELY(&lhs == this)) {
1095 auto const oldSize = size();
1096 auto const srcSize = lhs.size();
1097 if (capacity() >= srcSize && !store_.isShared()) {
1098 // great, just copy the contents
1099 if (oldSize < srcSize)
1100 store_.expand_noinit(srcSize - oldSize);
1102 store_.shrink(oldSize - srcSize);
1103 assert(size() == srcSize);
1104 fbstring_detail::pod_copy(lhs.begin(), lhs.end(), begin());
1105 store_.writeTerminator();
1107 // need to reallocate, so we may as well create a brand new string
1108 basic_fbstring(lhs).swap(*this);
1114 basic_fbstring& operator=(basic_fbstring&& goner) noexcept {
1115 if (FBSTRING_UNLIKELY(&goner == this)) {
1116 // Compatibility with std::basic_string<>,
1117 // C++11 21.4.2 [string.cons] / 23 requires self-move-assignment support.
1120 // No need of this anymore
1121 this->~basic_fbstring();
1122 // Move the goner into this
1123 new(&store_) fbstring_core<E>(std::move(goner.store_));
1127 #ifndef _LIBSTDCXX_FBSTRING
1128 // Compatibility with std::string
1129 basic_fbstring & operator=(const std::string & rhs) {
1130 return assign(rhs.data(), rhs.size());
1133 // Compatibility with std::string
1134 std::string toStdString() const {
1135 return std::string(data(), size());
1138 // A lot of code in fbcode still uses this method, so keep it here for now.
1139 const basic_fbstring& toStdString() const {
1144 basic_fbstring& operator=(const value_type* s) {
1148 basic_fbstring& operator=(value_type c) {
1150 store_.expand_noinit(1);
1151 } else if (store_.isShared()) {
1152 basic_fbstring(1, c).swap(*this);
1155 store_.shrink(size() - 1);
1157 *store_.mutable_data() = c;
1158 store_.writeTerminator();
1162 basic_fbstring& operator=(std::initializer_list<value_type> il) {
1163 return assign(il.begin(), il.end());
1166 // C++11 21.4.3 iterators:
1167 iterator begin() { return store_.mutable_data(); }
1169 const_iterator begin() const { return store_.data(); }
1171 const_iterator cbegin() const { return begin(); }
1174 return store_.mutable_data() + store_.size();
1177 const_iterator end() const {
1178 return store_.data() + store_.size();
1181 const_iterator cend() const { return end(); }
1183 reverse_iterator rbegin() {
1184 return reverse_iterator(end());
1187 const_reverse_iterator rbegin() const {
1188 return const_reverse_iterator(end());
1191 const_reverse_iterator crbegin() const { return rbegin(); }
1193 reverse_iterator rend() {
1194 return reverse_iterator(begin());
1197 const_reverse_iterator rend() const {
1198 return const_reverse_iterator(begin());
1201 const_reverse_iterator crend() const { return rend(); }
1204 // C++11 21.4.5, element access:
1205 const value_type& front() const { return *begin(); }
1206 const value_type& back() const {
1208 // Should be begin()[size() - 1], but that branches twice
1209 return *(end() - 1);
1211 value_type& front() { return *begin(); }
1212 value_type& back() {
1214 // Should be begin()[size() - 1], but that branches twice
1215 return *(end() - 1);
1222 // C++11 21.4.4 capacity:
1223 size_type size() const { return store_.size(); }
1225 size_type length() const { return size(); }
1227 size_type max_size() const {
1228 return std::numeric_limits<size_type>::max();
1231 void resize(const size_type n, const value_type c = value_type()) {
1232 auto size = this->size();
1234 store_.shrink(size - n);
1236 // Do this in two steps to minimize slack memory copied (see
1238 auto const capacity = this->capacity();
1239 assert(capacity >= size);
1240 if (size < capacity) {
1241 auto delta = std::min(n, capacity) - size;
1242 store_.expand_noinit(delta);
1243 fbstring_detail::pod_fill(begin() + size, end(), c);
1246 store_.writeTerminator();
1251 auto const delta = n - size;
1252 store_.expand_noinit(delta);
1253 fbstring_detail::pod_fill(end() - delta, end(), c);
1254 store_.writeTerminator();
1256 assert(this->size() == n);
1259 size_type capacity() const { return store_.capacity(); }
1261 void reserve(size_type res_arg = 0) {
1262 enforce(res_arg <= max_size(), std::__throw_length_error, "");
1263 store_.reserve(res_arg);
1266 void shrink_to_fit() {
1267 // Shrink only if slack memory is sufficiently large
1268 if (capacity() < size() * 3 / 2) {
1271 basic_fbstring(cbegin(), cend()).swap(*this);
1274 void clear() { resize(0); }
1276 bool empty() const { return size() == 0; }
1278 // C++11 21.4.5 element access:
1279 const_reference operator[](size_type pos) const {
1280 return *(begin() + pos);
1283 reference operator[](size_type pos) {
1284 return *(begin() + pos);
1287 const_reference at(size_type n) const {
1288 enforce(n <= size(), std::__throw_out_of_range, "");
1292 reference at(size_type n) {
1293 enforce(n < size(), std::__throw_out_of_range, "");
1297 // C++11 21.4.6 modifiers:
1298 basic_fbstring& operator+=(const basic_fbstring& str) {
1302 basic_fbstring& operator+=(const value_type* s) {
1306 basic_fbstring& operator+=(const value_type c) {
1311 basic_fbstring& operator+=(std::initializer_list<value_type> il) {
1316 basic_fbstring& append(const basic_fbstring& str) {
1318 auto desiredSize = size() + str.size();
1320 append(str.data(), str.size());
1321 assert(size() == desiredSize);
1325 basic_fbstring& append(const basic_fbstring& str, const size_type pos,
1327 const size_type sz = str.size();
1328 enforce(pos <= sz, std::__throw_out_of_range, "");
1329 procrustes(n, sz - pos);
1330 return append(str.data() + pos, n);
1333 basic_fbstring& append(const value_type* s, size_type n) {
1335 Invariant checker(*this);
1338 if (FBSTRING_UNLIKELY(!n)) {
1339 // Unlikely but must be done
1342 auto const oldSize = size();
1343 auto const oldData = data();
1344 // Check for aliasing (rare). We could use "<=" here but in theory
1345 // those do not work for pointers unless the pointers point to
1346 // elements in the same array. For that reason we use
1347 // std::less_equal, which is guaranteed to offer a total order
1348 // over pointers. See discussion at http://goo.gl/Cy2ya for more
1350 std::less_equal<const value_type*> le;
1351 if (FBSTRING_UNLIKELY(le(oldData, s) && !le(oldData + oldSize, s))) {
1352 assert(le(s + n, oldData + oldSize));
1353 const size_type offset = s - oldData;
1354 store_.reserve(oldSize + n);
1355 // Restore the source
1356 s = data() + offset;
1358 // Warning! Repeated appends with short strings may actually incur
1359 // practically quadratic performance. Avoid that by pushing back
1360 // the first character (which ensures exponential growth) and then
1361 // appending the rest normally. Worst case the append may incur a
1362 // second allocation but that will be rare.
1365 memcpy(store_.expand_noinit(n), s, n * sizeof(value_type));
1366 assert(size() == oldSize + n + 1);
1370 basic_fbstring& append(const value_type* s) {
1371 return append(s, traits_type::length(s));
1374 basic_fbstring& append(size_type n, value_type c) {
1375 resize(size() + n, c);
1379 template<class InputIterator>
1380 basic_fbstring& append(InputIterator first, InputIterator last) {
1381 insert(end(), first, last);
1385 basic_fbstring& append(std::initializer_list<value_type> il) {
1386 return append(il.begin(), il.end());
1389 void push_back(const value_type c) { // primitive
1390 store_.push_back(c);
1393 basic_fbstring& assign(const basic_fbstring& str) {
1394 if (&str == this) return *this;
1395 return assign(str.data(), str.size());
1398 basic_fbstring& assign(basic_fbstring&& str) {
1399 return *this = std::move(str);
1402 basic_fbstring& assign(const basic_fbstring& str, const size_type pos,
1404 const size_type sz = str.size();
1405 enforce(pos <= sz, std::__throw_out_of_range, "");
1406 procrustes(n, sz - pos);
1407 return assign(str.data() + pos, n);
1410 basic_fbstring& assign(const value_type* s, const size_type n) {
1411 Invariant checker(*this);
1414 std::copy(s, s + n, begin());
1416 assert(size() == n);
1418 const value_type *const s2 = s + size();
1419 std::copy(s, s2, begin());
1420 append(s2, n - size());
1421 assert(size() == n);
1423 store_.writeTerminator();
1424 assert(size() == n);
1428 basic_fbstring& assign(const value_type* s) {
1429 return assign(s, traits_type::length(s));
1432 basic_fbstring& assign(std::initializer_list<value_type> il) {
1433 return assign(il.begin(), il.end());
1436 template <class ItOrLength, class ItOrChar>
1437 basic_fbstring& assign(ItOrLength first_or_n, ItOrChar last_or_c) {
1438 return replace(begin(), end(), first_or_n, last_or_c);
1441 basic_fbstring& insert(size_type pos1, const basic_fbstring& str) {
1442 return insert(pos1, str.data(), str.size());
1445 basic_fbstring& insert(size_type pos1, const basic_fbstring& str,
1446 size_type pos2, size_type n) {
1447 enforce(pos2 <= str.length(), std::__throw_out_of_range, "");
1448 procrustes(n, str.length() - pos2);
1449 return insert(pos1, str.data() + pos2, n);
1452 basic_fbstring& insert(size_type pos, const value_type* s, size_type n) {
1453 enforce(pos <= length(), std::__throw_out_of_range, "");
1454 insert(begin() + pos, s, s + n);
1458 basic_fbstring& insert(size_type pos, const value_type* s) {
1459 return insert(pos, s, traits_type::length(s));
1462 basic_fbstring& insert(size_type pos, size_type n, value_type c) {
1463 enforce(pos <= length(), std::__throw_out_of_range, "");
1464 insert(begin() + pos, n, c);
1468 iterator insert(const_iterator p, const value_type c) {
1469 const size_type pos = p - begin();
1471 return begin() + pos;
1475 template <int i> class Selector {};
1477 iterator insertImplDiscr(const_iterator p,
1478 size_type n, value_type c, Selector<1>) {
1479 Invariant checker(*this);
1481 auto const pos = p - begin();
1482 assert(p >= begin() && p <= end());
1483 if (capacity() - size() < n) {
1484 const size_type sz = p - begin();
1485 reserve(size() + n);
1488 const iterator oldEnd = end();
1489 if (n < size_type(oldEnd - p)) {
1490 append(oldEnd - n, oldEnd);
1492 // reverse_iterator(oldEnd - n),
1493 // reverse_iterator(p),
1494 // reverse_iterator(oldEnd));
1495 fbstring_detail::pod_move(&*p, &*oldEnd - n,
1497 std::fill(begin() + pos, begin() + pos + n, c);
1499 append(n - (end() - p), c);
1500 append(iterator(p), oldEnd);
1501 std::fill(iterator(p), oldEnd, c);
1503 store_.writeTerminator();
1504 return begin() + pos;
1507 template<class InputIter>
1508 iterator insertImplDiscr(const_iterator i,
1509 InputIter b, InputIter e, Selector<0>) {
1510 return insertImpl(i, b, e,
1511 typename std::iterator_traits<InputIter>::iterator_category());
1514 template <class FwdIterator>
1515 iterator insertImpl(const_iterator i,
1516 FwdIterator s1, FwdIterator s2, std::forward_iterator_tag) {
1517 Invariant checker(*this);
1519 const size_type pos = i - begin();
1520 const typename std::iterator_traits<FwdIterator>::difference_type n2 =
1521 std::distance(s1, s2);
1523 using namespace fbstring_detail;
1524 assert(pos <= size());
1526 const typename std::iterator_traits<FwdIterator>::difference_type maxn2 =
1527 capacity() - size();
1529 // realloc the string
1530 reserve(size() + n2);
1533 if (pos + n2 <= size()) {
1534 const iterator tailBegin = end() - n2;
1535 store_.expand_noinit(n2);
1536 fbstring_detail::pod_copy(tailBegin, tailBegin + n2, end() - n2);
1537 std::copy(const_reverse_iterator(tailBegin), const_reverse_iterator(i),
1538 reverse_iterator(tailBegin + n2));
1539 std::copy(s1, s2, begin() + pos);
1542 const size_type old_size = size();
1543 std::advance(t, old_size - pos);
1544 const size_t newElems = std::distance(t, s2);
1545 store_.expand_noinit(n2);
1546 std::copy(t, s2, begin() + old_size);
1547 fbstring_detail::pod_copy(data() + pos, data() + old_size,
1548 begin() + old_size + newElems);
1549 std::copy(s1, t, begin() + pos);
1551 store_.writeTerminator();
1552 return begin() + pos;
1555 template <class InputIterator>
1556 iterator insertImpl(const_iterator i,
1557 InputIterator b, InputIterator e,
1558 std::input_iterator_tag) {
1559 const auto pos = i - begin();
1560 basic_fbstring temp(begin(), i);
1561 for (; b != e; ++b) {
1564 temp.append(i, cend());
1566 return begin() + pos;
1570 template <class ItOrLength, class ItOrChar>
1571 iterator insert(const_iterator p, ItOrLength first_or_n, ItOrChar last_or_c) {
1572 Selector<std::numeric_limits<ItOrLength>::is_specialized> sel;
1573 return insertImplDiscr(p, first_or_n, last_or_c, sel);
1576 iterator insert(const_iterator p, std::initializer_list<value_type> il) {
1577 return insert(p, il.begin(), il.end());
1580 basic_fbstring& erase(size_type pos = 0, size_type n = npos) {
1581 Invariant checker(*this);
1583 enforce(pos <= length(), std::__throw_out_of_range, "");
1584 procrustes(n, length() - pos);
1585 std::copy(begin() + pos + n, end(), begin() + pos);
1586 resize(length() - n);
1590 iterator erase(iterator position) {
1591 const size_type pos(position - begin());
1592 enforce(pos <= size(), std::__throw_out_of_range, "");
1594 return begin() + pos;
1597 iterator erase(iterator first, iterator last) {
1598 const size_type pos(first - begin());
1599 erase(pos, last - first);
1600 return begin() + pos;
1603 // Replaces at most n1 chars of *this, starting with pos1 with the
1605 basic_fbstring& replace(size_type pos1, size_type n1,
1606 const basic_fbstring& str) {
1607 return replace(pos1, n1, str.data(), str.size());
1610 // Replaces at most n1 chars of *this, starting with pos1,
1611 // with at most n2 chars of str starting with pos2
1612 basic_fbstring& replace(size_type pos1, size_type n1,
1613 const basic_fbstring& str,
1614 size_type pos2, size_type n2) {
1615 enforce(pos2 <= str.length(), std::__throw_out_of_range, "");
1616 return replace(pos1, n1, str.data() + pos2,
1617 std::min(n2, str.size() - pos2));
1620 // Replaces at most n1 chars of *this, starting with pos, with chars from s
1621 basic_fbstring& replace(size_type pos, size_type n1, const value_type* s) {
1622 return replace(pos, n1, s, traits_type::length(s));
1625 // Replaces at most n1 chars of *this, starting with pos, with n2
1628 // consolidated with
1630 // Replaces at most n1 chars of *this, starting with pos, with at
1631 // most n2 chars of str. str must have at least n2 chars.
1632 template <class StrOrLength, class NumOrChar>
1633 basic_fbstring& replace(size_type pos, size_type n1,
1634 StrOrLength s_or_n2, NumOrChar n_or_c) {
1635 Invariant checker(*this);
1637 enforce(pos <= size(), std::__throw_out_of_range, "");
1638 procrustes(n1, length() - pos);
1639 const iterator b = begin() + pos;
1640 return replace(b, b + n1, s_or_n2, n_or_c);
1643 basic_fbstring& replace(iterator i1, iterator i2, const basic_fbstring& str) {
1644 return replace(i1, i2, str.data(), str.length());
1647 basic_fbstring& replace(iterator i1, iterator i2, const value_type* s) {
1648 return replace(i1, i2, s, traits_type::length(s));
1652 basic_fbstring& replaceImplDiscr(iterator i1, iterator i2,
1653 const value_type* s, size_type n,
1656 assert(begin() <= i1 && i1 <= end());
1657 assert(begin() <= i2 && i2 <= end());
1658 return replace(i1, i2, s, s + n);
1661 basic_fbstring& replaceImplDiscr(iterator i1, iterator i2,
1662 size_type n2, value_type c, Selector<1>) {
1663 const size_type n1 = i2 - i1;
1665 std::fill(i1, i1 + n2, c);
1668 std::fill(i1, i2, c);
1669 insert(i2, n2 - n1, c);
1675 template <class InputIter>
1676 basic_fbstring& replaceImplDiscr(iterator i1, iterator i2,
1677 InputIter b, InputIter e,
1679 replaceImpl(i1, i2, b, e,
1680 typename std::iterator_traits<InputIter>::iterator_category());
1685 template <class FwdIterator>
1686 bool replaceAliased(iterator i1, iterator i2,
1687 FwdIterator s1, FwdIterator s2, std::false_type) {
1691 template <class FwdIterator>
1692 bool replaceAliased(iterator i1, iterator i2,
1693 FwdIterator s1, FwdIterator s2, std::true_type) {
1694 static const std::less_equal<const value_type*> le =
1695 std::less_equal<const value_type*>();
1696 const bool aliased = le(&*begin(), &*s1) && le(&*s1, &*end());
1700 // Aliased replace, copy to new string
1701 basic_fbstring temp;
1702 temp.reserve(size() - (i2 - i1) + std::distance(s1, s2));
1703 temp.append(begin(), i1).append(s1, s2).append(i2, end());
1708 template <class FwdIterator>
1709 void replaceImpl(iterator i1, iterator i2,
1710 FwdIterator s1, FwdIterator s2, std::forward_iterator_tag) {
1711 Invariant checker(*this);
1714 // Handle aliased replace
1715 if (replaceAliased(i1, i2, s1, s2,
1716 std::integral_constant<bool,
1717 std::is_same<FwdIterator, iterator>::value ||
1718 std::is_same<FwdIterator, const_iterator>::value>())) {
1722 auto const n1 = i2 - i1;
1724 auto const n2 = std::distance(s1, s2);
1729 std::copy(s1, s2, i1);
1733 fbstring_detail::copy_n(s1, n1, i1);
1734 std::advance(s1, n1);
1740 template <class InputIterator>
1741 void replaceImpl(iterator i1, iterator i2,
1742 InputIterator b, InputIterator e, std::input_iterator_tag) {
1743 basic_fbstring temp(begin(), i1);
1744 temp.append(b, e).append(i2, end());
1749 template <class T1, class T2>
1750 basic_fbstring& replace(iterator i1, iterator i2,
1751 T1 first_or_n_or_s, T2 last_or_c_or_n) {
1753 num1 = std::numeric_limits<T1>::is_specialized,
1754 num2 = std::numeric_limits<T2>::is_specialized;
1755 return replaceImplDiscr(
1756 i1, i2, first_or_n_or_s, last_or_c_or_n,
1757 Selector<num1 ? (num2 ? 1 : -1) : (num2 ? 2 : 0)>());
1760 size_type copy(value_type* s, size_type n, size_type pos = 0) const {
1761 enforce(pos <= size(), std::__throw_out_of_range, "");
1762 procrustes(n, size() - pos);
1764 fbstring_detail::pod_copy(
1771 void swap(basic_fbstring& rhs) {
1772 store_.swap(rhs.store_);
1775 const value_type* c_str() const {
1776 return store_.c_str();
1779 const value_type* data() const { return c_str(); }
1781 allocator_type get_allocator() const {
1782 return allocator_type();
1785 size_type find(const basic_fbstring& str, size_type pos = 0) const {
1786 return find(str.data(), pos, str.length());
1789 size_type find(const value_type* needle, const size_type pos,
1790 const size_type nsize) const {
1791 if (!nsize) return pos;
1792 auto const size = this->size();
1793 // nsize + pos can overflow (eg pos == npos), guard against that by checking
1794 // that nsize + pos does not wrap around.
1795 if (nsize + pos > size || nsize + pos < pos) return npos;
1796 // Don't use std::search, use a Boyer-Moore-like trick by comparing
1797 // the last characters first
1798 auto const haystack = data();
1799 auto const nsize_1 = nsize - 1;
1800 auto const lastNeedle = needle[nsize_1];
1802 // Boyer-Moore skip value for the last char in the needle. Zero is
1803 // not a valid value; skip will be computed the first time it's
1807 const E * i = haystack + pos;
1808 auto iEnd = haystack + size - nsize_1;
1811 // Boyer-Moore: match the last element in the needle
1812 while (i[nsize_1] != lastNeedle) {
1818 // Here we know that the last char matches
1819 // Continue in pedestrian mode
1820 for (size_t j = 0; ; ) {
1822 if (i[j] != needle[j]) {
1823 // Not found, we can skip
1824 // Compute the skip value lazily
1827 while (skip <= nsize_1 && needle[nsize_1 - skip] != lastNeedle) {
1834 // Check if done searching
1837 return i - haystack;
1844 size_type find(const value_type* s, size_type pos = 0) const {
1845 return find(s, pos, traits_type::length(s));
1848 size_type find (value_type c, size_type pos = 0) const {
1849 return find(&c, pos, 1);
1852 size_type rfind(const basic_fbstring& str, size_type pos = npos) const {
1853 return rfind(str.data(), pos, str.length());
1856 size_type rfind(const value_type* s, size_type pos, size_type n) const {
1857 if (n > length()) return npos;
1858 pos = std::min(pos, length() - n);
1859 if (n == 0) return pos;
1861 const_iterator i(begin() + pos);
1863 if (traits_type::eq(*i, *s)
1864 && traits_type::compare(&*i, s, n) == 0) {
1867 if (i == begin()) break;
1872 size_type rfind(const value_type* s, size_type pos = npos) const {
1873 return rfind(s, pos, traits_type::length(s));
1876 size_type rfind(value_type c, size_type pos = npos) const {
1877 return rfind(&c, pos, 1);
1880 size_type find_first_of(const basic_fbstring& str, size_type pos = 0) const {
1881 return find_first_of(str.data(), pos, str.length());
1884 size_type find_first_of(const value_type* s,
1885 size_type pos, size_type n) const {
1886 if (pos > length() || n == 0) return npos;
1887 const_iterator i(begin() + pos),
1889 for (; i != finish; ++i) {
1890 if (traits_type::find(s, n, *i) != 0) {
1897 size_type find_first_of(const value_type* s, size_type pos = 0) const {
1898 return find_first_of(s, pos, traits_type::length(s));
1901 size_type find_first_of(value_type c, size_type pos = 0) const {
1902 return find_first_of(&c, pos, 1);
1905 size_type find_last_of (const basic_fbstring& str,
1906 size_type pos = npos) const {
1907 return find_last_of(str.data(), pos, str.length());
1910 size_type find_last_of (const value_type* s, size_type pos,
1911 size_type n) const {
1912 if (!empty() && n > 0) {
1913 pos = std::min(pos, length() - 1);
1914 const_iterator i(begin() + pos);
1916 if (traits_type::find(s, n, *i) != 0) {
1919 if (i == begin()) break;
1925 size_type find_last_of (const value_type* s,
1926 size_type pos = npos) const {
1927 return find_last_of(s, pos, traits_type::length(s));
1930 size_type find_last_of (value_type c, size_type pos = npos) const {
1931 return find_last_of(&c, pos, 1);
1934 size_type find_first_not_of(const basic_fbstring& str,
1935 size_type pos = 0) const {
1936 return find_first_not_of(str.data(), pos, str.size());
1939 size_type find_first_not_of(const value_type* s, size_type pos,
1940 size_type n) const {
1941 if (pos < length()) {
1945 for (; i != finish; ++i) {
1946 if (traits_type::find(s, n, *i) == 0) {
1954 size_type find_first_not_of(const value_type* s,
1955 size_type pos = 0) const {
1956 return find_first_not_of(s, pos, traits_type::length(s));
1959 size_type find_first_not_of(value_type c, size_type pos = 0) const {
1960 return find_first_not_of(&c, pos, 1);
1963 size_type find_last_not_of(const basic_fbstring& str,
1964 size_type pos = npos) const {
1965 return find_last_not_of(str.data(), pos, str.length());
1968 size_type find_last_not_of(const value_type* s, size_type pos,
1969 size_type n) const {
1970 if (!this->empty()) {
1971 pos = std::min(pos, size() - 1);
1972 const_iterator i(begin() + pos);
1974 if (traits_type::find(s, n, *i) == 0) {
1977 if (i == begin()) break;
1983 size_type find_last_not_of(const value_type* s,
1984 size_type pos = npos) const {
1985 return find_last_not_of(s, pos, traits_type::length(s));
1988 size_type find_last_not_of (value_type c, size_type pos = npos) const {
1989 return find_last_not_of(&c, pos, 1);
1992 basic_fbstring substr(size_type pos = 0, size_type n = npos) const& {
1993 enforce(pos <= size(), std::__throw_out_of_range, "");
1994 return basic_fbstring(data() + pos, std::min(n, size() - pos));
1997 basic_fbstring substr(size_type pos = 0, size_type n = npos) && {
1998 enforce(pos <= size(), std::__throw_out_of_range, "");
2000 if (n < size()) resize(n);
2001 return std::move(*this);
2004 int compare(const basic_fbstring& str) const {
2005 // FIX due to Goncalo N M de Carvalho July 18, 2005
2006 return compare(0, size(), str);
2009 int compare(size_type pos1, size_type n1,
2010 const basic_fbstring& str) const {
2011 return compare(pos1, n1, str.data(), str.size());
2014 int compare(size_type pos1, size_type n1,
2015 const value_type* s) const {
2016 return compare(pos1, n1, s, traits_type::length(s));
2019 int compare(size_type pos1, size_type n1,
2020 const value_type* s, size_type n2) const {
2021 enforce(pos1 <= size(), std::__throw_out_of_range, "");
2022 procrustes(n1, size() - pos1);
2023 // The line below fixed by Jean-Francois Bastien, 04-23-2007. Thanks!
2024 const int r = traits_type::compare(pos1 + data(), s, std::min(n1, n2));
2025 return r != 0 ? r : n1 > n2 ? 1 : n1 < n2 ? -1 : 0;
2028 int compare(size_type pos1, size_type n1,
2029 const basic_fbstring& str,
2030 size_type pos2, size_type n2) const {
2031 enforce(pos2 <= str.size(), std::__throw_out_of_range, "");
2032 return compare(pos1, n1, str.data() + pos2,
2033 std::min(n2, str.size() - pos2));
2036 // Code from Jean-Francois Bastien (03/26/2007)
2037 int compare(const value_type* s) const {
2038 // Could forward to compare(0, size(), s, traits_type::length(s))
2039 // but that does two extra checks
2040 const size_type n1(size()), n2(traits_type::length(s));
2041 const int r = traits_type::compare(data(), s, std::min(n1, n2));
2042 return r != 0 ? r : n1 > n2 ? 1 : n1 < n2 ? -1 : 0;
2050 // non-member functions
2052 template <typename E, class T, class A, class S>
2054 basic_fbstring<E, T, A, S> operator+(const basic_fbstring<E, T, A, S>& lhs,
2055 const basic_fbstring<E, T, A, S>& rhs) {
2057 basic_fbstring<E, T, A, S> result;
2058 result.reserve(lhs.size() + rhs.size());
2059 result.append(lhs).append(rhs);
2060 return std::move(result);
2064 template <typename E, class T, class A, class S>
2066 basic_fbstring<E, T, A, S> operator+(basic_fbstring<E, T, A, S>&& lhs,
2067 const basic_fbstring<E, T, A, S>& rhs) {
2068 return std::move(lhs.append(rhs));
2072 template <typename E, class T, class A, class S>
2074 basic_fbstring<E, T, A, S> operator+(const basic_fbstring<E, T, A, S>& lhs,
2075 basic_fbstring<E, T, A, S>&& rhs) {
2076 if (rhs.capacity() >= lhs.size() + rhs.size()) {
2077 // Good, at least we don't need to reallocate
2078 return std::move(rhs.insert(0, lhs));
2080 // Meh, no go. Forward to operator+(const&, const&).
2081 auto const& rhsC = rhs;
2086 template <typename E, class T, class A, class S>
2088 basic_fbstring<E, T, A, S> operator+(basic_fbstring<E, T, A, S>&& lhs,
2089 basic_fbstring<E, T, A, S>&& rhs) {
2090 return std::move(lhs.append(rhs));
2094 template <typename E, class T, class A, class S>
2096 basic_fbstring<E, T, A, S> operator+(
2098 const basic_fbstring<E, T, A, S>& rhs) {
2100 basic_fbstring<E, T, A, S> result;
2101 const auto len = basic_fbstring<E, T, A, S>::traits_type::length(lhs);
2102 result.reserve(len + rhs.size());
2103 result.append(lhs, len).append(rhs);
2108 template <typename E, class T, class A, class S>
2110 basic_fbstring<E, T, A, S> operator+(
2112 basic_fbstring<E, T, A, S>&& rhs) {
2114 const auto len = basic_fbstring<E, T, A, S>::traits_type::length(lhs);
2115 if (rhs.capacity() >= len + rhs.size()) {
2116 // Good, at least we don't need to reallocate
2117 rhs.insert(rhs.begin(), lhs, lhs + len);
2120 // Meh, no go. Do it by hand since we have len already.
2121 basic_fbstring<E, T, A, S> result;
2122 result.reserve(len + rhs.size());
2123 result.append(lhs, len).append(rhs);
2128 template <typename E, class T, class A, class S>
2130 basic_fbstring<E, T, A, S> operator+(
2132 const basic_fbstring<E, T, A, S>& rhs) {
2134 basic_fbstring<E, T, A, S> result;
2135 result.reserve(1 + rhs.size());
2136 result.push_back(lhs);
2142 template <typename E, class T, class A, class S>
2144 basic_fbstring<E, T, A, S> operator+(
2146 basic_fbstring<E, T, A, S>&& rhs) {
2148 if (rhs.capacity() > rhs.size()) {
2149 // Good, at least we don't need to reallocate
2150 rhs.insert(rhs.begin(), lhs);
2153 // Meh, no go. Forward to operator+(E, const&).
2154 auto const& rhsC = rhs;
2159 template <typename E, class T, class A, class S>
2161 basic_fbstring<E, T, A, S> operator+(
2162 const basic_fbstring<E, T, A, S>& lhs,
2165 typedef typename basic_fbstring<E, T, A, S>::size_type size_type;
2166 typedef typename basic_fbstring<E, T, A, S>::traits_type traits_type;
2168 basic_fbstring<E, T, A, S> result;
2169 const size_type len = traits_type::length(rhs);
2170 result.reserve(lhs.size() + len);
2171 result.append(lhs).append(rhs, len);
2175 // C++11 21.4.8.1/10
2176 template <typename E, class T, class A, class S>
2178 basic_fbstring<E, T, A, S> operator+(
2179 basic_fbstring<E, T, A, S>&& lhs,
2182 return std::move(lhs += rhs);
2185 // C++11 21.4.8.1/11
2186 template <typename E, class T, class A, class S>
2188 basic_fbstring<E, T, A, S> operator+(
2189 const basic_fbstring<E, T, A, S>& lhs,
2192 basic_fbstring<E, T, A, S> result;
2193 result.reserve(lhs.size() + 1);
2195 result.push_back(rhs);
2199 // C++11 21.4.8.1/12
2200 template <typename E, class T, class A, class S>
2202 basic_fbstring<E, T, A, S> operator+(
2203 basic_fbstring<E, T, A, S>&& lhs,
2206 return std::move(lhs += rhs);
2209 template <typename E, class T, class A, class S>
2211 bool operator==(const basic_fbstring<E, T, A, S>& lhs,
2212 const basic_fbstring<E, T, A, S>& rhs) {
2213 return lhs.size() == rhs.size() && lhs.compare(rhs) == 0; }
2215 template <typename E, class T, class A, class S>
2217 bool operator==(const typename basic_fbstring<E, T, A, S>::value_type* lhs,
2218 const basic_fbstring<E, T, A, S>& rhs) {
2219 return rhs == lhs; }
2221 template <typename E, class T, class A, class S>
2223 bool operator==(const basic_fbstring<E, T, A, S>& lhs,
2224 const typename basic_fbstring<E, T, A, S>::value_type* rhs) {
2225 return lhs.compare(rhs) == 0; }
2227 template <typename E, class T, class A, class S>
2229 bool operator!=(const basic_fbstring<E, T, A, S>& lhs,
2230 const basic_fbstring<E, T, A, S>& rhs) {
2231 return !(lhs == rhs); }
2233 template <typename E, class T, class A, class S>
2235 bool operator!=(const typename basic_fbstring<E, T, A, S>::value_type* lhs,
2236 const basic_fbstring<E, T, A, S>& rhs) {
2237 return !(lhs == rhs); }
2239 template <typename E, class T, class A, class S>
2241 bool operator!=(const basic_fbstring<E, T, A, S>& lhs,
2242 const typename basic_fbstring<E, T, A, S>::value_type* rhs) {
2243 return !(lhs == rhs); }
2245 template <typename E, class T, class A, class S>
2247 bool operator<(const basic_fbstring<E, T, A, S>& lhs,
2248 const basic_fbstring<E, T, A, S>& rhs) {
2249 return lhs.compare(rhs) < 0; }
2251 template <typename E, class T, class A, class S>
2253 bool operator<(const basic_fbstring<E, T, A, S>& lhs,
2254 const typename basic_fbstring<E, T, A, S>::value_type* rhs) {
2255 return lhs.compare(rhs) < 0; }
2257 template <typename E, class T, class A, class S>
2259 bool operator<(const typename basic_fbstring<E, T, A, S>::value_type* lhs,
2260 const basic_fbstring<E, T, A, S>& rhs) {
2261 return rhs.compare(lhs) > 0; }
2263 template <typename E, class T, class A, class S>
2265 bool operator>(const basic_fbstring<E, T, A, S>& lhs,
2266 const basic_fbstring<E, T, A, S>& rhs) {
2269 template <typename E, class T, class A, class S>
2271 bool operator>(const basic_fbstring<E, T, A, S>& lhs,
2272 const typename basic_fbstring<E, T, A, S>::value_type* rhs) {
2275 template <typename E, class T, class A, class S>
2277 bool operator>(const typename basic_fbstring<E, T, A, S>::value_type* lhs,
2278 const basic_fbstring<E, T, A, S>& rhs) {
2281 template <typename E, class T, class A, class S>
2283 bool operator<=(const basic_fbstring<E, T, A, S>& lhs,
2284 const basic_fbstring<E, T, A, S>& rhs) {
2285 return !(rhs < lhs); }
2287 template <typename E, class T, class A, class S>
2289 bool operator<=(const basic_fbstring<E, T, A, S>& lhs,
2290 const typename basic_fbstring<E, T, A, S>::value_type* rhs) {
2291 return !(rhs < lhs); }
2293 template <typename E, class T, class A, class S>
2295 bool operator<=(const typename basic_fbstring<E, T, A, S>::value_type* lhs,
2296 const basic_fbstring<E, T, A, S>& rhs) {
2297 return !(rhs < lhs); }
2299 template <typename E, class T, class A, class S>
2301 bool operator>=(const basic_fbstring<E, T, A, S>& lhs,
2302 const basic_fbstring<E, T, A, S>& rhs) {
2303 return !(lhs < rhs); }
2305 template <typename E, class T, class A, class S>
2307 bool operator>=(const basic_fbstring<E, T, A, S>& lhs,
2308 const typename basic_fbstring<E, T, A, S>::value_type* rhs) {
2309 return !(lhs < rhs); }
2311 template <typename E, class T, class A, class S>
2313 bool operator>=(const typename basic_fbstring<E, T, A, S>::value_type* lhs,
2314 const basic_fbstring<E, T, A, S>& rhs) {
2315 return !(lhs < rhs);
2319 template <typename E, class T, class A, class S>
2320 void swap(basic_fbstring<E, T, A, S>& lhs, basic_fbstring<E, T, A, S>& rhs) {
2324 // TODO: make this faster.
2325 template <typename E, class T, class A, class S>
2328 typename basic_fbstring<E, T, A, S>::value_type,
2329 typename basic_fbstring<E, T, A, S>::traits_type>&
2331 std::basic_istream<typename basic_fbstring<E, T, A, S>::value_type,
2332 typename basic_fbstring<E, T, A, S>::traits_type>& is,
2333 basic_fbstring<E, T, A, S>& str) {
2334 typename std::basic_istream<E, T>::sentry sentry(is);
2335 typedef std::basic_istream<typename basic_fbstring<E, T, A, S>::value_type,
2336 typename basic_fbstring<E, T, A, S>::traits_type>
2338 typedef typename __istream_type::ios_base __ios_base;
2339 size_t extracted = 0;
2340 auto err = __ios_base::goodbit;
2342 auto n = is.width();
2347 for (auto got = is.rdbuf()->sgetc(); extracted != size_t(n); ++extracted) {
2348 if (got == T::eof()) {
2349 err |= __ios_base::eofbit;
2353 if (isspace(got)) break;
2355 got = is.rdbuf()->snextc();
2359 err |= __ios_base::failbit;
2367 template <typename E, class T, class A, class S>
2369 std::basic_ostream<typename basic_fbstring<E, T, A, S>::value_type,
2370 typename basic_fbstring<E, T, A, S>::traits_type>&
2372 std::basic_ostream<typename basic_fbstring<E, T, A, S>::value_type,
2373 typename basic_fbstring<E, T, A, S>::traits_type>& os,
2374 const basic_fbstring<E, T, A, S>& str) {
2376 typename std::basic_ostream<
2377 typename basic_fbstring<E, T, A, S>::value_type,
2378 typename basic_fbstring<E, T, A, S>::traits_type>::sentry __s(os);
2380 typedef std::ostreambuf_iterator<
2381 typename basic_fbstring<E, T, A, S>::value_type,
2382 typename basic_fbstring<E, T, A, S>::traits_type> _Ip;
2383 size_t __len = str.size();
2385 (os.flags() & std::ios_base::adjustfield) == std::ios_base::left;
2386 if (__pad_and_output(_Ip(os),
2388 __left ? str.data() + __len : str.data(),
2391 os.fill()).failed()) {
2392 os.setstate(std::ios_base::badbit | std::ios_base::failbit);
2395 #elif defined(_MSC_VER)
2396 // MSVC doesn't define __ostream_insert
2397 os.write(str.data(), str.size());
2399 std::__ostream_insert(os, str.data(), str.size());
2404 #ifndef _LIBSTDCXX_FBSTRING
2406 template <typename E, class T, class A, class S>
2408 std::basic_istream<typename basic_fbstring<E, T, A, S>::value_type,
2409 typename basic_fbstring<E, T, A, S>::traits_type>&
2411 std::basic_istream<typename basic_fbstring<E, T, A, S>::value_type,
2412 typename basic_fbstring<E, T, A, S>::traits_type>& is,
2413 basic_fbstring<E, T, A, S>& str,
2414 typename basic_fbstring<E, T, A, S>::value_type delim) {
2415 // Use the nonstandard getdelim()
2416 char * buf = nullptr;
2419 // This looks quadratic but it really depends on realloc
2420 auto const newSize = size + 128;
2421 buf = static_cast<char*>(checkedRealloc(buf, newSize));
2422 is.getline(buf + size, newSize - size, delim);
2423 if (is.bad() || is.eof() || !is.fail()) {
2424 // done by either failure, end of file, or normal read
2425 size += std::strlen(buf + size);
2428 // Here we have failed due to too short a buffer
2429 // Minus one to discount the terminating '\0'
2431 assert(buf[size] == 0);
2432 // Clear the error so we can continue reading
2435 basic_fbstring<E, T, A, S> result(buf, size, size + 1,
2436 AcquireMallocatedString());
2441 template <typename E, class T, class A, class S>
2443 std::basic_istream<typename basic_fbstring<E, T, A, S>::value_type,
2444 typename basic_fbstring<E, T, A, S>::traits_type>&
2446 std::basic_istream<typename basic_fbstring<E, T, A, S>::value_type,
2447 typename basic_fbstring<E, T, A, S>::traits_type>& is,
2448 basic_fbstring<E, T, A, S>& str) {
2449 // Just forward to the version with a delimiter
2450 return getline(is, str, '\n');
2455 template <typename E1, class T, class A, class S>
2456 const typename basic_fbstring<E1, T, A, S>::size_type
2457 basic_fbstring<E1, T, A, S>::npos =
2458 static_cast<typename basic_fbstring<E1, T, A, S>::size_type>(-1);
2460 #ifndef _LIBSTDCXX_FBSTRING
2461 // basic_string compatibility routines
2463 template <typename E, class T, class A, class S>
2465 bool operator==(const basic_fbstring<E, T, A, S>& lhs,
2466 const std::string& rhs) {
2467 return lhs.compare(0, lhs.size(), rhs.data(), rhs.size()) == 0;
2470 template <typename E, class T, class A, class S>
2472 bool operator==(const std::string& lhs,
2473 const basic_fbstring<E, T, A, S>& rhs) {
2477 template <typename E, class T, class A, class S>
2479 bool operator!=(const basic_fbstring<E, T, A, S>& lhs,
2480 const std::string& rhs) {
2481 return !(lhs == rhs);
2484 template <typename E, class T, class A, class S>
2486 bool operator!=(const std::string& lhs,
2487 const basic_fbstring<E, T, A, S>& rhs) {
2488 return !(lhs == rhs);
2491 #if !defined(_LIBSTDCXX_FBSTRING)
2492 typedef basic_fbstring<char> fbstring;
2495 // fbstring is relocatable
2496 template <class T, class R, class A, class S>
2497 FOLLY_ASSUME_RELOCATABLE(basic_fbstring<T, R, A, S>);
2500 _GLIBCXX_END_NAMESPACE_VERSION
2503 } // namespace folly
2505 #ifndef _LIBSTDCXX_FBSTRING
2507 // Hash functions to make fbstring usable with e.g. hash_map
2509 // Handle interaction with different C++ standard libraries, which
2510 // expect these types to be in different namespaces.
2512 #define FOLLY_FBSTRING_HASH1(T) \
2514 struct hash< ::folly::basic_fbstring<T> > { \
2515 size_t operator()(const ::folly::fbstring& s) const { \
2516 return ::folly::hash::fnv32_buf(s.data(), s.size()); \
2520 // The C++11 standard says that these four are defined
2521 #define FOLLY_FBSTRING_HASH \
2522 FOLLY_FBSTRING_HASH1(char) \
2523 FOLLY_FBSTRING_HASH1(char16_t) \
2524 FOLLY_FBSTRING_HASH1(char32_t) \
2525 FOLLY_FBSTRING_HASH1(wchar_t)
2533 #if FOLLY_HAVE_DEPRECATED_ASSOC
2534 #if defined(_GLIBCXX_SYMVER) && !defined(__BIONIC__)
2535 namespace __gnu_cxx {
2539 } // namespace __gnu_cxx
2540 #endif // _GLIBCXX_SYMVER && !__BIONIC__
2541 #endif // FOLLY_HAVE_DEPRECATED_ASSOC
2543 #undef FOLLY_FBSTRING_HASH
2544 #undef FOLLY_FBSTRING_HASH1
2546 #endif // _LIBSTDCXX_FBSTRING
2548 #pragma GCC diagnostic pop
2550 #undef FBSTRING_DISABLE_ADDRESS_SANITIZER
2552 #undef FBSTRING_LIKELY
2553 #undef FBSTRING_UNLIKELY
2555 #ifdef FOLLY_DEFINED_NDEBUG_FOR_FBSTRING
2557 #undef FOLLY_DEFINED_NDEBUG_FOR_FBSTRING
2558 #endif // FOLLY_DEFINED_NDEBUG_FOR_FBSTRING
2560 #endif // FOLLY_BASE_FBSTRING_H_