add conversion string_ref to arithmetic-type

This commit is contained in:
bolero-MURAKAMI 2013-02-19 10:51:12 +09:00
parent 1ef8a6a63b
commit 2e0bc89188
10 changed files with 424 additions and 8 deletions

View file

@ -6,8 +6,8 @@
namespace sprout {
//
// none_t
// none
// nullopt_t
// nullopt
//
typedef sprout::none_t nullopt_t;
namespace {

View file

@ -1,7 +1,7 @@
#ifndef SPROUT_STRING_FROM_STRING_HPP
#define SPROUT_STRING_FROM_STRING_HPP
#include <cstddef>
#include <sprout/config.hpp>
#include <sprout/string/string_to_int.hpp>
#include <sprout/string/string_to_float.hpp>

View file

@ -1,16 +1,118 @@
#ifndef SPROUT_STRING_NPOS_HPP
#define SPROUT_STRING_NPOS_HPP
#include <cstddef>
#include <sprout/config.hpp>
#include <sprout/type_traits/is_uint.hpp>
#include <sprout/type_traits/enabler_if.hpp>
namespace sprout {
//
// npos_t
// npos
//
struct npos_t {
public:
template<typename UIntType, typename sprout::enabler_if<sprout::is_uint<UIntType>::value>::type = sprout::enabler>
SPROUT_CONSTEXPR operator UIntType() const {
return UIntType(-1);
}
};
namespace {
SPROUT_STATIC_CONSTEXPR std::size_t npos = -1;
SPROUT_STATIC_CONSTEXPR sprout::npos_t npos{};
} // anonymous-namespace
//
// operator==
// operator!=
// operator<
// operator>
// operator<=
// operator>=
//
template<typename UIntType, typename sprout::enabler_if<sprout::is_uint<UIntType>::value>::type = sprout::enabler>
inline SPROUT_CONSTEXPR bool
operator==(UIntType const& lhs, sprout::npos_t rhs) {
return lhs == UIntType(rhs);
}
template<typename UIntType, typename sprout::enabler_if<sprout::is_uint<UIntType>::value>::type = sprout::enabler>
inline SPROUT_CONSTEXPR bool
operator==(sprout::npos_t lhs, UIntType const& rhs) {
return rhs == lhs;
}
inline SPROUT_CONSTEXPR bool
operator==(sprout::npos_t lhs, sprout::npos_t rhs) {
return true;
}
template<typename UIntType, typename sprout::enabler_if<sprout::is_uint<UIntType>::value>::type = sprout::enabler>
inline SPROUT_CONSTEXPR bool
operator!=(UIntType const& lhs, sprout::npos_t rhs) {
return !(lhs == rhs);
}
template<typename UIntType, typename sprout::enabler_if<sprout::is_uint<UIntType>::value>::type = sprout::enabler>
inline SPROUT_CONSTEXPR bool
operator!=(sprout::npos_t lhs, UIntType const& rhs) {
return !(lhs == rhs);
}
inline SPROUT_CONSTEXPR bool
operator!=(sprout::npos_t lhs, sprout::npos_t rhs) {
return !(lhs == rhs);
}
template<typename UIntType, typename sprout::enabler_if<sprout::is_uint<UIntType>::value>::type = sprout::enabler>
inline SPROUT_CONSTEXPR bool
operator<(UIntType const& lhs, sprout::npos_t rhs) {
return lhs < UIntType(rhs);
}
template<typename UIntType, typename sprout::enabler_if<sprout::is_uint<UIntType>::value>::type = sprout::enabler>
inline SPROUT_CONSTEXPR bool
operator<(sprout::npos_t lhs, UIntType const& rhs) {
return rhs == lhs;
}
inline SPROUT_CONSTEXPR bool
operator<(sprout::npos_t lhs, sprout::npos_t rhs) {
return false;
}
template<typename UIntType, typename sprout::enabler_if<sprout::is_uint<UIntType>::value>::type = sprout::enabler>
inline SPROUT_CONSTEXPR bool
operator>(UIntType const& lhs, sprout::npos_t rhs) {
return rhs < lhs;
}
template<typename UIntType, typename sprout::enabler_if<sprout::is_uint<UIntType>::value>::type = sprout::enabler>
inline SPROUT_CONSTEXPR bool
operator>(sprout::npos_t lhs, UIntType const& rhs) {
return rhs < lhs;
}
inline SPROUT_CONSTEXPR bool
operator>(sprout::npos_t lhs, sprout::npos_t rhs) {
return rhs < lhs;
}
template<typename UIntType, typename sprout::enabler_if<sprout::is_uint<UIntType>::value>::type = sprout::enabler>
inline SPROUT_CONSTEXPR bool
operator<=(UIntType const& lhs, sprout::npos_t rhs) {
return !(rhs < lhs);
}
template<typename UIntType, typename sprout::enabler_if<sprout::is_uint<UIntType>::value>::type = sprout::enabler>
inline SPROUT_CONSTEXPR bool
operator<=(sprout::npos_t lhs, UIntType const& rhs) {
return !(rhs < lhs);
}
inline SPROUT_CONSTEXPR bool
operator<=(sprout::npos_t lhs, sprout::npos_t rhs) {
return !(rhs < lhs);
}
template<typename UIntType, typename sprout::enabler_if<sprout::is_uint<UIntType>::value>::type = sprout::enabler>
inline SPROUT_CONSTEXPR bool
operator>=(UIntType const& lhs, sprout::npos_t rhs) {
return !(lhs < rhs);
}
template<typename UIntType, typename sprout::enabler_if<sprout::is_uint<UIntType>::value>::type = sprout::enabler>
inline SPROUT_CONSTEXPR bool
operator>=(sprout::npos_t lhs, UIntType const& rhs) {
return !(lhs < rhs);
}
inline SPROUT_CONSTEXPR bool
operator>=(sprout::npos_t lhs, sprout::npos_t rhs) {
return !(lhs < rhs);
}
} // namespace sprout
#endif // #ifndef SPROUT_STRING_NPOS_HPP

View file

@ -1,7 +1,7 @@
#ifndef SPROUT_STRING_TO_STRING_HPP
#define SPROUT_STRING_TO_STRING_HPP
#include <cstddef>
#include <sprout/config.hpp>
#include <sprout/string/int_to_string.hpp>
#include <sprout/string/float_to_string.hpp>

View file

@ -6,6 +6,7 @@
#include <sprout/utility/string_ref/comparison.hpp>
#include <sprout/utility/string_ref/io.hpp>
#include <sprout/utility/string_ref/hash.hpp>
#include <sprout/utility/string_ref/conversion.hpp>
#include <sprout/utility/string_ref/type_traits.hpp>
#include <sprout/utility/string_ref/alias.hpp>

View file

@ -0,0 +1,7 @@
#ifndef SPROUT_UTILITY_STRING_REF_CONVERSION_HPP
#define SPROUT_UTILITY_STRING_REF_CONVERSION_HPP
#include <cstddef>
#include <sprout/utility/string_ref/from_string.hpp>
#endif // #ifndef SPROUT_UTILITY_STRING_REF_CONVERSION_HPP

View file

@ -0,0 +1,8 @@
#ifndef SPROUT_UTILITY_STRING_REF_FROM_STRING_HPP
#define SPROUT_UTILITY_STRING_REF_FROM_STRING_HPP
#include <sprout/config.hpp>
#include <sprout/utility/string_ref/string_to_int.hpp>
#include <sprout/utility/string_ref/string_to_float.hpp>
#endif // #ifndef SPROUT_UTILITY_STRING_REF_FROM_STRING_HPP

View file

@ -82,11 +82,11 @@ namespace sprout {
{}
template<std::size_t N>
SPROUT_CONSTEXPR basic_string_ref(sprout::basic_string<T, N, Traits> const& str)
: ptr_(str.data()), len_(str.length())
: ptr_(str.data()), len_(str.size())
{}
template<typename Allocator>
SPROUT_CONSTEXPR basic_string_ref(std::basic_string<T, Traits, Allocator> const& str)
: ptr_(str.data()), len_(str.length())
: ptr_(str.data()), len_(str.size())
{}
SPROUT_CONSTEXPR basic_string_ref(const_pointer str, size_type len)
: ptr_(str), len_(len)
@ -550,6 +550,30 @@ namespace sprout {
{
lhs.swap(rhs);
}
//
// to_string_ref
//
template<typename T, std::size_t N, typename Traits>
inline SPROUT_CONSTEXPR sprout::basic_string_ref<T, Traits>
to_string_ref(sprout::basic_string<T, N, Traits> const& s) {
return sprout::basic_string_ref<T, Traits>(s);
}
template<typename T, typename Traits>
inline SPROUT_CONSTEXPR sprout::basic_string_ref<T, Traits>
to_string_ref(std::basic_string<T, Traits> const& s) {
return sprout::basic_string_ref<T, Traits>(s);
}
template<typename T>
inline SPROUT_CONSTEXPR sprout::basic_string_ref<T>
to_string_ref(T const* str) {
return sprout::basic_string_ref<T>(str);
}
template<typename T>
inline SPROUT_CONSTEXPR sprout::basic_string_ref<T>
to_string_ref(T const* str, std::size_t len) {
return sprout::basic_string_ref<T>(str, len);
}
} // namespace sprout
#endif // #ifndef SPROUT_UTILITY_STRING_REF_STRING_REF_HPP

View file

@ -0,0 +1,109 @@
#ifndef SPROUT_UTILITY_STRING_REF_STRING_TO_FLOAT_HPP
#define SPROUT_UTILITY_STRING_REF_STRING_TO_FLOAT_HPP
#include <string>
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/utility/string_ref/string_ref.hpp>
#include <sprout/cstdlib/str_to_float.hpp>
#include <sprout/type_traits/enabler_if.hpp>
namespace sprout {
namespace detail {
template<
typename FloatType, typename Elem, typename Traits
>
inline FloatType string_to_float_dynamic(sprout::basic_string_ref<Elem, Traits> const& str, std::size_t* idx) {
Elem* endptr = nullptr;
FloatType result = sprout::detail::str_to_float<FloatType>(str.c_str(), &endptr);
*idx = endptr - str.c_str();
return result;
}
}
//
// string_to_float
//
template<
typename FloatType, typename Elem, typename Traits,
typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR FloatType
string_to_float(sprout::basic_string_ref<Elem, Traits> const& str, std::size_t* idx) {
return !idx ? sprout::detail::str_to_float<FloatType>(str.begin())
: sprout::detail::string_to_float_dynamic<FloatType>(str, idx)
;
}
template<
typename FloatType, typename Elem, typename Traits,
typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR FloatType
string_to_float(sprout::basic_string_ref<Elem, Traits> const& str) {
return sprout::detail::str_to_float<FloatType>(str.begin());
}
//
// stof
//
template<typename Elem, typename Traits>
inline SPROUT_CONSTEXPR float
stof(sprout::basic_string_ref<Elem, Traits> const& str, std::size_t* idx) {
return sprout::string_to_float<float>(str, idx);
}
template<typename Elem, typename Traits>
inline SPROUT_CONSTEXPR float
stof(sprout::basic_string_ref<Elem, Traits> const& str) {
return sprout::string_to_float<float>(str);
}
//
// stod
//
template<typename Elem, typename Traits>
inline SPROUT_CONSTEXPR double
stod(sprout::basic_string_ref<Elem, Traits> const& str, std::size_t* idx) {
return sprout::string_to_float<double>(str, idx);
}
template<typename Elem, typename Traits>
inline SPROUT_CONSTEXPR double
stod(sprout::basic_string_ref<Elem, Traits> const& str) {
return sprout::string_to_float<double>(str);
}
//
// stold
//
template<typename Elem, typename Traits>
inline SPROUT_CONSTEXPR long double
stold(sprout::basic_string_ref<Elem, Traits> const& str, std::size_t* idx) {
return sprout::string_to_float<long double>(str, idx);
}
template<typename Elem, typename Traits>
inline SPROUT_CONSTEXPR long double
stold(sprout::basic_string_ref<Elem, Traits> const& str) {
return sprout::string_to_float<long double>(str);
}
//
// from_string
//
template<
typename FloatType, typename Elem, typename Traits,
typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR FloatType
from_string(sprout::basic_string_ref<Elem, Traits> const& str, std::size_t* idx) {
return sprout::string_to_float<FloatType>(str, idx);
}
template<
typename FloatType, typename Elem, typename Traits,
typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR FloatType
from_string(sprout::basic_string_ref<Elem, Traits> const& str) {
return sprout::string_to_float<FloatType>(str);
}
} // namespace sprout
#endif // #ifndef SPROUT_UTILITY_STRING_REF_STRING_TO_FLOAT_HPP

View file

@ -0,0 +1,165 @@
#ifndef SPROUT_UTILITY_STRING_REF_STRING_TO_INT_HPP
#define SPROUT_UTILITY_STRING_REF_STRING_TO_INT_HPP
#include <cstdint>
#include <string>
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/utility/string_ref/string_ref.hpp>
#include <sprout/cstdlib/str_to_int.hpp>
#include <sprout/type_traits/enabler_if.hpp>
namespace sprout {
namespace detail {
template<typename IntType, typename Elem, typename Traits>
inline IntType
string_to_int_dynamic(sprout::basic_string_ref<Elem, Traits> const& str, std::size_t* idx, int base = 10) {
Elem* endptr = nullptr;
IntType result = sprout::detail::str_to_int<IntType>(str.c_str(), &endptr, base);
*idx = endptr - str.c_str();
return result;
}
}
//
// string_to_int
//
template<
typename IntType, typename Elem, typename Traits,
typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR IntType
string_to_int(sprout::basic_string_ref<Elem, Traits> const& str, std::size_t* idx, int base = 10) {
return !idx ? sprout::detail::str_to_int<IntType>(str.begin(), base)
: sprout::detail::string_to_int_dynamic<IntType>(str, idx, base)
;
}
template<
typename IntType, typename Elem, typename Traits,
typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR IntType
string_to_int(sprout::basic_string_ref<Elem, Traits> const& str, int base = 10) {
return sprout::detail::str_to_int<IntType>(str.begin(), base);
}
//
// stoi
//
template<typename Elem, typename Traits>
inline SPROUT_CONSTEXPR int
stoi(sprout::basic_string_ref<Elem, Traits> const& str, std::size_t* idx, int base = 10) {
return sprout::string_to_int<int>(str, idx, base);
}
template<typename Elem, typename Traits>
inline SPROUT_CONSTEXPR int
stoi(sprout::basic_string_ref<Elem, Traits> const& str, int base = 10) {
return sprout::string_to_int<int>(str, base);
}
//
// stol
//
template<typename Elem, typename Traits>
inline SPROUT_CONSTEXPR long
stol(sprout::basic_string_ref<Elem, Traits> const& str, std::size_t* idx, int base = 10) {
return sprout::string_to_int<long>(str, idx, base);
}
template<typename Elem, typename Traits>
inline SPROUT_CONSTEXPR long
stol(sprout::basic_string_ref<Elem, Traits> const& str, int base = 10) {
return sprout::string_to_int<long>(str, base);
}
//
// stoul
//
template<typename Elem, typename Traits>
inline SPROUT_CONSTEXPR unsigned long
stoul(sprout::basic_string_ref<Elem, Traits> const& str, std::size_t* idx, int base = 10) {
return sprout::string_to_int<unsigned long>(str, idx, base);
}
template<typename Elem, typename Traits>
inline SPROUT_CONSTEXPR unsigned long
stoul(sprout::basic_string_ref<Elem, Traits> const& str, int base = 10) {
return sprout::string_to_int<unsigned long>(str, base);
}
//
// stoll
//
template<typename Elem, typename Traits>
inline SPROUT_CONSTEXPR long long
stoll(sprout::basic_string_ref<Elem, Traits> const& str, std::size_t* idx, int base = 10) {
return sprout::string_to_int<long long>(str, idx, base);
}
template<typename Elem, typename Traits>
inline SPROUT_CONSTEXPR long long
stoll(sprout::basic_string_ref<Elem, Traits> const& str, int base = 10) {
return sprout::string_to_int<long long>(str, base);
}
//
// stoull
//
template<typename Elem, typename Traits>
inline SPROUT_CONSTEXPR unsigned long long
stoull(sprout::basic_string_ref<Elem, Traits> const& str, std::size_t* idx, int base = 10) {
return sprout::string_to_int<unsigned long long>(str, idx, base);
}
template<typename Elem, typename Traits>
inline SPROUT_CONSTEXPR unsigned long long
stoull(sprout::basic_string_ref<Elem, Traits> const& str, int base = 10) {
return sprout::string_to_int<unsigned long long>(str, base);
}
//
// stoimax
//
template<typename Elem, typename Traits>
inline SPROUT_CONSTEXPR std::intmax_t
stoimax(sprout::basic_string_ref<Elem, Traits> const& str, std::size_t* idx, int base = 10) {
return sprout::string_to_int<std::intmax_t>(str, idx, base);
}
template<typename Elem, typename Traits>
inline SPROUT_CONSTEXPR std::intmax_t
stoimax(sprout::basic_string_ref<Elem, Traits> const& str, int base = 10) {
return sprout::string_to_int<std::intmax_t>(str, base);
}
//
// stoumax
//
template<typename Elem, typename Traits>
inline SPROUT_CONSTEXPR std::uintmax_t
stoumax(sprout::basic_string_ref<Elem, Traits> const& str, std::size_t* idx, int base = 10) {
return sprout::string_to_int<std::uintmax_t>(str, idx, base);
}
template<typename Elem, typename Traits>
inline SPROUT_CONSTEXPR std::uintmax_t
stoumax(sprout::basic_string_ref<Elem, Traits> const& str, int base = 10) {
return sprout::string_to_int<std::uintmax_t>(str, base);
}
//
// from_string
//
template<
typename IntType, typename Elem, typename Traits,
typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR IntType
from_string(sprout::basic_string_ref<Elem, Traits> const& str, std::size_t* idx) {
return sprout::string_to_int<IntType>(str, idx, 0);
}
template<
typename IntType, typename Elem, typename Traits,
typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR IntType
from_string(sprout::basic_string_ref<Elem, Traits> const& str) {
return sprout::string_to_int<IntType>(str, 0);
}
} // namespace sprout
#endif // #ifndef SPROUT_UTILITY_STRING_REF_STRING_TO_INT_HPP