2013-08-08 09:54:33 +00:00
|
|
|
/*=============================================================================
|
2016-02-25 09:48:28 +00:00
|
|
|
Copyright (c) 2011-2016 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-16 04:32:47 +00:00
|
|
|
#ifndef SPROUT_STRING_STRING_TO_FLOAT_HPP
|
|
|
|
#define SPROUT_STRING_STRING_TO_FLOAT_HPP
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <type_traits>
|
|
|
|
#include <sprout/config.hpp>
|
2014-04-30 07:30:26 +00:00
|
|
|
#include <sprout/workaround/std/cstddef.hpp>
|
2012-04-16 04:32:47 +00:00
|
|
|
#include <sprout/string/string.hpp>
|
|
|
|
#include <sprout/cstdlib/str_to_float.hpp>
|
2012-05-26 15:43:38 +00:00
|
|
|
#include <sprout/type_traits/enabler_if.hpp>
|
2012-04-16 04:32:47 +00:00
|
|
|
|
|
|
|
namespace sprout {
|
|
|
|
namespace detail {
|
|
|
|
template<
|
2012-10-05 15:58:56 +00:00
|
|
|
typename FloatType, typename Elem, std::size_t N, typename Traits
|
2012-04-16 04:32:47 +00:00
|
|
|
>
|
|
|
|
inline FloatType string_to_float_dynamic(sprout::basic_string<Elem, N, 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<
|
2012-10-05 15:58:56 +00:00
|
|
|
typename FloatType, typename Elem, std::size_t N, typename Traits,
|
2012-04-16 04:32:47 +00:00
|
|
|
typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler
|
|
|
|
>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR FloatType
|
|
|
|
string_to_float(sprout::basic_string<Elem, N, Traits> const& str, std::size_t* idx) {
|
2012-04-16 04:32:47 +00:00
|
|
|
return !idx ? sprout::detail::str_to_float<FloatType>(str.begin())
|
|
|
|
: sprout::detail::string_to_float_dynamic<FloatType>(str, idx)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<
|
2012-10-05 15:58:56 +00:00
|
|
|
typename FloatType, typename Elem, std::size_t N, typename Traits,
|
2012-04-16 04:32:47 +00:00
|
|
|
typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler
|
|
|
|
>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR FloatType
|
|
|
|
string_to_float(sprout::basic_string<Elem, N, Traits> const& str) {
|
2012-04-16 04:32:47 +00:00
|
|
|
return sprout::detail::str_to_float<FloatType>(str.begin());
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// stof
|
|
|
|
//
|
|
|
|
template<typename Elem, std::size_t N, typename Traits>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR float
|
|
|
|
stof(sprout::basic_string<Elem, N, Traits> const& str, std::size_t* idx) {
|
2012-04-16 04:32:47 +00:00
|
|
|
return sprout::string_to_float<float>(str, idx);
|
|
|
|
}
|
|
|
|
template<typename Elem, std::size_t N, typename Traits>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR float
|
|
|
|
stof(sprout::basic_string<Elem, N, Traits> const& str) {
|
2012-04-16 04:32:47 +00:00
|
|
|
return sprout::string_to_float<float>(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// stod
|
|
|
|
//
|
|
|
|
template<typename Elem, std::size_t N, typename Traits>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR double
|
|
|
|
stod(sprout::basic_string<Elem, N, Traits> const& str, std::size_t* idx) {
|
2012-04-16 04:32:47 +00:00
|
|
|
return sprout::string_to_float<double>(str, idx);
|
|
|
|
}
|
|
|
|
template<typename Elem, std::size_t N, typename Traits>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR double
|
|
|
|
stod(sprout::basic_string<Elem, N, Traits> const& str) {
|
2012-04-16 04:32:47 +00:00
|
|
|
return sprout::string_to_float<double>(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// stold
|
|
|
|
//
|
|
|
|
template<typename Elem, std::size_t N, typename Traits>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR long double
|
|
|
|
stold(sprout::basic_string<Elem, N, Traits> const& str, std::size_t* idx) {
|
2012-04-16 04:32:47 +00:00
|
|
|
return sprout::string_to_float<long double>(str, idx);
|
|
|
|
}
|
|
|
|
template<typename Elem, std::size_t N, typename Traits>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR long double
|
|
|
|
stold(sprout::basic_string<Elem, N, Traits> const& str) {
|
2012-04-16 04:32:47 +00:00
|
|
|
return sprout::string_to_float<long double>(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// from_string
|
|
|
|
//
|
|
|
|
template<
|
2012-10-05 15:58:56 +00:00
|
|
|
typename FloatType, typename Elem, std::size_t N, typename Traits,
|
2012-04-16 04:32:47 +00:00
|
|
|
typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler
|
|
|
|
>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR FloatType
|
|
|
|
from_string(sprout::basic_string<Elem, N, Traits> const& str, std::size_t* idx) {
|
2012-04-16 04:32:47 +00:00
|
|
|
return sprout::string_to_float<FloatType>(str, idx);
|
|
|
|
}
|
|
|
|
template<
|
2012-10-05 15:58:56 +00:00
|
|
|
typename FloatType, typename Elem, std::size_t N, typename Traits,
|
2012-04-16 04:32:47 +00:00
|
|
|
typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler
|
|
|
|
>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR FloatType
|
|
|
|
from_string(sprout::basic_string<Elem, N, Traits> const& str) {
|
2012-04-16 04:32:47 +00:00
|
|
|
return sprout::string_to_float<FloatType>(str);
|
|
|
|
}
|
|
|
|
} // namespace sprout
|
|
|
|
|
|
|
|
#endif // #ifndef SPROUT_STRING_STRING_TO_FLOAT_HPP
|