diff --git a/include/helpers/lexical_cast.hpp b/include/helpers/lexical_cast.hpp index 4a1a1f5..18332c9 100644 --- a/include/helpers/lexical_cast.hpp +++ b/include/helpers/lexical_cast.hpp @@ -80,9 +80,10 @@ namespace dinhelp { ArrayRetType retval; F div = 1; + constexpr const std::size_t charset_offs = (Tag::lower_case ? Tag::base : 0); const auto sign_length = (is_negative::check(parFrom) and Tag::sign_allowed ? 1 : 0); for (std::size_t z = 0; z < Tag::count_digits(parFrom) - sign_length; ++z) { - retval.push_back(static_cast((Tag::make_unsigned(parFrom) / div) % Tag::base)); + retval.push_back(static_cast(((Tag::make_unsigned(parFrom) / div) % Tag::base) + charset_offs)); div *= Tag::base; } std::reverse(retval.begin(), retval.end()); @@ -99,6 +100,21 @@ namespace dinhelp { } return retval * dinhelp::customize::char_to_int::sgn(parFrom); }; + + template + struct hex { + enum { + base = 16, + sign_allowed = 0, + lower_case = (LowerCase ? 1 : 0) + }; + + static std::size_t count_digits ( T parValue ) a_pure; + static typename std::make_unsigned::type make_unsigned ( T parValue ) a_pure; + static constexpr std::size_t count_digits_bt (std::size_t parNum) { + return (parNum == 0 ? 0 : static_cast(std::log10(static_cast(parNum)) / std::log10(static_cast(base)))) + 1; + } + }; } //namespace implem namespace tags { @@ -106,7 +122,8 @@ namespace dinhelp { struct dec { enum { base = 10, - sign_allowed = 1 + sign_allowed = 1, + lower_case = 0 }; template @@ -121,24 +138,16 @@ namespace dinhelp { }; template - struct hex { - enum { - base = 16, - sign_allowed = 0 - }; - - static std::size_t count_digits ( T parValue ) a_pure; - static typename std::make_unsigned::type make_unsigned ( T parValue ) a_pure; - static constexpr std::size_t count_digits_bt (std::size_t parNum) { - return (parNum == 0 ? 0 : static_cast(std::log10(static_cast(parNum)) / std::log10(static_cast(base)))) + 1; - } - }; + using hex = dinhelp::implem::hex; + template + using hexl = dinhelp::implem::hex; template struct bin { enum { base = 2, - sign_allowed = 0 + sign_allowed = 0, + lower_case = 0 }; static std::size_t count_digits ( T parValue ) a_pure; @@ -172,16 +181,6 @@ namespace dinhelp { return dinhelp::implem::abs(parValue); } - template - std::size_t hex::count_digits (T parValue) { - return std::max(((sizeof(parValue) * CHAR_BIT - dinhelp::implem::count_leading_zeroes::type>(make_unsigned(parValue))) + (CHAR_BIT / 2 - 1)) / (CHAR_BIT / 2), 1); - } - - template - typename std::make_unsigned::type hex::make_unsigned (T parValue) { - return static_cast::type>(parValue); - } - template std::size_t bin::count_digits (T parValue) { return std::max((sizeof(parValue) * CHAR_BIT - dinhelp::implem::count_leading_zeroes::type>(make_unsigned(parValue))), 1); @@ -240,6 +239,16 @@ namespace dinhelp { //We need to cast before negating x to avoid the overflow. return (parValue < 0 ? -static_cast::type>(parValue) : parValue); } + + template + std::size_t hex::count_digits (T parValue) { + return std::max(((sizeof(parValue) * CHAR_BIT - dinhelp::implem::count_leading_zeroes::type>(make_unsigned(parValue))) + (CHAR_BIT / 2 - 1)) / (CHAR_BIT / 2), 1); + } + + template + typename std::make_unsigned::type hex::make_unsigned (T parValue) { + return static_cast::type>(parValue); + } } //namespace implem template class Tag=tags::dec, typename F=void> @@ -254,7 +263,11 @@ namespace dinhelp { static std::string make (const MaxSizedArray &parIndices, int parNegative) { static const char symbols[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', - 'C', 'D', 'E', 'F'}; + 'C', 'D', 'E', 'F', + '0', '1', '2', '3', '4', '5', + '6', '7', '8', '9', 'a', 'b', + 'c', 'd', 'e', 'f' + }; std::string retval(parIndices.size() + parNegative, '-'); for (auto z = parNegative; z < static_cast(parIndices.size()) + parNegative; ++z) { retval[z] = symbols[parIndices[z - parNegative]];