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