- // Keep the string at medium size. Don't forget to allocate
- // one extra Char for the terminating null.
- size_t capacityBytes = goodMallocSize((1 + minCapacity) * sizeof(Char));
- // Also copies terminator.
- ml_.data_ = static_cast<Char*>(smartRealloc(
- ml_.data_,
- (ml_.size_ + 1) * sizeof(Char),
- (ml_.capacity() + 1) * sizeof(Char),
- capacityBytes));
- ml_.setCapacity(capacityBytes / sizeof(Char) - 1, Category::isMedium);
+ if (minCapacity <= maxMediumSize) {
+ // Keep the string at medium size. Don't forget to allocate
+ // one extra Char for the terminating null.
+ size_t capacityBytes = goodMallocSize((1 + minCapacity) * sizeof(Char));
+ // Also copies terminator.
+ ml_.data_ = static_cast<Char*>(smartRealloc(
+ ml_.data_,
+ (ml_.size_ + 1) * sizeof(Char),
+ (ml_.capacity() + 1) * sizeof(Char),
+ capacityBytes));
+ ml_.setCapacity(capacityBytes / sizeof(Char) - 1, Category::isMedium);
+ } else {
+ // Conversion from medium to large string
+ fbstring_core nascent;
+ // Will recurse to another branch of this function
+ nascent.reserve(minCapacity);
+ nascent.ml_.size_ = ml_.size_;
+ // Also copies terminator.
+ fbstring_detail::podCopy(
+ ml_.data_, ml_.data_ + ml_.size_ + 1, nascent.ml_.data_);
+ nascent.swap(*this);
+ FBSTRING_ASSERT(capacity() >= minCapacity);
+ }