26#ifndef GUL17_TO_NUMBER_H_
27#define GUL17_TO_NUMBER_H_
53constexpr inline bool is_digit(
char c)
noexcept
55 return c >=
'0' &&
c <=
'9';
60 if (
c >=
'0' &&
c <=
'9')
62 if (
c >=
'a' &&
c <=
'z')
64 if (
c >=
'A' &&
c <=
'Z')
71template <
typename NumberType,
bool count_magnitude = false>
126 str.remove_prefix(1);
129 str.remove_prefix(1);
152template <
typename NumberType>
154 typename std::conditional<
155 (std::numeric_limits<std::uint64_t>::digits10 >= std::numeric_limits<NumberType>::digits10),
180template <
typename NumberType>
181constexpr inline std::optional<NumberType>
to_normalized_float(std::string_view
i1, std::string_view
i2)
noexcept
183 static_assert(std::numeric_limits<FloatConversionIntType<NumberType>>
::digits10
184 >= std::numeric_limits<NumberType>::digits10,
185 "FloatConversionIntType is too small for NumberType");
187 i1 =
i1.substr(0, std::min(
i1.length(),
189 i2 =
i2.substr(0, std::min(
i2.length(),
196 if (
not i2.empty()) {
198 if (
not f2.has_value())
202 if (
not i1.empty()) {
205 if (
not f1.has_value())
213template <
typename NumberType>
216 std::optional<NumberType> result;
238template <
typename NumberType>
247 return {
true, std::make_optional(std::numeric_limits<NumberType>::infinity()) };
250 return {
true, std::make_optional(std::numeric_limits<NumberType>::infinity()) };
256 return {
true, std::make_optional(std::numeric_limits<NumberType>::quiet_NaN()) };
259 str.remove_prefix(4);
260 str.remove_suffix(1);
261 while (
str.length()) {
264 str.remove_prefix(1);
267 return {
true, std::make_optional(std::numeric_limits<NumberType>::quiet_NaN()) };
269 return {
false, {} };
303template <
typename NumberType>
311 auto e_pos =
str.find_first_of(
"eE");
312 if (
e_pos != std::string_view::npos)
368 using CalcType = std::conditional_t<
369 std::greater<>()(
sizeof(
NumberType),
sizeof(
double)),
396template <
typename NumberType>
412 catch (
const std::exception &)
499template <
typename NumberType>
500constexpr inline std::enable_if_t<std::is_integral<NumberType>::value
and
501 std::is_unsigned<NumberType>::value,
502 std::optional<NumberType>>
505 return detail::to_unsigned_integer<NumberType>(
str);
509template <
typename NumberType>
510constexpr inline std::enable_if_t<std::is_integral<NumberType>::value
and
511 std::is_signed<NumberType>::value,
512 std::optional<NumberType>>
518 if (
str.front() ==
'-')
520 using UnsignedT = std::make_unsigned_t<NumberType>;
522 static_cast<UnsignedT>(std::numeric_limits<NumberType>::max()) + 1;
524 str.remove_prefix(1);
526 auto result = detail::to_unsigned_integer<UnsignedT>(
str);
531 return std::numeric_limits<NumberType>::lowest();
538 return detail::to_unsigned_integer<NumberType>(
str);
542template <
typename NumberType>
543constexpr inline std::enable_if_t<std::is_floating_point<NumberType>::value,
544 std::optional<NumberType>>
558# pragma warning( pop )
563 return detail::strtold_wrapper<NumberType>(
str);
566 if (
str.front() ==
'-')
568 str.remove_prefix(1);
569 auto result = detail::to_unsigned_float<NumberType>(
str);
575 return detail::to_unsigned_float<NumberType>(
str);
580constexpr inline std::optional<bool> to_number<bool>(std::string_view
str)
noexcept
582 if (
str.length() == 1) {
auto constexpr bit_set(unsigned bit) noexcept -> ReturnT
Set a bit in an integral type.
Definition bit_manip.h:124
constexpr bool equals_nocase(std::string_view str1, std::string_view str2) noexcept
Determine whether a string is equal to another one, making no distinction between upper and lower cas...
Definition substring_checks.h:169
constexpr bool starts_with_nocase(std::string_view str, std::string_view prefix) noexcept
Determine whether a string starts with another string.
Definition substring_checks.h:317
constexpr std::enable_if_t< std::is_integral< NumberType >::value and std::is_unsigned< NumberType >::value, std::optional< NumberType > > to_number(std::string_view str) noexcept
Convert an ASCII std::string_view into a number.
Definition to_number.h:503
Definition of macros used internally by GUL.
Namespace gul17 contains all functions and classes of the General Utility Library.
Definition doxygen.h:29
Definition of contains(), ends_with(), and starts_with().