Sprout/sprout/detail/char_conversion.hpp

65 lines
2.9 KiB
C++
Raw Normal View History

2013-08-08 09:54:33 +00:00
/*=============================================================================
2014-01-08 07:48:12 +00:00
Copyright (c) 2011-2014 Bolero MURAKAMI
2013-08-08 09:54:33 +00:00
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)
=============================================================================*/
2012-04-14 15:48:31 +00:00
#ifndef SPROUT_DETAIL_CHAR_CONVERSION_HPP
#define SPROUT_DETAIL_CHAR_CONVERSION_HPP
#include <stdexcept>
#include <sprout/config.hpp>
#include <sprout/workaround/std/cstddef.hpp>
#include <sprout/ctype/ascii.hpp>
2013-03-18 10:12:21 +00:00
#include <sprout/assert.hpp>
2014-10-27 01:16:49 +00:00
#include <sprout/static_assert.hpp>
#include <sprout/detail/char_literal.hpp>
#include <sprout/detail/char_type_of_consecutive.hpp>
2012-04-14 15:48:31 +00:00
namespace sprout {
namespace detail {
template<typename Elem, typename IntType>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR Elem
int_to_char(IntType val, int base) {
2014-10-27 01:16:49 +00:00
SPROUT_STATIC_ASSERT(sprout::detail::is_char_type_of_consecutive_digits<Elem>::value);
SPROUT_STATIC_ASSERT(sprout::detail::is_char_type_of_consecutive_lower_alphabet<Elem>::value);
2013-03-18 10:12:21 +00:00
return SPROUT_ASSERT(2 <= base && base <= 36), SPROUT_ASSERT(IntType(0) <= val && val < static_cast<IntType>(base)),
2014-10-27 01:16:49 +00:00
val < 10 ? SPROUT_CHAR_LITERAL('0', Elem) + val
: SPROUT_CHAR_LITERAL('a', Elem) + (val - 10)
2012-04-14 15:48:31 +00:00
;
}
template<typename Elem, typename IntType>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR Elem
int_to_char(IntType val) {
2014-10-27 01:16:49 +00:00
SPROUT_STATIC_ASSERT(sprout::detail::is_char_type_of_consecutive_digits<Elem>::value);
2013-03-18 10:12:21 +00:00
return SPROUT_ASSERT(IntType(0) <= val && val < IntType(10)),
2014-10-27 01:16:49 +00:00
SPROUT_CHAR_LITERAL('0', Elem) + val
2012-04-14 15:48:31 +00:00
;
}
template<typename IntType, typename Elem>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR IntType
char_to_int(Elem c, int base) {
2014-10-27 01:16:49 +00:00
SPROUT_STATIC_ASSERT(sprout::detail::is_char_type_of_consecutive_digits<Elem>::value);
SPROUT_STATIC_ASSERT(sprout::detail::is_char_type_of_consecutive_lower_alphabet<Elem>::value);
SPROUT_STATIC_ASSERT(sprout::detail::is_char_type_of_consecutive_upper_alphabet<Elem>::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
2012-04-14 15:48:31 +00:00
: static_cast<IntType>(-1)
;
}
template<typename IntType, typename Elem>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR IntType
char_to_int(Elem c) {
2014-10-27 01:16:49 +00:00
SPROUT_STATIC_ASSERT(sprout::detail::is_char_type_of_consecutive_digits<Elem>::value);
return sprout::ascii::isdigit(c) ? c - SPROUT_CHAR_LITERAL('0', Elem)
2012-04-14 15:48:31 +00:00
: static_cast<IntType>(-1)
;
}
} // namespace detail
} // namespace sprout
#endif // #ifndef SPROUT_DETAIL_CHAR_CONVERSION_HPP