add atoi, atol, strtol

This commit is contained in:
bolero-MURAKAMI 2012-04-15 00:48:31 +09:00
parent 1968a6fac4
commit 50fd5f33bf
11 changed files with 307 additions and 55 deletions

View file

@ -2,7 +2,7 @@
#define SPROUT_CSTDLIB_HPP
#include <sprout/config.hpp>
#include <sprout/cstdlib/abs.hpp>
#include <sprout/cstdlib/div.hpp>
#include <sprout/cstdlib/conversion.hpp>
#include <sprout/cstdlib/arithmetic.hpp>
#endif // #ifndef SPROUT_CSTDLIB_HPP

View file

@ -0,0 +1,8 @@
#ifndef SPROUT_CSTDLIB_ARITHMETIC_HPP
#define SPROUT_CSTDLIB_ARITHMETIC_HPP
#include <sprout/config.hpp>
#include <sprout/cstdlib/abs.hpp>
#include <sprout/cstdlib/div.hpp>
#endif // #ifndef SPROUT_CSTDLIB_ARITHMETIC_HPP

20
sprout/cstdlib/atoi.hpp Normal file
View file

@ -0,0 +1,20 @@
#ifndef SPROUT_CSTDLIB_ATOI_HPP
#define SPROUT_CSTDLIB_ATOI_HPP
#include <sprout/config.hpp>
#include <sprout/cstdlib/decimal_to_int.hpp>
namespace sprout {
//
// atoi
//
inline SPROUT_CONSTEXPR int atoi(char const* str) {
return sprout::decimal_to_int<int>(str);
}
template<typename Char>
inline SPROUT_CONSTEXPR int atoi(Char const* str) {
return sprout::decimal_to_int<int>(str);
}
} // namespace sprout
#endif // #ifndef SPROUT_CSTDLIB_ATOI_HPP

20
sprout/cstdlib/atol.hpp Normal file
View file

@ -0,0 +1,20 @@
#ifndef SPROUT_CSTDLIB_ATOL_HPP
#define SPROUT_CSTDLIB_ATOL_HPP
#include <sprout/config.hpp>
#include <sprout/cstdlib/decimal_to_int.hpp>
namespace sprout {
//
// atol
//
inline SPROUT_CONSTEXPR long atol(char const* str) {
return sprout::decimal_to_int<long>(str);
}
template<typename Char>
inline SPROUT_CONSTEXPR int atol(Char const* str) {
return sprout::decimal_to_int<int>(str);
}
} // namespace sprout
#endif // #ifndef SPROUT_CSTDLIB_ATOL_HPP

View file

@ -0,0 +1,11 @@
#ifndef SPROUT_CSTDLIB_CONVERSION_HPP
#define SPROUT_CSTDLIB_CONVERSION_HPP
#include <sprout/config.hpp>
#include <sprout/cstdlib/decimal_to_int.hpp>
#include <sprout/cstdlib/str_to_int.hpp>
#include <sprout/cstdlib/atoi.hpp>
#include <sprout/cstdlib/atol.hpp>
#include <sprout/cstdlib/strtol.hpp>
#endif // #ifndef SPROUT_CSTDLIB_CONVERSION_HPP

View file

@ -0,0 +1,51 @@
#ifndef SPROUT_CSTDLIB_DECIMAL_TO_INT_HPP
#define SPROUT_CSTDLIB_DECIMAL_TO_INT_HPP
#include <iterator>
#include <limits>
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/iterator/operation.hpp>
#include <sprout/ctype/ascii.hpp>
#include <sprout/utility/enabler_if.hpp>
namespace sprout {
namespace detail {
// Copyright (c) 2011 osyo-manga : http://d.hatena.ne.jp/osyo-manga/
template<typename IntType, typename CStrIterator>
inline SPROUT_CONSTEXPR IntType decimal_to_int_impl(CStrIterator str, IntType val, bool negative) {
return !sprout::ascii::isdigit(*str)
? negative ? -val : val
: val > (std::numeric_limits<IntType>::max() - (*str - static_cast<typename std::iterator_traits<CStrIterator>::value_type>('0')) - (negative ? 1 : 0)) / 10
? (negative ? std::numeric_limits<IntType>::min() : std::numeric_limits<IntType>::max())
: sprout::detail::decimal_to_int_impl<IntType>(
sprout::next(str),
val * 10 + (*str - static_cast<typename std::iterator_traits<CStrIterator>::value_type>('0')),
negative
)
;
}
template<typename IntType, typename CStrIterator>
inline SPROUT_CONSTEXPR IntType decimal_to_int(CStrIterator str) {
return sprout::ascii::isspace(*str)
? sprout::detail::decimal_to_int<IntType>(sprout::next(str))
: *str == static_cast<typename std::iterator_traits<CStrIterator>::value_type>('-')
? sprout::detail::decimal_to_int_impl<IntType>(sprout::next(str), IntType(), true)
: *str == static_cast<typename std::iterator_traits<CStrIterator>::value_type>('+')
? sprout::detail::decimal_to_int_impl<IntType>(sprout::next(str), IntType(), false)
: sprout::detail::decimal_to_int_impl<IntType>(str, IntType(), false)
;
}
} // namespace detail
//
// decimal_to_int
//
template<typename IntType, typename Char, typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler>
inline SPROUT_CONSTEXPR IntType decimal_to_int(Char const* str) {
return sprout::detail::decimal_to_int<IntType>(str);
}
} // namespace sprout
#endif // #ifndef SPROUT_CSTDLIB_DECIMAL_TO_INT_HPP

View file

@ -0,0 +1,95 @@
#ifndef SPROUT_CSTDLIB_STR_TO_INT_HPP
#define SPROUT_CSTDLIB_STR_TO_INT_HPP
#include <cstddef>
#include <cstdlib>
#include <limits>
#include <sprout/config.hpp>
#include <sprout/iterator/operation.hpp>
#include <sprout/utility/enabler_if.hpp>
#include <sprout/detail/char_conversion.hpp>
namespace sprout {
namespace detail {
// Copyright (c) 2011 osyo-manga : http://d.hatena.ne.jp/osyo-manga/
template<typename IntType, typename CStrIterator>
inline SPROUT_CONSTEXPR IntType str_to_int_impl_1(CStrIterator str, int base, IntType val, IntType x, bool negative) {
return x == static_cast<IntType>(-1)
? (negative ? -val : val)
: val > (std::numeric_limits<IntType>::max() - x - (negative ? 1 : 0)) / base
? (negative ? std::numeric_limits<IntType>::min() : std::numeric_limits<IntType>::max())
: sprout::detail::str_to_int_impl_1<IntType>(
sprout::next(str),
base,
val * base + x,
sprout::detail::char_to_int<IntType>(*sprout::next(str), base),
negative
)
;
}
template<typename IntType, typename CStrIterator>
inline SPROUT_CONSTEXPR IntType str_to_int_impl(CStrIterator str, int base, bool negative) {
return *str == static_cast<typename std::iterator_traits<CStrIterator>::value_type>('0')
? *sprout::next(str) == static_cast<typename std::iterator_traits<CStrIterator>::value_type>('x')
|| *sprout::next(str) == static_cast<typename std::iterator_traits<CStrIterator>::value_type>('X')
? sprout::detail::str_to_int_impl_1<IntType>(
sprout::next(str, 2),
base ? base : 16,
IntType(),
sprout::detail::char_to_int<IntType>(*sprout::next(str, 2), base),
negative
)
: sprout::detail::str_to_int_impl_1<IntType>(
sprout::next(str),
base ? base : 8,
IntType(),
sprout::detail::char_to_int<IntType>(*sprout::next(str), base),
negative
)
: sprout::detail::str_to_int_impl_1<IntType>(
str,
base ? base : 10,
IntType(),
sprout::detail::char_to_int<IntType>(*str, base),
negative
)
;
}
template<typename IntType, typename CStrIterator>
inline SPROUT_CONSTEXPR IntType str_to_int(CStrIterator str, int base) {
return sprout::ascii::isspace(*str)
? sprout::detail::str_to_int<IntType>(sprout::next(str), base)
: *str == static_cast<typename std::iterator_traits<CStrIterator>::value_type>('-')
? sprout::detail::str_to_int_impl<IntType>(sprout::next(str), base, true)
: *str == static_cast<typename std::iterator_traits<CStrIterator>::value_type>('+')
? sprout::detail::str_to_int_impl<IntType>(sprout::next(str), base, false)
: sprout::detail::str_to_int_impl<IntType>(str, base, false)
;
}
template<typename IntType, typename CStrIterator, typename CharPtr>
inline SPROUT_CONSTEXPR IntType str_to_int(CStrIterator str, CharPtr* endptr, int base) {
return !endptr ? sprout::detail::str_to_int<IntType>(str, base)
: std::strtol(str, endptr, base)
;
}
} // namespace detail
//
// str_to_int
//
template<typename IntType, typename Char, typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler>
inline SPROUT_CONSTEXPR IntType str_to_int(Char const* str, Char** endptr, int base = 10) {
return sprout::detail::str_to_int<IntType>(str, endptr, base);
}
template<typename IntType, typename Char, typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler>
inline SPROUT_CONSTEXPR IntType str_to_int(Char const* str, std::nullptr_t endptr, int base = 10) {
return sprout::detail::str_to_int<IntType>(str, base);
}
template<typename IntType, typename Char, typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler>
inline SPROUT_CONSTEXPR IntType str_to_int(Char const* str, int base = 10) {
return sprout::detail::str_to_int<IntType>(str, base);
}
} // namespace sprout
#endif // #ifndef SPROUT_CSTDLIB_STR_TO_INT_HPP

29
sprout/cstdlib/strtol.hpp Normal file
View file

@ -0,0 +1,29 @@
#ifndef SPROUT_CSTDLIB_STRTOL_HPP
#define SPROUT_CSTDLIB_STRTOL_HPP
#include <cstddef>
#include <sprout/config.hpp>
#include <sprout/cstdlib/str_to_int.hpp>
namespace sprout {
//
// strtol
//
inline SPROUT_CONSTEXPR long strtol(char const* str, char** endptr, int base = 10){
return sprout::str_to_int<long>(str, endptr, base);
}
template<typename Char>
inline SPROUT_CONSTEXPR long strtol(Char const* str, Char** endptr, int base = 10){
return sprout::str_to_int<long>(str, endptr, base);
}
template<typename Char>
inline SPROUT_CONSTEXPR long strtol(Char const* str, std::nullptr_t endptr, int base = 10){
return sprout::str_to_int<long>(str, base);
}
template<typename Char>
inline SPROUT_CONSTEXPR long strtol(Char const* str, int base = 10){
return sprout::str_to_int<long>(str, base);
}
} // namespace sprout
#endif // #ifndef SPROUT_CSTDLIB_STRTOL_HPP

View file

@ -0,0 +1,41 @@
#ifndef SPROUT_DETAIL_CHAR_CONVERSION_HPP
#define SPROUT_DETAIL_CHAR_CONVERSION_HPP
#include <cstddef>
#include <stdexcept>
#include <sprout/config.hpp>
namespace sprout {
namespace detail {
template<typename Elem, typename IntType>
inline SPROUT_CONSTEXPR Elem int_to_char(IntType val, std::size_t base){
return val >= 0 && val < 10 ? static_cast<Elem>('0') + val
: val >= 10 && val < base ? static_cast<Elem>('a') + (val - 10)
: throw std::invalid_argument("value out of bounds")
;
}
template<typename Elem, typename IntType>
inline SPROUT_CONSTEXPR Elem int_to_char(IntType val){
return val >= 0 && val < 10 ? static_cast<Elem>('0') + val
: throw std::invalid_argument("value out of bounds")
;
}
template<typename IntType, typename Elem>
inline SPROUT_CONSTEXPR IntType char_to_int(Elem c, std::size_t base){
return c >= static_cast<Elem>('0') && c <= static_cast<Elem>('9') ? c - static_cast<Elem>('0')
: c >= static_cast<Elem>('a') && c <= static_cast<Elem>('a' + (base - 11)) ? c - static_cast<Elem>('a') + 10
: c >= static_cast<Elem>('A') && c <= static_cast<Elem>('A' + (base - 11)) ? c - static_cast<Elem>('A') + 10
: static_cast<IntType>(-1)
;
}
template<typename IntType, typename Elem>
inline SPROUT_CONSTEXPR IntType char_to_int(Elem c){
return c >= static_cast<Elem>('0') && c <= static_cast<Elem>('9') ? c - static_cast<Elem>('0')
: static_cast<IntType>(-1)
;
}
} // namespace detail
} // namespace sprout
#endif // #ifndef SPROUT_DETAIL_CHAR_CONVERSION_HPP

View file

@ -6,32 +6,32 @@
#include <sprout/config.hpp>
namespace sprout {
namespace detail {
template<typename R, typename T, typename... Values>
inline SPROUT_CONSTEXPR typename std::enable_if<
sizeof...(Values) == 0,
R
>::type param_at(
std::size_t n,
T const& v,
Values const&... values
)
{
return v;
}
template<typename R, typename T, typename... Values>
inline SPROUT_CONSTEXPR typename std::enable_if<
sizeof...(Values) != 0,
R
>::type param_at(
std::size_t n,
T const& v,
Values const&... values
)
{
return n == 0 ? v : sprout::detail::param_at<R>(n - 1, values...);
}
} // namespace detail
namespace detail {
template<typename R, typename T, typename... Values>
inline SPROUT_CONSTEXPR typename std::enable_if<
sizeof...(Values) == 0,
R
>::type param_at(
std::size_t n,
T const& v,
Values const&... values
)
{
return v;
}
template<typename R, typename T, typename... Values>
inline SPROUT_CONSTEXPR typename std::enable_if<
sizeof...(Values) != 0,
R
>::type param_at(
std::size_t n,
T const& v,
Values const&... values
)
{
return n == 0 ? v : sprout::detail::param_at<R>(n - 1, values...);
}
} // namespace detail
} // namespace sprout
#endif // #ifndef SPROUT_DETAIL_PARAM_AT_HPP

View file

@ -2,39 +2,16 @@
#define SPROUT_STRING_TO_STRING_HPP
#include <cstddef>
#include <stdexcept>
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/string/string.hpp>
#include <sprout/string/make_string.hpp>
#include <sprout/integer/integer_digits.hpp>
#include <sprout/utility/enabler_if.hpp>
#include <sprout/detail/char_conversion.hpp>
namespace sprout {
namespace detail {
template<
typename Elem,
std::size_t Base,
typename IntType,
typename sprout::enabler_if<(Base < 10)>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR Elem int_to_char(IntType val){
return val >= 0 && val < 10 ? static_cast<Elem>('0') + val
: throw std::invalid_argument("value out of bounds")
;
}
template<
typename Elem,
std::size_t Base,
typename IntType,
typename sprout::enabler_if<(Base >= 10 && Base < 36)>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR Elem int_to_char(IntType val){
return val >= 0 && val < 10 ? static_cast<Elem>('0') + val
: val >= 10 && val < 36 ? static_cast<Elem>('a') + (val - 10)
: throw std::invalid_argument("value out of bounds")
;
}
template<
typename Elem,
std::size_t Base,
@ -64,7 +41,7 @@ namespace sprout {
: sprout::detail::int_to_string_impl_1<Elem, Base>(
val / Base,
negative,
sprout::detail::int_to_char<Elem, Base>(val % Base),
sprout::detail::int_to_char<Elem>(val % Base, Base),
args...
)
;
@ -102,7 +79,7 @@ namespace sprout {
? sprout::make_string_as<Elem>(args...)
: sprout::detail::uint_to_string_impl_1<Elem, Base>(
val / Base,
sprout::detail::int_to_char<Elem, Base>(val % Base),
sprout::detail::int_to_char<Elem>(val % Base, Base),
args...
)
;