2f025154422ca7400eb84af76917d439709b2b8a
[folly.git] / folly / FBString.h
1 /*
2  * Copyright 2016 Facebook, Inc.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 // @author: Andrei Alexandrescu (aalexandre)
18 // String type.
19
20 #ifndef FOLLY_BASE_FBSTRING_H_
21 #define FOLLY_BASE_FBSTRING_H_
22
23 #include <atomic>
24 #include <limits>
25 #include <type_traits>
26
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
32
33 #pragma GCC system_header
34
35 // When used as std::string replacement always disable assertions.
36 #ifndef NDEBUG
37 #define NDEBUG
38 #define FOLLY_DEFINED_NDEBUG_FOR_FBSTRING
39 #endif // NDEBUG
40
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
46 #else
47 #include "basic_fbstring_malloc.h" // nolint
48 #undef FOLLY_MALLOC_H_
49 #endif
50
51 #else // !_LIBSTDCXX_FBSTRING
52
53 #include <folly/Portability.h>
54
55 // libc++ doesn't provide this header, nor does msvc
56 #ifdef FOLLY_HAVE_BITS_CXXCONFIG_H
57 #include <bits/c++config.h>
58 #endif
59
60 #include <string>
61 #include <cstring>
62 #include <cassert>
63 #include <algorithm>
64
65 #include <folly/Traits.h>
66 #include <folly/Malloc.h>
67 #include <folly/Hash.h>
68 #include <folly/ScopeGuard.h>
69
70 #if FOLLY_HAVE_DEPRECATED_ASSOC
71 #ifdef _GLIBCXX_SYMVER
72 #include <ext/hash_set>
73 #include <ext/hash_map>
74 #endif
75 #endif
76
77 #endif
78
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))
84 #else
85 #define FBSTRING_LIKELY(x)   (x)
86 #define FBSTRING_UNLIKELY(x) (x)
87 #endif
88
89 #pragma GCC diagnostic push
90 // Ignore shadowing warnings within this file, so includers can use -Wshadow.
91 #pragma GCC diagnostic ignored "-Wshadow"
92 // GCC 4.9 has a false positive in setSmallSize (probably
93 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59124), disable
94 // compile-time array bound checking.
95 #pragma GCC diagnostic ignored "-Warray-bounds"
96
97 // FBString cannot use throw when replacing std::string, though it may still
98 // use std::__throw_*
99 // nolint
100 #define throw FOLLY_FBSTRING_MAY_NOT_USE_THROW
101
102 #ifdef _LIBSTDCXX_FBSTRING
103 namespace std _GLIBCXX_VISIBILITY(default) {
104 _GLIBCXX_BEGIN_NAMESPACE_VERSION
105 #else
106 namespace folly {
107 #endif
108
109 #if defined(__clang__)
110 # if __has_feature(address_sanitizer)
111 #  define FBSTRING_SANITIZE_ADDRESS
112 # endif
113 #elif defined (__GNUC__) && \
114       (((__GNUC__ == 4) && (__GNUC_MINOR__ >= 8)) || (__GNUC__ >= 5)) && \
115       __SANITIZE_ADDRESS__
116 # define FBSTRING_SANITIZE_ADDRESS
117 #endif
118
119 // When compiling with ASan, always heap-allocate the string even if
120 // it would fit in-situ, so that ASan can detect access to the string
121 // buffer after it has been invalidated (destroyed, resized, etc.).
122 // Note that this flag doesn't remove support for in-situ strings, as
123 // that would break ABI-compatibility and wouldn't allow linking code
124 // compiled with this flag with code compiled without.
125 #ifdef FBSTRING_SANITIZE_ADDRESS
126 # define FBSTRING_DISABLE_SMALL_STRING_OPTIMIZATION
127 #endif
128
129 namespace fbstring_detail {
130
131 template <class InIt, class OutIt>
132 inline
133 OutIt copy_n(InIt b,
134              typename std::iterator_traits<InIt>::difference_type n,
135              OutIt d) {
136   for (; n != 0; --n, ++b, ++d) {
137     *d = *b;
138   }
139   return d;
140 }
141
142 template <class Pod, class T>
143 inline void pod_fill(Pod* b, Pod* e, T c) {
144   assert(b && e && b <= e);
145   /*static*/ if (sizeof(T) == 1) {
146     memset(b, c, e - b);
147   } else {
148     auto const ee = b + ((e - b) & ~7u);
149     for (; b != ee; b += 8) {
150       b[0] = c;
151       b[1] = c;
152       b[2] = c;
153       b[3] = c;
154       b[4] = c;
155       b[5] = c;
156       b[6] = c;
157       b[7] = c;
158     }
159     // Leftovers
160     for (; b != e; ++b) {
161       *b = c;
162     }
163   }
164 }
165
166 /*
167  * Lightly structured memcpy, simplifies copying PODs and introduces
168  * some asserts. Unfortunately using this function may cause
169  * measurable overhead (presumably because it adjusts from a begin/end
170  * convention to a pointer/size convention, so it does some extra
171  * arithmetic even though the caller might have done the inverse
172  * adaptation outside).
173  */
174 template <class Pod>
175 inline void pod_copy(const Pod* b, const Pod* e, Pod* d) {
176   assert(e >= b);
177   assert(d >= e || d + (e - b) <= b);
178   memcpy(d, b, (e - b) * sizeof(Pod));
179 }
180
181 /*
182  * Lightly structured memmove, simplifies copying PODs and introduces
183  * some asserts
184  */
185 template <class Pod>
186 inline void pod_move(const Pod* b, const Pod* e, Pod* d) {
187   assert(e >= b);
188   memmove(d, b, (e - b) * sizeof(*b));
189 }
190
191 } // namespace fbstring_detail
192
193 /**
194  * Defines a special acquisition method for constructing fbstring
195  * objects. AcquireMallocatedString means that the user passes a
196  * pointer to a malloc-allocated string that the fbstring object will
197  * take into custody.
198  */
199 enum class AcquireMallocatedString {};
200
201 /*
202  * fbstring_core_model is a mock-up type that defines all required
203  * signatures of a fbstring core. The fbstring class itself uses such
204  * a core object to implement all of the numerous member functions
205  * required by the standard.
206  *
207  * If you want to define a new core, copy the definition below and
208  * implement the primitives. Then plug the core into basic_fbstring as
209  * a template argument.
210
211 template <class Char>
212 class fbstring_core_model {
213 public:
214   fbstring_core_model();
215   fbstring_core_model(const fbstring_core_model &);
216   ~fbstring_core_model();
217   // Returns a pointer to string's buffer (currently only contiguous
218   // strings are supported). The pointer is guaranteed to be valid
219   // until the next call to a non-const member function.
220   const Char * data() const;
221   // Much like data(), except the string is prepared to support
222   // character-level changes. This call is a signal for
223   // e.g. reference-counted implementation to fork the data. The
224   // pointer is guaranteed to be valid until the next call to a
225   // non-const member function.
226   Char * mutable_data();
227   // Returns a pointer to string's buffer and guarantees that a
228   // readable '\0' lies right after the buffer. The pointer is
229   // guaranteed to be valid until the next call to a non-const member
230   // function.
231   const Char * c_str() const;
232   // Shrinks the string by delta characters. Asserts that delta <=
233   // size().
234   void shrink(size_t delta);
235   // Expands the string by delta characters (i.e. after this call
236   // size() will report the old size() plus delta) but without
237   // initializing the expanded region. The expanded region is
238   // zero-terminated. Returns a pointer to the memory to be
239   // initialized (the beginning of the expanded portion). The caller
240   // is expected to fill the expanded area appropriately.
241   // If expGrowth is true, exponential growth is guaranteed.
242   // It is not guaranteed not to reallocate even if size() + delta <
243   // capacity(), so all references to the buffer are invalidated.
244   Char* expand_noinit(size_t delta, bool expGrowth);
245   // Expands the string by one character and sets the last character
246   // to c.
247   void push_back(Char c);
248   // Returns the string's size.
249   size_t size() const;
250   // Returns the string's capacity, i.e. maximum size that the string
251   // can grow to without reallocation. Note that for reference counted
252   // strings that's technically a lie - even assigning characters
253   // within the existing size would cause a reallocation.
254   size_t capacity() const;
255   // Returns true if the data underlying the string is actually shared
256   // across multiple strings (in a refcounted fashion).
257   bool isShared() const;
258   // Makes sure that at least minCapacity characters are available for
259   // the string without reallocation. For reference-counted strings,
260   // it should fork the data even if minCapacity < size().
261   void reserve(size_t minCapacity);
262 private:
263   // Do not implement
264   fbstring_core_model& operator=(const fbstring_core_model &);
265 };
266 */
267
268 /**
269  * This is the core of the string. The code should work on 32- and
270  * 64-bit and both big- and little-endianan architectures with any
271  * Char size.
272  *
273  * The storage is selected as follows (assuming we store one-byte
274  * characters on a 64-bit machine): (a) "small" strings between 0 and
275  * 23 chars are stored in-situ without allocation (the rightmost byte
276  * stores the size); (b) "medium" strings from 24 through 254 chars
277  * are stored in malloc-allocated memory that is copied eagerly; (c)
278  * "large" strings of 255 chars and above are stored in a similar
279  * structure as medium arrays, except that the string is
280  * reference-counted and copied lazily. the reference count is
281  * allocated right before the character array.
282  *
283  * The discriminator between these three strategies sits in two
284  * bits of the rightmost char of the storage. If neither is set, then the
285  * string is small (and its length sits in the lower-order bits on
286  * little-endian or the high-order bits on big-endian of that
287  * rightmost character). If the MSb is set, the string is medium width.
288  * If the second MSb is set, then the string is large. On little-endian,
289  * these 2 bits are the 2 MSbs of MediumLarge::capacity_, while on
290  * big-endian, these 2 bits are the 2 LSbs. This keeps both little-endian
291  * and big-endian fbstring_core equivalent with merely different ops used
292  * to extract capacity/category.
293  */
294 template <class Char> class fbstring_core {
295 protected:
296 // It's MSVC, so we just have to guess ... and allow an override
297 #ifdef _MSC_VER
298 # ifdef FOLLY_ENDIAN_BE
299   static constexpr auto kIsLittleEndian = false;
300 # else
301   static constexpr auto kIsLittleEndian = true;
302 # endif
303 #else
304   static constexpr auto kIsLittleEndian =
305       __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__;
306 #endif
307 public:
308   fbstring_core() noexcept { reset(); }
309
310   fbstring_core(const fbstring_core & rhs) {
311     assert(&rhs != this);
312     // Simplest case first: small strings are bitblitted
313     if (rhs.category() == Category::isSmall) {
314       static_assert(offsetof(MediumLarge, data_) == 0,
315           "fbstring layout failure");
316       static_assert(offsetof(MediumLarge, size_) == sizeof(ml_.data_),
317           "fbstring layout failure");
318       static_assert(offsetof(MediumLarge, capacity_) == 2 * sizeof(ml_.data_),
319           "fbstring layout failure");
320       // Just write the whole thing, don't look at details. In
321       // particular we need to copy capacity anyway because we want
322       // to set the size (don't forget that the last character,
323       // which stores a short string's length, is shared with the
324       // ml_.capacity field).
325       ml_ = rhs.ml_;
326       assert(category() == Category::isSmall && this->size() == rhs.size());
327     } else if (rhs.category() == Category::isLarge) {
328       // Large strings are just refcounted
329       ml_ = rhs.ml_;
330       RefCounted::incrementRefs(ml_.data_);
331       assert(category() == Category::isLarge && size() == rhs.size());
332     } else {
333       // Medium strings are copied eagerly. Don't forget to allocate
334       // one extra Char for the null terminator.
335       auto const allocSize =
336            goodMallocSize((1 + rhs.ml_.size_) * sizeof(Char));
337       ml_.data_ = static_cast<Char*>(checkedMalloc(allocSize));
338       // Also copies terminator.
339       fbstring_detail::pod_copy(rhs.ml_.data_,
340                                 rhs.ml_.data_ + rhs.ml_.size_ + 1,
341                                 ml_.data_);
342       ml_.size_ = rhs.ml_.size_;
343       ml_.setCapacity(allocSize / sizeof(Char) - 1, Category::isMedium);
344       assert(category() == Category::isMedium);
345     }
346     assert(size() == rhs.size());
347     assert(memcmp(data(), rhs.data(), size() * sizeof(Char)) == 0);
348   }
349
350   fbstring_core(fbstring_core&& goner) noexcept {
351     // Take goner's guts
352     ml_ = goner.ml_;
353     // Clean goner's carcass
354     goner.reset();
355   }
356
357   fbstring_core(const Char *const data, const size_t size) {
358 #ifndef NDEBUG
359 #ifndef _LIBSTDCXX_FBSTRING
360     SCOPE_EXIT {
361       assert(this->size() == size);
362       assert(memcmp(this->data(), data, size * sizeof(Char)) == 0);
363     };
364 #endif
365 #endif
366
367 #ifndef FBSTRING_DISABLE_SMALL_STRING_OPTIMIZATION
368     // Simplest case first: small strings are bitblitted
369     if (size <= maxSmallSize) {
370       // Layout is: Char* data_, size_t size_, size_t capacity_
371       static_assert(sizeof(*this) == sizeof(Char*) + 2 * sizeof(size_t),
372           "fbstring has unexpected size");
373       static_assert(sizeof(Char*) == sizeof(size_t),
374           "fbstring size assumption violation");
375       // sizeof(size_t) must be a power of 2
376       static_assert((sizeof(size_t) & (sizeof(size_t) - 1)) == 0,
377           "fbstring size assumption violation");
378
379       // If data is aligned, use fast word-wise copying. Otherwise,
380       // use conservative memcpy.
381       if (reinterpret_cast<size_t>(data) & (sizeof(size_t) - 1)) {
382         fbstring_detail::pod_copy(data, data + size, small_);
383       } else {
384         // Copy one word at a time.
385         // NOTE: This reads bytes which are outside the range of the
386         // string, and makes ASan unhappy, but the small case is
387         // disabled under ASan.
388
389         const size_t byteSize = size * sizeof(Char);
390         constexpr size_t wordWidth = sizeof(size_t);
391         switch ((byteSize + wordWidth - 1) / wordWidth) { // Number of words.
392         case 3:
393           ml_.capacity_ = reinterpret_cast<const size_t*>(data)[2];
394         case 2:
395           ml_.size_ = reinterpret_cast<const size_t*>(data)[1];
396         case 1:
397           ml_.data_ = *reinterpret_cast<Char**>(const_cast<Char*>(data));
398         case 0:
399           break;
400         }
401       }
402       setSmallSize(size);
403     } else
404 #endif // FBSTRING_DISABLE_SMALL_STRING_OPTIMIZATION
405     {
406       if (size <= maxMediumSize) {
407         // Medium strings are allocated normally. Don't forget to
408         // allocate one extra Char for the terminating null.
409         auto const allocSize = goodMallocSize((1 + size) * sizeof(Char));
410         ml_.data_ = static_cast<Char*>(checkedMalloc(allocSize));
411         fbstring_detail::pod_copy(data, data + size, ml_.data_);
412         ml_.size_ = size;
413         ml_.setCapacity(allocSize / sizeof(Char) - 1, Category::isMedium);
414       } else {
415         // Large strings are allocated differently
416         size_t effectiveCapacity = size;
417         auto const newRC = RefCounted::create(data, & effectiveCapacity);
418         ml_.data_ = newRC->data_;
419         ml_.size_ = size;
420         ml_.setCapacity(effectiveCapacity, Category::isLarge);
421       }
422       ml_.data_[size] = '\0';
423     }
424   }
425
426   ~fbstring_core() noexcept {
427     auto const c = category();
428     if (c == Category::isSmall) {
429       return;
430     }
431     if (c == Category::isMedium) {
432       free(ml_.data_);
433       return;
434     }
435     RefCounted::decrementRefs(ml_.data_);
436   }
437
438   // Snatches a previously mallocated string. The parameter "size"
439   // is the size of the string, and the parameter "allocatedSize"
440   // is the size of the mallocated block.  The string must be
441   // \0-terminated, so allocatedSize >= size + 1 and data[size] == '\0'.
442   //
443   // So if you want a 2-character string, pass malloc(3) as "data",
444   // pass 2 as "size", and pass 3 as "allocatedSize".
445   fbstring_core(Char * const data,
446                 const size_t size,
447                 const size_t allocatedSize,
448                 AcquireMallocatedString) {
449     if (size > 0) {
450       assert(allocatedSize >= size + 1);
451       assert(data[size] == '\0');
452       // Use the medium string storage
453       ml_.data_ = data;
454       ml_.size_ = size;
455       // Don't forget about null terminator
456       ml_.setCapacity(allocatedSize - 1, Category::isMedium);
457     } else {
458       // No need for the memory
459       free(data);
460       reset();
461     }
462   }
463
464   // swap below doesn't test whether &rhs == this (and instead
465   // potentially does extra work) on the premise that the rarity of
466   // that situation actually makes the check more expensive than is
467   // worth.
468   void swap(fbstring_core & rhs) {
469     auto const t = ml_;
470     ml_ = rhs.ml_;
471     rhs.ml_ = t;
472   }
473
474   // In C++11 data() and c_str() are 100% equivalent.
475   const Char * data() const {
476     return c_str();
477   }
478
479   Char * mutable_data() {
480     auto const c = category();
481     if (c == Category::isSmall) {
482       return small_;
483     }
484     assert(c == Category::isMedium || c == Category::isLarge);
485     if (c == Category::isLarge && RefCounted::refs(ml_.data_) > 1) {
486       // Ensure unique.
487       size_t effectiveCapacity = ml_.capacity();
488       auto const newRC = RefCounted::create(& effectiveCapacity);
489       // If this fails, someone placed the wrong capacity in an
490       // fbstring.
491       assert(effectiveCapacity >= ml_.capacity());
492       // Also copies terminator.
493       fbstring_detail::pod_copy(ml_.data_, ml_.data_ + ml_.size_ + 1,
494                                 newRC->data_);
495       RefCounted::decrementRefs(ml_.data_);
496       ml_.data_ = newRC->data_;
497     }
498     return ml_.data_;
499   }
500
501   const Char * c_str() const {
502     auto const c = category();
503     if (c == Category::isSmall) {
504       assert(small_[smallSize()] == '\0');
505       return small_;
506     }
507     assert(c == Category::isMedium || c == Category::isLarge);
508     assert(ml_.data_[ml_.size_] == '\0');
509     return ml_.data_;
510   }
511
512   void shrink(const size_t delta) {
513     if (category() == Category::isSmall) {
514       // Check for underflow
515       assert(delta <= smallSize());
516       setSmallSize(smallSize() - delta);
517     } else if (category() == Category::isMedium ||
518                RefCounted::refs(ml_.data_) == 1) {
519       // Medium strings and unique large strings need no special
520       // handling.
521       assert(ml_.size_ >= delta);
522       ml_.size_ -= delta;
523       ml_.data_[ml_.size_] = '\0';
524     } else {
525       assert(ml_.size_ >= delta);
526       // Shared large string, must make unique. This is because of the
527       // durn terminator must be written, which may trample the shared
528       // data.
529       if (delta) {
530         fbstring_core(ml_.data_, ml_.size_ - delta).swap(*this);
531       }
532       // No need to write the terminator.
533     }
534   }
535
536   void reserve(size_t minCapacity) {
537     if (category() == Category::isLarge) {
538       // Ensure unique
539       if (RefCounted::refs(ml_.data_) > 1) {
540         // We must make it unique regardless; in-place reallocation is
541         // useless if the string is shared. In order to not surprise
542         // people, reserve the new block at current capacity or
543         // more. That way, a string's capacity never shrinks after a
544         // call to reserve.
545         minCapacity = std::max(minCapacity, ml_.capacity());
546         auto const newRC = RefCounted::create(& minCapacity);
547         // Also copies terminator.
548         fbstring_detail::pod_copy(ml_.data_, ml_.data_ + ml_.size_ + 1,
549                                    newRC->data_);
550         RefCounted::decrementRefs(ml_.data_);
551         ml_.data_ = newRC->data_;
552         ml_.setCapacity(minCapacity, Category::isLarge);
553         // size remains unchanged
554       } else {
555         // String is not shared, so let's try to realloc (if needed)
556         if (minCapacity > ml_.capacity()) {
557           // Asking for more memory
558           auto const newRC =
559                RefCounted::reallocate(ml_.data_, ml_.size_,
560                                       ml_.capacity(), minCapacity);
561           ml_.data_ = newRC->data_;
562           ml_.setCapacity(minCapacity, Category::isLarge);
563         }
564         assert(capacity() >= minCapacity);
565       }
566     } else if (category() == Category::isMedium) {
567       // String is not shared
568       if (minCapacity <= ml_.capacity()) {
569         return; // nothing to do, there's enough room
570       }
571       if (minCapacity <= maxMediumSize) {
572         // Keep the string at medium size. Don't forget to allocate
573         // one extra Char for the terminating null.
574         size_t capacityBytes = goodMallocSize((1 + minCapacity) * sizeof(Char));
575         // Also copies terminator.
576         ml_.data_ = static_cast<Char *>(
577           smartRealloc(
578             ml_.data_,
579             (ml_.size_ + 1) * sizeof(Char),
580             (ml_.capacity() + 1) * sizeof(Char),
581             capacityBytes));
582         ml_.setCapacity(capacityBytes / sizeof(Char) - 1, Category::isMedium);
583       } else {
584         // Conversion from medium to large string
585         fbstring_core nascent;
586         // Will recurse to another branch of this function
587         nascent.reserve(minCapacity);
588         nascent.ml_.size_ = ml_.size_;
589         // Also copies terminator.
590         fbstring_detail::pod_copy(ml_.data_, ml_.data_ + ml_.size_ + 1,
591                                   nascent.ml_.data_);
592         nascent.swap(*this);
593         assert(capacity() >= minCapacity);
594       }
595     } else {
596       assert(category() == Category::isSmall);
597 #ifndef FBSTRING_DISABLE_SMALL_STRING_OPTIMIZATION
598       if (minCapacity <= maxSmallSize) {
599         // small
600         // Nothing to do, everything stays put
601       } else
602 #endif // FBSTRING_DISABLE_SMALL_STRING_OPTIMIZATION
603       if (minCapacity <= maxMediumSize) {
604         // medium
605         // Don't forget to allocate one extra Char for the terminating null
606         auto const allocSizeBytes =
607           goodMallocSize((1 + minCapacity) * sizeof(Char));
608         auto const pData = static_cast<Char*>(checkedMalloc(allocSizeBytes));
609         auto const size = smallSize();
610         // Also copies terminator.
611         fbstring_detail::pod_copy(small_, small_ + size + 1, pData);
612         ml_.data_ = pData;
613         ml_.size_ = size;
614         ml_.setCapacity(allocSizeBytes / sizeof(Char) - 1, Category::isMedium);
615       } else {
616         // large
617         auto const newRC = RefCounted::create(& minCapacity);
618         auto const size = smallSize();
619         // Also copies terminator.
620         fbstring_detail::pod_copy(small_, small_ + size + 1, newRC->data_);
621         ml_.data_ = newRC->data_;
622         ml_.size_ = size;
623         ml_.setCapacity(minCapacity, Category::isLarge);
624         assert(capacity() >= minCapacity);
625       }
626     }
627     assert(capacity() >= minCapacity);
628   }
629
630   Char * expand_noinit(const size_t delta, bool expGrowth = false) {
631     // Strategy is simple: make room, then change size
632     assert(capacity() >= size());
633     size_t sz, newSz;
634     if (category() == Category::isSmall) {
635       sz = smallSize();
636       newSz = sz + delta;
637 #ifndef FBSTRING_DISABLE_SMALL_STRING_OPTIMIZATION
638       if (FBSTRING_LIKELY(newSz <= maxSmallSize)) {
639         setSmallSize(newSz);
640         return small_ + sz;
641       }
642 #endif // FBSTRING_DISABLE_SMALL_STRING_OPTIMIZATION
643       reserve(expGrowth ? std::max(newSz, 2 * maxSmallSize) : newSz);
644     } else {
645       sz = ml_.size_;
646       newSz = sz + delta;
647       if (FBSTRING_UNLIKELY(newSz > capacity())) {
648         // ensures not shared
649         reserve(expGrowth ? std::max(newSz, 1 + capacity() * 3 / 2) : newSz);
650       }
651     }
652     assert(capacity() >= newSz);
653     // Category can't be small - we took care of that above
654     assert(category() == Category::isMedium || category() == Category::isLarge);
655     ml_.size_ = newSz;
656     ml_.data_[newSz] = '\0';
657     assert(size() == newSz);
658     return ml_.data_ + sz;
659   }
660
661   void push_back(Char c) {
662     *expand_noinit(1, /* expGrowth = */ true) = c;
663   }
664
665   size_t size() const {
666     return category() == Category::isSmall ? smallSize() : ml_.size_;
667   }
668
669   size_t capacity() const {
670     switch (category()) {
671       case Category::isSmall:
672         return maxSmallSize;
673       case Category::isLarge:
674         // For large-sized strings, a multi-referenced chunk has no
675         // available capacity. This is because any attempt to append
676         // data would trigger a new allocation.
677         if (RefCounted::refs(ml_.data_) > 1) return ml_.size_;
678       default: {}
679     }
680     return ml_.capacity();
681   }
682
683   bool isShared() const {
684     return category() == Category::isLarge && RefCounted::refs(ml_.data_) > 1;
685   }
686
687 private:
688   // Disabled
689   fbstring_core & operator=(const fbstring_core & rhs);
690
691   // Equivalent to setSmallSize(0) but a few ns faster in
692   // microbenchmarks.
693   void reset() {
694     ml_.capacity_ = kIsLittleEndian
695       ? maxSmallSize << (8 * (sizeof(size_t) - sizeof(Char)))
696       : maxSmallSize << 2;
697     small_[0] = '\0';
698     assert(category() == Category::isSmall && size() == 0);
699   }
700
701   struct RefCounted {
702     std::atomic<size_t> refCount_;
703     Char data_[1];
704
705     static RefCounted * fromData(Char * p) {
706       return static_cast<RefCounted*>(
707         static_cast<void*>(
708           static_cast<unsigned char*>(static_cast<void*>(p))
709           - sizeof(refCount_)));
710     }
711
712     static size_t refs(Char * p) {
713       return fromData(p)->refCount_.load(std::memory_order_acquire);
714     }
715
716     static void incrementRefs(Char * p) {
717       fromData(p)->refCount_.fetch_add(1, std::memory_order_acq_rel);
718     }
719
720     static void decrementRefs(Char * p) {
721       auto const dis = fromData(p);
722       size_t oldcnt = dis->refCount_.fetch_sub(1, std::memory_order_acq_rel);
723       assert(oldcnt > 0);
724       if (oldcnt == 1) {
725         free(dis);
726       }
727     }
728
729     static RefCounted * create(size_t * size) {
730       // Don't forget to allocate one extra Char for the terminating
731       // null. In this case, however, one Char is already part of the
732       // struct.
733       const size_t allocSize = goodMallocSize(
734         sizeof(RefCounted) + *size * sizeof(Char));
735       auto result = static_cast<RefCounted*>(checkedMalloc(allocSize));
736       result->refCount_.store(1, std::memory_order_release);
737       *size = (allocSize - sizeof(RefCounted)) / sizeof(Char);
738       return result;
739     }
740
741     static RefCounted * create(const Char * data, size_t * size) {
742       const size_t effectiveSize = *size;
743       auto result = create(size);
744       fbstring_detail::pod_copy(data, data + effectiveSize, result->data_);
745       return result;
746     }
747
748     static RefCounted * reallocate(Char *const data,
749                                    const size_t currentSize,
750                                    const size_t currentCapacity,
751                                    const size_t newCapacity) {
752       assert(newCapacity > 0 && newCapacity > currentSize);
753       auto const dis = fromData(data);
754       assert(dis->refCount_.load(std::memory_order_acquire) == 1);
755       // Don't forget to allocate one extra Char for the terminating
756       // null. In this case, however, one Char is already part of the
757       // struct.
758       auto result = static_cast<RefCounted*>(
759              smartRealloc(dis,
760                           sizeof(RefCounted) + currentSize * sizeof(Char),
761                           sizeof(RefCounted) + currentCapacity * sizeof(Char),
762                           sizeof(RefCounted) + newCapacity * sizeof(Char)));
763       assert(result->refCount_.load(std::memory_order_acquire) == 1);
764       return result;
765     }
766   };
767
768   typedef std::conditional<sizeof(size_t) == 4, uint32_t, uint64_t>::type
769           category_type;
770
771   enum class Category : category_type {
772     isSmall = 0,
773     isMedium = kIsLittleEndian
774       ? sizeof(size_t) == 4 ? 0x80000000 : 0x8000000000000000
775       : 0x2,
776     isLarge =  kIsLittleEndian
777       ? sizeof(size_t) == 4 ? 0x40000000 : 0x4000000000000000
778       : 0x1,
779   };
780
781   Category category() const {
782     // works for both big-endian and little-endian
783     return static_cast<Category>(ml_.capacity_ & categoryExtractMask);
784   }
785
786   struct MediumLarge {
787     Char * data_;
788     size_t size_;
789     size_t capacity_;
790
791     size_t capacity() const {
792       return kIsLittleEndian
793         ? capacity_ & capacityExtractMask
794         : capacity_ >> 2;
795     }
796
797     void setCapacity(size_t cap, Category cat) {
798         capacity_ = kIsLittleEndian
799           ? cap | static_cast<category_type>(cat)
800           : (cap << 2) | static_cast<category_type>(cat);
801     }
802   };
803
804   union {
805     Char small_[sizeof(MediumLarge) / sizeof(Char)];
806     MediumLarge ml_;
807   };
808
809   enum : size_t {
810     lastChar = sizeof(MediumLarge) - 1,
811     maxSmallSize = lastChar / sizeof(Char),
812     maxMediumSize = 254 / sizeof(Char),            // coincides with the small
813                                                    // bin size in dlmalloc
814     categoryExtractMask = kIsLittleEndian
815       ? sizeof(size_t) == 4 ? 0xC0000000 : 0xC000000000000000
816       : 0x3,
817     capacityExtractMask = kIsLittleEndian
818       ? ~categoryExtractMask
819       : 0x0 /*unused*/,
820   };
821   static_assert(!(sizeof(MediumLarge) % sizeof(Char)),
822                 "Corrupt memory layout for fbstring.");
823
824   size_t smallSize() const {
825     assert(category() == Category::isSmall);
826     constexpr auto shift = kIsLittleEndian ? 0 : 2;
827     auto smallShifted = static_cast<size_t>(small_[maxSmallSize]) >> shift;
828     assert(static_cast<size_t>(maxSmallSize) >= smallShifted);
829     return static_cast<size_t>(maxSmallSize) - smallShifted;
830   }
831
832   void setSmallSize(size_t s) {
833     // Warning: this should work with uninitialized strings too,
834     // so don't assume anything about the previous value of
835     // small_[maxSmallSize].
836     assert(s <= maxSmallSize);
837     constexpr auto shift = kIsLittleEndian ? 0 : 2;
838     small_[maxSmallSize] = (maxSmallSize - s) << shift;
839     small_[s] = '\0';
840     assert(category() == Category::isSmall && size() == s);
841   }
842 };
843
844 #ifndef _LIBSTDCXX_FBSTRING
845 /**
846  * Dummy fbstring core that uses an actual std::string. This doesn't
847  * make any sense - it's just for testing purposes.
848  */
849 template <class Char>
850 class dummy_fbstring_core {
851 public:
852   dummy_fbstring_core() {
853   }
854   dummy_fbstring_core(const dummy_fbstring_core& another)
855       : backend_(another.backend_) {
856   }
857   dummy_fbstring_core(const Char * s, size_t n)
858       : backend_(s, n) {
859   }
860   void swap(dummy_fbstring_core & rhs) {
861     backend_.swap(rhs.backend_);
862   }
863   const Char * data() const {
864     return backend_.data();
865   }
866   Char * mutable_data() {
867     //assert(!backend_.empty());
868     return &*backend_.begin();
869   }
870   void shrink(size_t delta) {
871     assert(delta <= size());
872     backend_.resize(size() - delta);
873   }
874   Char * expand_noinit(size_t delta) {
875     auto const sz = size();
876     backend_.resize(size() + delta);
877     return backend_.data() + sz;
878   }
879   void push_back(Char c) {
880     backend_.push_back(c);
881   }
882   size_t size() const {
883     return backend_.size();
884   }
885   size_t capacity() const {
886     return backend_.capacity();
887   }
888   bool isShared() const {
889     return false;
890   }
891   void reserve(size_t minCapacity) {
892     backend_.reserve(minCapacity);
893   }
894
895 private:
896   std::basic_string<Char> backend_;
897 };
898 #endif // !_LIBSTDCXX_FBSTRING
899
900 /**
901  * This is the basic_string replacement. For conformity,
902  * basic_fbstring takes the same template parameters, plus the last
903  * one which is the core.
904  */
905 #ifdef _LIBSTDCXX_FBSTRING
906 template <typename E, class T, class A, class Storage>
907 #else
908 template <typename E,
909           class T = std::char_traits<E>,
910           class A = std::allocator<E>,
911           class Storage = fbstring_core<E> >
912 #endif
913 class basic_fbstring {
914
915   static void enforce(
916       bool condition,
917       void (*throw_exc)(const char*),
918       const char* msg) {
919     if (!condition) throw_exc(msg);
920   }
921
922   bool isSane() const {
923     return
924       begin() <= end() &&
925       empty() == (size() == 0) &&
926       empty() == (begin() == end()) &&
927       size() <= max_size() &&
928       capacity() <= max_size() &&
929       size() <= capacity() &&
930       begin()[size()] == '\0';
931   }
932
933   struct Invariant;
934   friend struct Invariant;
935   struct Invariant {
936 #ifndef NDEBUG
937     explicit Invariant(const basic_fbstring& s) : s_(s) {
938       assert(s_.isSane());
939     }
940     ~Invariant() {
941       assert(s_.isSane());
942     }
943   private:
944     const basic_fbstring& s_;
945 #else
946     explicit Invariant(const basic_fbstring&) {}
947 #endif
948     Invariant& operator=(const Invariant&);
949   };
950
951 public:
952   // types
953   typedef T traits_type;
954   typedef typename traits_type::char_type value_type;
955   typedef A allocator_type;
956   typedef typename A::size_type size_type;
957   typedef typename A::difference_type difference_type;
958
959   typedef typename A::reference reference;
960   typedef typename A::const_reference const_reference;
961   typedef typename A::pointer pointer;
962   typedef typename A::const_pointer const_pointer;
963
964   typedef E* iterator;
965   typedef const E* const_iterator;
966   typedef std::reverse_iterator<iterator
967 #ifdef NO_ITERATOR_TRAITS
968                                 , value_type
969 #endif
970                                 > reverse_iterator;
971   typedef std::reverse_iterator<const_iterator
972 #ifdef NO_ITERATOR_TRAITS
973                                 , const value_type
974 #endif
975                                 > const_reverse_iterator;
976
977   static const size_type npos;                     // = size_type(-1)
978   typedef std::true_type IsRelocatable;
979
980 private:
981   static void procrustes(size_type& n, size_type nmax) {
982     if (n > nmax) n = nmax;
983   }
984
985 public:
986   // C++11 21.4.2 construct/copy/destroy
987
988   // Note: while the following two constructors can be (and previously were)
989   // collapsed into one constructor written this way:
990   //
991   //   explicit basic_fbstring(const A& a = A()) noexcept { }
992   //
993   // This can cause Clang (at least version 3.7) to fail with the error:
994   //   "chosen constructor is explicit in copy-initialization ...
995   //   in implicit initialization of field '(x)' with omitted initializer"
996   //
997   // if used in a struct which is default-initialized.  Hence the split into
998   // these two separate constructors.
999
1000   basic_fbstring() noexcept : basic_fbstring(A()) {
1001   }
1002
1003   explicit basic_fbstring(const A&) noexcept {
1004   }
1005
1006   basic_fbstring(const basic_fbstring& str)
1007       : store_(str.store_) {
1008   }
1009
1010   // Move constructor
1011   basic_fbstring(basic_fbstring&& goner) noexcept
1012       : store_(std::move(goner.store_)) {
1013   }
1014
1015 #ifndef _LIBSTDCXX_FBSTRING
1016   // This is defined for compatibility with std::string
1017   /* implicit */ basic_fbstring(const std::string& str)
1018       : store_(str.data(), str.size()) {
1019   }
1020 #endif
1021
1022   basic_fbstring(const basic_fbstring& str,
1023                  size_type pos,
1024                  size_type n = npos,
1025                  const A& /* a */ = A()) {
1026     assign(str, pos, n);
1027   }
1028
1029   /* implicit */ basic_fbstring(const value_type* s, const A& /*a*/ = A())
1030       : store_(s, s
1031           ? traits_type::length(s)
1032           : (std::__throw_logic_error(
1033                 "basic_fbstring: null pointer initializer not valid"),
1034              0)) {
1035   }
1036
1037   basic_fbstring(const value_type* s, size_type n, const A& /*a*/ = A())
1038       : store_(s, n) {
1039   }
1040
1041   basic_fbstring(size_type n, value_type c, const A& /*a*/ = A()) {
1042     auto const pData = store_.expand_noinit(n);
1043     fbstring_detail::pod_fill(pData, pData + n, c);
1044   }
1045
1046   template <class InIt>
1047   basic_fbstring(InIt begin, InIt end,
1048                  typename std::enable_if<
1049                  !std::is_same<typename std::remove_const<InIt>::type,
1050                  value_type*>::value, const A>::type & /*a*/ = A()) {
1051     assign(begin, end);
1052   }
1053
1054   // Specialization for const char*, const char*
1055   basic_fbstring(const value_type* b, const value_type* e)
1056       : store_(b, e - b) {
1057   }
1058
1059   // Nonstandard constructor
1060   basic_fbstring(value_type *s, size_type n, size_type c,
1061                  AcquireMallocatedString a)
1062       : store_(s, n, c, a) {
1063   }
1064
1065   // Construction from initialization list
1066   basic_fbstring(std::initializer_list<value_type> il) {
1067     assign(il.begin(), il.end());
1068   }
1069
1070   ~basic_fbstring() noexcept {
1071   }
1072
1073   basic_fbstring& operator=(const basic_fbstring& lhs) {
1074     Invariant checker(*this);
1075
1076     if (FBSTRING_UNLIKELY(&lhs == this)) {
1077       return *this;
1078     }
1079     auto const oldSize = size();
1080     auto const srcSize = lhs.size();
1081     if (capacity() >= srcSize && !store_.isShared()) {
1082       // great, just copy the contents
1083       if (oldSize < srcSize) {
1084         store_.expand_noinit(srcSize - oldSize);
1085       } else {
1086         store_.shrink(oldSize - srcSize);
1087       }
1088       assert(size() == srcSize);
1089       auto srcData = lhs.data();
1090       fbstring_detail::pod_copy(
1091           srcData, srcData + srcSize, store_.mutable_data());
1092     } else {
1093       // need to reallocate, so we may as well create a brand new string
1094       basic_fbstring(lhs).swap(*this);
1095     }
1096     return *this;
1097   }
1098
1099   // Move assignment
1100   basic_fbstring& operator=(basic_fbstring&& goner) noexcept {
1101     if (FBSTRING_UNLIKELY(&goner == this)) {
1102       // Compatibility with std::basic_string<>,
1103       // C++11 21.4.2 [string.cons] / 23 requires self-move-assignment support.
1104       return *this;
1105     }
1106     // No need of this anymore
1107     this->~basic_fbstring();
1108     // Move the goner into this
1109     new(&store_) fbstring_core<E>(std::move(goner.store_));
1110     return *this;
1111   }
1112
1113 #ifndef _LIBSTDCXX_FBSTRING
1114   // Compatibility with std::string
1115   basic_fbstring & operator=(const std::string & rhs) {
1116     return assign(rhs.data(), rhs.size());
1117   }
1118
1119   // Compatibility with std::string
1120   std::string toStdString() const {
1121     return std::string(data(), size());
1122   }
1123 #else
1124   // A lot of code in fbcode still uses this method, so keep it here for now.
1125   const basic_fbstring& toStdString() const {
1126     return *this;
1127   }
1128 #endif
1129
1130   basic_fbstring& operator=(const value_type* s) {
1131     return assign(s);
1132   }
1133
1134   basic_fbstring& operator=(value_type c) {
1135     Invariant checker(*this);
1136
1137     if (empty()) {
1138       store_.expand_noinit(1);
1139     } else if (store_.isShared()) {
1140       basic_fbstring(1, c).swap(*this);
1141       return *this;
1142     } else {
1143       store_.shrink(size() - 1);
1144     }
1145     front() = c;
1146     return *this;
1147   }
1148
1149   basic_fbstring& operator=(std::initializer_list<value_type> il) {
1150     return assign(il.begin(), il.end());
1151   }
1152
1153   // C++11 21.4.3 iterators:
1154   iterator begin() { return store_.mutable_data(); }
1155
1156   const_iterator begin() const { return store_.data(); }
1157
1158   const_iterator cbegin() const { return begin(); }
1159
1160   iterator end() {
1161     return store_.mutable_data() + store_.size();
1162   }
1163
1164   const_iterator end() const {
1165     return store_.data() + store_.size();
1166   }
1167
1168   const_iterator cend() const { return end(); }
1169
1170   reverse_iterator rbegin() {
1171     return reverse_iterator(end());
1172   }
1173
1174   const_reverse_iterator rbegin() const {
1175     return const_reverse_iterator(end());
1176   }
1177
1178   const_reverse_iterator crbegin() const { return rbegin(); }
1179
1180   reverse_iterator rend() {
1181     return reverse_iterator(begin());
1182   }
1183
1184   const_reverse_iterator rend() const {
1185     return const_reverse_iterator(begin());
1186   }
1187
1188   const_reverse_iterator crend() const { return rend(); }
1189
1190   // Added by C++11
1191   // C++11 21.4.5, element access:
1192   const value_type& front() const { return *begin(); }
1193   const value_type& back() const {
1194     assert(!empty());
1195     // Should be begin()[size() - 1], but that branches twice
1196     return *(end() - 1);
1197   }
1198   value_type& front() { return *begin(); }
1199   value_type& back() {
1200     assert(!empty());
1201     // Should be begin()[size() - 1], but that branches twice
1202     return *(end() - 1);
1203   }
1204   void pop_back() {
1205     assert(!empty());
1206     store_.shrink(1);
1207   }
1208
1209   // C++11 21.4.4 capacity:
1210   size_type size() const { return store_.size(); }
1211
1212   size_type length() const { return size(); }
1213
1214   size_type max_size() const {
1215     return std::numeric_limits<size_type>::max();
1216   }
1217
1218   void resize(const size_type n, const value_type c = value_type()) {
1219     Invariant checker(*this);
1220
1221     auto size = this->size();
1222     if (n <= size) {
1223       store_.shrink(size - n);
1224     } else {
1225       auto const delta = n - size;
1226       auto pData = store_.expand_noinit(delta);
1227       fbstring_detail::pod_fill(pData, pData + delta, c);
1228     }
1229     assert(this->size() == n);
1230   }
1231
1232   size_type capacity() const { return store_.capacity(); }
1233
1234   void reserve(size_type res_arg = 0) {
1235     enforce(res_arg <= max_size(), std::__throw_length_error, "");
1236     store_.reserve(res_arg);
1237   }
1238
1239   void shrink_to_fit() {
1240     // Shrink only if slack memory is sufficiently large
1241     if (capacity() < size() * 3 / 2) {
1242       return;
1243     }
1244     basic_fbstring(cbegin(), cend()).swap(*this);
1245   }
1246
1247   void clear() { resize(0); }
1248
1249   bool empty() const { return size() == 0; }
1250
1251   // C++11 21.4.5 element access:
1252   const_reference operator[](size_type pos) const {
1253     return *(begin() + pos);
1254   }
1255
1256   reference operator[](size_type pos) {
1257     return *(begin() + pos);
1258   }
1259
1260   const_reference at(size_type n) const {
1261     enforce(n <= size(), std::__throw_out_of_range, "");
1262     return (*this)[n];
1263   }
1264
1265   reference at(size_type n) {
1266     enforce(n < size(), std::__throw_out_of_range, "");
1267     return (*this)[n];
1268   }
1269
1270   // C++11 21.4.6 modifiers:
1271   basic_fbstring& operator+=(const basic_fbstring& str) {
1272     return append(str);
1273   }
1274
1275   basic_fbstring& operator+=(const value_type* s) {
1276     return append(s);
1277   }
1278
1279   basic_fbstring& operator+=(const value_type c) {
1280     push_back(c);
1281     return *this;
1282   }
1283
1284   basic_fbstring& operator+=(std::initializer_list<value_type> il) {
1285     append(il);
1286     return *this;
1287   }
1288
1289   basic_fbstring& append(const basic_fbstring& str) {
1290 #ifndef NDEBUG
1291     auto desiredSize = size() + str.size();
1292 #endif
1293     append(str.data(), str.size());
1294     assert(size() == desiredSize);
1295     return *this;
1296   }
1297
1298   basic_fbstring& append(const basic_fbstring& str, const size_type pos,
1299                          size_type n) {
1300     const size_type sz = str.size();
1301     enforce(pos <= sz, std::__throw_out_of_range, "");
1302     procrustes(n, sz - pos);
1303     return append(str.data() + pos, n);
1304   }
1305
1306   basic_fbstring& append(const value_type* s, size_type n) {
1307     Invariant checker(*this);
1308
1309     if (FBSTRING_UNLIKELY(!n)) {
1310       // Unlikely but must be done
1311       return *this;
1312     }
1313     auto const oldSize = size();
1314     auto const oldData = data();
1315     // Check for aliasing (rare). We could use "<=" here but in theory
1316     // those do not work for pointers unless the pointers point to
1317     // elements in the same array. For that reason we use
1318     // std::less_equal, which is guaranteed to offer a total order
1319     // over pointers. See discussion at http://goo.gl/Cy2ya for more
1320     // info.
1321     std::less_equal<const value_type*> le;
1322     if (FBSTRING_UNLIKELY(le(oldData, s) && !le(oldData + oldSize, s))) {
1323       assert(le(s + n, oldData + oldSize));
1324       const size_type offset = s - oldData;
1325       store_.reserve(oldSize + n);
1326       // Restore the source
1327       s = data() + offset;
1328     }
1329
1330     fbstring_detail::pod_copy(
1331         s, s + n, store_.expand_noinit(n, /* expGrowth = */ true));
1332     assert(size() == oldSize + n);
1333     return *this;
1334   }
1335
1336   basic_fbstring& append(const value_type* s) {
1337     return append(s, traits_type::length(s));
1338   }
1339
1340   basic_fbstring& append(size_type n, value_type c) {
1341     resize(size() + n, c);
1342     return *this;
1343   }
1344
1345   template<class InputIterator>
1346   basic_fbstring& append(InputIterator first, InputIterator last) {
1347     insert(end(), first, last);
1348     return *this;
1349   }
1350
1351   basic_fbstring& append(std::initializer_list<value_type> il) {
1352     return append(il.begin(), il.end());
1353   }
1354
1355   void push_back(const value_type c) {             // primitive
1356     store_.push_back(c);
1357   }
1358
1359   basic_fbstring& assign(const basic_fbstring& str) {
1360     if (&str == this) return *this;
1361     return assign(str.data(), str.size());
1362   }
1363
1364   basic_fbstring& assign(basic_fbstring&& str) {
1365     return *this = std::move(str);
1366   }
1367
1368   basic_fbstring& assign(const basic_fbstring& str, const size_type pos,
1369                          size_type n) {
1370     const size_type sz = str.size();
1371     enforce(pos <= sz, std::__throw_out_of_range, "");
1372     procrustes(n, sz - pos);
1373     return assign(str.data() + pos, n);
1374   }
1375
1376   basic_fbstring& assign(const value_type* s, const size_type n) {
1377     Invariant checker(*this);
1378
1379     // s can alias this, we need to use pod_move.
1380     if (size() >= n) {
1381       fbstring_detail::pod_move(s, s + n, store_.mutable_data());
1382       resize(n);
1383       assert(size() == n);
1384     } else {
1385       const value_type *const s2 = s + size();
1386       fbstring_detail::pod_move(s, s2, store_.mutable_data());
1387       append(s2, n - size());
1388       assert(size() == n);
1389     }
1390     assert(size() == n);
1391     return *this;
1392   }
1393
1394   basic_fbstring& assign(const value_type* s) {
1395     return assign(s, traits_type::length(s));
1396   }
1397
1398   basic_fbstring& assign(std::initializer_list<value_type> il) {
1399     return assign(il.begin(), il.end());
1400   }
1401
1402   template <class ItOrLength, class ItOrChar>
1403   basic_fbstring& assign(ItOrLength first_or_n, ItOrChar last_or_c) {
1404     return replace(begin(), end(), first_or_n, last_or_c);
1405   }
1406
1407   basic_fbstring& insert(size_type pos1, const basic_fbstring& str) {
1408     return insert(pos1, str.data(), str.size());
1409   }
1410
1411   basic_fbstring& insert(size_type pos1, const basic_fbstring& str,
1412                          size_type pos2, size_type n) {
1413     enforce(pos2 <= str.length(), std::__throw_out_of_range, "");
1414     procrustes(n, str.length() - pos2);
1415     return insert(pos1, str.data() + pos2, n);
1416   }
1417
1418   basic_fbstring& insert(size_type pos, const value_type* s, size_type n) {
1419     enforce(pos <= length(), std::__throw_out_of_range, "");
1420     insert(begin() + pos, s, s + n);
1421     return *this;
1422   }
1423
1424   basic_fbstring& insert(size_type pos, const value_type* s) {
1425     return insert(pos, s, traits_type::length(s));
1426   }
1427
1428   basic_fbstring& insert(size_type pos, size_type n, value_type c) {
1429     enforce(pos <= length(), std::__throw_out_of_range, "");
1430     insert(begin() + pos, n, c);
1431     return *this;
1432   }
1433
1434   iterator insert(const_iterator p, const value_type c) {
1435     const size_type pos = p - begin();
1436     insert(p, 1, c);
1437     return begin() + pos;
1438   }
1439
1440 #ifndef _LIBSTDCXX_FBSTRING
1441  private:
1442   typedef std::basic_istream<value_type, traits_type> istream_type;
1443
1444  public:
1445   friend inline istream_type& getline(istream_type& is,
1446                                       basic_fbstring& str,
1447                                       value_type delim) {
1448     Invariant checker(str);
1449
1450     str.clear();
1451     size_t size = 0;
1452     while (true) {
1453       size_t avail = str.capacity() - size;
1454       // fbstring has 1 byte extra capacity for the null terminator,
1455       // and getline null-terminates the read string.
1456       is.getline(str.store_.expand_noinit(avail), avail + 1, delim);
1457       size += is.gcount();
1458
1459       if (is.bad() || is.eof() || !is.fail()) {
1460         // Done by either failure, end of file, or normal read.
1461         if (!is.bad() && !is.eof()) {
1462           --size; // gcount() also accounts for the delimiter.
1463         }
1464         str.resize(size);
1465         break;
1466       }
1467
1468       assert(size == str.size());
1469       assert(size == str.capacity());
1470       // Start at minimum allocation 63 + terminator = 64.
1471       str.reserve(std::max<size_t>(63, 3 * size / 2));
1472       // Clear the error so we can continue reading.
1473       is.clear();
1474     }
1475     return is;
1476   }
1477
1478   friend inline istream_type& getline(istream_type& is, basic_fbstring& str) {
1479     return getline(is, str, '\n');
1480   }
1481 #endif
1482
1483 private:
1484   template <int i> class Selector {};
1485
1486   iterator insertImplDiscr(const_iterator p,
1487                            size_type n, value_type c, Selector<1>) {
1488     Invariant checker(*this);
1489
1490     auto const pos = p - begin();
1491     assert(p >= begin() && p <= end());
1492     if (capacity() - size() < n) {
1493       const size_type sz = p - begin();
1494       reserve(size() + n);
1495       p = begin() + sz;
1496     }
1497     const iterator oldEnd = end();
1498     if (n < size_type(oldEnd - p)) {
1499       append(oldEnd - n, oldEnd);
1500       // Also copies terminator.
1501       fbstring_detail::pod_move(&*p, &*oldEnd - n + 1, begin() + pos + n);
1502       std::fill(begin() + pos, begin() + pos + n, c);
1503     } else {
1504       append(n - (end() - p), c);
1505       append(iterator(p), oldEnd);
1506       std::fill(iterator(p), oldEnd, c);
1507     }
1508     return begin() + pos;
1509   }
1510
1511   template<class InputIter>
1512   iterator insertImplDiscr(const_iterator i,
1513                            InputIter b, InputIter e, Selector<0>) {
1514     return insertImpl(i, b, e,
1515                typename std::iterator_traits<InputIter>::iterator_category());
1516   }
1517
1518   template <class FwdIterator>
1519   iterator insertImpl(const_iterator i,
1520                       FwdIterator s1,
1521                       FwdIterator s2,
1522                       std::forward_iterator_tag) {
1523     Invariant checker(*this);
1524
1525     const size_type pos = i - begin();
1526     auto n = std::distance(s1, s2);
1527     assert(n >= 0);
1528     assert(pos <= size());
1529
1530     auto oldSize = size();
1531     store_.expand_noinit(n, /* expGrowth = */ true);
1532     auto b = begin();
1533     fbstring_detail::pod_move(b + pos, b + oldSize, b + pos + n);
1534     std::copy(s1, s2, b + pos);
1535
1536     return b + pos;
1537   }
1538
1539   template <class InputIterator>
1540   iterator insertImpl(const_iterator i,
1541                       InputIterator b, InputIterator e,
1542                       std::input_iterator_tag) {
1543     const auto pos = i - begin();
1544     basic_fbstring temp(begin(), i);
1545     for (; b != e; ++b) {
1546       temp.push_back(*b);
1547     }
1548     temp.append(i, cend());
1549     swap(temp);
1550     return begin() + pos;
1551   }
1552
1553 public:
1554   template <class ItOrLength, class ItOrChar>
1555   iterator insert(const_iterator p, ItOrLength first_or_n, ItOrChar last_or_c) {
1556     Selector<std::numeric_limits<ItOrLength>::is_specialized> sel;
1557     return insertImplDiscr(p, first_or_n, last_or_c, sel);
1558   }
1559
1560   iterator insert(const_iterator p, std::initializer_list<value_type> il) {
1561     return insert(p, il.begin(), il.end());
1562   }
1563
1564   basic_fbstring& erase(size_type pos = 0, size_type n = npos) {
1565     Invariant checker(*this);
1566
1567     enforce(pos <= length(), std::__throw_out_of_range, "");
1568     procrustes(n, length() - pos);
1569     std::copy(begin() + pos + n, end(), begin() + pos);
1570     resize(length() - n);
1571     return *this;
1572   }
1573
1574   iterator erase(iterator position) {
1575     const size_type pos(position - begin());
1576     enforce(pos <= size(), std::__throw_out_of_range, "");
1577     erase(pos, 1);
1578     return begin() + pos;
1579   }
1580
1581   iterator erase(iterator first, iterator last) {
1582     const size_type pos(first - begin());
1583     erase(pos, last - first);
1584     return begin() + pos;
1585   }
1586
1587   // Replaces at most n1 chars of *this, starting with pos1 with the
1588   // content of str
1589   basic_fbstring& replace(size_type pos1, size_type n1,
1590                           const basic_fbstring& str) {
1591     return replace(pos1, n1, str.data(), str.size());
1592   }
1593
1594   // Replaces at most n1 chars of *this, starting with pos1,
1595   // with at most n2 chars of str starting with pos2
1596   basic_fbstring& replace(size_type pos1, size_type n1,
1597                           const basic_fbstring& str,
1598                           size_type pos2, size_type n2) {
1599     enforce(pos2 <= str.length(), std::__throw_out_of_range, "");
1600     return replace(pos1, n1, str.data() + pos2,
1601                    std::min(n2, str.size() - pos2));
1602   }
1603
1604   // Replaces at most n1 chars of *this, starting with pos, with chars from s
1605   basic_fbstring& replace(size_type pos, size_type n1, const value_type* s) {
1606     return replace(pos, n1, s, traits_type::length(s));
1607   }
1608
1609   // Replaces at most n1 chars of *this, starting with pos, with n2
1610   // occurrences of c
1611   //
1612   // consolidated with
1613   //
1614   // Replaces at most n1 chars of *this, starting with pos, with at
1615   // most n2 chars of str.  str must have at least n2 chars.
1616   template <class StrOrLength, class NumOrChar>
1617   basic_fbstring& replace(size_type pos, size_type n1,
1618                           StrOrLength s_or_n2, NumOrChar n_or_c) {
1619     Invariant checker(*this);
1620
1621     enforce(pos <= size(), std::__throw_out_of_range, "");
1622     procrustes(n1, length() - pos);
1623     const iterator b = begin() + pos;
1624     return replace(b, b + n1, s_or_n2, n_or_c);
1625   }
1626
1627   basic_fbstring& replace(iterator i1, iterator i2, const basic_fbstring& str) {
1628     return replace(i1, i2, str.data(), str.length());
1629   }
1630
1631   basic_fbstring& replace(iterator i1, iterator i2, const value_type* s) {
1632     return replace(i1, i2, s, traits_type::length(s));
1633   }
1634
1635 private:
1636   basic_fbstring& replaceImplDiscr(iterator i1, iterator i2,
1637                                    const value_type* s, size_type n,
1638                                    Selector<2>) {
1639     assert(i1 <= i2);
1640     assert(begin() <= i1 && i1 <= end());
1641     assert(begin() <= i2 && i2 <= end());
1642     return replace(i1, i2, s, s + n);
1643   }
1644
1645   basic_fbstring& replaceImplDiscr(iterator i1, iterator i2,
1646                                    size_type n2, value_type c, Selector<1>) {
1647     const size_type n1 = i2 - i1;
1648     if (n1 > n2) {
1649       std::fill(i1, i1 + n2, c);
1650       erase(i1 + n2, i2);
1651     } else {
1652       std::fill(i1, i2, c);
1653       insert(i2, n2 - n1, c);
1654     }
1655     assert(isSane());
1656     return *this;
1657   }
1658
1659   template <class InputIter>
1660   basic_fbstring& replaceImplDiscr(iterator i1, iterator i2,
1661                                    InputIter b, InputIter e,
1662                                    Selector<0>) {
1663     replaceImpl(i1, i2, b, e,
1664                 typename std::iterator_traits<InputIter>::iterator_category());
1665     return *this;
1666   }
1667
1668 private:
1669  template <class FwdIterator>
1670  bool replaceAliased(iterator /* i1 */,
1671                      iterator /* i2 */,
1672                      FwdIterator /* s1 */,
1673                      FwdIterator /* s2 */,
1674                      std::false_type) {
1675     return false;
1676   }
1677
1678   template <class FwdIterator>
1679   bool replaceAliased(iterator i1, iterator i2,
1680                       FwdIterator s1, FwdIterator s2, std::true_type) {
1681     static const std::less_equal<const value_type*> le =
1682       std::less_equal<const value_type*>();
1683     const bool aliased = le(&*begin(), &*s1) && le(&*s1, &*end());
1684     if (!aliased) {
1685       return false;
1686     }
1687     // Aliased replace, copy to new string
1688     basic_fbstring temp;
1689     temp.reserve(size() - (i2 - i1) + std::distance(s1, s2));
1690     temp.append(begin(), i1).append(s1, s2).append(i2, end());
1691     swap(temp);
1692     return true;
1693   }
1694
1695   template <class FwdIterator>
1696   void replaceImpl(iterator i1, iterator i2,
1697                    FwdIterator s1, FwdIterator s2, std::forward_iterator_tag) {
1698     Invariant checker(*this);
1699
1700     // Handle aliased replace
1701     if (replaceAliased(i1, i2, s1, s2,
1702           std::integral_constant<bool,
1703             std::is_same<FwdIterator, iterator>::value ||
1704             std::is_same<FwdIterator, const_iterator>::value>())) {
1705       return;
1706     }
1707
1708     auto const n1 = i2 - i1;
1709     assert(n1 >= 0);
1710     auto const n2 = std::distance(s1, s2);
1711     assert(n2 >= 0);
1712
1713     if (n1 > n2) {
1714       // shrinks
1715       std::copy(s1, s2, i1);
1716       erase(i1 + n2, i2);
1717     } else {
1718       // grows
1719       fbstring_detail::copy_n(s1, n1, i1);
1720       std::advance(s1, n1);
1721       insert(i2, s1, s2);
1722     }
1723     assert(isSane());
1724   }
1725
1726   template <class InputIterator>
1727   void replaceImpl(iterator i1, iterator i2,
1728                    InputIterator b, InputIterator e, std::input_iterator_tag) {
1729     basic_fbstring temp(begin(), i1);
1730     temp.append(b, e).append(i2, end());
1731     swap(temp);
1732   }
1733
1734 public:
1735   template <class T1, class T2>
1736   basic_fbstring& replace(iterator i1, iterator i2,
1737                           T1 first_or_n_or_s, T2 last_or_c_or_n) {
1738     const bool
1739       num1 = std::numeric_limits<T1>::is_specialized,
1740       num2 = std::numeric_limits<T2>::is_specialized;
1741     return replaceImplDiscr(
1742       i1, i2, first_or_n_or_s, last_or_c_or_n,
1743       Selector<num1 ? (num2 ? 1 : -1) : (num2 ? 2 : 0)>());
1744   }
1745
1746   size_type copy(value_type* s, size_type n, size_type pos = 0) const {
1747     enforce(pos <= size(), std::__throw_out_of_range, "");
1748     procrustes(n, size() - pos);
1749
1750     fbstring_detail::pod_copy(
1751       data() + pos,
1752       data() + pos + n,
1753       s);
1754     return n;
1755   }
1756
1757   void swap(basic_fbstring& rhs) {
1758     store_.swap(rhs.store_);
1759   }
1760
1761   const value_type* c_str() const {
1762     return store_.c_str();
1763   }
1764
1765   const value_type* data() const { return c_str(); }
1766
1767   allocator_type get_allocator() const {
1768     return allocator_type();
1769   }
1770
1771   size_type find(const basic_fbstring& str, size_type pos = 0) const {
1772     return find(str.data(), pos, str.length());
1773   }
1774
1775   size_type find(const value_type* needle, const size_type pos,
1776                  const size_type nsize) const {
1777     if (!nsize) return pos;
1778     auto const size = this->size();
1779     // nsize + pos can overflow (eg pos == npos), guard against that by checking
1780     // that nsize + pos does not wrap around.
1781     if (nsize + pos > size || nsize + pos < pos) return npos;
1782     // Don't use std::search, use a Boyer-Moore-like trick by comparing
1783     // the last characters first
1784     auto const haystack = data();
1785     auto const nsize_1 = nsize - 1;
1786     auto const lastNeedle = needle[nsize_1];
1787
1788     // Boyer-Moore skip value for the last char in the needle. Zero is
1789     // not a valid value; skip will be computed the first time it's
1790     // needed.
1791     size_type skip = 0;
1792
1793     const E * i = haystack + pos;
1794     auto iEnd = haystack + size - nsize_1;
1795
1796     while (i < iEnd) {
1797       // Boyer-Moore: match the last element in the needle
1798       while (i[nsize_1] != lastNeedle) {
1799         if (++i == iEnd) {
1800           // not found
1801           return npos;
1802         }
1803       }
1804       // Here we know that the last char matches
1805       // Continue in pedestrian mode
1806       for (size_t j = 0; ; ) {
1807         assert(j < nsize);
1808         if (i[j] != needle[j]) {
1809           // Not found, we can skip
1810           // Compute the skip value lazily
1811           if (skip == 0) {
1812             skip = 1;
1813             while (skip <= nsize_1 && needle[nsize_1 - skip] != lastNeedle) {
1814               ++skip;
1815             }
1816           }
1817           i += skip;
1818           break;
1819         }
1820         // Check if done searching
1821         if (++j == nsize) {
1822           // Yay
1823           return i - haystack;
1824         }
1825       }
1826     }
1827     return npos;
1828   }
1829
1830   size_type find(const value_type* s, size_type pos = 0) const {
1831     return find(s, pos, traits_type::length(s));
1832   }
1833
1834   size_type find (value_type c, size_type pos = 0) const {
1835     return find(&c, pos, 1);
1836   }
1837
1838   size_type rfind(const basic_fbstring& str, size_type pos = npos) const {
1839     return rfind(str.data(), pos, str.length());
1840   }
1841
1842   size_type rfind(const value_type* s, size_type pos, size_type n) const {
1843     if (n > length()) return npos;
1844     pos = std::min(pos, length() - n);
1845     if (n == 0) return pos;
1846
1847     const_iterator i(begin() + pos);
1848     for (; ; --i) {
1849       if (traits_type::eq(*i, *s)
1850           && traits_type::compare(&*i, s, n) == 0) {
1851         return i - begin();
1852       }
1853       if (i == begin()) break;
1854     }
1855     return npos;
1856   }
1857
1858   size_type rfind(const value_type* s, size_type pos = npos) const {
1859     return rfind(s, pos, traits_type::length(s));
1860   }
1861
1862   size_type rfind(value_type c, size_type pos = npos) const {
1863     return rfind(&c, pos, 1);
1864   }
1865
1866   size_type find_first_of(const basic_fbstring& str, size_type pos = 0) const {
1867     return find_first_of(str.data(), pos, str.length());
1868   }
1869
1870   size_type find_first_of(const value_type* s,
1871                           size_type pos, size_type n) const {
1872     if (pos > length() || n == 0) return npos;
1873     const_iterator i(begin() + pos),
1874       finish(end());
1875     for (; i != finish; ++i) {
1876       if (traits_type::find(s, n, *i) != 0) {
1877         return i - begin();
1878       }
1879     }
1880     return npos;
1881   }
1882
1883   size_type find_first_of(const value_type* s, size_type pos = 0) const {
1884     return find_first_of(s, pos, traits_type::length(s));
1885   }
1886
1887   size_type find_first_of(value_type c, size_type pos = 0) const {
1888     return find_first_of(&c, pos, 1);
1889   }
1890
1891   size_type find_last_of (const basic_fbstring& str,
1892                           size_type pos = npos) const {
1893     return find_last_of(str.data(), pos, str.length());
1894   }
1895
1896   size_type find_last_of (const value_type* s, size_type pos,
1897                           size_type n) const {
1898     if (!empty() && n > 0) {
1899       pos = std::min(pos, length() - 1);
1900       const_iterator i(begin() + pos);
1901       for (;; --i) {
1902         if (traits_type::find(s, n, *i) != 0) {
1903           return i - begin();
1904         }
1905         if (i == begin()) break;
1906       }
1907     }
1908     return npos;
1909   }
1910
1911   size_type find_last_of (const value_type* s,
1912                           size_type pos = npos) const {
1913     return find_last_of(s, pos, traits_type::length(s));
1914   }
1915
1916   size_type find_last_of (value_type c, size_type pos = npos) const {
1917     return find_last_of(&c, pos, 1);
1918   }
1919
1920   size_type find_first_not_of(const basic_fbstring& str,
1921                               size_type pos = 0) const {
1922     return find_first_not_of(str.data(), pos, str.size());
1923   }
1924
1925   size_type find_first_not_of(const value_type* s, size_type pos,
1926                               size_type n) const {
1927     if (pos < length()) {
1928       const_iterator
1929         i(begin() + pos),
1930         finish(end());
1931       for (; i != finish; ++i) {
1932         if (traits_type::find(s, n, *i) == 0) {
1933           return i - begin();
1934         }
1935       }
1936     }
1937     return npos;
1938   }
1939
1940   size_type find_first_not_of(const value_type* s,
1941                               size_type pos = 0) const {
1942     return find_first_not_of(s, pos, traits_type::length(s));
1943   }
1944
1945   size_type find_first_not_of(value_type c, size_type pos = 0) const {
1946     return find_first_not_of(&c, pos, 1);
1947   }
1948
1949   size_type find_last_not_of(const basic_fbstring& str,
1950                              size_type pos = npos) const {
1951     return find_last_not_of(str.data(), pos, str.length());
1952   }
1953
1954   size_type find_last_not_of(const value_type* s, size_type pos,
1955                              size_type n) const {
1956     if (!this->empty()) {
1957       pos = std::min(pos, size() - 1);
1958       const_iterator i(begin() + pos);
1959       for (;; --i) {
1960         if (traits_type::find(s, n, *i) == 0) {
1961           return i - begin();
1962         }
1963         if (i == begin()) break;
1964       }
1965     }
1966     return npos;
1967   }
1968
1969   size_type find_last_not_of(const value_type* s,
1970                              size_type pos = npos) const {
1971     return find_last_not_of(s, pos, traits_type::length(s));
1972   }
1973
1974   size_type find_last_not_of (value_type c, size_type pos = npos) const {
1975     return find_last_not_of(&c, pos, 1);
1976   }
1977
1978   basic_fbstring substr(size_type pos = 0, size_type n = npos) const& {
1979     enforce(pos <= size(), std::__throw_out_of_range, "");
1980     return basic_fbstring(data() + pos, std::min(n, size() - pos));
1981   }
1982
1983   basic_fbstring substr(size_type pos = 0, size_type n = npos) && {
1984     enforce(pos <= size(), std::__throw_out_of_range, "");
1985     erase(0, pos);
1986     if (n < size()) resize(n);
1987     return std::move(*this);
1988   }
1989
1990   int compare(const basic_fbstring& str) const {
1991     // FIX due to Goncalo N M de Carvalho July 18, 2005
1992     return compare(0, size(), str);
1993   }
1994
1995   int compare(size_type pos1, size_type n1,
1996               const basic_fbstring& str) const {
1997     return compare(pos1, n1, str.data(), str.size());
1998   }
1999
2000   int compare(size_type pos1, size_type n1,
2001               const value_type* s) const {
2002     return compare(pos1, n1, s, traits_type::length(s));
2003   }
2004
2005   int compare(size_type pos1, size_type n1,
2006               const value_type* s, size_type n2) const {
2007     enforce(pos1 <= size(), std::__throw_out_of_range, "");
2008     procrustes(n1, size() - pos1);
2009     // The line below fixed by Jean-Francois Bastien, 04-23-2007. Thanks!
2010     const int r = traits_type::compare(pos1 + data(), s, std::min(n1, n2));
2011     return r != 0 ? r : n1 > n2 ? 1 : n1 < n2 ? -1 : 0;
2012   }
2013
2014   int compare(size_type pos1, size_type n1,
2015               const basic_fbstring& str,
2016               size_type pos2, size_type n2) const {
2017     enforce(pos2 <= str.size(), std::__throw_out_of_range, "");
2018     return compare(pos1, n1, str.data() + pos2,
2019                    std::min(n2, str.size() - pos2));
2020   }
2021
2022   // Code from Jean-Francois Bastien (03/26/2007)
2023   int compare(const value_type* s) const {
2024     // Could forward to compare(0, size(), s, traits_type::length(s))
2025     // but that does two extra checks
2026     const size_type n1(size()), n2(traits_type::length(s));
2027     const int r = traits_type::compare(data(), s, std::min(n1, n2));
2028     return r != 0 ? r : n1 > n2 ? 1 : n1 < n2 ? -1 : 0;
2029   }
2030
2031 private:
2032   // Data
2033   Storage store_;
2034 };
2035
2036 // non-member functions
2037 // C++11 21.4.8.1/1
2038 template <typename E, class T, class A, class S>
2039 inline
2040 basic_fbstring<E, T, A, S> operator+(const basic_fbstring<E, T, A, S>& lhs,
2041                                      const basic_fbstring<E, T, A, S>& rhs) {
2042
2043   basic_fbstring<E, T, A, S> result;
2044   result.reserve(lhs.size() + rhs.size());
2045   result.append(lhs).append(rhs);
2046   return std::move(result);
2047 }
2048
2049 // C++11 21.4.8.1/2
2050 template <typename E, class T, class A, class S>
2051 inline
2052 basic_fbstring<E, T, A, S> operator+(basic_fbstring<E, T, A, S>&& lhs,
2053                                      const basic_fbstring<E, T, A, S>& rhs) {
2054   return std::move(lhs.append(rhs));
2055 }
2056
2057 // C++11 21.4.8.1/3
2058 template <typename E, class T, class A, class S>
2059 inline
2060 basic_fbstring<E, T, A, S> operator+(const basic_fbstring<E, T, A, S>& lhs,
2061                                      basic_fbstring<E, T, A, S>&& rhs) {
2062   if (rhs.capacity() >= lhs.size() + rhs.size()) {
2063     // Good, at least we don't need to reallocate
2064     return std::move(rhs.insert(0, lhs));
2065   }
2066   // Meh, no go. Forward to operator+(const&, const&).
2067   auto const& rhsC = rhs;
2068   return lhs + rhsC;
2069 }
2070
2071 // C++11 21.4.8.1/4
2072 template <typename E, class T, class A, class S>
2073 inline
2074 basic_fbstring<E, T, A, S> operator+(basic_fbstring<E, T, A, S>&& lhs,
2075                                      basic_fbstring<E, T, A, S>&& rhs) {
2076   return std::move(lhs.append(rhs));
2077 }
2078
2079 // C++11 21.4.8.1/5
2080 template <typename E, class T, class A, class S>
2081 inline
2082 basic_fbstring<E, T, A, S> operator+(
2083   const E* lhs,
2084   const basic_fbstring<E, T, A, S>& rhs) {
2085   //
2086   basic_fbstring<E, T, A, S> result;
2087   const auto len = basic_fbstring<E, T, A, S>::traits_type::length(lhs);
2088   result.reserve(len + rhs.size());
2089   result.append(lhs, len).append(rhs);
2090   return result;
2091 }
2092
2093 // C++11 21.4.8.1/6
2094 template <typename E, class T, class A, class S>
2095 inline
2096 basic_fbstring<E, T, A, S> operator+(
2097   const E* lhs,
2098   basic_fbstring<E, T, A, S>&& rhs) {
2099   //
2100   const auto len = basic_fbstring<E, T, A, S>::traits_type::length(lhs);
2101   if (rhs.capacity() >= len + rhs.size()) {
2102     // Good, at least we don't need to reallocate
2103     rhs.insert(rhs.begin(), lhs, lhs + len);
2104     return rhs;
2105   }
2106   // Meh, no go. Do it by hand since we have len already.
2107   basic_fbstring<E, T, A, S> result;
2108   result.reserve(len + rhs.size());
2109   result.append(lhs, len).append(rhs);
2110   return result;
2111 }
2112
2113 // C++11 21.4.8.1/7
2114 template <typename E, class T, class A, class S>
2115 inline
2116 basic_fbstring<E, T, A, S> operator+(
2117   E lhs,
2118   const basic_fbstring<E, T, A, S>& rhs) {
2119
2120   basic_fbstring<E, T, A, S> result;
2121   result.reserve(1 + rhs.size());
2122   result.push_back(lhs);
2123   result.append(rhs);
2124   return result;
2125 }
2126
2127 // C++11 21.4.8.1/8
2128 template <typename E, class T, class A, class S>
2129 inline
2130 basic_fbstring<E, T, A, S> operator+(
2131   E lhs,
2132   basic_fbstring<E, T, A, S>&& rhs) {
2133   //
2134   if (rhs.capacity() > rhs.size()) {
2135     // Good, at least we don't need to reallocate
2136     rhs.insert(rhs.begin(), lhs);
2137     return rhs;
2138   }
2139   // Meh, no go. Forward to operator+(E, const&).
2140   auto const& rhsC = rhs;
2141   return lhs + rhsC;
2142 }
2143
2144 // C++11 21.4.8.1/9
2145 template <typename E, class T, class A, class S>
2146 inline
2147 basic_fbstring<E, T, A, S> operator+(
2148   const basic_fbstring<E, T, A, S>& lhs,
2149   const E* rhs) {
2150
2151   typedef typename basic_fbstring<E, T, A, S>::size_type size_type;
2152   typedef typename basic_fbstring<E, T, A, S>::traits_type traits_type;
2153
2154   basic_fbstring<E, T, A, S> result;
2155   const size_type len = traits_type::length(rhs);
2156   result.reserve(lhs.size() + len);
2157   result.append(lhs).append(rhs, len);
2158   return result;
2159 }
2160
2161 // C++11 21.4.8.1/10
2162 template <typename E, class T, class A, class S>
2163 inline
2164 basic_fbstring<E, T, A, S> operator+(
2165   basic_fbstring<E, T, A, S>&& lhs,
2166   const E* rhs) {
2167   //
2168   return std::move(lhs += rhs);
2169 }
2170
2171 // C++11 21.4.8.1/11
2172 template <typename E, class T, class A, class S>
2173 inline
2174 basic_fbstring<E, T, A, S> operator+(
2175   const basic_fbstring<E, T, A, S>& lhs,
2176   E rhs) {
2177
2178   basic_fbstring<E, T, A, S> result;
2179   result.reserve(lhs.size() + 1);
2180   result.append(lhs);
2181   result.push_back(rhs);
2182   return result;
2183 }
2184
2185 // C++11 21.4.8.1/12
2186 template <typename E, class T, class A, class S>
2187 inline
2188 basic_fbstring<E, T, A, S> operator+(
2189   basic_fbstring<E, T, A, S>&& lhs,
2190   E rhs) {
2191   //
2192   return std::move(lhs += rhs);
2193 }
2194
2195 template <typename E, class T, class A, class S>
2196 inline
2197 bool operator==(const basic_fbstring<E, T, A, S>& lhs,
2198                 const basic_fbstring<E, T, A, S>& rhs) {
2199   return lhs.size() == rhs.size() && lhs.compare(rhs) == 0; }
2200
2201 template <typename E, class T, class A, class S>
2202 inline
2203 bool operator==(const typename basic_fbstring<E, T, A, S>::value_type* lhs,
2204                 const basic_fbstring<E, T, A, S>& rhs) {
2205   return rhs == lhs; }
2206
2207 template <typename E, class T, class A, class S>
2208 inline
2209 bool operator==(const basic_fbstring<E, T, A, S>& lhs,
2210                 const typename basic_fbstring<E, T, A, S>::value_type* rhs) {
2211   return lhs.compare(rhs) == 0; }
2212
2213 template <typename E, class T, class A, class S>
2214 inline
2215 bool operator!=(const basic_fbstring<E, T, A, S>& lhs,
2216                 const basic_fbstring<E, T, A, S>& rhs) {
2217   return !(lhs == rhs); }
2218
2219 template <typename E, class T, class A, class S>
2220 inline
2221 bool operator!=(const typename basic_fbstring<E, T, A, S>::value_type* lhs,
2222                 const basic_fbstring<E, T, A, S>& rhs) {
2223   return !(lhs == rhs); }
2224
2225 template <typename E, class T, class A, class S>
2226 inline
2227 bool operator!=(const basic_fbstring<E, T, A, S>& lhs,
2228                 const typename basic_fbstring<E, T, A, S>::value_type* rhs) {
2229   return !(lhs == rhs); }
2230
2231 template <typename E, class T, class A, class S>
2232 inline
2233 bool operator<(const basic_fbstring<E, T, A, S>& lhs,
2234                const basic_fbstring<E, T, A, S>& rhs) {
2235   return lhs.compare(rhs) < 0; }
2236
2237 template <typename E, class T, class A, class S>
2238 inline
2239 bool operator<(const basic_fbstring<E, T, A, S>& lhs,
2240                const typename basic_fbstring<E, T, A, S>::value_type* rhs) {
2241   return lhs.compare(rhs) < 0; }
2242
2243 template <typename E, class T, class A, class S>
2244 inline
2245 bool operator<(const typename basic_fbstring<E, T, A, S>::value_type* lhs,
2246                const basic_fbstring<E, T, A, S>& rhs) {
2247   return rhs.compare(lhs) > 0; }
2248
2249 template <typename E, class T, class A, class S>
2250 inline
2251 bool operator>(const basic_fbstring<E, T, A, S>& lhs,
2252                const basic_fbstring<E, T, A, S>& rhs) {
2253   return rhs < lhs; }
2254
2255 template <typename E, class T, class A, class S>
2256 inline
2257 bool operator>(const basic_fbstring<E, T, A, S>& lhs,
2258                const typename basic_fbstring<E, T, A, S>::value_type* rhs) {
2259   return rhs < lhs; }
2260
2261 template <typename E, class T, class A, class S>
2262 inline
2263 bool operator>(const typename basic_fbstring<E, T, A, S>::value_type* lhs,
2264                const basic_fbstring<E, T, A, S>& rhs) {
2265   return rhs < lhs; }
2266
2267 template <typename E, class T, class A, class S>
2268 inline
2269 bool operator<=(const basic_fbstring<E, T, A, S>& lhs,
2270                 const basic_fbstring<E, T, A, S>& rhs) {
2271   return !(rhs < lhs); }
2272
2273 template <typename E, class T, class A, class S>
2274 inline
2275 bool operator<=(const basic_fbstring<E, T, A, S>& lhs,
2276                 const typename basic_fbstring<E, T, A, S>::value_type* rhs) {
2277   return !(rhs < lhs); }
2278
2279 template <typename E, class T, class A, class S>
2280 inline
2281 bool operator<=(const typename basic_fbstring<E, T, A, S>::value_type* lhs,
2282                 const basic_fbstring<E, T, A, S>& rhs) {
2283   return !(rhs < lhs); }
2284
2285 template <typename E, class T, class A, class S>
2286 inline
2287 bool operator>=(const basic_fbstring<E, T, A, S>& lhs,
2288                 const basic_fbstring<E, T, A, S>& rhs) {
2289   return !(lhs < rhs); }
2290
2291 template <typename E, class T, class A, class S>
2292 inline
2293 bool operator>=(const basic_fbstring<E, T, A, S>& lhs,
2294                 const typename basic_fbstring<E, T, A, S>::value_type* rhs) {
2295   return !(lhs < rhs); }
2296
2297 template <typename E, class T, class A, class S>
2298 inline
2299 bool operator>=(const typename basic_fbstring<E, T, A, S>::value_type* lhs,
2300                 const basic_fbstring<E, T, A, S>& rhs) {
2301  return !(lhs < rhs);
2302 }
2303
2304 // C++11 21.4.8.8
2305 template <typename E, class T, class A, class S>
2306 void swap(basic_fbstring<E, T, A, S>& lhs, basic_fbstring<E, T, A, S>& rhs) {
2307   lhs.swap(rhs);
2308 }
2309
2310 // TODO: make this faster.
2311 template <typename E, class T, class A, class S>
2312 inline
2313 std::basic_istream<
2314   typename basic_fbstring<E, T, A, S>::value_type,
2315   typename basic_fbstring<E, T, A, S>::traits_type>&
2316   operator>>(
2317     std::basic_istream<typename basic_fbstring<E, T, A, S>::value_type,
2318     typename basic_fbstring<E, T, A, S>::traits_type>& is,
2319     basic_fbstring<E, T, A, S>& str) {
2320   typename std::basic_istream<E, T>::sentry sentry(is);
2321   typedef std::basic_istream<typename basic_fbstring<E, T, A, S>::value_type,
2322                              typename basic_fbstring<E, T, A, S>::traits_type>
2323                         __istream_type;
2324   typedef typename __istream_type::ios_base __ios_base;
2325   size_t extracted = 0;
2326   auto err = __ios_base::goodbit;
2327   if (sentry) {
2328     auto n = is.width();
2329     if (n <= 0) {
2330       n = str.max_size();
2331     }
2332     str.erase();
2333     for (auto got = is.rdbuf()->sgetc(); extracted != size_t(n); ++extracted) {
2334       if (got == T::eof()) {
2335         err |= __ios_base::eofbit;
2336         is.width(0);
2337         break;
2338       }
2339       if (isspace(got)) break;
2340       str.push_back(got);
2341       got = is.rdbuf()->snextc();
2342     }
2343   }
2344   if (!extracted) {
2345     err |= __ios_base::failbit;
2346   }
2347   if (err) {
2348     is.setstate(err);
2349   }
2350   return is;
2351 }
2352
2353 template <typename E, class T, class A, class S>
2354 inline
2355 std::basic_ostream<typename basic_fbstring<E, T, A, S>::value_type,
2356                    typename basic_fbstring<E, T, A, S>::traits_type>&
2357 operator<<(
2358   std::basic_ostream<typename basic_fbstring<E, T, A, S>::value_type,
2359   typename basic_fbstring<E, T, A, S>::traits_type>& os,
2360     const basic_fbstring<E, T, A, S>& str) {
2361 #if _LIBCPP_VERSION
2362   typename std::basic_ostream<
2363     typename basic_fbstring<E, T, A, S>::value_type,
2364     typename basic_fbstring<E, T, A, S>::traits_type>::sentry __s(os);
2365   if (__s) {
2366     typedef std::ostreambuf_iterator<
2367       typename basic_fbstring<E, T, A, S>::value_type,
2368       typename basic_fbstring<E, T, A, S>::traits_type> _Ip;
2369     size_t __len = str.size();
2370     bool __left =
2371       (os.flags() & std::ios_base::adjustfield) == std::ios_base::left;
2372     if (__pad_and_output(_Ip(os),
2373                          str.data(),
2374                          __left ? str.data() + __len : str.data(),
2375                          str.data() + __len,
2376                          os,
2377                          os.fill()).failed()) {
2378       os.setstate(std::ios_base::badbit | std::ios_base::failbit);
2379     }
2380   }
2381 #elif defined(_MSC_VER)
2382   // MSVC doesn't define __ostream_insert
2383   os.write(str.data(), str.size());
2384 #else
2385   std::__ostream_insert(os, str.data(), str.size());
2386 #endif
2387   return os;
2388 }
2389
2390 template <typename E1, class T, class A, class S>
2391 const typename basic_fbstring<E1, T, A, S>::size_type
2392 basic_fbstring<E1, T, A, S>::npos =
2393               static_cast<typename basic_fbstring<E1, T, A, S>::size_type>(-1);
2394
2395 #ifndef _LIBSTDCXX_FBSTRING
2396 // basic_string compatibility routines
2397
2398 template <typename E, class T, class A, class S>
2399 inline
2400 bool operator==(const basic_fbstring<E, T, A, S>& lhs,
2401                 const std::string& rhs) {
2402   return lhs.compare(0, lhs.size(), rhs.data(), rhs.size()) == 0;
2403 }
2404
2405 template <typename E, class T, class A, class S>
2406 inline
2407 bool operator==(const std::string& lhs,
2408                 const basic_fbstring<E, T, A, S>& rhs) {
2409   return rhs == lhs;
2410 }
2411
2412 template <typename E, class T, class A, class S>
2413 inline
2414 bool operator!=(const basic_fbstring<E, T, A, S>& lhs,
2415                 const std::string& rhs) {
2416   return !(lhs == rhs);
2417 }
2418
2419 template <typename E, class T, class A, class S>
2420 inline
2421 bool operator!=(const std::string& lhs,
2422                 const basic_fbstring<E, T, A, S>& rhs) {
2423   return !(lhs == rhs);
2424 }
2425
2426 #if !defined(_LIBSTDCXX_FBSTRING)
2427 typedef basic_fbstring<char> fbstring;
2428 #endif
2429
2430 // fbstring is relocatable
2431 template <class T, class R, class A, class S>
2432 FOLLY_ASSUME_RELOCATABLE(basic_fbstring<T, R, A, S>);
2433
2434 #else
2435 _GLIBCXX_END_NAMESPACE_VERSION
2436 #endif
2437
2438 } // namespace folly
2439
2440 #ifndef _LIBSTDCXX_FBSTRING
2441
2442 // Hash functions to make fbstring usable with e.g. hash_map
2443 //
2444 // Handle interaction with different C++ standard libraries, which
2445 // expect these types to be in different namespaces.
2446
2447 #define FOLLY_FBSTRING_HASH1(T) \
2448   template <> \
2449   struct hash< ::folly::basic_fbstring<T> > { \
2450     size_t operator()(const ::folly::fbstring& s) const { \
2451       return ::folly::hash::fnv32_buf(s.data(), s.size()); \
2452     } \
2453   };
2454
2455 // The C++11 standard says that these four are defined
2456 #define FOLLY_FBSTRING_HASH \
2457   FOLLY_FBSTRING_HASH1(char) \
2458   FOLLY_FBSTRING_HASH1(char16_t) \
2459   FOLLY_FBSTRING_HASH1(char32_t) \
2460   FOLLY_FBSTRING_HASH1(wchar_t)
2461
2462 namespace std {
2463
2464 FOLLY_FBSTRING_HASH
2465
2466 }  // namespace std
2467
2468 #if FOLLY_HAVE_DEPRECATED_ASSOC
2469 #if defined(_GLIBCXX_SYMVER) && !defined(__BIONIC__)
2470 namespace __gnu_cxx {
2471
2472 FOLLY_FBSTRING_HASH
2473
2474 }  // namespace __gnu_cxx
2475 #endif // _GLIBCXX_SYMVER && !__BIONIC__
2476 #endif // FOLLY_HAVE_DEPRECATED_ASSOC
2477
2478 #undef FOLLY_FBSTRING_HASH
2479 #undef FOLLY_FBSTRING_HASH1
2480
2481 #endif // _LIBSTDCXX_FBSTRING
2482
2483 #pragma GCC diagnostic pop
2484
2485 #undef FBSTRING_DISABLE_SMALL_STRING_OPTIMIZATION
2486 #undef FBSTRING_SANITIZE_ADDRESS
2487 #undef throw
2488 #undef FBSTRING_LIKELY
2489 #undef FBSTRING_UNLIKELY
2490
2491 #ifdef FOLLY_DEFINED_NDEBUG_FOR_FBSTRING
2492 #undef NDEBUG
2493 #undef FOLLY_DEFINED_NDEBUG_FOR_FBSTRING
2494 #endif // FOLLY_DEFINED_NDEBUG_FOR_FBSTRING
2495
2496 #endif // FOLLY_BASE_FBSTRING_H_