bool quote;
};
-// Keep this in sync with ConversionCode in Conv.h
-constexpr const std::array<
- ErrorString,
- static_cast<std::size_t>(ConversionCode::NUM_ERROR_CODES)>
+// Keep this in sync with ConversionError::Code in Conv.h
+constexpr const std::array<ErrorString, ConversionError::NUM_ERROR_CODES>
kErrorStrings{{
{"Success", true},
{"Empty input string", true},
} // anonymous namespace
-Expected<bool, ConversionCode> str_to_bool(StringPiece* src) noexcept {
+ConversionError makeConversionError(
+ ConversionError::Code code,
+ const char* input,
+ size_t inputLen) {
+ assert(code >= 0 && code < kErrorStrings.size());
+ const ErrorString& err = kErrorStrings[code];
+ if (code == ConversionError::EMPTY_INPUT_STRING && inputLen == 0) {
+ return ConversionError(err.string, code);
+ }
+ std::string tmp(err.string);
+ tmp.append(": ");
+ if (err.quote) {
+ tmp.append(1, '"');
+ }
+ if (input && inputLen > 0) {
+ tmp.append(input, inputLen);
+ }
+ if (err.quote) {
+ tmp.append(1, '"');
+ }
+ return ConversionError(tmp, code);
+}
+
+ConversionResult<bool> str_to_bool(StringPiece* src) {
auto b = src->begin(), e = src->end();
for (;; ++b) {
if (b >= e) {
- return makeUnexpected(ConversionCode::EMPTY_INPUT_STRING);
+ return ConversionResult<bool>(ConversionError::EMPTY_INPUT_STRING);
}
if (!std::isspace(*b)) {
break;
result = false;
for (; b < e && isdigit(*b); ++b) {
if (result || (*b != '0' && *b != '1')) {
- return makeUnexpected(ConversionCode::BOOL_OVERFLOW);
+ return ConversionResult<bool>(ConversionError::BOOL_OVERFLOW);
}
result = (*b == '1');
}
} else if (bool_str_cmp(&b, len, "off")) {
result = false;
} else {
- return makeUnexpected(ConversionCode::BOOL_INVALID_VALUE);
+ return ConversionResult<bool>(ConversionError::BOOL_INVALID_VALUE);
}
break;
default:
- return makeUnexpected(ConversionCode::BOOL_INVALID_VALUE);
+ return ConversionResult<bool>(ConversionError::BOOL_INVALID_VALUE);
}
src->assign(b, e);
- return result;
+ return ConversionResult<bool>(result);
}
/**
* StringPiece parameter to munch the already-parsed characters.
*/
template <class Tgt>
-Expected<Tgt, ConversionCode> str_to_floating(StringPiece* src) noexcept {
+ConversionResult<Tgt> str_to_floating(StringPiece* src) {
using namespace double_conversion;
static StringToDoubleConverter
conv(StringToDoubleConverter::ALLOW_TRAILING_JUNK
nullptr, nullptr);
if (src->empty()) {
- return makeUnexpected(ConversionCode::EMPTY_INPUT_STRING);
+ return ConversionResult<Tgt>(ConversionError::EMPTY_INPUT_STRING);
}
int length;
// that was processed, so we need to check if that character was
// whitespace or not.
if (length == 0 || (result == 0.0 && std::isspace((*src)[length - 1]))) {
- return makeUnexpected(ConversionCode::EMPTY_INPUT_STRING);
+ return ConversionResult<Tgt>(ConversionError::EMPTY_INPUT_STRING);
}
src->advance(length);
- return result;
+ return ConversionResult<Tgt>(result);
}
auto* e = src->end();
if (result == 0.0) {
// All bets are off
- return makeUnexpected(ConversionCode::STRING_TO_FLOAT_ERROR);
+ return ConversionResult<Tgt>(ConversionError::STRING_TO_FLOAT_ERROR);
}
if (negative) {
src->assign(b, e);
- return result;
+ return ConversionResult<Tgt>(result);
}
-template Expected<float, ConversionCode> str_to_floating<float>(
- StringPiece* src) noexcept;
-template Expected<double, ConversionCode> str_to_floating<double>(
- StringPiece* src) noexcept;
+template ConversionResult<float> str_to_floating<float>(StringPiece* src);
+template ConversionResult<double> str_to_floating<double>(StringPiece* src);
/**
* This class takes care of additional processing needed for signed values,
template <typename T>
class SignedValueHandler<T, true> {
public:
- ConversionCode init(const char*& b) {
+ ConversionError::Code init(const char*& b) {
negative_ = false;
if (!std::isdigit(*b)) {
if (*b == '-') {
negative_ = true;
} else if (UNLIKELY(*b != '+')) {
- return ConversionCode::INVALID_LEADING_CHAR;
+ return ConversionError::INVALID_LEADING_CHAR;
}
++b;
}
- return ConversionCode::SUCCESS;
+ return ConversionError::SUCCESS;
}
- ConversionCode overflow() {
- return negative_ ? ConversionCode::NEGATIVE_OVERFLOW
- : ConversionCode::POSITIVE_OVERFLOW;
+ ConversionError::Code overflow() {
+ return negative_ ? ConversionError::NEGATIVE_OVERFLOW
+ : ConversionError::POSITIVE_OVERFLOW;
}
template <typename U>
- Expected<T, ConversionCode> finalize(U value) {
+ ConversionResult<T> finalize(U value) {
T rv;
if (negative_) {
rv = -value;
if (UNLIKELY(rv > 0)) {
- return makeUnexpected(ConversionCode::NEGATIVE_OVERFLOW);
+ return ConversionResult<T>(ConversionError::NEGATIVE_OVERFLOW);
}
} else {
rv = value;
if (UNLIKELY(rv < 0)) {
- return makeUnexpected(ConversionCode::POSITIVE_OVERFLOW);
+ return ConversionResult<T>(ConversionError::POSITIVE_OVERFLOW);
}
}
- return rv;
+ return ConversionResult<T>(rv);
}
private:
template <typename T>
class SignedValueHandler<T, false> {
public:
- ConversionCode init(const char*&) {
- return ConversionCode::SUCCESS;
+ ConversionError::Code init(const char*&) {
+ return ConversionError::SUCCESS;
}
- ConversionCode overflow() {
- return ConversionCode::POSITIVE_OVERFLOW;
+ ConversionError::Code overflow() {
+ return ConversionError::POSITIVE_OVERFLOW;
}
- Expected<T, ConversionCode> finalize(T value) {
- return value;
+ ConversionResult<T> finalize(T value) {
+ return ConversionResult<T>(value);
}
};
* an appropriate error.
*/
template <class Tgt>
-inline Expected<Tgt, ConversionCode> digits_to(
- const char* b,
- const char* const e) noexcept {
+inline ConversionResult<Tgt> digits_to(const char* b, const char* const e) {
using UT = typename std::make_unsigned<Tgt>::type;
assert(b <= e);
SignedValueHandler<Tgt> sgn;
auto err = sgn.init(b);
- if (UNLIKELY(err != ConversionCode::SUCCESS)) {
- return makeUnexpected(err);
+ if (UNLIKELY(err != ConversionError::SUCCESS)) {
+ return ConversionResult<Tgt>(err);
}
size_t size = e - b;
if (b < e && *b == '0') {
for (++b;; ++b) {
if (b == e) {
- return Tgt(0); // just zeros, e.g. "0000"
+ return ConversionResult<Tgt>(Tgt(0)); // just zeros, e.g. "0000"
}
if (*b != '0') {
size = e - b;
if (size > std::numeric_limits<UT>::digits10 &&
(size != std::numeric_limits<UT>::digits10 + 1 ||
strncmp(b, MaxString<UT>::value, size) > 0)) {
- return makeUnexpected(sgn.overflow());
+ return ConversionResult<Tgt>(sgn.overflow());
}
}
default:
assert(b == e);
if (size == 0) {
- return makeUnexpected(ConversionCode::NO_DIGITS);
+ return ConversionResult<Tgt>(ConversionError::NO_DIGITS);
}
break;
}
return sgn.finalize(result);
outOfRange:
- return makeUnexpected(ConversionCode::NON_DIGIT_CHAR);
+ return ConversionResult<Tgt>(ConversionError::NON_DIGIT_CHAR);
}
-template Expected<char, ConversionCode> digits_to<char>(
- const char*,
- const char*) noexcept;
-template Expected<signed char, ConversionCode> digits_to<signed char>(
+template ConversionResult<char> digits_to<char>(const char*, const char*);
+template ConversionResult<signed char> digits_to<signed char>(
const char*,
- const char*) noexcept;
-template Expected<unsigned char, ConversionCode> digits_to<unsigned char>(
+ const char*);
+template ConversionResult<unsigned char> digits_to<unsigned char>(
const char*,
- const char*) noexcept;
+ const char*);
-template Expected<short, ConversionCode> digits_to<short>(
- const char*,
- const char*) noexcept;
-template Expected<unsigned short, ConversionCode> digits_to<unsigned short>(
+template ConversionResult<short> digits_to<short>(const char*, const char*);
+template ConversionResult<unsigned short> digits_to<unsigned short>(
const char*,
- const char*) noexcept;
+ const char*);
-template Expected<int, ConversionCode> digits_to<int>(
+template ConversionResult<int> digits_to<int>(const char*, const char*);
+template ConversionResult<unsigned int> digits_to<unsigned int>(
const char*,
- const char*) noexcept;
-template Expected<unsigned int, ConversionCode> digits_to<unsigned int>(
- const char*,
- const char*) noexcept;
+ const char*);
-template Expected<long, ConversionCode> digits_to<long>(
+template ConversionResult<long> digits_to<long>(const char*, const char*);
+template ConversionResult<unsigned long> digits_to<unsigned long>(
const char*,
const char*);
-template Expected<unsigned long, ConversionCode> digits_to<unsigned long>(
- const char*,
- const char*) noexcept;
-template Expected<long long, ConversionCode> digits_to<long long>(
+template ConversionResult<long long> digits_to<long long>(
+ const char*,
+ const char*);
+template ConversionResult<unsigned long long> digits_to<unsigned long long>(
const char*,
- const char*) noexcept;
-template Expected<unsigned long long, ConversionCode>
-digits_to<unsigned long long>(const char*, const char*) noexcept;
+ const char*);
#if FOLLY_HAVE_INT128_T
-template Expected<__int128, ConversionCode> digits_to<__int128>(
+template ConversionResult<__int128> digits_to<__int128>(
+ const char*,
+ const char*);
+template ConversionResult<unsigned __int128> digits_to<unsigned __int128>(
const char*,
- const char*) noexcept;
-template Expected<unsigned __int128, ConversionCode>
-digits_to<unsigned __int128>(const char*, const char*) noexcept;
+ const char*);
#endif
/**
* StringPiece parameter to munch the already-parsed characters.
*/
template <class Tgt>
-Expected<Tgt, ConversionCode> str_to_integral(StringPiece* src) noexcept {
+ConversionResult<Tgt> str_to_integral(StringPiece* src) {
using UT = typename std::make_unsigned<Tgt>::type;
auto b = src->data(), past = src->data() + src->size();
for (;; ++b) {
if (UNLIKELY(b >= past)) {
- return makeUnexpected(ConversionCode::EMPTY_INPUT_STRING);
+ return ConversionResult<Tgt>(ConversionError::EMPTY_INPUT_STRING);
}
if (!std::isspace(*b)) {
break;
SignedValueHandler<Tgt> sgn;
auto err = sgn.init(b);
- if (UNLIKELY(err != ConversionCode::SUCCESS)) {
- return makeUnexpected(err);
+ if (UNLIKELY(err != ConversionError::SUCCESS)) {
+ return ConversionResult<Tgt>(err);
}
if (std::is_signed<Tgt>::value && UNLIKELY(b >= past)) {
- return makeUnexpected(ConversionCode::NO_DIGITS);
+ return ConversionResult<Tgt>(ConversionError::NO_DIGITS);
}
if (UNLIKELY(!isdigit(*b))) {
- return makeUnexpected(ConversionCode::NON_DIGIT_CHAR);
+ return ConversionResult<Tgt>(ConversionError::NON_DIGIT_CHAR);
}
auto m = findFirstNonDigit(b + 1, past);
auto tmp = digits_to<UT>(b, m);
- if (UNLIKELY(!tmp.hasValue())) {
- return makeUnexpected(
- tmp.error() == ConversionCode::POSITIVE_OVERFLOW ? sgn.overflow()
- : tmp.error());
+ if (UNLIKELY(!tmp.success())) {
+ return ConversionResult<Tgt>(
+ tmp.error == ConversionError::POSITIVE_OVERFLOW ? sgn.overflow()
+ : tmp.error);
}
- auto res = sgn.finalize(tmp.value());
+ auto res = sgn.finalize(tmp.value);
- if (res.hasValue()) {
+ if (res.success()) {
src->advance(m - src->data());
}
return res;
}
-template Expected<char, ConversionCode> str_to_integral<char>(
- StringPiece* src) noexcept;
-template Expected<signed char, ConversionCode> str_to_integral<signed char>(
- StringPiece* src) noexcept;
-template Expected<unsigned char, ConversionCode> str_to_integral<unsigned char>(
- StringPiece* src) noexcept;
-
-template Expected<short, ConversionCode> str_to_integral<short>(
- StringPiece* src) noexcept;
-template Expected<unsigned short, ConversionCode>
-str_to_integral<unsigned short>(StringPiece* src) noexcept;
-
-template Expected<int, ConversionCode> str_to_integral<int>(
- StringPiece* src) noexcept;
-template Expected<unsigned int, ConversionCode> str_to_integral<unsigned int>(
- StringPiece* src) noexcept;
-
-template Expected<long, ConversionCode> str_to_integral<long>(
- StringPiece* src) noexcept;
-template Expected<unsigned long, ConversionCode> str_to_integral<unsigned long>(
- StringPiece* src) noexcept;
-
-template Expected<long long, ConversionCode> str_to_integral<long long>(
- StringPiece* src) noexcept;
-template Expected<unsigned long long, ConversionCode>
-str_to_integral<unsigned long long>(StringPiece* src) noexcept;
+template ConversionResult<char> str_to_integral<char>(StringPiece* src);
+template ConversionResult<signed char> str_to_integral<signed char>(
+ StringPiece* src);
+template ConversionResult<unsigned char> str_to_integral<unsigned char>(
+ StringPiece* src);
+
+template ConversionResult<short> str_to_integral<short>(StringPiece* src);
+template ConversionResult<unsigned short> str_to_integral<unsigned short>(
+ StringPiece* src);
+
+template ConversionResult<int> str_to_integral<int>(StringPiece* src);
+template ConversionResult<unsigned int> str_to_integral<unsigned int>(
+ StringPiece* src);
+
+template ConversionResult<long> str_to_integral<long>(StringPiece* src);
+template ConversionResult<unsigned long> str_to_integral<unsigned long>(
+ StringPiece* src);
+
+template ConversionResult<long long> str_to_integral<long long>(
+ StringPiece* src);
+template ConversionResult<unsigned long long>
+str_to_integral<unsigned long long>(StringPiece* src);
#if FOLLY_HAVE_INT128_T
-template Expected<__int128, ConversionCode> str_to_integral<__int128>(
- StringPiece* src) noexcept;
-template Expected<unsigned __int128, ConversionCode>
-str_to_integral<unsigned __int128>(StringPiece* src) noexcept;
+template ConversionResult<__int128> str_to_integral<__int128>(StringPiece* src);
+template ConversionResult<unsigned __int128> str_to_integral<unsigned __int128>(
+ StringPiece* src);
#endif
} // namespace detail
-
-ConversionError makeConversionError(ConversionCode code, StringPiece input) {
- using namespace detail;
- static_assert(
- std::is_unsigned<std::underlying_type<ConversionCode>::type>::value,
- "ConversionCode should be unsigned");
- assert((std::size_t)code < kErrorStrings.size());
- const ErrorString& err = kErrorStrings[(std::size_t)code];
- if (code == ConversionCode::EMPTY_INPUT_STRING && input.empty()) {
- return {err.string, code};
- }
- std::string tmp(err.string);
- tmp.append(": ");
- if (err.quote) {
- tmp.append(1, '"');
- }
- if (input.size() > 0) {
- tmp.append(input.data(), input.size());
- }
- if (err.quote) {
- tmp.append(1, '"');
- }
- return {tmp, code};
-}
-
} // namespace folly
#include <double-conversion/double-conversion.h> // V8 JavaScript implementation
#include <folly/Demangle.h>
-#include <folly/Expected.h>
#include <folly/FBString.h>
#include <folly/Likely.h>
#include <folly/Range.h>
-#include <folly/Unit.h>
#include <folly/portability/Math.h>
namespace folly {
-// Keep this in sync with kErrorStrings in Conv.cpp
-enum class ConversionCode : unsigned char {
- SUCCESS,
- EMPTY_INPUT_STRING,
- NO_DIGITS,
- BOOL_OVERFLOW,
- BOOL_INVALID_VALUE,
- NON_DIGIT_CHAR,
- INVALID_LEADING_CHAR,
- POSITIVE_OVERFLOW,
- NEGATIVE_OVERFLOW,
- STRING_TO_FLOAT_ERROR,
- NON_WHITESPACE_AFTER_END,
- ARITH_POSITIVE_OVERFLOW,
- ARITH_NEGATIVE_OVERFLOW,
- ARITH_LOSS_OF_PRECISION,
- NUM_ERROR_CODES, // has to be the last entry
-};
-
-struct ConversionErrorBase : std::range_error {
- using std::range_error::range_error;
-};
-
-class ConversionError : public ConversionErrorBase {
+class ConversionError : public std::range_error {
public:
- ConversionError(const std::string& str, ConversionCode code)
- : ConversionErrorBase(str), code_(code) {}
+ // Keep this in sync with kErrorStrings in Conv.cpp
+ enum Code {
+ SUCCESS,
+ EMPTY_INPUT_STRING,
+ NO_DIGITS,
+ BOOL_OVERFLOW,
+ BOOL_INVALID_VALUE,
+ NON_DIGIT_CHAR,
+ INVALID_LEADING_CHAR,
+ POSITIVE_OVERFLOW,
+ NEGATIVE_OVERFLOW,
+ STRING_TO_FLOAT_ERROR,
+ NON_WHITESPACE_AFTER_END,
+ ARITH_POSITIVE_OVERFLOW,
+ ARITH_NEGATIVE_OVERFLOW,
+ ARITH_LOSS_OF_PRECISION,
+ NUM_ERROR_CODES, // has to be the last entry
+ };
- ConversionError(const char* str, ConversionCode code)
- : ConversionErrorBase(str), code_(code) {}
+ ConversionError(const std::string& str, Code code)
+ : std::range_error(str), code_(code) {}
- ConversionCode errorCode() const {
- return code_;
- }
+ ConversionError(const char* str, Code code)
+ : std::range_error(str), code_(code) {}
+
+ Code errorCode() const { return code_; }
private:
- ConversionCode code_;
+ Code code_;
};
-/*******************************************************************************
- * Custom Error Translation
- *
- * Your overloaded parseTo() function can return a custom error code on failure.
- * ::folly::to() will call makeConversionError to translate that error code into
- * an object to throw. makeConversionError is found by argument-dependent
- * lookup. It should have this signature:
- *
- * namespace other_namespace {
- * enum YourErrorCode { BAD_ERROR, WORSE_ERROR };
- *
- * struct YourConversionError : ConversionErrorBase {
- * YourConversionError(const char* what) : ConversionErrorBase(what) {}
- * };
- *
- * YourConversionError
- * makeConversionError(YourErrorCode code, ::folly::StringPiece sp) {
- * ...
- * return YourConversionError(messageString);
- * }
- ******************************************************************************/
-ConversionError makeConversionError(ConversionCode code, StringPiece sp);
-
namespace detail {
+
+ConversionError makeConversionError(
+ ConversionError::Code code,
+ const char* input,
+ size_t inputLen);
+
+inline ConversionError makeConversionError(
+ ConversionError::Code code,
+ const std::string& str) {
+ return makeConversionError(code, str.data(), str.size());
+}
+
+inline ConversionError makeConversionError(
+ ConversionError::Code code,
+ StringPiece sp) {
+ return makeConversionError(code, sp.data(), sp.size());
+}
+
/**
* Enforce that the suffix following a number is made up only of whitespace.
*/
-inline ConversionCode enforceWhitespaceErr(StringPiece sp) {
+inline ConversionError::Code enforceWhitespaceErr(StringPiece sp) {
for (auto c : sp) {
- if (UNLIKELY(!std::isspace(c))) {
- return ConversionCode::NON_WHITESPACE_AFTER_END;
+ if (!std::isspace(c)) {
+ return ConversionError::NON_WHITESPACE_AFTER_END;
}
}
- return ConversionCode::SUCCESS;
+ return ConversionError::SUCCESS;
}
/**
*/
inline void enforceWhitespace(StringPiece sp) {
auto err = enforceWhitespaceErr(sp);
- if (err != ConversionCode::SUCCESS) {
- throw makeConversionError(err, sp);
+ if (err != ConversionError::SUCCESS) {
+ throw detail::makeConversionError(err, sp);
}
}
-}
/**
- * The identity conversion function.
- * tryTo<T>(T) returns itself for all types T.
+ * A simple std::pair-like wrapper to wrap both a value and an error
*/
-template <class Tgt, class Src>
-typename std::enable_if<
- std::is_same<Tgt, typename std::decay<Src>::type>::value,
- Expected<Tgt, ConversionCode>>::type
-tryTo(Src&& value) {
- return std::forward<Src>(value);
-}
+template <typename T>
+struct ConversionResult {
+ explicit ConversionResult(T v) : value(v) {}
+ explicit ConversionResult(ConversionError::Code e) : error(e) {}
-template <class Tgt, class Src>
-typename std::enable_if<
- std::is_same<Tgt, typename std::decay<Src>::type>::value,
- Tgt>::type
-to(Src&& value) {
- return std::forward<Src>(value);
-}
+ bool success() const {
+ return error == ConversionError::SUCCESS;
+ }
-/*******************************************************************************
- * Arithmetic to boolean
- ******************************************************************************/
+ T value;
+ ConversionError::Code error{ConversionError::SUCCESS};
+};
+}
/**
- * Unchecked conversion from arithmetic to boolean. This is different from the
- * other arithmetic conversions because we use the C convention of treating any
- * non-zero value as true, instead of range checking.
+ * The identity conversion function.
+ * to<T>(T) returns itself for all types T.
*/
template <class Tgt, class Src>
-typename std::enable_if<
- std::is_arithmetic<Src>::value && !std::is_same<Tgt, Src>::value &&
- std::is_same<Tgt, bool>::value,
- Expected<Tgt, ConversionCode>>::type
-tryTo(const Src& value) {
- return value != Src();
+typename std::enable_if<std::is_same<Tgt, Src>::value, Tgt>::type
+to(const Src & value) {
+ return value;
}
template <class Tgt, class Src>
-typename std::enable_if<
- std::is_arithmetic<Src>::value && !std::is_same<Tgt, Src>::value &&
- std::is_same<Tgt, bool>::value,
- Tgt>::type
-to(const Src& value) {
- return value != Src();
+typename std::enable_if<std::is_same<Tgt, Src>::value, Tgt>::type
+to(Src && value) {
+ return std::forward<Src>(value);
}
/*******************************************************************************
namespace detail {
-template <typename... Ts>
-struct LastElementImpl {
- static void call(Ignored<Ts>...) {}
-};
-
-template <typename Head, typename... Ts>
-struct LastElementImpl<Head, Ts...> {
- template <typename Last>
- static Last call(Ignored<Ts>..., Last&& last) {
- return std::forward<Last>(last);
- }
-};
+template <class T>
+const T& getLastElement(const T & v) {
+ return v;
+}
-template <typename... Ts>
-auto getLastElement(const Ts&... ts)
- -> decltype(LastElementImpl<Ts...>::call(ts...)) {
- return LastElementImpl<Ts...>::call(ts...);
+template <class T, class... Ts>
+typename std::tuple_element<
+ sizeof...(Ts),
+ std::tuple<T, Ts...> >::type const&
+ getLastElement(const T&, const Ts&... vs) {
+ return getLastElement(vs...);
}
+// This class exists to specialize away std::tuple_element in the case where we
+// have 0 template arguments. Without this, Clang/libc++ will blow a
+// static_assert even if tuple_element is protected by an enable_if.
template <class... Ts>
-struct LastElement : std::decay<decltype(
- LastElementImpl<Ts...>::call(std::declval<Ts>()...))> {
+struct last_element {
+ typedef typename std::enable_if<
+ sizeof...(Ts) >= 1,
+ typename std::tuple_element<
+ sizeof...(Ts) - 1, std::tuple<Ts...>
+ >::type>::type type;
+};
+
+template <>
+struct last_element<> {
+ typedef void type;
};
} // namespace detail
}
}
-template <class Src>
-typename std::enable_if<std::is_convertible<Src, const char*>::value, size_t>::
- type
- estimateSpaceNeeded(Src value) {
+template<class Src>
+typename std::enable_if<
+ std::is_convertible<Src, const char*>::value,
+ size_t>::type
+estimateSpaceNeeded(Src value) {
const char *c = value;
if (c) {
return folly::StringPiece(value).size();
}
template <class T, class... Ts>
-typename std::enable_if<
- sizeof...(Ts) >= 2 &&
- IsSomeString<typename std::remove_pointer<
- typename detail::LastElement<const Ts&...>::type>::type>::value>::type
+typename std::enable_if<sizeof...(Ts) >= 2
+ && IsSomeString<
+ typename std::remove_pointer<
+ typename detail::last_element<Ts...>::type
+ >::type>::value>::type
toAppendStrImpl(const T& v, const Ts&... vs) {
toAppend(v, getLastElement(vs...));
toAppendStrImpl(vs...);
}
template <class Delimiter, class T, class... Ts>
-typename std::enable_if<
- sizeof...(Ts) >= 2 &&
- IsSomeString<typename std::remove_pointer<
- typename detail::LastElement<const Ts&...>::type>::type>::value>::type
+typename std::enable_if<sizeof...(Ts) >= 2
+ && IsSomeString<
+ typename std::remove_pointer<
+ typename detail::last_element<Ts...>::type
+ >::type>::value>::type
toAppendDelimStrImpl(const Delimiter& delim, const T& v, const Ts&... vs) {
// we are really careful here, calling toAppend with just one element does
// not try to estimate space needed (as we already did that). If we call
* }
*/
template <class... Ts>
-typename std::enable_if<
- sizeof...(Ts) >= 3 &&
- IsSomeString<typename std::remove_pointer<
- typename detail::LastElement<const Ts&...>::type>::type>::value>::type
+typename std::enable_if<sizeof...(Ts) >= 3
+ && IsSomeString<
+ typename std::remove_pointer<
+ typename detail::last_element<Ts...>::type
+ >::type>::value>::type
toAppend(const Ts&... vs) {
::folly::detail::toAppendStrImpl(vs...);
}
* will probably save a few calls to malloc.
*/
template <class... Ts>
-typename std::enable_if<IsSomeString<typename std::remove_pointer<
- typename detail::LastElement<const Ts&...>::type>::type>::value>::type
+typename std::enable_if<
+ IsSomeString<
+ typename std::remove_pointer<
+ typename detail::last_element<Ts...>::type
+ >::type>::value>::type
toAppendFit(const Ts&... vs) {
::folly::detail::reserveInTarget(vs...);
toAppend(vs...);
* comments for toAppend for details about memory allocation.
*/
template <class Delimiter, class... Ts>
-typename std::enable_if<
- sizeof...(Ts) >= 3 &&
- IsSomeString<typename std::remove_pointer<
- typename detail::LastElement<const Ts&...>::type>::type>::value>::type
+typename std::enable_if<sizeof...(Ts) >= 3
+ && IsSomeString<
+ typename std::remove_pointer<
+ typename detail::last_element<Ts...>::type
+ >::type>::value>::type
toAppendDelim(const Delimiter& delim, const Ts&... vs) {
detail::toAppendDelimStrImpl(delim, vs...);
}
* Detail in comment for toAppendFit
*/
template <class Delimiter, class... Ts>
-typename std::enable_if<IsSomeString<typename std::remove_pointer<
- typename detail::LastElement<const Ts&...>::type>::type>::value>::type
+typename std::enable_if<
+ IsSomeString<
+ typename std::remove_pointer<
+ typename detail::last_element<Ts...>::type
+ >::type>::value>::type
toAppendDelimFit(const Delimiter& delim, const Ts&... vs) {
detail::reserveInTargetDelim(delim, vs...);
toAppendDelim(delim, vs...);
*/
template <class Tgt, class... Ts>
typename std::enable_if<
- IsSomeString<Tgt>::value &&
- (sizeof...(Ts) != 1 ||
- !std::is_same<Tgt, typename detail::LastElement<const Ts&...>::type>::
- value),
- Tgt>::type
+ IsSomeString<Tgt>::value && (
+ sizeof...(Ts) != 1 ||
+ !std::is_same<Tgt, typename detail::last_element<Ts...>::type>::value),
+ Tgt>::type
to(const Ts&... vs) {
Tgt result;
toAppendFit(vs..., &result);
*/
template <class Tgt, class Delim, class... Ts>
typename std::enable_if<
- IsSomeString<Tgt>::value &&
- (sizeof...(Ts) != 1 ||
- !std::is_same<Tgt, typename detail::LastElement<const Ts&...>::type>::
- value),
- Tgt>::type
+ IsSomeString<Tgt>::value && (
+ sizeof...(Ts) != 1 ||
+ !std::is_same<Tgt, typename detail::last_element<Ts...>::type>::value),
+ Tgt>::type
toDelim(const Delim& delim, const Ts&... vs) {
Tgt result;
toAppendDelimFit(delim, vs..., &result);
namespace detail {
-Expected<bool, ConversionCode> str_to_bool(StringPiece* src) noexcept;
+ConversionResult<bool> str_to_bool(StringPiece* src);
template <typename T>
-Expected<T, ConversionCode> str_to_floating(StringPiece* src) noexcept;
+ConversionResult<T> str_to_floating(StringPiece* src);
-extern template Expected<float, ConversionCode> str_to_floating<float>(
- StringPiece* src) noexcept;
-extern template Expected<double, ConversionCode> str_to_floating<double>(
- StringPiece* src) noexcept;
+extern template ConversionResult<float> str_to_floating<float>(
+ StringPiece* src);
+extern template ConversionResult<double> str_to_floating<double>(
+ StringPiece* src);
template <class Tgt>
-Expected<Tgt, ConversionCode> digits_to(const char* b, const char* e) noexcept;
+ConversionResult<Tgt> digits_to(const char* b, const char* e);
-extern template Expected<char, ConversionCode> digits_to<char>(
+extern template ConversionResult<char> digits_to<char>(
const char*,
- const char*) noexcept;
-extern template Expected<signed char, ConversionCode> digits_to<signed char>(
+ const char*);
+extern template ConversionResult<signed char> digits_to<signed char>(
const char*,
- const char*) noexcept;
-extern template Expected<unsigned char, ConversionCode>
-digits_to<unsigned char>(const char*, const char*) noexcept;
-
-extern template Expected<short, ConversionCode> digits_to<short>(
+ const char*);
+extern template ConversionResult<unsigned char> digits_to<unsigned char>(
const char*,
- const char*) noexcept;
-extern template Expected<unsigned short, ConversionCode>
-digits_to<unsigned short>(const char*, const char*) noexcept;
+ const char*);
-extern template Expected<int, ConversionCode> digits_to<int>(
+extern template ConversionResult<short> digits_to<short>(
const char*,
- const char*) noexcept;
-extern template Expected<unsigned int, ConversionCode> digits_to<unsigned int>(
+ const char*);
+extern template ConversionResult<unsigned short> digits_to<unsigned short>(
const char*,
- const char*) noexcept;
+ const char*);
-extern template Expected<long, ConversionCode> digits_to<long>(
+extern template ConversionResult<int> digits_to<int>(const char*, const char*);
+extern template ConversionResult<unsigned int> digits_to<unsigned int>(
const char*,
- const char*) noexcept;
-extern template Expected<unsigned long, ConversionCode>
-digits_to<unsigned long>(const char*, const char*) noexcept;
+ const char*);
-extern template Expected<long long, ConversionCode> digits_to<long long>(
+extern template ConversionResult<long> digits_to<long>(
const char*,
- const char*) noexcept;
-extern template Expected<unsigned long long, ConversionCode>
-digits_to<unsigned long long>(const char*, const char*) noexcept;
+ const char*);
+extern template ConversionResult<unsigned long> digits_to<unsigned long>(
+ const char*,
+ const char*);
+
+extern template ConversionResult<long long> digits_to<long long>(
+ const char*,
+ const char*);
+extern template ConversionResult<unsigned long long>
+digits_to<unsigned long long>(const char*, const char*);
#if FOLLY_HAVE_INT128_T
-extern template Expected<__int128, ConversionCode> digits_to<__int128>(
+extern template ConversionResult<__int128> digits_to<__int128>(
const char*,
- const char*) noexcept;
-extern template Expected<unsigned __int128, ConversionCode>
-digits_to<unsigned __int128>(const char*, const char*) noexcept;
+ const char*);
+extern template ConversionResult<unsigned __int128>
+digits_to<unsigned __int128>(const char*, const char*);
#endif
template <class T>
-Expected<T, ConversionCode> str_to_integral(StringPiece* src) noexcept;
-
-extern template Expected<char, ConversionCode> str_to_integral<char>(
- StringPiece* src) noexcept;
-extern template Expected<signed char, ConversionCode>
-str_to_integral<signed char>(StringPiece* src) noexcept;
-extern template Expected<unsigned char, ConversionCode>
-str_to_integral<unsigned char>(StringPiece* src) noexcept;
-
-extern template Expected<short, ConversionCode> str_to_integral<short>(
- StringPiece* src) noexcept;
-extern template Expected<unsigned short, ConversionCode>
-str_to_integral<unsigned short>(StringPiece* src) noexcept;
-
-extern template Expected<int, ConversionCode> str_to_integral<int>(
- StringPiece* src) noexcept;
-extern template Expected<unsigned int, ConversionCode>
-str_to_integral<unsigned int>(StringPiece* src) noexcept;
-
-extern template Expected<long, ConversionCode> str_to_integral<long>(
- StringPiece* src) noexcept;
-extern template Expected<unsigned long, ConversionCode>
-str_to_integral<unsigned long>(StringPiece* src) noexcept;
-
-extern template Expected<long long, ConversionCode> str_to_integral<long long>(
- StringPiece* src) noexcept;
-extern template Expected<unsigned long long, ConversionCode>
-str_to_integral<unsigned long long>(StringPiece* src) noexcept;
+ConversionResult<T> str_to_integral(StringPiece* src);
+
+extern template ConversionResult<char> str_to_integral<char>(StringPiece* src);
+extern template ConversionResult<signed char> str_to_integral<signed char>(
+ StringPiece* src);
+extern template ConversionResult<unsigned char> str_to_integral<unsigned char>(
+ StringPiece* src);
+
+extern template ConversionResult<short> str_to_integral<short>(
+ StringPiece* src);
+extern template ConversionResult<unsigned short>
+str_to_integral<unsigned short>(StringPiece* src);
+
+extern template ConversionResult<int> str_to_integral<int>(StringPiece* src);
+extern template ConversionResult<unsigned int> str_to_integral<unsigned int>(
+ StringPiece* src);
+
+extern template ConversionResult<long> str_to_integral<long>(StringPiece* src);
+extern template ConversionResult<unsigned long> str_to_integral<unsigned long>(
+ StringPiece* src);
+
+extern template ConversionResult<long long> str_to_integral<long long>(
+ StringPiece* src);
+extern template ConversionResult<unsigned long long>
+str_to_integral<unsigned long long>(StringPiece* src);
#if FOLLY_HAVE_INT128_T
-extern template Expected<__int128, ConversionCode> str_to_integral<__int128>(
- StringPiece* src) noexcept;
-extern template Expected<unsigned __int128, ConversionCode>
-str_to_integral<unsigned __int128>(StringPiece* src) noexcept;
+extern template ConversionResult<__int128> str_to_integral<__int128>(
+ StringPiece* src);
+extern template ConversionResult<unsigned __int128>
+str_to_integral<unsigned __int128>(StringPiece* src);
#endif
template <typename T>
-typename std::
- enable_if<std::is_same<T, bool>::value, Expected<T, ConversionCode>>::type
- convertTo(StringPiece* src) noexcept {
+typename std::enable_if<std::is_same<T, bool>::value, ConversionResult<T>>::type
+convertTo(StringPiece* src) {
return str_to_bool(src);
}
template <typename T>
typename std::enable_if<
- std::is_floating_point<T>::value,
- Expected<T, ConversionCode>>::type
-convertTo(StringPiece* src) noexcept {
+ std::is_floating_point<T>::value, ConversionResult<T>>::type
+convertTo(StringPiece* src) {
return str_to_floating<T>(src);
}
template <typename T>
typename std::enable_if<
std::is_integral<T>::value && !std::is_same<T, bool>::value,
- Expected<T, ConversionCode>>::type
-convertTo(StringPiece* src) noexcept {
+ ConversionResult<T>>::type
+convertTo(StringPiece* src) {
return str_to_integral<T>(src);
}
+template <typename T>
+struct WrapperInfo { using type = T; };
+
+template <typename T, typename Gen>
+typename std::enable_if<
+ std::is_same<typename WrapperInfo<T>::type, T>::value, T>::type
+inline wrap(ConversionResult<typename WrapperInfo<T>::type> res, Gen&& gen) {
+ if (LIKELY(res.success())) {
+ return res.value;
+ }
+ throw detail::makeConversionError(res.error, gen());
+}
+
} // namespace detail
/**
*/
template <typename Tgt>
typename std::enable_if<
- std::is_integral<Tgt>::value && !std::is_same<Tgt, bool>::value,
- Expected<Tgt, ConversionCode>>::type
-tryTo(const char* b, const char* e) {
- return detail::digits_to<Tgt>(b, e);
-}
-
-template <typename Tgt>
-typename std::enable_if<
- std::is_integral<Tgt>::value && !std::is_same<Tgt, bool>::value,
+ std::is_integral<typename detail::WrapperInfo<Tgt>::type>::value &&
+ !std::is_same<typename detail::WrapperInfo<Tgt>::type, bool>::value,
Tgt>::type
to(const char* b, const char* e) {
- return tryTo<Tgt>(b, e).thenOrThrow(
- [](Tgt res) { return res; },
- [=](ConversionCode code) {
- return makeConversionError(code, StringPiece(b, e));
- });
+ auto res = detail::digits_to<typename detail::WrapperInfo<Tgt>::type>(b, e);
+ return detail::wrap<Tgt>(res, [&] { return StringPiece(b, e); });
}
/*******************************************************************************
******************************************************************************/
/**
- * Parsing strings to numeric types.
+ * Parsing strings to numeric types. These routines differ from
+ * parseTo(str, numeric) routines in that they take a POINTER TO a StringPiece
+ * and alter that StringPiece to reflect progress information.
*/
template <typename Tgt>
-FOLLY_WARN_UNUSED_RESULT inline typename std::enable_if<
- std::is_arithmetic<Tgt>::value,
- Expected<StringPiece, ConversionCode>>::type
+typename std::enable_if<
+ std::is_arithmetic<typename detail::WrapperInfo<Tgt>::type>::value>::type
+parseTo(StringPiece* src, Tgt& out) {
+ auto res = detail::convertTo<typename detail::WrapperInfo<Tgt>::type>(src);
+ out = detail::wrap<Tgt>(res, [&] { return *src; });
+}
+
+template <typename Tgt>
+typename std::enable_if<
+ std::is_arithmetic<typename detail::WrapperInfo<Tgt>::type>::value>::type
parseTo(StringPiece src, Tgt& out) {
- return detail::convertTo<Tgt>(&src).then(
- [&](Tgt res) { return void(out = res), src; });
+ auto res = detail::convertTo<typename detail::WrapperInfo<Tgt>::type>(&src);
+ if (LIKELY(res.success())) {
+ res.error = detail::enforceWhitespaceErr(src);
+ }
+ out = detail::wrap<Tgt>(res, [&] { return src; });
}
/*******************************************************************************
* Integral / Floating Point to integral / Floating Point
******************************************************************************/
+/**
+ * Unchecked conversion from arithmetic to boolean. This is different from the
+ * other arithmetic conversions because we use the C convention of treating any
+ * non-zero value as true, instead of range checking.
+ */
+template <class Tgt, class Src>
+typename std::enable_if<
+ std::is_arithmetic<Src>::value && !std::is_same<Tgt, Src>::value &&
+ std::is_same<Tgt, bool>::value,
+ Tgt>::type
+to(const Src& value) {
+ return value != Src();
+}
+
namespace detail {
/**
std::is_integral<Src>::value && !std::is_same<Tgt, Src>::value &&
!std::is_same<Tgt, bool>::value &&
std::is_integral<Tgt>::value,
- Expected<Tgt, ConversionCode>>::type
-convertTo(const Src& value) noexcept {
+ ConversionResult<Tgt>>::type
+convertTo(const Src& value) {
/* static */ if (
std::numeric_limits<Tgt>::max() < std::numeric_limits<Src>::max()) {
if (greater_than<Tgt, std::numeric_limits<Tgt>::max()>(value)) {
- return makeUnexpected(ConversionCode::ARITH_POSITIVE_OVERFLOW);
+ return ConversionResult<Tgt>(ConversionError::ARITH_POSITIVE_OVERFLOW);
}
}
/* static */ if (
std::is_signed<Src>::value &&
(!std::is_signed<Tgt>::value || sizeof(Src) > sizeof(Tgt))) {
if (less_than<Tgt, std::numeric_limits<Tgt>::min()>(value)) {
- return makeUnexpected(ConversionCode::ARITH_NEGATIVE_OVERFLOW);
+ return ConversionResult<Tgt>(ConversionError::ARITH_NEGATIVE_OVERFLOW);
}
}
- return static_cast<Tgt>(value);
+ return ConversionResult<Tgt>(static_cast<Tgt>(value));
}
/**
typename std::enable_if<
std::is_floating_point<Tgt>::value && std::is_floating_point<Src>::value &&
!std::is_same<Tgt, Src>::value,
- Expected<Tgt, ConversionCode>>::type
-convertTo(const Src& value) noexcept {
+ ConversionResult<Tgt>>::type
+convertTo(const Src& value) {
/* static */ if (
std::numeric_limits<Tgt>::max() < std::numeric_limits<Src>::max()) {
if (value > std::numeric_limits<Tgt>::max()) {
- return makeUnexpected(ConversionCode::ARITH_POSITIVE_OVERFLOW);
+ return ConversionResult<Tgt>(ConversionError::ARITH_POSITIVE_OVERFLOW);
}
if (value < std::numeric_limits<Tgt>::lowest()) {
- return makeUnexpected(ConversionCode::ARITH_NEGATIVE_OVERFLOW);
+ return ConversionResult<Tgt>(ConversionError::ARITH_NEGATIVE_OVERFLOW);
}
}
- return boost::implicit_cast<Tgt>(value);
+ return ConversionResult<Tgt>(boost::implicit_cast<Tgt>(value));
}
/**
typename std::enable_if<
(std::is_integral<Src>::value && std::is_floating_point<Tgt>::value) ||
(std::is_floating_point<Src>::value && std::is_integral<Tgt>::value),
- Expected<Tgt, ConversionCode>>::type
-convertTo(const Src& value) noexcept {
+ ConversionResult<Tgt>>::type
+convertTo(const Src& value) {
if (LIKELY(checkConversion<Tgt>(value))) {
Tgt result = static_cast<Tgt>(value);
if (LIKELY(checkConversion<Src>(result))) {
Src witness = static_cast<Src>(result);
if (LIKELY(value == witness)) {
- return result;
+ return ConversionResult<Tgt>(result);
}
}
}
- return makeUnexpected(ConversionCode::ARITH_LOSS_OF_PRECISION);
+ return ConversionResult<Tgt>(ConversionError::ARITH_LOSS_OF_PRECISION);
}
template <typename Tgt, typename Src>
template <typename Tgt, typename Src>
typename std::enable_if<
- detail::IsArithToArith<Tgt, Src>::value,
- Expected<Tgt, ConversionCode>>::type
-tryTo(const Src& value) noexcept {
- return detail::convertTo<Tgt>(value);
-}
-
-template <typename Tgt, typename Src>
-typename std::enable_if<detail::IsArithToArith<Tgt, Src>::value, Tgt>::type to(
- const Src& value) {
- return tryTo<Tgt>(value).thenOrThrow(
- [](Tgt res) { return res; },
- [&](ConversionCode e) {
- return makeConversionError(e, detail::errorValue<Tgt>(value));
- });
+ detail::IsArithToArith<
+ typename detail::WrapperInfo<Tgt>::type, Src>::value, Tgt>::type
+to(const Src& value) {
+ auto res = detail::convertTo<typename detail::WrapperInfo<Tgt>::type>(value);
+ return detail::wrap<Tgt>(res, [&] {
+ return detail::errorValue<typename detail::WrapperInfo<Tgt>::type>(value);
+ });
}
/*******************************************************************************
* argument-dependent lookup:
*
* namespace other_namespace {
- * ::folly::Expected<::folly::StringPiece, SomeErrorCode>
- * parseTo(::folly::StringPiece, OtherType&) noexcept;
+ * void parseTo(::folly::StringPiece, OtherType&);
* }
******************************************************************************/
template <class T>
-FOLLY_WARN_UNUSED_RESULT typename std::enable_if<
- std::is_enum<T>::value,
- Expected<StringPiece, ConversionCode>>::type
-parseTo(StringPiece in, T& out) noexcept {
- typename std::underlying_type<T>::type tmp{};
- auto restOrError = parseTo(in, tmp);
- out = static_cast<T>(tmp); // Harmless if parseTo fails
- return restOrError;
-}
-
-FOLLY_WARN_UNUSED_RESULT
-inline Expected<StringPiece, ConversionCode> parseTo(
- StringPiece in,
- StringPiece& out) noexcept {
- out = in;
- return StringPiece{in.end(), in.end()};
+typename std::enable_if<std::is_enum<T>::value>::type
+parseTo(StringPiece in, T& out) {
+ typename std::underlying_type<T>::type tmp;
+ parseTo(in, tmp);
+ out = static_cast<T>(tmp);
}
-FOLLY_WARN_UNUSED_RESULT
-inline Expected<StringPiece, ConversionCode> parseTo(
- StringPiece in,
- std::string& out) {
- out.clear();
- out.append(in.data(), in.size()); // TODO try/catch?
- return StringPiece{in.end(), in.end()};
+inline void parseTo(StringPiece in, StringPiece& out) {
+ out = in;
}
-FOLLY_WARN_UNUSED_RESULT
-inline Expected<StringPiece, ConversionCode> parseTo(
- StringPiece in,
- fbstring& out) {
+inline void parseTo(StringPiece in, std::string& out) {
out.clear();
- out.append(in.data(), in.size()); // TODO try/catch?
- return StringPiece{in.end(), in.end()};
+ out.append(in.data(), in.size());
}
-namespace detail {
-template <typename Tgt>
-using ParseToResult = decltype(parseTo(StringPiece{}, std::declval<Tgt&>()));
-
-struct CheckTrailingSpace {
- Expected<Unit, ConversionCode> operator()(StringPiece sp) const {
- auto e = enforceWhitespaceErr(sp);
- if (UNLIKELY(e != ConversionCode::SUCCESS))
- return makeUnexpected(e);
- return unit;
- }
-};
-
-template <class Error>
-struct ReturnUnit {
- template <class T>
- constexpr Expected<Unit, Error> operator()(T&&) const {
- return unit;
- }
-};
-
-// Older versions of the parseTo customization point threw on error and
-// returned void. Handle that.
-template <class Tgt>
-inline typename std::enable_if<
- std::is_void<ParseToResult<Tgt>>::value,
- Expected<StringPiece, ConversionCode>>::type
-parseToWrap(StringPiece sp, Tgt& out) {
- parseTo(sp, out);
- return StringPiece(sp.end(), sp.end());
-}
-
-template <class Tgt>
-inline typename std::enable_if<
- !std::is_void<ParseToResult<Tgt>>::value,
- ParseToResult<Tgt>>::type
-parseToWrap(StringPiece sp, Tgt& out) {
- return parseTo(sp, out);
+inline void parseTo(StringPiece in, fbstring& out) {
+ out.clear();
+ out.append(in.data(), in.size());
}
-template <typename Tgt>
-using ParseToError = ExpectedErrorType<decltype(
- detail::parseToWrap(StringPiece{}, std::declval<Tgt&>()))>;
-
-} // namespace detail
-
/**
* String or StringPiece to target conversion. Accepts leading and trailing
* whitespace, but no non-space trailing characters.
*/
template <class Tgt>
-inline typename std::enable_if<
- !std::is_same<StringPiece, Tgt>::value,
- Expected<Tgt, detail::ParseToError<Tgt>>>::type
-tryTo(StringPiece src) {
- Tgt result{};
- using Error = detail::ParseToError<Tgt>;
- using Check = typename std::conditional<
- std::is_arithmetic<Tgt>::value,
- detail::CheckTrailingSpace,
- detail::ReturnUnit<Error>>::type;
- return parseTo(src, result).then(Check(), [&](Unit) {
- return std::move(result);
- });
-}
-
-template <class Tgt>
-inline
- typename std::enable_if<!std::is_same<StringPiece, Tgt>::value, Tgt>::type
- to(StringPiece src) {
- Tgt result{};
- using Error = detail::ParseToError<Tgt>;
- using Check = typename std::conditional<
- std::is_arithmetic<Tgt>::value,
- detail::CheckTrailingSpace,
- detail::ReturnUnit<Error>>::type;
- auto tmp = detail::parseToWrap(src, result);
- return tmp
- .thenOrThrow(Check(), [&](Error e) { throw makeConversionError(e, src); })
- .thenOrThrow(
- [&](Unit) { return std::move(result); },
- [&](Error e) { throw makeConversionError(e, tmp.value()); });
-}
-
-/**
- * tryTo/to that take the strings by pointer so the caller gets information
- * about how much of the string was consumed by the conversion. These do not
- * check for trailing whitepsace.
- */
-template <class Tgt>
-Expected<Tgt, detail::ParseToError<Tgt>> tryTo(StringPiece* src) {
+typename std::enable_if<!std::is_same<StringPiece, Tgt>::value, Tgt>::type
+to(StringPiece src) {
Tgt result;
- return parseTo(*src, result).then([&, src](StringPiece sp) -> Tgt {
- *src = sp;
- return std::move(result);
- });
+ parseTo(src, result);
+ return result;
}
template <class Tgt>
Tgt to(StringPiece* src) {
Tgt result;
- using Error = detail::ParseToError<Tgt>;
- return parseTo(*src, result)
- .thenOrThrow(
- [&, src](StringPiece sp) -> Tgt {
- *src = sp;
- return std::move(result);
- },
- [=](Error e) { return makeConversionError(e, *src); });
+ parseTo(src, result);
+ return result;
}
/*******************************************************************************
template <class Tgt, class Src>
typename std::enable_if<
- std::is_enum<Src>::value && !std::is_same<Src, Tgt>::value,
- Expected<Tgt, ConversionCode>>::type
-tryTo(const Src& value) {
- using I = typename std::underlying_type<Src>::type;
- return tryTo<Tgt>(static_cast<I>(value));
-}
-
-template <class Tgt, class Src>
-typename std::enable_if<
- std::is_enum<Tgt>::value && !std::is_same<Src, Tgt>::value,
- Tgt>::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,
- Tgt>::type
-to(const Src& value) {
+ std::is_enum<Src>::value && !std::is_same<Src, Tgt>::value, Tgt>::type
+to(const Src & value) {
return to<Tgt>(static_cast<typename std::underlying_type<Src>::type>(value));
}