//------------------------------------------------------------------------------ /* This file is part of Beast: https://github.com/vinniefalco/Beast Copyright 2013, Vinnie Falco Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ //============================================================================== #ifndef BEAST_LEXICALCAST_H_INCLUDED #define BEAST_LEXICALCAST_H_INCLUDED #include #include // #include #include #include #include #include #include namespace beast { namespace detail { #ifdef _MSC_VER #pragma warning(push) #pragma warning(disable: 4800) #pragma warning(disable: 4804) #endif template bool parse_integral (Int& num, FwdIt first, FwdIt last, Accumulator accumulator) { num = 0; if (first == last) return false; while (first != last) { auto const c = *first++; if (c < '0' || c > '9') return false; if (!accumulator(num, Int(c - '0'))) return false; } return true; } template bool parse_negative_integral (Int& num, FwdIt first, FwdIt last) { Int limit_value = std::numeric_limits ::min() / 10; Int limit_digit = std::numeric_limits ::min() % 10; if (limit_digit < 0) limit_digit = -limit_digit; return parse_integral (num, first, last, [limit_value, limit_digit](Int& value, Int digit) { assert ((digit >= 0) && (digit <= 9)); if (value < limit_value || (value == limit_value && digit > limit_digit)) return false; value = (value * 10) - digit; return true; }); } template bool parse_positive_integral (Int& num, FwdIt first, FwdIt last) { Int limit_value = std::numeric_limits ::max() / 10; Int limit_digit = std::numeric_limits ::max() % 10; return parse_integral (num, first, last, [limit_value, limit_digit](Int& value, Int digit) { assert ((digit >= 0) && (digit <= 9)); if (value > limit_value || (value == limit_value && digit > limit_digit)) return false; value = (value * 10) + digit; return true; }); } template bool parseSigned (IntType& result, FwdIt first, FwdIt last) { static_assert(std::is_signed::value, "You may only call parseSigned with a signed integral type."); if (first != last && *first == '-') return parse_negative_integral (result, first + 1, last); if (first != last && *first == '+') return parse_positive_integral (result, first + 1, last); return parse_positive_integral (result, first, last); } template bool parseUnsigned (UIntType& result, FwdIt first, FwdIt last) { static_assert(std::is_unsigned::value, "You may only call parseUnsigned with an unsigned integral type."); if (first != last && *first == '+') return parse_positive_integral (result, first + 1, last); return parse_positive_integral (result, first, last); } //------------------------------------------------------------------------------ // These specializatons get called by the non-member functions to do the work template struct LexicalCast; // conversion to std::string template struct LexicalCast { template std::enable_if_t ::value, bool> operator () (std::string& out, Arithmetic in) { out = std::to_string (in); return true; } template std::enable_if_t ::value, bool> operator () (std::string& out, Enumeration in) { out = std::to_string ( static_cast > (in)); return true; } }; // Parse std::string to number template struct LexicalCast { static_assert (std::is_integral ::value, "beast::LexicalCast can only be used with integral types"); template std::enable_if_t ::value, bool> operator () (Integral& out, std::string const& in) const { return parseUnsigned (out, std::begin(in), std::end(in)); } template std::enable_if_t ::value, bool> operator () (Integral& out, std::string const& in) const { return parseSigned (out, std::begin(in), std::end(in)); } }; //------------------------------------------------------------------------------ // Conversion from null terminated char const* template struct LexicalCast { bool operator() (Out& out, char const* in) const { return LexicalCast ()(out, in); } }; // Conversion from null terminated char* // The string is not modified. template struct LexicalCast { bool operator() (Out& out, char* in) const { return LexicalCast ()(out, in); } }; #ifdef _MSC_VER #pragma warning(pop) #endif } // detail //------------------------------------------------------------------------------ /** Thrown when a conversion is not possible with LexicalCast. Only used in the throw variants of lexicalCast. */ struct BadLexicalCast : public std::bad_cast { }; /** Intelligently convert from one type to another. @return `false` if there was a parsing or range error */ template bool lexicalCastChecked (Out& out, In in) { return detail::LexicalCast () (out, in); } /** Convert from one type to another, throw on error An exception of type BadLexicalCast is thrown if the conversion fails. @return The new type. */ template Out lexicalCastThrow (In in) { Out out; if (lexicalCastChecked (out, in)) return out; throw BadLexicalCast (); } /** Convert from one type to another. @param defaultValue The value returned if parsing fails @return The new type. */ template Out lexicalCast (In in, Out defaultValue = Out ()) { Out out; if (lexicalCastChecked (out, in)) return out; return defaultValue; } } // beast #endif