26#ifndef GUL17_TO_NUMBER_H_
27#define GUL17_TO_NUMBER_H_
54constexpr inline bool is_digit(
char c)
noexcept
56 return c >=
'0' &&
c <=
'9';
61 if (
c >=
'0' &&
c <=
'9')
63 if (
c >=
'a' &&
c <=
'z')
65 if (
c >=
'A' &&
c <=
'Z')
72template <
typename NumberType,
bool count_magnitude = false>
127 str.remove_prefix(1);
130 str.remove_prefix(1);
153template <
typename NumberType>
155 typename std::conditional<
156 (std::numeric_limits<std::uint64_t>::digits10 >= std::numeric_limits<NumberType>::digits10),
181template <
typename NumberType>
182constexpr inline std::optional<NumberType>
to_normalized_float(std::string_view
i1, std::string_view
i2)
noexcept
184 static_assert(std::numeric_limits<FloatConversionIntType<NumberType>>
::digits10
185 >= std::numeric_limits<NumberType>::digits10,
186 "FloatConversionIntType is too small for NumberType");
188 i1 =
i1.substr(0, std::min(
i1.length(),
190 i2 =
i2.substr(0, std::min(
i2.length(),
197 if (
not i2.empty()) {
199 if (
not f2.has_value())
203 if (
not i1.empty()) {
206 if (
not f1.has_value())
214template <
typename NumberType>
217 std::optional<NumberType> result;
239template <
typename NumberType>
248 return {
true, std::make_optional(std::numeric_limits<NumberType>::infinity()) };
251 return {
true, std::make_optional(std::numeric_limits<NumberType>::infinity()) };
257 return {
true, std::make_optional(std::numeric_limits<NumberType>::quiet_NaN()) };
260 str.remove_prefix(4);
261 str.remove_suffix(1);
262 while (
str.length()) {
265 str.remove_prefix(1);
268 return {
true, std::make_optional(std::numeric_limits<NumberType>::quiet_NaN()) };
270 return {
false, {} };
304template <
typename NumberType>
312 auto e_pos =
str.find_first_of(
"eE");
313 if (
e_pos != std::string_view::npos)
369 using CalcType = std::conditional_t<
370 std::greater<>()(
sizeof(
NumberType),
sizeof(
double)),
397template <
typename NumberType>
413 catch (
const std::exception &)
500template <
typename NumberType>
501constexpr inline std::enable_if_t<std::is_integral<NumberType>::value
and
502 std::is_unsigned<NumberType>::value,
503 std::optional<NumberType>>
506 return detail::to_unsigned_integer<NumberType>(
str);
510template <
typename NumberType>
511constexpr inline std::enable_if_t<std::is_integral<NumberType>::value
and
512 std::is_signed<NumberType>::value,
513 std::optional<NumberType>>
519 if (
str.front() ==
'-')
521 using UnsignedT = std::make_unsigned_t<NumberType>;
523 static_cast<UnsignedT>(std::numeric_limits<NumberType>::max()) + 1;
525 str.remove_prefix(1);
527 auto result = detail::to_unsigned_integer<UnsignedT>(
str);
532 return std::numeric_limits<NumberType>::lowest();
539 return detail::to_unsigned_integer<NumberType>(
str);
543template <
typename NumberType>
544constexpr inline std::enable_if_t<std::is_floating_point<NumberType>::value,
545 std::optional<NumberType>>
559# pragma warning( pop )
564 return detail::strtold_wrapper<NumberType>(
str);
567 if (
str.front() ==
'-')
569 str.remove_prefix(1);
570 auto result = detail::to_unsigned_float<NumberType>(
str);
576 return detail::to_unsigned_float<NumberType>(
str);
581constexpr inline std::optional<bool> to_number<bool>(std::string_view
str)
noexcept
583 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:504
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().