/*
- * Copyright 2017 Facebook, Inc.
+ * Copyright 2011-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
#include <typeinfo>
#include <utility>
-#include <boost/implicit_cast.hpp>
#include <double-conversion/double-conversion.h> // V8 JavaScript implementation
#include <folly/Demangle.h>
throw makeConversionError(err, sp);
}
}
-}
+} // namespace detail
/**
* The identity conversion function.
return p;
}
-}
+} // namespace detail
#endif
/**
// 10^i, defined for i 0 through 19.
// This is 20 * 8 == 160 bytes, which fits neatly into 5 cache lines
// (assuming a cache line size of 64).
- static const uint64_t powersOf10[20] FOLLY_ALIGNED(64) = {
+ alignas(64) static const uint64_t powersOf10[20] = {
1,
10,
100,
};
// "count leading zeroes" operation not valid; for 0; special case this.
- if UNLIKELY (! v) {
+ if (UNLIKELY(!v)) {
return 1;
}
// return that log_10 lower bound, plus adjust if input >= 10^(that bound)
// in case there's a small error and we misjudged length.
- return minLength + (uint32_t) (UNLIKELY (v >= powersOf10[minLength]));
+ return minLength + uint32_t(v >= powersOf10[minLength]);
#else
uint32_t result = 1;
- for (;;) {
- if (LIKELY(v < 10)) return result;
- if (LIKELY(v < 100)) return result + 1;
- if (LIKELY(v < 1000)) return result + 2;
- if (LIKELY(v < 10000)) return result + 3;
+ while (true) {
+ if (LIKELY(v < 10)) {
+ return result;
+ }
+ if (LIKELY(v < 100)) {
+ return result + 1;
+ }
+ if (LIKELY(v < 1000)) {
+ return result + 2;
+ }
+ if (LIKELY(v < 10000)) {
+ return result + 3;
+ }
// Skip ahead by 4 orders of magnitude
v /= 10000U;
result += 4;
*result += value;
}
-template<class T>
+template <class T>
constexpr typename std::enable_if<
std::is_same<T, char>::value,
size_t>::type
return 1;
}
+template <size_t N>
+constexpr size_t estimateSpaceNeeded(const char (&)[N]) {
+ return N;
+}
+
/**
* Everything implicitly convertible to const char* gets appended.
*/
return 0;
}
-template<class Src>
+template <class Src>
+typename std::enable_if<IsSomeString<Src>::value, size_t>::type
+estimateSpaceNeeded(Src const& value) {
+ return value.size();
+}
+
+template <class Src>
typename std::enable_if<
- (std::is_convertible<Src, folly::StringPiece>::value ||
- IsSomeString<Src>::value) &&
- !std::is_convertible<Src, const char*>::value,
- size_t>::type
+ std::is_convertible<Src, folly::StringPiece>::value &&
+ !IsSomeString<Src>::value &&
+ !std::is_convertible<Src, const char*>::value,
+ size_t>::type
estimateSpaceNeeded(Src value) {
return folly::StringPiece(value).size();
}
return 0;
}
-template<class Src>
+template <class Src>
typename std::enable_if<
std::is_pointer<Src>::value &&
IsSomeString<std::remove_pointer<Src>>::value,
result->append(buffer + p, buffer + sizeof(buffer));
}
-template<class T>
+template <class T>
constexpr typename std::enable_if<
std::is_same<T, __int128>::value,
size_t>::type
return detail::digitsEnough<__int128>();
}
-template<class T>
+template <class T>
constexpr typename std::enable_if<
std::is_same<T, unsigned __int128>::value,
size_t>::type
if (value < 0) {
result->push_back('-');
result->append(
- buffer, uint64ToBufferUnsafe(uint64_t(-uint64_t(value)), buffer));
+ buffer,
+ uint64ToBufferUnsafe(~static_cast<uint64_t>(value) + 1, buffer));
} else {
result->append(buffer, uint64ToBufferUnsafe(uint64_t(value), buffer));
}
namespace detail {
constexpr int kConvMaxDecimalInShortestLow = -6;
constexpr int kConvMaxDecimalInShortestHigh = 21;
-} // folly::detail
+} // namespace detail
/** Wrapper around DoubleToStringConverter **/
template <class Tgt, class Src>
* for estimateSpaceNeed for your type, so that we allocate
* as much as you need instead of the default
*/
-template<class Src>
+template <class Src>
struct HasLengthEstimator : std::false_type {};
template <class Src>
return estimateSpaceToReserve(sofar + estimateSpaceNeeded(v), vs...);
}
-template<class...Ts>
+template <class... Ts>
void reserveInTarget(const Ts&...vs) {
getLastElement(vs...)->reserve(estimateSpaceToReserve(0, vs...));
}
-template<class Delimiter, class...Ts>
+template <class Delimiter, class... Ts>
void reserveInTargetDelim(const Delimiter& d, const Ts&...vs) {
static_assert(sizeof...(vs) >= 2, "Needs at least 2 args");
size_t fordelim = (sizeof...(vs) - 2) *
toAppend(delim, detail::getLastElement(vs...));
toAppendDelimStrImpl(delim, vs...);
}
-} // folly::detail
-
+} // namespace detail
/**
* Variadic conversion to string. Appends each element in turn.
* toDelim<SomeString>(SomeString str) returns itself.
*/
template <class Tgt, class Delim, class Src>
-typename std::enable_if<IsSomeString<Tgt>::value &&
- std::is_same<Tgt, Src>::value,
- Tgt>::type
-toDelim(const Delim& /* delim */, const Src& value) {
- return value;
+typename std::enable_if<
+ IsSomeString<Tgt>::value &&
+ std::is_same<Tgt, typename std::decay<Src>::type>::value,
+ Tgt>::type
+toDelim(const Delim& /* delim */, Src&& value) {
+ return std::forward<Src>(value);
}
/**
* Parsing strings to numeric types.
*/
template <typename Tgt>
-FOLLY_WARN_UNUSED_RESULT inline typename std::enable_if<
+FOLLY_NODISCARD inline typename std::enable_if<
std::is_arithmetic<Tgt>::value,
Expected<StringPiece, ConversionCode>>::type
parseTo(StringPiece src, Tgt& out) {
namespace detail {
/**
- * Bool to integral doesn't need any special checks, and this
+ * Bool to integral/float doesn't need any special checks, and this
* overload means we aren't trying to see if a bool is less than
* an integer.
*/
template <class Tgt>
typename std::enable_if<
- !std::is_same<Tgt, bool>::value && std::is_integral<Tgt>::value,
+ !std::is_same<Tgt, bool>::value &&
+ (std::is_integral<Tgt>::value || std::is_floating_point<Tgt>::value),
Expected<Tgt, ConversionCode>>::type
convertTo(const bool& value) noexcept {
return static_cast<Tgt>(value ? 1 : 0);
* }
******************************************************************************/
template <class T>
-FOLLY_WARN_UNUSED_RESULT typename std::enable_if<
+FOLLY_NODISCARD typename std::enable_if<
std::is_enum<T>::value,
Expected<StringPiece, ConversionCode>>::type
parseTo(StringPiece in, T& out) noexcept {
return restOrError;
}
-FOLLY_WARN_UNUSED_RESULT
+FOLLY_NODISCARD
inline Expected<StringPiece, ConversionCode> parseTo(
StringPiece in,
StringPiece& out) noexcept {
return StringPiece{in.end(), in.end()};
}
-FOLLY_WARN_UNUSED_RESULT
+FOLLY_NODISCARD
inline Expected<StringPiece, ConversionCode> parseTo(
StringPiece in,
std::string& out) {
return StringPiece{in.end(), in.end()};
}
-FOLLY_WARN_UNUSED_RESULT
+FOLLY_NODISCARD
inline Expected<StringPiece, ConversionCode> parseTo(
StringPiece in,
fbstring& out) {
struct CheckTrailingSpace {
Expected<Unit, ConversionCode> operator()(StringPiece sp) const {
auto e = enforceWhitespaceErr(sp);
- if (UNLIKELY(e != ConversionCode::SUCCESS))
+ if (UNLIKELY(e != ConversionCode::SUCCESS)) {
return makeUnexpected(e);
+ }
return unit;
}
};
});
}
+template <class Tgt, class Src>
+inline typename std::enable_if<
+ IsSomeString<Src>::value && !std::is_same<StringPiece, Tgt>::value,
+ Tgt>::type
+to(Src const& src) {
+ return to<Tgt>(StringPiece(src.data(), src.size()));
+}
+
template <class Tgt>
inline
typename std::enable_if<!std::is_same<StringPiece, Tgt>::value, Tgt>::type
template <class Tgt, class Src>
typename std::enable_if<
- std::is_enum<Src>::value && !std::is_same<Src, Tgt>::value,
+ std::is_enum<Src>::value && !std::is_same<Src, Tgt>::value &&
+ !std::is_convertible<Tgt, StringPiece>::value,
Expected<Tgt, ConversionCode>>::type
tryTo(const Src& value) {
using I = typename std::underlying_type<Src>::type;
template <class Tgt, class Src>
typename std::enable_if<
- std::is_enum<Tgt>::value && !std::is_same<Src, Tgt>::value,
- Tgt>::type
+ !std::is_convertible<Src, StringPiece>::valuea &&
+ std::is_enum<Tgt>::value && !std::is_same<Src, Tgt>::value,
+ Expected<Tgt, ConversionCode>>::type
tryTo(const Src& value) {
using I = typename std::underlying_type<Tgt>::type;
return tryTo<I>(value).then([](I i) { return static_cast<Tgt>(i); });
template <class Tgt, class Src>
typename std::enable_if<
- std::is_enum<Src>::value && !std::is_same<Src, Tgt>::value,
+ std::is_enum<Src>::value && !std::is_same<Src, Tgt>::value &&
+ !std::is_convertible<Tgt, StringPiece>::value,
Tgt>::type
to(const Src& value) {
return to<Tgt>(static_cast<typename std::underlying_type<Src>::type>(value));
template <class Tgt, class Src>
typename std::enable_if<
- std::is_enum<Tgt>::value && !std::is_same<Src, Tgt>::value, Tgt>::type
-to(const Src & value) {
+ !std::is_convertible<Src, StringPiece>::value && std::is_enum<Tgt>::value &&
+ !std::is_same<Src, Tgt>::value,
+ Tgt>::type
+to(const Src& value) {
return static_cast<Tgt>(to<typename std::underlying_type<Tgt>::type>(value));
}