#pragma once
-#include <folly/FBString.h>
#include <folly/Portability.h>
#include <folly/hash/SpookyHashV2.h>
#include <folly/portability/BitsFunctexcept.h>
#include <glog/logging.h>
#include <algorithm>
#include <array>
+#include <cassert>
#include <climits>
#include <cstddef>
#include <cstring>
#include <string>
#include <type_traits>
-// libc++ doesn't provide this header, nor does msvc
-#ifdef FOLLY_HAVE_BITS_CXXCONFIG_H
-// This file appears in two locations: inside fbcode and in the
-// libstdc++ source code (when embedding fbstring as std::string).
-// To aid in this schizophrenic use, two macros are defined in
-// c++config.h:
-// _LIBSTDCXX_FBSTRING - Set inside libstdc++. This is useful to
-// gate use inside fbcode v. libstdc++
-#include <bits/c++config.h>
-#endif
-
#include <folly/CpuId.h>
#include <folly/Likely.h>
#include <folly/Traits.h>
namespace folly {
+/**
+ * Ubiquitous helper template for knowing what's a string.
+ */
+template <class T>
+struct IsSomeString : std::false_type {};
+
+template <>
+struct IsSomeString<std::string> : std::true_type {};
+
template <class Iter>
class Range;
Range(const Range& other, size_type first, size_type length = npos)
: Range(other.subpiece(first, length)) {}
- template <class T = Iter, typename detail::IsCharPointer<T>::const_type = 0>
- /* implicit */ Range(const fbstring& str)
- : b_(str.data()), e_(b_ + str.size()) {}
+ template <
+ class Container,
+ class = typename std::enable_if<
+ std::is_same<Iter, typename Container::const_pointer>::value>::type,
+ class = decltype(
+ Iter(std::declval<Container const&>().data()),
+ Iter(
+ std::declval<Container const&>().data() +
+ std::declval<Container const&>().size()))>
+ /* implicit */ constexpr Range(Container const& container)
+ : b_(container.data()), e_(b_ + container.size()) {}
- template <class T = Iter, typename detail::IsCharPointer<T>::const_type = 0>
- Range(const fbstring& str, fbstring::size_type startFrom) {
- if (UNLIKELY(startFrom > str.size())) {
+ template <
+ class Container,
+ class = typename std::enable_if<
+ std::is_same<Iter, typename Container::const_pointer>::value>::type,
+ class = decltype(
+ Iter(std::declval<Container const&>().data()),
+ Iter(
+ std::declval<Container const&>().data() +
+ std::declval<Container const&>().size()))>
+ Range(Container const& container, typename Container::size_type startFrom) {
+ auto const cdata = container.data();
+ auto const csize = container.size();
+ if (UNLIKELY(startFrom > csize)) {
std::__throw_out_of_range("index out of range");
}
- b_ = str.data() + startFrom;
- e_ = str.data() + str.size();
+ b_ = cdata + startFrom;
+ e_ = cdata + csize;
}
- template <class T = Iter, typename detail::IsCharPointer<T>::const_type = 0>
+ template <
+ class Container,
+ class = typename std::enable_if<
+ std::is_same<Iter, typename Container::const_pointer>::value>::type,
+ class = decltype(
+ Iter(std::declval<Container const&>().data()),
+ Iter(
+ std::declval<Container const&>().data() +
+ std::declval<Container const&>().size()))>
Range(
- const fbstring& str,
- fbstring::size_type startFrom,
- fbstring::size_type size) {
- if (UNLIKELY(startFrom > str.size())) {
+ Container const& container,
+ typename Container::size_type startFrom,
+ typename Container::size_type size) {
+ auto const cdata = container.data();
+ auto const csize = container.size();
+ if (UNLIKELY(startFrom > csize)) {
std::__throw_out_of_range("index out of range");
}
- b_ = str.data() + startFrom;
- if (str.size() - startFrom < size) {
- e_ = str.data() + str.size();
+ b_ = cdata + startFrom;
+ if (csize - startFrom < size) {
+ e_ = cdata + csize;
} else {
e_ = b_ + size;
}
assert(b_ < e_);
return detail::value_before(e_);
}
- // Works only for Range<const char*> and Range<char*>
- std::string str() const {
- return std::string(b_, size());
- }
- std::string toString() const {
- return str();
+
+ template <typename Tgt>
+ auto to() const
+ -> decltype(Tgt(std::declval<Iter const&>(), std::declval<size_type>())) {
+ return Tgt(b_, size());
}
// Works only for Range<const char*> and Range<char*>
- fbstring fbstr() const {
- return fbstring(b_, size());
+ template <typename Tgt = std::string>
+ auto str() const
+ -> decltype(Tgt(std::declval<Iter const&>(), std::declval<size_type>())) {
+ return to<Tgt>();
}
- fbstring toFbstring() const {
- return fbstr();
+ template <typename Tgt = std::string>
+ auto toString() const
+ -> decltype(Tgt(std::declval<Iter const&>(), std::declval<size_type>())) {
+ return to<Tgt>();
}
const_range_type castToConst() const {
}
};
-/**
- * Ubiquitous helper template for knowing what's a string
- */
-template <class T>
-struct IsSomeString {
- enum {
- value =
- std::is_same<T, std::string>::value || std::is_same<T, fbstring>::value
- };
-};
-
/**
* _sp is a user-defined literal suffix to make an appropriate Range
* specialization from a literal string.
#if !FOLLY_HAVE_PTHREAD || _WIN32
return false;
#else
- auto trimmedName = name.fbstr().substr(0, kMaxThreadNameLength - 1);
+ auto const piece = name.subpiece(0, kMaxThreadNameLength - 1);
+ auto const data = piece.data();
+ auto const size = piece.size();
+ char trimmedName[kMaxThreadNameLength];
+ std::memcpy(trimmedName, data, size);
+ std::memset(trimmedName + size, 0, kMaxThreadNameLength - size);
auto id = stdTidToPthreadId(tid);
#if FOLLY_HAS_PTHREAD_SETNAME_NP_THREAD_NAME
- return 0 == pthread_setname_np(id, trimmedName.c_str());
+ return 0 == pthread_setname_np(id, const_cast<char const*>(trimmedName));
#elif FOLLY_HAS_PTHREAD_SETNAME_NP_NAME
// Since OS X 10.6 it is possible for a thread to set its own name,
// but not that of some other thread.
if (pthread_equal(pthread_self(), id)) {
- return 0 == pthread_setname_np(trimmedName.c_str());
+ return 0 == pthread_setname_np(const_cast<char const*>(trimmedName));
}
return false;
#else