1
0
Fork 0
mirror of https://github.com/bolero-MURAKAMI/Sprout synced 2025-08-03 12:49:50 +00:00

fix char literal implementation

This commit is contained in:
bolero-MURAKAMI 2014-10-27 10:16:49 +09:00
parent 773855410b
commit 897d1e25b6
18 changed files with 325 additions and 95 deletions

View file

@ -13,38 +13,48 @@
#include <sprout/workaround/std/cstddef.hpp>
#include <sprout/ctype/ascii.hpp>
#include <sprout/assert.hpp>
#include <sprout/static_assert.hpp>
#include <sprout/detail/char_literal.hpp>
#include <sprout/detail/char_type_of_consecutive.hpp>
namespace sprout {
namespace detail {
template<typename Elem, typename IntType>
inline SPROUT_CONSTEXPR Elem
int_to_char(IntType val, int base) {
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);
return SPROUT_ASSERT(2 <= base && base <= 36), SPROUT_ASSERT(IntType(0) <= val && val < static_cast<IntType>(base)),
val < 10 ? static_cast<Elem>('0') + val
: static_cast<Elem>('a') + (val - 10)
val < 10 ? SPROUT_CHAR_LITERAL('0', Elem) + val
: SPROUT_CHAR_LITERAL('a', Elem) + (val - 10)
;
}
template<typename Elem, typename IntType>
inline SPROUT_CONSTEXPR Elem
int_to_char(IntType val) {
SPROUT_STATIC_ASSERT(sprout::detail::is_char_type_of_consecutive_digits<Elem>::value);
return SPROUT_ASSERT(IntType(0) <= val && val < IntType(10)),
static_cast<Elem>('0') + val
SPROUT_CHAR_LITERAL('0', Elem) + val
;
}
template<typename IntType, typename Elem>
inline SPROUT_CONSTEXPR IntType
char_to_int(Elem c, int base) {
return sprout::ascii::isdigit(c) && c - static_cast<Elem>('0') < base ? c - static_cast<Elem>('0')
: sprout::ascii::islower(c) && c - static_cast<Elem>('a') + 10 < base ? c - static_cast<Elem>('a') + 10
: sprout::ascii::isupper(c) && c - static_cast<Elem>('A') + 10 < base ? c - static_cast<Elem>('A') + 10
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
: static_cast<IntType>(-1)
;
}
template<typename IntType, typename Elem>
inline SPROUT_CONSTEXPR IntType
char_to_int(Elem c) {
return sprout::ascii::isdigit(c) ? c - static_cast<Elem>('0')
SPROUT_STATIC_ASSERT(sprout::detail::is_char_type_of_consecutive_digits<Elem>::value);
return sprout::ascii::isdigit(c) ? c - SPROUT_CHAR_LITERAL('0', Elem)
: static_cast<IntType>(-1)
;
}