2 * Copyright 2016 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 // @author: Andrei Alexandrescu (aalexandre)
24 #include <type_traits>
26 // This file appears in two locations: inside fbcode and in the
27 // libstdc++ source code (when embedding fbstring as std::string).
28 // To aid in this schizophrenic use, _LIBSTDCXX_FBSTRING is defined in
29 // libstdc++'s c++config.h, to gate use inside fbcode v. libstdc++.
30 #ifdef _LIBSTDCXX_FBSTRING
32 #pragma GCC system_header
34 // When used as std::string replacement always disable assertions.
37 #define FOLLY_DEFINED_NDEBUG_FOR_FBSTRING
40 // Handle the cases where the fbcode version (folly/Malloc.h) is included
41 // either before or after this inclusion.
42 #ifdef FOLLY_MALLOC_H_
43 #undef FOLLY_MALLOC_H_
44 #include "basic_fbstring_malloc.h" // nolint
46 #include "basic_fbstring_malloc.h" // nolint
47 #undef FOLLY_MALLOC_H_
50 #else // !_LIBSTDCXX_FBSTRING
52 #include <folly/Portability.h>
54 // libc++ doesn't provide this header, nor does msvc
55 #ifdef FOLLY_HAVE_BITS_CXXCONFIG_H
56 #include <bits/c++config.h>
64 #include <folly/Traits.h>
65 #include <folly/Malloc.h>
66 #include <folly/Hash.h>
67 #include <folly/ScopeGuard.h>
69 #if FOLLY_HAVE_DEPRECATED_ASSOC
70 #ifdef _GLIBCXX_SYMVER
71 #include <ext/hash_set>
72 #include <ext/hash_map>
78 // We defined these here rather than including Likely.h to avoid
79 // redefinition errors when fbstring is imported into libstdc++.
80 #if defined(__GNUC__) && __GNUC__ >= 4
81 #define FBSTRING_LIKELY(x) (__builtin_expect((x), 1))
82 #define FBSTRING_UNLIKELY(x) (__builtin_expect((x), 0))
84 #define FBSTRING_LIKELY(x) (x)
85 #define FBSTRING_UNLIKELY(x) (x)
88 #pragma GCC diagnostic push
89 // Ignore shadowing warnings within this file, so includers can use -Wshadow.
90 #pragma GCC diagnostic ignored "-Wshadow"
91 // GCC 4.9 has a false positive in setSmallSize (probably
92 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59124), disable
93 // compile-time array bound checking.
94 #pragma GCC diagnostic ignored "-Warray-bounds"
96 // FBString cannot use throw when replacing std::string, though it may still
99 #define throw FOLLY_FBSTRING_MAY_NOT_USE_THROW
101 #ifdef _LIBSTDCXX_FBSTRING
102 namespace std _GLIBCXX_VISIBILITY(default) {
103 _GLIBCXX_BEGIN_NAMESPACE_VERSION
108 #if defined(__clang__)
109 # if __has_feature(address_sanitizer)
110 # define FBSTRING_SANITIZE_ADDRESS
112 #elif defined (__GNUC__) && \
113 (((__GNUC__ == 4) && (__GNUC_MINOR__ >= 8)) || (__GNUC__ >= 5)) && \
115 # define FBSTRING_SANITIZE_ADDRESS
118 // When compiling with ASan, always heap-allocate the string even if
119 // it would fit in-situ, so that ASan can detect access to the string
120 // buffer after it has been invalidated (destroyed, resized, etc.).
121 // Note that this flag doesn't remove support for in-situ strings, as
122 // that would break ABI-compatibility and wouldn't allow linking code
123 // compiled with this flag with code compiled without.
124 #ifdef FBSTRING_SANITIZE_ADDRESS
125 # define FBSTRING_DISABLE_SSO true
127 # define FBSTRING_DISABLE_SSO false
130 namespace fbstring_detail {
132 template <class InIt, class OutIt>
135 typename std::iterator_traits<InIt>::difference_type n,
137 for (; n != 0; --n, ++b, ++d) {
143 template <class Pod, class T>
144 inline void pod_fill(Pod* b, Pod* e, T c) {
145 assert(b && e && b <= e);
146 /*static*/ if (sizeof(T) == 1) {
149 auto const ee = b + ((e - b) & ~7u);
150 for (; b != ee; b += 8) {
161 for (; b != e; ++b) {
168 * Lightly structured memcpy, simplifies copying PODs and introduces
169 * some asserts. Unfortunately using this function may cause
170 * measurable overhead (presumably because it adjusts from a begin/end
171 * convention to a pointer/size convention, so it does some extra
172 * arithmetic even though the caller might have done the inverse
173 * adaptation outside).
176 inline void pod_copy(const Pod* b, const Pod* e, Pod* d) {
178 assert(d >= e || d + (e - b) <= b);
179 memcpy(d, b, (e - b) * sizeof(Pod));
183 * Lightly structured memmove, simplifies copying PODs and introduces
187 inline void pod_move(const Pod* b, const Pod* e, Pod* d) {
189 memmove(d, b, (e - b) * sizeof(*b));
192 } // namespace fbstring_detail
195 * Defines a special acquisition method for constructing fbstring
196 * objects. AcquireMallocatedString means that the user passes a
197 * pointer to a malloc-allocated string that the fbstring object will
200 enum class AcquireMallocatedString {};
203 * fbstring_core_model is a mock-up type that defines all required
204 * signatures of a fbstring core. The fbstring class itself uses such
205 * a core object to implement all of the numerous member functions
206 * required by the standard.
208 * If you want to define a new core, copy the definition below and
209 * implement the primitives. Then plug the core into basic_fbstring as
210 * a template argument.
212 template <class Char>
213 class fbstring_core_model {
215 fbstring_core_model();
216 fbstring_core_model(const fbstring_core_model &);
217 ~fbstring_core_model();
218 // Returns a pointer to string's buffer (currently only contiguous
219 // strings are supported). The pointer is guaranteed to be valid
220 // until the next call to a non-const member function.
221 const Char * data() const;
222 // Much like data(), except the string is prepared to support
223 // character-level changes. This call is a signal for
224 // e.g. reference-counted implementation to fork the data. The
225 // pointer is guaranteed to be valid until the next call to a
226 // non-const member function.
227 Char * mutable_data();
228 // Returns a pointer to string's buffer and guarantees that a
229 // readable '\0' lies right after the buffer. The pointer is
230 // guaranteed to be valid until the next call to a non-const member
232 const Char * c_str() const;
233 // Shrinks the string by delta characters. Asserts that delta <=
235 void shrink(size_t delta);
236 // Expands the string by delta characters (i.e. after this call
237 // size() will report the old size() plus delta) but without
238 // initializing the expanded region. The expanded region is
239 // zero-terminated. Returns a pointer to the memory to be
240 // initialized (the beginning of the expanded portion). The caller
241 // is expected to fill the expanded area appropriately.
242 // If expGrowth is true, exponential growth is guaranteed.
243 // It is not guaranteed not to reallocate even if size() + delta <
244 // capacity(), so all references to the buffer are invalidated.
245 Char* expand_noinit(size_t delta, bool expGrowth);
246 // Expands the string by one character and sets the last character
248 void push_back(Char c);
249 // Returns the string's size.
251 // Returns the string's capacity, i.e. maximum size that the string
252 // can grow to without reallocation. Note that for reference counted
253 // strings that's technically a lie - even assigning characters
254 // within the existing size would cause a reallocation.
255 size_t capacity() const;
256 // Returns true if the data underlying the string is actually shared
257 // across multiple strings (in a refcounted fashion).
258 bool isShared() const;
259 // Makes sure that at least minCapacity characters are available for
260 // the string without reallocation. For reference-counted strings,
261 // it should fork the data even if minCapacity < size().
262 void reserve(size_t minCapacity);
265 fbstring_core_model& operator=(const fbstring_core_model &);
270 * This is the core of the string. The code should work on 32- and
271 * 64-bit and both big- and little-endianan architectures with any
274 * The storage is selected as follows (assuming we store one-byte
275 * characters on a 64-bit machine): (a) "small" strings between 0 and
276 * 23 chars are stored in-situ without allocation (the rightmost byte
277 * stores the size); (b) "medium" strings from 24 through 254 chars
278 * are stored in malloc-allocated memory that is copied eagerly; (c)
279 * "large" strings of 255 chars and above are stored in a similar
280 * structure as medium arrays, except that the string is
281 * reference-counted and copied lazily. the reference count is
282 * allocated right before the character array.
284 * The discriminator between these three strategies sits in two
285 * bits of the rightmost char of the storage. If neither is set, then the
286 * string is small (and its length sits in the lower-order bits on
287 * little-endian or the high-order bits on big-endian of that
288 * rightmost character). If the MSb is set, the string is medium width.
289 * If the second MSb is set, then the string is large. On little-endian,
290 * these 2 bits are the 2 MSbs of MediumLarge::capacity_, while on
291 * big-endian, these 2 bits are the 2 LSbs. This keeps both little-endian
292 * and big-endian fbstring_core equivalent with merely different ops used
293 * to extract capacity/category.
295 template <class Char> class fbstring_core {
297 // It's MSVC, so we just have to guess ... and allow an override
299 # ifdef FOLLY_ENDIAN_BE
300 static constexpr auto kIsLittleEndian = false;
302 static constexpr auto kIsLittleEndian = true;
305 static constexpr auto kIsLittleEndian =
306 __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__;
309 fbstring_core() noexcept { reset(); }
311 fbstring_core(const fbstring_core & rhs) {
312 assert(&rhs != this);
313 // Simplest case first: small strings are bitblitted
314 if (rhs.category() == Category::isSmall) {
315 static_assert(offsetof(MediumLarge, data_) == 0,
316 "fbstring layout failure");
317 static_assert(offsetof(MediumLarge, size_) == sizeof(ml_.data_),
318 "fbstring layout failure");
319 static_assert(offsetof(MediumLarge, capacity_) == 2 * sizeof(ml_.data_),
320 "fbstring layout failure");
321 // Just write the whole thing, don't look at details. In
322 // particular we need to copy capacity anyway because we want
323 // to set the size (don't forget that the last character,
324 // which stores a short string's length, is shared with the
325 // ml_.capacity field).
327 assert(category() == Category::isSmall && this->size() == rhs.size());
328 } else if (rhs.category() == Category::isLarge) {
329 // Large strings are just refcounted
331 RefCounted::incrementRefs(ml_.data_);
332 assert(category() == Category::isLarge && size() == rhs.size());
334 // Medium strings are copied eagerly. Don't forget to allocate
335 // one extra Char for the null terminator.
336 auto const allocSize =
337 goodMallocSize((1 + rhs.ml_.size_) * sizeof(Char));
338 ml_.data_ = static_cast<Char*>(checkedMalloc(allocSize));
339 // Also copies terminator.
340 fbstring_detail::pod_copy(rhs.ml_.data_,
341 rhs.ml_.data_ + rhs.ml_.size_ + 1,
343 ml_.size_ = rhs.ml_.size_;
344 ml_.setCapacity(allocSize / sizeof(Char) - 1, Category::isMedium);
345 assert(category() == Category::isMedium);
347 assert(size() == rhs.size());
348 assert(memcmp(data(), rhs.data(), size() * sizeof(Char)) == 0);
351 fbstring_core(fbstring_core&& goner) noexcept {
354 // Clean goner's carcass
358 fbstring_core(const Char *const data,
360 bool disableSSO = FBSTRING_DISABLE_SSO) {
362 #ifndef _LIBSTDCXX_FBSTRING
364 assert(this->size() == size);
365 assert(size == 0 || memcmp(this->data(), data, size * sizeof(Char)) == 0);
370 // Simplest case first: small strings are bitblitted
371 if (!disableSSO && size <= maxSmallSize) {
372 // Layout is: Char* data_, size_t size_, size_t capacity_
373 static_assert(sizeof(*this) == sizeof(Char*) + 2 * sizeof(size_t),
374 "fbstring has unexpected size");
375 static_assert(sizeof(Char*) == sizeof(size_t),
376 "fbstring size assumption violation");
377 // sizeof(size_t) must be a power of 2
378 static_assert((sizeof(size_t) & (sizeof(size_t) - 1)) == 0,
379 "fbstring size assumption violation");
381 // If data is aligned, use fast word-wise copying. Otherwise,
382 // use conservative memcpy.
383 // The word-wise path reads bytes which are outside the range of
384 // the string, and makes ASan unhappy, so we disable it when
385 // compiling with ASan.
386 #ifndef FBSTRING_SANITIZE_ADDRESS
387 if ((reinterpret_cast<size_t>(data) & (sizeof(size_t) - 1)) == 0) {
388 const size_t byteSize = size * sizeof(Char);
389 constexpr size_t wordWidth = sizeof(size_t);
390 switch ((byteSize + wordWidth - 1) / wordWidth) { // Number of words.
392 ml_.capacity_ = reinterpret_cast<const size_t*>(data)[2];
394 ml_.size_ = reinterpret_cast<const size_t*>(data)[1];
396 ml_.data_ = *reinterpret_cast<Char**>(const_cast<Char*>(data));
404 fbstring_detail::pod_copy(data, data + size, small_);
409 if (size <= maxMediumSize) {
410 // Medium strings are allocated normally. Don't forget to
411 // allocate one extra Char for the terminating null.
412 auto const allocSize = goodMallocSize((1 + size) * sizeof(Char));
413 ml_.data_ = static_cast<Char*>(checkedMalloc(allocSize));
414 fbstring_detail::pod_copy(data, data + size, ml_.data_);
416 ml_.setCapacity(allocSize / sizeof(Char) - 1, Category::isMedium);
418 // Large strings are allocated differently
419 size_t effectiveCapacity = size;
420 auto const newRC = RefCounted::create(data, & effectiveCapacity);
421 ml_.data_ = newRC->data_;
423 ml_.setCapacity(effectiveCapacity, Category::isLarge);
425 ml_.data_[size] = '\0';
429 ~fbstring_core() noexcept {
430 auto const c = category();
431 if (c == Category::isSmall) {
434 if (c == Category::isMedium) {
438 RefCounted::decrementRefs(ml_.data_);
441 // Snatches a previously mallocated string. The parameter "size"
442 // is the size of the string, and the parameter "allocatedSize"
443 // is the size of the mallocated block. The string must be
444 // \0-terminated, so allocatedSize >= size + 1 and data[size] == '\0'.
446 // So if you want a 2-character string, pass malloc(3) as "data",
447 // pass 2 as "size", and pass 3 as "allocatedSize".
448 fbstring_core(Char * const data,
450 const size_t allocatedSize,
451 AcquireMallocatedString) {
453 assert(allocatedSize >= size + 1);
454 assert(data[size] == '\0');
455 // Use the medium string storage
458 // Don't forget about null terminator
459 ml_.setCapacity(allocatedSize - 1, Category::isMedium);
461 // No need for the memory
467 // swap below doesn't test whether &rhs == this (and instead
468 // potentially does extra work) on the premise that the rarity of
469 // that situation actually makes the check more expensive than is
471 void swap(fbstring_core & rhs) {
477 // In C++11 data() and c_str() are 100% equivalent.
478 const Char * data() const {
482 Char * mutable_data() {
483 auto const c = category();
484 if (c == Category::isSmall) {
487 assert(c == Category::isMedium || c == Category::isLarge);
488 if (c == Category::isLarge && RefCounted::refs(ml_.data_) > 1) {
490 size_t effectiveCapacity = ml_.capacity();
491 auto const newRC = RefCounted::create(& effectiveCapacity);
492 // If this fails, someone placed the wrong capacity in an
494 assert(effectiveCapacity >= ml_.capacity());
495 // Also copies terminator.
496 fbstring_detail::pod_copy(ml_.data_, ml_.data_ + ml_.size_ + 1,
498 RefCounted::decrementRefs(ml_.data_);
499 ml_.data_ = newRC->data_;
504 const Char * c_str() const {
505 auto const c = category();
506 if (c == Category::isSmall) {
507 assert(small_[smallSize()] == '\0');
510 assert(c == Category::isMedium || c == Category::isLarge);
511 assert(ml_.data_[ml_.size_] == '\0');
515 void shrink(const size_t delta) {
516 if (category() == Category::isSmall) {
517 // Check for underflow
518 assert(delta <= smallSize());
519 setSmallSize(smallSize() - delta);
520 } else if (category() == Category::isMedium ||
521 RefCounted::refs(ml_.data_) == 1) {
522 // Medium strings and unique large strings need no special
524 assert(ml_.size_ >= delta);
526 ml_.data_[ml_.size_] = '\0';
528 assert(ml_.size_ >= delta);
529 // Shared large string, must make unique. This is because of the
530 // durn terminator must be written, which may trample the shared
533 fbstring_core(ml_.data_, ml_.size_ - delta).swap(*this);
535 // No need to write the terminator.
539 void reserve(size_t minCapacity, bool disableSSO = FBSTRING_DISABLE_SSO) {
540 if (category() == Category::isLarge) {
542 if (RefCounted::refs(ml_.data_) > 1) {
543 // We must make it unique regardless; in-place reallocation is
544 // useless if the string is shared. In order to not surprise
545 // people, reserve the new block at current capacity or
546 // more. That way, a string's capacity never shrinks after a
548 minCapacity = std::max(minCapacity, ml_.capacity());
549 auto const newRC = RefCounted::create(& minCapacity);
550 // Also copies terminator.
551 fbstring_detail::pod_copy(ml_.data_, ml_.data_ + ml_.size_ + 1,
553 RefCounted::decrementRefs(ml_.data_);
554 ml_.data_ = newRC->data_;
555 ml_.setCapacity(minCapacity, Category::isLarge);
556 // size remains unchanged
558 // String is not shared, so let's try to realloc (if needed)
559 if (minCapacity > ml_.capacity()) {
560 // Asking for more memory
562 RefCounted::reallocate(ml_.data_, ml_.size_,
563 ml_.capacity(), minCapacity);
564 ml_.data_ = newRC->data_;
565 ml_.setCapacity(minCapacity, Category::isLarge);
567 assert(capacity() >= minCapacity);
569 } else if (category() == Category::isMedium) {
570 // String is not shared
571 if (minCapacity <= ml_.capacity()) {
572 return; // nothing to do, there's enough room
574 if (minCapacity <= maxMediumSize) {
575 // Keep the string at medium size. Don't forget to allocate
576 // one extra Char for the terminating null.
577 size_t capacityBytes = goodMallocSize((1 + minCapacity) * sizeof(Char));
578 // Also copies terminator.
579 ml_.data_ = static_cast<Char *>(
582 (ml_.size_ + 1) * sizeof(Char),
583 (ml_.capacity() + 1) * sizeof(Char),
585 ml_.setCapacity(capacityBytes / sizeof(Char) - 1, Category::isMedium);
587 // Conversion from medium to large string
588 fbstring_core nascent;
589 // Will recurse to another branch of this function
590 nascent.reserve(minCapacity);
591 nascent.ml_.size_ = ml_.size_;
592 // Also copies terminator.
593 fbstring_detail::pod_copy(ml_.data_, ml_.data_ + ml_.size_ + 1,
596 assert(capacity() >= minCapacity);
599 assert(category() == Category::isSmall);
600 if (!disableSSO && minCapacity <= maxSmallSize) {
602 // Nothing to do, everything stays put
603 } else if (minCapacity <= maxMediumSize) {
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);
614 ml_.setCapacity(allocSizeBytes / sizeof(Char) - 1, Category::isMedium);
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_;
623 ml_.setCapacity(minCapacity, Category::isLarge);
624 assert(capacity() >= minCapacity);
627 assert(capacity() >= minCapacity);
630 Char * expand_noinit(const size_t delta,
631 bool expGrowth = false,
632 bool disableSSO = FBSTRING_DISABLE_SSO) {
633 // Strategy is simple: make room, then change size
634 assert(capacity() >= size());
636 if (category() == Category::isSmall) {
639 if (!disableSSO && FBSTRING_LIKELY(newSz <= maxSmallSize)) {
643 reserve(expGrowth ? std::max(newSz, 2 * maxSmallSize) : newSz);
647 if (FBSTRING_UNLIKELY(newSz > capacity())) {
648 // ensures not shared
649 reserve(expGrowth ? std::max(newSz, 1 + capacity() * 3 / 2) : newSz);
652 assert(capacity() >= newSz);
653 // Category can't be small - we took care of that above
654 assert(category() == Category::isMedium || category() == Category::isLarge);
656 ml_.data_[newSz] = '\0';
657 assert(size() == newSz);
658 return ml_.data_ + sz;
661 void push_back(Char c) {
662 *expand_noinit(1, /* expGrowth = */ true) = c;
665 size_t size() const {
666 return category() == Category::isSmall ? smallSize() : ml_.size_;
669 size_t capacity() const {
670 switch (category()) {
671 case Category::isSmall:
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_;
680 return ml_.capacity();
683 bool isShared() const {
684 return category() == Category::isLarge && RefCounted::refs(ml_.data_) > 1;
689 fbstring_core & operator=(const fbstring_core & rhs);
691 // Equivalent to setSmallSize(0) but a few ns faster in
694 ml_.capacity_ = kIsLittleEndian
695 ? maxSmallSize << (8 * (sizeof(size_t) - sizeof(Char)))
698 assert(category() == Category::isSmall && size() == 0);
702 std::atomic<size_t> refCount_;
705 static RefCounted * fromData(Char * p) {
706 return static_cast<RefCounted*>(
708 static_cast<unsigned char*>(static_cast<void*>(p))
709 - sizeof(refCount_)));
712 static size_t refs(Char * p) {
713 return fromData(p)->refCount_.load(std::memory_order_acquire);
716 static void incrementRefs(Char * p) {
717 fromData(p)->refCount_.fetch_add(1, std::memory_order_acq_rel);
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);
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
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);
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_);
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
758 auto result = static_cast<RefCounted*>(
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);
768 typedef std::conditional<sizeof(size_t) == 4, uint32_t, uint64_t>::type
771 enum class Category : category_type {
773 isMedium = kIsLittleEndian
774 ? sizeof(size_t) == 4 ? 0x80000000 : 0x8000000000000000
776 isLarge = kIsLittleEndian
777 ? sizeof(size_t) == 4 ? 0x40000000 : 0x4000000000000000
781 Category category() const {
782 // works for both big-endian and little-endian
783 return static_cast<Category>(ml_.capacity_ & categoryExtractMask);
791 size_t capacity() const {
792 return kIsLittleEndian
793 ? capacity_ & capacityExtractMask
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);
805 Char small_[sizeof(MediumLarge) / sizeof(Char)];
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
817 capacityExtractMask = kIsLittleEndian
818 ? ~categoryExtractMask
821 static_assert(!(sizeof(MediumLarge) % sizeof(Char)),
822 "Corrupt memory layout for fbstring.");
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;
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;
840 assert(category() == Category::isSmall && size() == s);
844 #ifndef _LIBSTDCXX_FBSTRING
846 * Dummy fbstring core that uses an actual std::string. This doesn't
847 * make any sense - it's just for testing purposes.
849 template <class Char>
850 class dummy_fbstring_core {
852 dummy_fbstring_core() {
854 dummy_fbstring_core(const dummy_fbstring_core& another)
855 : backend_(another.backend_) {
857 dummy_fbstring_core(const Char * s, size_t n)
860 void swap(dummy_fbstring_core & rhs) {
861 backend_.swap(rhs.backend_);
863 const Char * data() const {
864 return backend_.data();
866 Char * mutable_data() {
867 //assert(!backend_.empty());
868 return &*backend_.begin();
870 void shrink(size_t delta) {
871 assert(delta <= size());
872 backend_.resize(size() - delta);
874 Char * expand_noinit(size_t delta) {
875 auto const sz = size();
876 backend_.resize(size() + delta);
877 return backend_.data() + sz;
879 void push_back(Char c) {
880 backend_.push_back(c);
882 size_t size() const {
883 return backend_.size();
885 size_t capacity() const {
886 return backend_.capacity();
888 bool isShared() const {
891 void reserve(size_t minCapacity) {
892 backend_.reserve(minCapacity);
896 std::basic_string<Char> backend_;
898 #endif // !_LIBSTDCXX_FBSTRING
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.
905 #ifdef _LIBSTDCXX_FBSTRING
906 template <typename E, class T, class A, class Storage>
908 template <typename E,
909 class T = std::char_traits<E>,
910 class A = std::allocator<E>,
911 class Storage = fbstring_core<E> >
913 class basic_fbstring {
917 void (*throw_exc)(const char*),
919 if (!condition) throw_exc(msg);
922 bool isSane() const {
925 empty() == (size() == 0) &&
926 empty() == (begin() == end()) &&
927 size() <= max_size() &&
928 capacity() <= max_size() &&
929 size() <= capacity() &&
930 begin()[size()] == '\0';
934 friend struct Invariant;
937 explicit Invariant(const basic_fbstring& s) : s_(s) {
944 const basic_fbstring& s_;
946 explicit Invariant(const basic_fbstring&) {}
948 Invariant& operator=(const Invariant&);
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;
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;
965 typedef const E* const_iterator;
966 typedef std::reverse_iterator<iterator
967 #ifdef NO_ITERATOR_TRAITS
971 typedef std::reverse_iterator<const_iterator
972 #ifdef NO_ITERATOR_TRAITS
975 > const_reverse_iterator;
977 static const size_type npos; // = size_type(-1)
978 typedef std::true_type IsRelocatable;
981 static void procrustes(size_type& n, size_type nmax) {
982 if (n > nmax) n = nmax;
986 // C++11 21.4.2 construct/copy/destroy
988 // Note: while the following two constructors can be (and previously were)
989 // collapsed into one constructor written this way:
991 // explicit basic_fbstring(const A& a = A()) noexcept { }
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"
997 // if used in a struct which is default-initialized. Hence the split into
998 // these two separate constructors.
1000 basic_fbstring() noexcept : basic_fbstring(A()) {
1003 explicit basic_fbstring(const A&) noexcept {
1006 basic_fbstring(const basic_fbstring& str)
1007 : store_(str.store_) {
1011 basic_fbstring(basic_fbstring&& goner) noexcept
1012 : store_(std::move(goner.store_)) {
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()) {
1022 basic_fbstring(const basic_fbstring& str,
1025 const A& /* a */ = A()) {
1026 assign(str, pos, n);
1029 /* implicit */ basic_fbstring(const value_type* s, const A& /*a*/ = A())
1031 ? traits_type::length(s)
1032 : (std::__throw_logic_error(
1033 "basic_fbstring: null pointer initializer not valid"),
1037 basic_fbstring(const value_type* s, size_type n, const A& /*a*/ = A())
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);
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()) {
1054 // Specialization for const char*, const char*
1055 basic_fbstring(const value_type* b, const value_type* e)
1056 : store_(b, e - b) {
1059 // Nonstandard constructor
1060 basic_fbstring(value_type *s, size_type n, size_type c,
1061 AcquireMallocatedString a)
1062 : store_(s, n, c, a) {
1065 // Construction from initialization list
1066 basic_fbstring(std::initializer_list<value_type> il) {
1067 assign(il.begin(), il.end());
1070 ~basic_fbstring() noexcept {
1073 basic_fbstring& operator=(const basic_fbstring& lhs) {
1074 Invariant checker(*this);
1076 if (FBSTRING_UNLIKELY(&lhs == this)) {
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);
1086 store_.shrink(oldSize - srcSize);
1088 assert(size() == srcSize);
1089 auto srcData = lhs.data();
1090 fbstring_detail::pod_copy(
1091 srcData, srcData + srcSize, store_.mutable_data());
1093 // need to reallocate, so we may as well create a brand new string
1094 basic_fbstring(lhs).swap(*this);
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.
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_));
1113 #ifndef _LIBSTDCXX_FBSTRING
1114 // Compatibility with std::string
1115 basic_fbstring & operator=(const std::string & rhs) {
1116 return assign(rhs.data(), rhs.size());
1119 // Compatibility with std::string
1120 std::string toStdString() const {
1121 return std::string(data(), size());
1124 // A lot of code in fbcode still uses this method, so keep it here for now.
1125 const basic_fbstring& toStdString() const {
1130 basic_fbstring& operator=(const value_type* s) {
1134 basic_fbstring& operator=(value_type c) {
1135 Invariant checker(*this);
1138 store_.expand_noinit(1);
1139 } else if (store_.isShared()) {
1140 basic_fbstring(1, c).swap(*this);
1143 store_.shrink(size() - 1);
1149 basic_fbstring& operator=(std::initializer_list<value_type> il) {
1150 return assign(il.begin(), il.end());
1153 // C++11 21.4.3 iterators:
1154 iterator begin() { return store_.mutable_data(); }
1156 const_iterator begin() const { return store_.data(); }
1158 const_iterator cbegin() const { return begin(); }
1161 return store_.mutable_data() + store_.size();
1164 const_iterator end() const {
1165 return store_.data() + store_.size();
1168 const_iterator cend() const { return end(); }
1170 reverse_iterator rbegin() {
1171 return reverse_iterator(end());
1174 const_reverse_iterator rbegin() const {
1175 return const_reverse_iterator(end());
1178 const_reverse_iterator crbegin() const { return rbegin(); }
1180 reverse_iterator rend() {
1181 return reverse_iterator(begin());
1184 const_reverse_iterator rend() const {
1185 return const_reverse_iterator(begin());
1188 const_reverse_iterator crend() const { return rend(); }
1191 // C++11 21.4.5, element access:
1192 const value_type& front() const { return *begin(); }
1193 const value_type& back() const {
1195 // Should be begin()[size() - 1], but that branches twice
1196 return *(end() - 1);
1198 value_type& front() { return *begin(); }
1199 value_type& back() {
1201 // Should be begin()[size() - 1], but that branches twice
1202 return *(end() - 1);
1209 // C++11 21.4.4 capacity:
1210 size_type size() const { return store_.size(); }
1212 size_type length() const { return size(); }
1214 size_type max_size() const {
1215 return std::numeric_limits<size_type>::max();
1218 void resize(const size_type n, const value_type c = value_type()) {
1219 Invariant checker(*this);
1221 auto size = this->size();
1223 store_.shrink(size - n);
1225 auto const delta = n - size;
1226 auto pData = store_.expand_noinit(delta);
1227 fbstring_detail::pod_fill(pData, pData + delta, c);
1229 assert(this->size() == n);
1232 size_type capacity() const { return store_.capacity(); }
1234 void reserve(size_type res_arg = 0) {
1235 enforce(res_arg <= max_size(), std::__throw_length_error, "");
1236 store_.reserve(res_arg);
1239 void shrink_to_fit() {
1240 // Shrink only if slack memory is sufficiently large
1241 if (capacity() < size() * 3 / 2) {
1244 basic_fbstring(cbegin(), cend()).swap(*this);
1247 void clear() { resize(0); }
1249 bool empty() const { return size() == 0; }
1251 // C++11 21.4.5 element access:
1252 const_reference operator[](size_type pos) const {
1253 return *(begin() + pos);
1256 reference operator[](size_type pos) {
1257 return *(begin() + pos);
1260 const_reference at(size_type n) const {
1261 enforce(n <= size(), std::__throw_out_of_range, "");
1265 reference at(size_type n) {
1266 enforce(n < size(), std::__throw_out_of_range, "");
1270 // C++11 21.4.6 modifiers:
1271 basic_fbstring& operator+=(const basic_fbstring& str) {
1275 basic_fbstring& operator+=(const value_type* s) {
1279 basic_fbstring& operator+=(const value_type c) {
1284 basic_fbstring& operator+=(std::initializer_list<value_type> il) {
1289 basic_fbstring& append(const basic_fbstring& str) {
1291 auto desiredSize = size() + str.size();
1293 append(str.data(), str.size());
1294 assert(size() == desiredSize);
1298 basic_fbstring& append(const basic_fbstring& str, const size_type pos,
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);
1306 basic_fbstring& append(const value_type* s, size_type n) {
1307 Invariant checker(*this);
1309 if (FBSTRING_UNLIKELY(!n)) {
1310 // Unlikely but must be done
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
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;
1330 fbstring_detail::pod_copy(
1331 s, s + n, store_.expand_noinit(n, /* expGrowth = */ true));
1332 assert(size() == oldSize + n);
1336 basic_fbstring& append(const value_type* s) {
1337 return append(s, traits_type::length(s));
1340 basic_fbstring& append(size_type n, value_type c) {
1341 Invariant checker(*this);
1342 auto pData = store_.expand_noinit(n, /* expGrowth = */ true);
1343 fbstring_detail::pod_fill(pData, pData + n, c);
1347 template<class InputIterator>
1348 basic_fbstring& append(InputIterator first, InputIterator last) {
1349 insert(end(), first, last);
1353 basic_fbstring& append(std::initializer_list<value_type> il) {
1354 return append(il.begin(), il.end());
1357 void push_back(const value_type c) { // primitive
1358 store_.push_back(c);
1361 basic_fbstring& assign(const basic_fbstring& str) {
1362 if (&str == this) return *this;
1363 return assign(str.data(), str.size());
1366 basic_fbstring& assign(basic_fbstring&& str) {
1367 return *this = std::move(str);
1370 basic_fbstring& assign(const basic_fbstring& str, const size_type pos,
1372 const size_type sz = str.size();
1373 enforce(pos <= sz, std::__throw_out_of_range, "");
1374 procrustes(n, sz - pos);
1375 return assign(str.data() + pos, n);
1378 basic_fbstring& assign(const value_type* s, const size_type n) {
1379 Invariant checker(*this);
1381 // s can alias this, we need to use pod_move.
1384 } else if (size() >= n) {
1385 fbstring_detail::pod_move(s, s + n, store_.mutable_data());
1387 assert(size() == n);
1389 const value_type *const s2 = s + size();
1390 fbstring_detail::pod_move(s, s2, store_.mutable_data());
1391 append(s2, n - size());
1392 assert(size() == n);
1394 assert(size() == n);
1398 basic_fbstring& assign(const value_type* s) {
1399 return assign(s, traits_type::length(s));
1402 basic_fbstring& assign(std::initializer_list<value_type> il) {
1403 return assign(il.begin(), il.end());
1406 template <class ItOrLength, class ItOrChar>
1407 basic_fbstring& assign(ItOrLength first_or_n, ItOrChar last_or_c) {
1408 return replace(begin(), end(), first_or_n, last_or_c);
1411 basic_fbstring& insert(size_type pos1, const basic_fbstring& str) {
1412 return insert(pos1, str.data(), str.size());
1415 basic_fbstring& insert(size_type pos1, const basic_fbstring& str,
1416 size_type pos2, size_type n) {
1417 enforce(pos2 <= str.length(), std::__throw_out_of_range, "");
1418 procrustes(n, str.length() - pos2);
1419 return insert(pos1, str.data() + pos2, n);
1422 basic_fbstring& insert(size_type pos, const value_type* s, size_type n) {
1423 enforce(pos <= length(), std::__throw_out_of_range, "");
1424 insert(begin() + pos, s, s + n);
1428 basic_fbstring& insert(size_type pos, const value_type* s) {
1429 return insert(pos, s, traits_type::length(s));
1432 basic_fbstring& insert(size_type pos, size_type n, value_type c) {
1433 enforce(pos <= length(), std::__throw_out_of_range, "");
1434 insert(begin() + pos, n, c);
1438 iterator insert(const_iterator p, const value_type c) {
1439 const size_type pos = p - begin();
1441 return begin() + pos;
1444 #ifndef _LIBSTDCXX_FBSTRING
1446 typedef std::basic_istream<value_type, traits_type> istream_type;
1449 friend inline istream_type& getline(istream_type& is,
1450 basic_fbstring& str,
1452 Invariant checker(str);
1457 size_t avail = str.capacity() - size;
1458 // fbstring has 1 byte extra capacity for the null terminator,
1459 // and getline null-terminates the read string.
1460 is.getline(str.store_.expand_noinit(avail), avail + 1, delim);
1461 size += is.gcount();
1463 if (is.bad() || is.eof() || !is.fail()) {
1464 // Done by either failure, end of file, or normal read.
1465 if (!is.bad() && !is.eof()) {
1466 --size; // gcount() also accounts for the delimiter.
1472 assert(size == str.size());
1473 assert(size == str.capacity());
1474 // Start at minimum allocation 63 + terminator = 64.
1475 str.reserve(std::max<size_t>(63, 3 * size / 2));
1476 // Clear the error so we can continue reading.
1482 friend inline istream_type& getline(istream_type& is, basic_fbstring& str) {
1483 return getline(is, str, '\n');
1488 template <int i> class Selector {};
1490 iterator insertImplDiscr(const_iterator i,
1491 size_type n, value_type c, Selector<1>) {
1492 Invariant checker(*this);
1494 assert(i >= begin() && i <= end());
1495 const size_type pos = i - begin();
1497 auto oldSize = size();
1498 store_.expand_noinit(n, /* expGrowth = */ true);
1500 fbstring_detail::pod_move(b + pos, b + oldSize, b + pos + n);
1501 fbstring_detail::pod_fill(b + pos, b + pos + n, c);
1506 template<class InputIter>
1507 iterator insertImplDiscr(const_iterator i,
1508 InputIter b, InputIter e, Selector<0>) {
1509 return insertImpl(i, b, e,
1510 typename std::iterator_traits<InputIter>::iterator_category());
1513 template <class FwdIterator>
1514 iterator insertImpl(const_iterator i,
1517 std::forward_iterator_tag) {
1518 Invariant checker(*this);
1520 assert(i >= begin() && i <= end());
1521 const size_type pos = i - begin();
1522 auto n = std::distance(s1, s2);
1525 auto oldSize = size();
1526 store_.expand_noinit(n, /* expGrowth = */ true);
1528 fbstring_detail::pod_move(b + pos, b + oldSize, b + pos + n);
1529 std::copy(s1, s2, b + pos);
1534 template <class InputIterator>
1535 iterator insertImpl(const_iterator i,
1536 InputIterator b, InputIterator e,
1537 std::input_iterator_tag) {
1538 const auto pos = i - begin();
1539 basic_fbstring temp(begin(), i);
1540 for (; b != e; ++b) {
1543 temp.append(i, cend());
1545 return begin() + pos;
1549 template <class ItOrLength, class ItOrChar>
1550 iterator insert(const_iterator p, ItOrLength first_or_n, ItOrChar last_or_c) {
1551 Selector<std::numeric_limits<ItOrLength>::is_specialized> sel;
1552 return insertImplDiscr(p, first_or_n, last_or_c, sel);
1555 iterator insert(const_iterator p, std::initializer_list<value_type> il) {
1556 return insert(p, il.begin(), il.end());
1559 basic_fbstring& erase(size_type pos = 0, size_type n = npos) {
1560 Invariant checker(*this);
1562 enforce(pos <= length(), std::__throw_out_of_range, "");
1563 procrustes(n, length() - pos);
1564 std::copy(begin() + pos + n, end(), begin() + pos);
1565 resize(length() - n);
1569 iterator erase(iterator position) {
1570 const size_type pos(position - begin());
1571 enforce(pos <= size(), std::__throw_out_of_range, "");
1573 return begin() + pos;
1576 iterator erase(iterator first, iterator last) {
1577 const size_type pos(first - begin());
1578 erase(pos, last - first);
1579 return begin() + pos;
1582 // Replaces at most n1 chars of *this, starting with pos1 with the
1584 basic_fbstring& replace(size_type pos1, size_type n1,
1585 const basic_fbstring& str) {
1586 return replace(pos1, n1, str.data(), str.size());
1589 // Replaces at most n1 chars of *this, starting with pos1,
1590 // with at most n2 chars of str starting with pos2
1591 basic_fbstring& replace(size_type pos1, size_type n1,
1592 const basic_fbstring& str,
1593 size_type pos2, size_type n2) {
1594 enforce(pos2 <= str.length(), std::__throw_out_of_range, "");
1595 return replace(pos1, n1, str.data() + pos2,
1596 std::min(n2, str.size() - pos2));
1599 // Replaces at most n1 chars of *this, starting with pos, with chars from s
1600 basic_fbstring& replace(size_type pos, size_type n1, const value_type* s) {
1601 return replace(pos, n1, s, traits_type::length(s));
1604 // Replaces at most n1 chars of *this, starting with pos, with n2
1607 // consolidated with
1609 // Replaces at most n1 chars of *this, starting with pos, with at
1610 // most n2 chars of str. str must have at least n2 chars.
1611 template <class StrOrLength, class NumOrChar>
1612 basic_fbstring& replace(size_type pos, size_type n1,
1613 StrOrLength s_or_n2, NumOrChar n_or_c) {
1614 Invariant checker(*this);
1616 enforce(pos <= size(), std::__throw_out_of_range, "");
1617 procrustes(n1, length() - pos);
1618 const iterator b = begin() + pos;
1619 return replace(b, b + n1, s_or_n2, n_or_c);
1622 basic_fbstring& replace(iterator i1, iterator i2, const basic_fbstring& str) {
1623 return replace(i1, i2, str.data(), str.length());
1626 basic_fbstring& replace(iterator i1, iterator i2, const value_type* s) {
1627 return replace(i1, i2, s, traits_type::length(s));
1631 basic_fbstring& replaceImplDiscr(iterator i1, iterator i2,
1632 const value_type* s, size_type n,
1635 assert(begin() <= i1 && i1 <= end());
1636 assert(begin() <= i2 && i2 <= end());
1637 return replace(i1, i2, s, s + n);
1640 basic_fbstring& replaceImplDiscr(iterator i1, iterator i2,
1641 size_type n2, value_type c, Selector<1>) {
1642 const size_type n1 = i2 - i1;
1644 std::fill(i1, i1 + n2, c);
1647 std::fill(i1, i2, c);
1648 insert(i2, n2 - n1, c);
1654 template <class InputIter>
1655 basic_fbstring& replaceImplDiscr(iterator i1, iterator i2,
1656 InputIter b, InputIter e,
1658 replaceImpl(i1, i2, b, e,
1659 typename std::iterator_traits<InputIter>::iterator_category());
1664 template <class FwdIterator>
1665 bool replaceAliased(iterator /* i1 */,
1667 FwdIterator /* s1 */,
1668 FwdIterator /* s2 */,
1673 template <class FwdIterator>
1674 bool replaceAliased(iterator i1, iterator i2,
1675 FwdIterator s1, FwdIterator s2, std::true_type) {
1676 static const std::less_equal<const value_type*> le =
1677 std::less_equal<const value_type*>();
1678 const bool aliased = le(&*begin(), &*s1) && le(&*s1, &*end());
1682 // Aliased replace, copy to new string
1683 basic_fbstring temp;
1684 temp.reserve(size() - (i2 - i1) + std::distance(s1, s2));
1685 temp.append(begin(), i1).append(s1, s2).append(i2, end());
1690 template <class FwdIterator>
1691 void replaceImpl(iterator i1, iterator i2,
1692 FwdIterator s1, FwdIterator s2, std::forward_iterator_tag) {
1693 Invariant checker(*this);
1695 // Handle aliased replace
1696 if (replaceAliased(i1, i2, s1, s2,
1697 std::integral_constant<bool,
1698 std::is_same<FwdIterator, iterator>::value ||
1699 std::is_same<FwdIterator, const_iterator>::value>())) {
1703 auto const n1 = i2 - i1;
1705 auto const n2 = std::distance(s1, s2);
1710 std::copy(s1, s2, i1);
1714 fbstring_detail::copy_n(s1, n1, i1);
1715 std::advance(s1, n1);
1721 template <class InputIterator>
1722 void replaceImpl(iterator i1, iterator i2,
1723 InputIterator b, InputIterator e, std::input_iterator_tag) {
1724 basic_fbstring temp(begin(), i1);
1725 temp.append(b, e).append(i2, end());
1730 template <class T1, class T2>
1731 basic_fbstring& replace(iterator i1, iterator i2,
1732 T1 first_or_n_or_s, T2 last_or_c_or_n) {
1734 num1 = std::numeric_limits<T1>::is_specialized,
1735 num2 = std::numeric_limits<T2>::is_specialized;
1736 return replaceImplDiscr(
1737 i1, i2, first_or_n_or_s, last_or_c_or_n,
1738 Selector<num1 ? (num2 ? 1 : -1) : (num2 ? 2 : 0)>());
1741 size_type copy(value_type* s, size_type n, size_type pos = 0) const {
1742 enforce(pos <= size(), std::__throw_out_of_range, "");
1743 procrustes(n, size() - pos);
1746 fbstring_detail::pod_copy(data() + pos, data() + pos + n, s);
1751 void swap(basic_fbstring& rhs) {
1752 store_.swap(rhs.store_);
1755 const value_type* c_str() const {
1756 return store_.c_str();
1759 const value_type* data() const { return c_str(); }
1761 allocator_type get_allocator() const {
1762 return allocator_type();
1765 size_type find(const basic_fbstring& str, size_type pos = 0) const {
1766 return find(str.data(), pos, str.length());
1769 size_type find(const value_type* needle, const size_type pos,
1770 const size_type nsize) const {
1771 auto const size = this->size();
1772 // nsize + pos can overflow (eg pos == npos), guard against that by checking
1773 // that nsize + pos does not wrap around.
1774 if (nsize + pos > size || nsize + pos < pos) return npos;
1776 if (nsize == 0) return pos;
1777 // Don't use std::search, use a Boyer-Moore-like trick by comparing
1778 // the last characters first
1779 auto const haystack = data();
1780 auto const nsize_1 = nsize - 1;
1781 auto const lastNeedle = needle[nsize_1];
1783 // Boyer-Moore skip value for the last char in the needle. Zero is
1784 // not a valid value; skip will be computed the first time it's
1788 const E * i = haystack + pos;
1789 auto iEnd = haystack + size - nsize_1;
1792 // Boyer-Moore: match the last element in the needle
1793 while (i[nsize_1] != lastNeedle) {
1799 // Here we know that the last char matches
1800 // Continue in pedestrian mode
1801 for (size_t j = 0; ; ) {
1803 if (i[j] != needle[j]) {
1804 // Not found, we can skip
1805 // Compute the skip value lazily
1808 while (skip <= nsize_1 && needle[nsize_1 - skip] != lastNeedle) {
1815 // Check if done searching
1818 return i - haystack;
1825 size_type find(const value_type* s, size_type pos = 0) const {
1826 return find(s, pos, traits_type::length(s));
1829 size_type find (value_type c, size_type pos = 0) const {
1830 return find(&c, pos, 1);
1833 size_type rfind(const basic_fbstring& str, size_type pos = npos) const {
1834 return rfind(str.data(), pos, str.length());
1837 size_type rfind(const value_type* s, size_type pos, size_type n) const {
1838 if (n > length()) return npos;
1839 pos = std::min(pos, length() - n);
1840 if (n == 0) return pos;
1842 const_iterator i(begin() + pos);
1844 if (traits_type::eq(*i, *s)
1845 && traits_type::compare(&*i, s, n) == 0) {
1848 if (i == begin()) break;
1853 size_type rfind(const value_type* s, size_type pos = npos) const {
1854 return rfind(s, pos, traits_type::length(s));
1857 size_type rfind(value_type c, size_type pos = npos) const {
1858 return rfind(&c, pos, 1);
1861 size_type find_first_of(const basic_fbstring& str, size_type pos = 0) const {
1862 return find_first_of(str.data(), pos, str.length());
1865 size_type find_first_of(const value_type* s,
1866 size_type pos, size_type n) const {
1867 if (pos > length() || n == 0) return npos;
1868 const_iterator i(begin() + pos),
1870 for (; i != finish; ++i) {
1871 if (traits_type::find(s, n, *i) != 0) {
1878 size_type find_first_of(const value_type* s, size_type pos = 0) const {
1879 return find_first_of(s, pos, traits_type::length(s));
1882 size_type find_first_of(value_type c, size_type pos = 0) const {
1883 return find_first_of(&c, pos, 1);
1886 size_type find_last_of (const basic_fbstring& str,
1887 size_type pos = npos) const {
1888 return find_last_of(str.data(), pos, str.length());
1891 size_type find_last_of (const value_type* s, size_type pos,
1892 size_type n) const {
1893 if (!empty() && n > 0) {
1894 pos = std::min(pos, length() - 1);
1895 const_iterator i(begin() + pos);
1897 if (traits_type::find(s, n, *i) != 0) {
1900 if (i == begin()) break;
1906 size_type find_last_of (const value_type* s,
1907 size_type pos = npos) const {
1908 return find_last_of(s, pos, traits_type::length(s));
1911 size_type find_last_of (value_type c, size_type pos = npos) const {
1912 return find_last_of(&c, pos, 1);
1915 size_type find_first_not_of(const basic_fbstring& str,
1916 size_type pos = 0) const {
1917 return find_first_not_of(str.data(), pos, str.size());
1920 size_type find_first_not_of(const value_type* s, size_type pos,
1921 size_type n) const {
1922 if (pos < length()) {
1926 for (; i != finish; ++i) {
1927 if (traits_type::find(s, n, *i) == 0) {
1935 size_type find_first_not_of(const value_type* s,
1936 size_type pos = 0) const {
1937 return find_first_not_of(s, pos, traits_type::length(s));
1940 size_type find_first_not_of(value_type c, size_type pos = 0) const {
1941 return find_first_not_of(&c, pos, 1);
1944 size_type find_last_not_of(const basic_fbstring& str,
1945 size_type pos = npos) const {
1946 return find_last_not_of(str.data(), pos, str.length());
1949 size_type find_last_not_of(const value_type* s, size_type pos,
1950 size_type n) const {
1951 if (!this->empty()) {
1952 pos = std::min(pos, size() - 1);
1953 const_iterator i(begin() + pos);
1955 if (traits_type::find(s, n, *i) == 0) {
1958 if (i == begin()) break;
1964 size_type find_last_not_of(const value_type* s,
1965 size_type pos = npos) const {
1966 return find_last_not_of(s, pos, traits_type::length(s));
1969 size_type find_last_not_of (value_type c, size_type pos = npos) const {
1970 return find_last_not_of(&c, pos, 1);
1973 basic_fbstring substr(size_type pos = 0, size_type n = npos) const& {
1974 enforce(pos <= size(), std::__throw_out_of_range, "");
1975 return basic_fbstring(data() + pos, std::min(n, size() - pos));
1978 basic_fbstring substr(size_type pos = 0, size_type n = npos) && {
1979 enforce(pos <= size(), std::__throw_out_of_range, "");
1981 if (n < size()) resize(n);
1982 return std::move(*this);
1985 int compare(const basic_fbstring& str) const {
1986 // FIX due to Goncalo N M de Carvalho July 18, 2005
1987 return compare(0, size(), str);
1990 int compare(size_type pos1, size_type n1,
1991 const basic_fbstring& str) const {
1992 return compare(pos1, n1, str.data(), str.size());
1995 int compare(size_type pos1, size_type n1,
1996 const value_type* s) const {
1997 return compare(pos1, n1, s, traits_type::length(s));
2000 int compare(size_type pos1, size_type n1,
2001 const value_type* s, size_type n2) const {
2002 enforce(pos1 <= size(), std::__throw_out_of_range, "");
2003 procrustes(n1, size() - pos1);
2004 // The line below fixed by Jean-Francois Bastien, 04-23-2007. Thanks!
2005 const int r = traits_type::compare(pos1 + data(), s, std::min(n1, n2));
2006 return r != 0 ? r : n1 > n2 ? 1 : n1 < n2 ? -1 : 0;
2009 int compare(size_type pos1, size_type n1,
2010 const basic_fbstring& str,
2011 size_type pos2, size_type n2) const {
2012 enforce(pos2 <= str.size(), std::__throw_out_of_range, "");
2013 return compare(pos1, n1, str.data() + pos2,
2014 std::min(n2, str.size() - pos2));
2017 // Code from Jean-Francois Bastien (03/26/2007)
2018 int compare(const value_type* s) const {
2019 // Could forward to compare(0, size(), s, traits_type::length(s))
2020 // but that does two extra checks
2021 const size_type n1(size()), n2(traits_type::length(s));
2022 const int r = traits_type::compare(data(), s, std::min(n1, n2));
2023 return r != 0 ? r : n1 > n2 ? 1 : n1 < n2 ? -1 : 0;
2031 // non-member functions
2033 template <typename E, class T, class A, class S>
2035 basic_fbstring<E, T, A, S> operator+(const basic_fbstring<E, T, A, S>& lhs,
2036 const basic_fbstring<E, T, A, S>& rhs) {
2038 basic_fbstring<E, T, A, S> result;
2039 result.reserve(lhs.size() + rhs.size());
2040 result.append(lhs).append(rhs);
2041 return std::move(result);
2045 template <typename E, class T, class A, class S>
2047 basic_fbstring<E, T, A, S> operator+(basic_fbstring<E, T, A, S>&& lhs,
2048 const basic_fbstring<E, T, A, S>& rhs) {
2049 return std::move(lhs.append(rhs));
2053 template <typename E, class T, class A, class S>
2055 basic_fbstring<E, T, A, S> operator+(const basic_fbstring<E, T, A, S>& lhs,
2056 basic_fbstring<E, T, A, S>&& rhs) {
2057 if (rhs.capacity() >= lhs.size() + rhs.size()) {
2058 // Good, at least we don't need to reallocate
2059 return std::move(rhs.insert(0, lhs));
2061 // Meh, no go. Forward to operator+(const&, const&).
2062 auto const& rhsC = rhs;
2067 template <typename E, class T, class A, class S>
2069 basic_fbstring<E, T, A, S> operator+(basic_fbstring<E, T, A, S>&& lhs,
2070 basic_fbstring<E, T, A, S>&& rhs) {
2071 return std::move(lhs.append(rhs));
2075 template <typename E, class T, class A, class S>
2077 basic_fbstring<E, T, A, S> operator+(
2079 const basic_fbstring<E, T, A, S>& rhs) {
2081 basic_fbstring<E, T, A, S> result;
2082 const auto len = basic_fbstring<E, T, A, S>::traits_type::length(lhs);
2083 result.reserve(len + rhs.size());
2084 result.append(lhs, len).append(rhs);
2089 template <typename E, class T, class A, class S>
2091 basic_fbstring<E, T, A, S> operator+(
2093 basic_fbstring<E, T, A, S>&& rhs) {
2095 const auto len = basic_fbstring<E, T, A, S>::traits_type::length(lhs);
2096 if (rhs.capacity() >= len + rhs.size()) {
2097 // Good, at least we don't need to reallocate
2098 rhs.insert(rhs.begin(), lhs, lhs + len);
2101 // Meh, no go. Do it by hand since we have len already.
2102 basic_fbstring<E, T, A, S> result;
2103 result.reserve(len + rhs.size());
2104 result.append(lhs, len).append(rhs);
2109 template <typename E, class T, class A, class S>
2111 basic_fbstring<E, T, A, S> operator+(
2113 const basic_fbstring<E, T, A, S>& rhs) {
2115 basic_fbstring<E, T, A, S> result;
2116 result.reserve(1 + rhs.size());
2117 result.push_back(lhs);
2123 template <typename E, class T, class A, class S>
2125 basic_fbstring<E, T, A, S> operator+(
2127 basic_fbstring<E, T, A, S>&& rhs) {
2129 if (rhs.capacity() > rhs.size()) {
2130 // Good, at least we don't need to reallocate
2131 rhs.insert(rhs.begin(), lhs);
2134 // Meh, no go. Forward to operator+(E, const&).
2135 auto const& rhsC = rhs;
2140 template <typename E, class T, class A, class S>
2142 basic_fbstring<E, T, A, S> operator+(
2143 const basic_fbstring<E, T, A, S>& lhs,
2146 typedef typename basic_fbstring<E, T, A, S>::size_type size_type;
2147 typedef typename basic_fbstring<E, T, A, S>::traits_type traits_type;
2149 basic_fbstring<E, T, A, S> result;
2150 const size_type len = traits_type::length(rhs);
2151 result.reserve(lhs.size() + len);
2152 result.append(lhs).append(rhs, len);
2156 // C++11 21.4.8.1/10
2157 template <typename E, class T, class A, class S>
2159 basic_fbstring<E, T, A, S> operator+(
2160 basic_fbstring<E, T, A, S>&& lhs,
2163 return std::move(lhs += rhs);
2166 // C++11 21.4.8.1/11
2167 template <typename E, class T, class A, class S>
2169 basic_fbstring<E, T, A, S> operator+(
2170 const basic_fbstring<E, T, A, S>& lhs,
2173 basic_fbstring<E, T, A, S> result;
2174 result.reserve(lhs.size() + 1);
2176 result.push_back(rhs);
2180 // C++11 21.4.8.1/12
2181 template <typename E, class T, class A, class S>
2183 basic_fbstring<E, T, A, S> operator+(
2184 basic_fbstring<E, T, A, S>&& lhs,
2187 return std::move(lhs += rhs);
2190 template <typename E, class T, class A, class S>
2192 bool operator==(const basic_fbstring<E, T, A, S>& lhs,
2193 const basic_fbstring<E, T, A, S>& rhs) {
2194 return lhs.size() == rhs.size() && lhs.compare(rhs) == 0; }
2196 template <typename E, class T, class A, class S>
2198 bool operator==(const typename basic_fbstring<E, T, A, S>::value_type* lhs,
2199 const basic_fbstring<E, T, A, S>& rhs) {
2200 return rhs == lhs; }
2202 template <typename E, class T, class A, class S>
2204 bool operator==(const basic_fbstring<E, T, A, S>& lhs,
2205 const typename basic_fbstring<E, T, A, S>::value_type* rhs) {
2206 return lhs.compare(rhs) == 0; }
2208 template <typename E, class T, class A, class S>
2210 bool operator!=(const basic_fbstring<E, T, A, S>& lhs,
2211 const basic_fbstring<E, T, A, S>& rhs) {
2212 return !(lhs == rhs); }
2214 template <typename E, class T, class A, class S>
2216 bool operator!=(const typename basic_fbstring<E, T, A, S>::value_type* lhs,
2217 const basic_fbstring<E, T, A, S>& rhs) {
2218 return !(lhs == rhs); }
2220 template <typename E, class T, class A, class S>
2222 bool operator!=(const basic_fbstring<E, T, A, S>& lhs,
2223 const typename basic_fbstring<E, T, A, S>::value_type* rhs) {
2224 return !(lhs == rhs); }
2226 template <typename E, class T, class A, class S>
2228 bool operator<(const basic_fbstring<E, T, A, S>& lhs,
2229 const basic_fbstring<E, T, A, S>& rhs) {
2230 return lhs.compare(rhs) < 0; }
2232 template <typename E, class T, class A, class S>
2234 bool operator<(const basic_fbstring<E, T, A, S>& lhs,
2235 const typename basic_fbstring<E, T, A, S>::value_type* rhs) {
2236 return lhs.compare(rhs) < 0; }
2238 template <typename E, class T, class A, class S>
2240 bool operator<(const typename basic_fbstring<E, T, A, S>::value_type* lhs,
2241 const basic_fbstring<E, T, A, S>& rhs) {
2242 return rhs.compare(lhs) > 0; }
2244 template <typename E, class T, class A, class S>
2246 bool operator>(const basic_fbstring<E, T, A, S>& lhs,
2247 const basic_fbstring<E, T, A, S>& rhs) {
2250 template <typename E, class T, class A, class S>
2252 bool operator>(const basic_fbstring<E, T, A, S>& lhs,
2253 const typename basic_fbstring<E, T, A, S>::value_type* rhs) {
2256 template <typename E, class T, class A, class S>
2258 bool operator>(const typename basic_fbstring<E, T, A, S>::value_type* lhs,
2259 const basic_fbstring<E, T, A, S>& rhs) {
2262 template <typename E, class T, class A, class S>
2264 bool operator<=(const basic_fbstring<E, T, A, S>& lhs,
2265 const basic_fbstring<E, T, A, S>& rhs) {
2266 return !(rhs < lhs); }
2268 template <typename E, class T, class A, class S>
2270 bool operator<=(const basic_fbstring<E, T, A, S>& lhs,
2271 const typename basic_fbstring<E, T, A, S>::value_type* rhs) {
2272 return !(rhs < lhs); }
2274 template <typename E, class T, class A, class S>
2276 bool operator<=(const typename basic_fbstring<E, T, A, S>::value_type* lhs,
2277 const basic_fbstring<E, T, A, S>& rhs) {
2278 return !(rhs < lhs); }
2280 template <typename E, class T, class A, class S>
2282 bool operator>=(const basic_fbstring<E, T, A, S>& lhs,
2283 const basic_fbstring<E, T, A, S>& rhs) {
2284 return !(lhs < rhs); }
2286 template <typename E, class T, class A, class S>
2288 bool operator>=(const basic_fbstring<E, T, A, S>& lhs,
2289 const typename basic_fbstring<E, T, A, S>::value_type* rhs) {
2290 return !(lhs < rhs); }
2292 template <typename E, class T, class A, class S>
2294 bool operator>=(const typename basic_fbstring<E, T, A, S>::value_type* lhs,
2295 const basic_fbstring<E, T, A, S>& rhs) {
2296 return !(lhs < rhs);
2300 template <typename E, class T, class A, class S>
2301 void swap(basic_fbstring<E, T, A, S>& lhs, basic_fbstring<E, T, A, S>& rhs) {
2305 // TODO: make this faster.
2306 template <typename E, class T, class A, class S>
2309 typename basic_fbstring<E, T, A, S>::value_type,
2310 typename basic_fbstring<E, T, A, S>::traits_type>&
2312 std::basic_istream<typename basic_fbstring<E, T, A, S>::value_type,
2313 typename basic_fbstring<E, T, A, S>::traits_type>& is,
2314 basic_fbstring<E, T, A, S>& str) {
2315 typename std::basic_istream<E, T>::sentry sentry(is);
2316 typedef std::basic_istream<typename basic_fbstring<E, T, A, S>::value_type,
2317 typename basic_fbstring<E, T, A, S>::traits_type>
2319 typedef typename __istream_type::ios_base __ios_base;
2320 size_t extracted = 0;
2321 auto err = __ios_base::goodbit;
2323 auto n = is.width();
2328 for (auto got = is.rdbuf()->sgetc(); extracted != size_t(n); ++extracted) {
2329 if (got == T::eof()) {
2330 err |= __ios_base::eofbit;
2334 if (isspace(got)) break;
2336 got = is.rdbuf()->snextc();
2340 err |= __ios_base::failbit;
2348 template <typename E, class T, class A, class S>
2350 std::basic_ostream<typename basic_fbstring<E, T, A, S>::value_type,
2351 typename basic_fbstring<E, T, A, S>::traits_type>&
2353 std::basic_ostream<typename basic_fbstring<E, T, A, S>::value_type,
2354 typename basic_fbstring<E, T, A, S>::traits_type>& os,
2355 const basic_fbstring<E, T, A, S>& str) {
2357 typename std::basic_ostream<
2358 typename basic_fbstring<E, T, A, S>::value_type,
2359 typename basic_fbstring<E, T, A, S>::traits_type>::sentry __s(os);
2361 typedef std::ostreambuf_iterator<
2362 typename basic_fbstring<E, T, A, S>::value_type,
2363 typename basic_fbstring<E, T, A, S>::traits_type> _Ip;
2364 size_t __len = str.size();
2366 (os.flags() & std::ios_base::adjustfield) == std::ios_base::left;
2367 if (__pad_and_output(_Ip(os),
2369 __left ? str.data() + __len : str.data(),
2372 os.fill()).failed()) {
2373 os.setstate(std::ios_base::badbit | std::ios_base::failbit);
2376 #elif defined(_MSC_VER)
2377 // MSVC doesn't define __ostream_insert
2378 os.write(str.data(), str.size());
2380 std::__ostream_insert(os, str.data(), str.size());
2385 template <typename E1, class T, class A, class S>
2386 const typename basic_fbstring<E1, T, A, S>::size_type
2387 basic_fbstring<E1, T, A, S>::npos =
2388 static_cast<typename basic_fbstring<E1, T, A, S>::size_type>(-1);
2390 #ifndef _LIBSTDCXX_FBSTRING
2391 // basic_string compatibility routines
2393 template <typename E, class T, class A, class S>
2395 bool operator==(const basic_fbstring<E, T, A, S>& lhs,
2396 const std::string& rhs) {
2397 return lhs.compare(0, lhs.size(), rhs.data(), rhs.size()) == 0;
2400 template <typename E, class T, class A, class S>
2402 bool operator==(const std::string& lhs,
2403 const basic_fbstring<E, T, A, S>& rhs) {
2407 template <typename E, class T, class A, class S>
2409 bool operator!=(const basic_fbstring<E, T, A, S>& lhs,
2410 const std::string& rhs) {
2411 return !(lhs == rhs);
2414 template <typename E, class T, class A, class S>
2416 bool operator!=(const std::string& lhs,
2417 const basic_fbstring<E, T, A, S>& rhs) {
2418 return !(lhs == rhs);
2421 #if !defined(_LIBSTDCXX_FBSTRING)
2422 typedef basic_fbstring<char> fbstring;
2425 // fbstring is relocatable
2426 template <class T, class R, class A, class S>
2427 FOLLY_ASSUME_RELOCATABLE(basic_fbstring<T, R, A, S>);
2430 _GLIBCXX_END_NAMESPACE_VERSION
2433 } // namespace folly
2435 #ifndef _LIBSTDCXX_FBSTRING
2437 // Hash functions to make fbstring usable with e.g. hash_map
2439 // Handle interaction with different C++ standard libraries, which
2440 // expect these types to be in different namespaces.
2442 #define FOLLY_FBSTRING_HASH1(T) \
2444 struct hash< ::folly::basic_fbstring<T>> { \
2445 size_t operator()(const ::folly::basic_fbstring<T>& s) const { \
2446 return ::folly::hash::fnv32_buf(s.data(), s.size() * sizeof(T)); \
2450 // The C++11 standard says that these four are defined
2451 #define FOLLY_FBSTRING_HASH \
2452 FOLLY_FBSTRING_HASH1(char) \
2453 FOLLY_FBSTRING_HASH1(char16_t) \
2454 FOLLY_FBSTRING_HASH1(char32_t) \
2455 FOLLY_FBSTRING_HASH1(wchar_t)
2463 #if FOLLY_HAVE_DEPRECATED_ASSOC
2464 #if defined(_GLIBCXX_SYMVER) && !defined(__BIONIC__)
2465 namespace __gnu_cxx {
2469 } // namespace __gnu_cxx
2470 #endif // _GLIBCXX_SYMVER && !__BIONIC__
2471 #endif // FOLLY_HAVE_DEPRECATED_ASSOC
2473 #undef FOLLY_FBSTRING_HASH
2474 #undef FOLLY_FBSTRING_HASH1
2476 #endif // _LIBSTDCXX_FBSTRING
2478 #pragma GCC diagnostic pop
2480 #undef FBSTRING_DISABLE_SSO
2481 #undef FBSTRING_SANITIZE_ADDRESS
2483 #undef FBSTRING_LIKELY
2484 #undef FBSTRING_UNLIKELY
2486 #ifdef FOLLY_DEFINED_NDEBUG_FOR_FBSTRING
2488 #undef FOLLY_DEFINED_NDEBUG_FOR_FBSTRING
2489 #endif // FOLLY_DEFINED_NDEBUG_FOR_FBSTRING