/*============================================================================= Copyright (c) 2011-2014 Bolero MURAKAMI https://github.com/bolero-MURAKAMI/Sprout Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) =============================================================================*/ #ifndef SPROUT_DETAIL_CHAR_CONVERSION_HPP #define SPROUT_DETAIL_CHAR_CONVERSION_HPP #include #include #include #include #include #include #include #include namespace sprout { namespace detail { template inline SPROUT_CONSTEXPR Elem int_to_char(IntType val, int base) { SPROUT_STATIC_ASSERT(sprout::detail::is_char_type_of_consecutive_digits::value); SPROUT_STATIC_ASSERT(sprout::detail::is_char_type_of_consecutive_lower_alphabet::value); return SPROUT_ASSERT(2 <= base && base <= 36), SPROUT_ASSERT(IntType(0) <= val && val < static_cast(base)), val < 10 ? SPROUT_CHAR_LITERAL('0', Elem) + val : SPROUT_CHAR_LITERAL('a', Elem) + (val - 10) ; } template inline SPROUT_CONSTEXPR Elem int_to_char(IntType val) { SPROUT_STATIC_ASSERT(sprout::detail::is_char_type_of_consecutive_digits::value); return SPROUT_ASSERT(IntType(0) <= val && val < IntType(10)), SPROUT_CHAR_LITERAL('0', Elem) + val ; } template inline SPROUT_CONSTEXPR IntType char_to_int(Elem c, int base) { SPROUT_STATIC_ASSERT(sprout::detail::is_char_type_of_consecutive_digits::value); SPROUT_STATIC_ASSERT(sprout::detail::is_char_type_of_consecutive_lower_alphabet::value); SPROUT_STATIC_ASSERT(sprout::detail::is_char_type_of_consecutive_upper_alphabet::value); return sprout::ascii::isdigit(c) && c - SPROUT_CHAR_LITERAL('0', Elem) < base ? c - SPROUT_CHAR_LITERAL('0', Elem) : sprout::ascii::islower(c) && c - SPROUT_CHAR_LITERAL('a', Elem) + 10 < base ? c - SPROUT_CHAR_LITERAL('a', Elem) + 10 : sprout::ascii::isupper(c) && c - SPROUT_CHAR_LITERAL('A', Elem) + 10 < base ? c - SPROUT_CHAR_LITERAL('A', Elem) + 10 : static_cast(-1) ; } template inline SPROUT_CONSTEXPR IntType char_to_int(Elem c) { SPROUT_STATIC_ASSERT(sprout::detail::is_char_type_of_consecutive_digits::value); return sprout::ascii::isdigit(c) ? c - SPROUT_CHAR_LITERAL('0', Elem) : static_cast(-1) ; } } // namespace detail } // namespace sprout #endif // #ifndef SPROUT_DETAIL_CHAR_CONVERSION_HPP