// @author: Andrei Alexandrescu (aalexandre)
// String type.
-#ifndef FOLLY_BASE_FBSTRING_H_
-#define FOLLY_BASE_FBSTRING_H_
+#pragma once
#include <atomic>
#include <limits>
// that would break ABI-compatibility and wouldn't allow linking code
// compiled with this flag with code compiled without.
#ifdef FBSTRING_SANITIZE_ADDRESS
-# define FBSTRING_DISABLE_SMALL_STRING_OPTIMIZATION
+# define FBSTRING_DISABLE_SSO true
+#else
+# define FBSTRING_DISABLE_SSO false
#endif
namespace fbstring_detail {
goner.reset();
}
- fbstring_core(const Char *const data, const size_t size) {
+ fbstring_core(const Char *const data,
+ const size_t size,
+ bool disableSSO = FBSTRING_DISABLE_SSO) {
#ifndef NDEBUG
#ifndef _LIBSTDCXX_FBSTRING
SCOPE_EXIT {
assert(this->size() == size);
- assert(memcmp(this->data(), data, size * sizeof(Char)) == 0);
+ assert(size == 0 || memcmp(this->data(), data, size * sizeof(Char)) == 0);
};
#endif
#endif
-#ifndef FBSTRING_DISABLE_SMALL_STRING_OPTIMIZATION
// Simplest case first: small strings are bitblitted
- if (size <= maxSmallSize) {
+ if (!disableSSO && size <= maxSmallSize) {
// Layout is: Char* data_, size_t size_, size_t capacity_
static_assert(sizeof(*this) == sizeof(Char*) + 2 * sizeof(size_t),
"fbstring has unexpected size");
// If data is aligned, use fast word-wise copying. Otherwise,
// use conservative memcpy.
- if (reinterpret_cast<size_t>(data) & (sizeof(size_t) - 1)) {
- fbstring_detail::pod_copy(data, data + size, small_);
- } else {
- // Copy one word at a time.
- // NOTE: This reads bytes which are outside the range of the
- // string, and makes ASan unhappy, but the small case is
- // disabled under ASan.
-
+ // The word-wise path reads bytes which are outside the range of
+ // the string, and makes ASan unhappy, so we disable it when
+ // compiling with ASan.
+#ifndef FBSTRING_SANITIZE_ADDRESS
+ if ((reinterpret_cast<size_t>(data) & (sizeof(size_t) - 1)) == 0) {
const size_t byteSize = size * sizeof(Char);
constexpr size_t wordWidth = sizeof(size_t);
switch ((byteSize + wordWidth - 1) / wordWidth) { // Number of words.
case 0:
break;
}
+ } else
+#endif
+ {
+ if (size != 0) {
+ fbstring_detail::pod_copy(data, data + size, small_);
+ }
}
setSmallSize(size);
- } else
-#endif // FBSTRING_DISABLE_SMALL_STRING_OPTIMIZATION
- {
+ } else {
if (size <= maxMediumSize) {
// Medium strings are allocated normally. Don't forget to
// allocate one extra Char for the terminating null.
}
}
- void reserve(size_t minCapacity) {
+ void reserve(size_t minCapacity, bool disableSSO = FBSTRING_DISABLE_SSO) {
if (category() == Category::isLarge) {
// Ensure unique
if (RefCounted::refs(ml_.data_) > 1) {
}
} else {
assert(category() == Category::isSmall);
-#ifndef FBSTRING_DISABLE_SMALL_STRING_OPTIMIZATION
- if (minCapacity <= maxSmallSize) {
+ if (!disableSSO && minCapacity <= maxSmallSize) {
// small
// Nothing to do, everything stays put
- } else
-#endif // FBSTRING_DISABLE_SMALL_STRING_OPTIMIZATION
- if (minCapacity <= maxMediumSize) {
+ } else if (minCapacity <= maxMediumSize) {
// medium
// Don't forget to allocate one extra Char for the terminating null
auto const allocSizeBytes =
assert(capacity() >= minCapacity);
}
- Char * expand_noinit(const size_t delta, bool expGrowth = false) {
+ Char * expand_noinit(const size_t delta,
+ bool expGrowth = false,
+ bool disableSSO = FBSTRING_DISABLE_SSO) {
// Strategy is simple: make room, then change size
assert(capacity() >= size());
size_t sz, newSz;
if (category() == Category::isSmall) {
sz = smallSize();
newSz = sz + delta;
-#ifndef FBSTRING_DISABLE_SMALL_STRING_OPTIMIZATION
- if (FBSTRING_LIKELY(newSz <= maxSmallSize)) {
+ if (!disableSSO && FBSTRING_LIKELY(newSz <= maxSmallSize)) {
setSmallSize(newSz);
return small_ + sz;
}
-#endif // FBSTRING_DISABLE_SMALL_STRING_OPTIMIZATION
reserve(expGrowth ? std::max(newSz, 2 * maxSmallSize) : newSz);
} else {
sz = ml_.size_;
}
basic_fbstring& append(size_type n, value_type c) {
- resize(size() + n, c);
+ Invariant checker(*this);
+ auto pData = store_.expand_noinit(n, /* expGrowth = */ true);
+ fbstring_detail::pod_fill(pData, pData + n, c);
return *this;
}
Invariant checker(*this);
// s can alias this, we need to use pod_move.
- if (size() >= n) {
+ if (n == 0) {
+ resize(0);
+ } else if (size() >= n) {
fbstring_detail::pod_move(s, s + n, store_.mutable_data());
resize(n);
assert(size() == n);
private:
template <int i> class Selector {};
- iterator insertImplDiscr(const_iterator p,
+ iterator insertImplDiscr(const_iterator i,
size_type n, value_type c, Selector<1>) {
Invariant checker(*this);
- auto const pos = p - begin();
- assert(p >= begin() && p <= end());
- if (capacity() - size() < n) {
- const size_type sz = p - begin();
- reserve(size() + n);
- p = begin() + sz;
- }
- const iterator oldEnd = end();
- if (n < size_type(oldEnd - p)) {
- append(oldEnd - n, oldEnd);
- // Also copies terminator.
- fbstring_detail::pod_move(&*p, &*oldEnd - n + 1, begin() + pos + n);
- std::fill(begin() + pos, begin() + pos + n, c);
- } else {
- append(n - (end() - p), c);
- append(iterator(p), oldEnd);
- std::fill(iterator(p), oldEnd, c);
- }
- return begin() + pos;
+ assert(i >= begin() && i <= end());
+ const size_type pos = i - begin();
+
+ auto oldSize = size();
+ store_.expand_noinit(n, /* expGrowth = */ true);
+ auto b = begin();
+ fbstring_detail::pod_move(b + pos, b + oldSize, b + pos + n);
+ fbstring_detail::pod_fill(b + pos, b + pos + n, c);
+
+ return b + pos;
}
template<class InputIter>
std::forward_iterator_tag) {
Invariant checker(*this);
+ assert(i >= begin() && i <= end());
const size_type pos = i - begin();
auto n = std::distance(s1, s2);
assert(n >= 0);
- assert(pos <= size());
auto oldSize = size();
store_.expand_noinit(n, /* expGrowth = */ true);
enforce(pos <= size(), std::__throw_out_of_range, "");
procrustes(n, size() - pos);
- fbstring_detail::pod_copy(
- data() + pos,
- data() + pos + n,
- s);
+ if (n != 0) {
+ fbstring_detail::pod_copy(data() + pos, data() + pos + n, s);
+ }
return n;
}
// Handle interaction with different C++ standard libraries, which
// expect these types to be in different namespaces.
-#define FOLLY_FBSTRING_HASH1(T) \
- template <> \
- struct hash< ::folly::basic_fbstring<T> > { \
- size_t operator()(const ::folly::fbstring& s) const { \
- return ::folly::hash::fnv32_buf(s.data(), s.size()); \
- } \
+#define FOLLY_FBSTRING_HASH1(T) \
+ template <> \
+ struct hash<::folly::basic_fbstring<T>> { \
+ size_t operator()(const ::folly::basic_fbstring<T>& s) const { \
+ return ::folly::hash::fnv32_buf(s.data(), s.size() * sizeof(T)); \
+ } \
};
// The C++11 standard says that these four are defined
#pragma GCC diagnostic pop
-#undef FBSTRING_DISABLE_SMALL_STRING_OPTIMIZATION
+#undef FBSTRING_DISABLE_SSO
#undef FBSTRING_SANITIZE_ADDRESS
#undef throw
#undef FBSTRING_LIKELY
#undef NDEBUG
#undef FOLLY_DEFINED_NDEBUG_FOR_FBSTRING
#endif // FOLLY_DEFINED_NDEBUG_FOR_FBSTRING
-
-#endif // FOLLY_BASE_FBSTRING_H_