| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- /**
- * Utility functions for managing numeric types - such as converting between
- * floating-point and integer types.
- *
- * None of these are particularly complex functions, but storing them in a
- * single header with descriptive names helps the reader quickly recognize what
- * is being done.
- */
- #pragma once
- #include <charconv>
- #include <cmath>
- #include <limits>
- #include <stdexcept>
- #include <string_view>
- #include <system_error>
- namespace jvalidate::detail {
- /**
- * @brief Determine if a floating point number is actually an integer (in the
- * mathematical sense).
- */
- inline bool is_json_integer(double number) { return std::floor(number) == number; }
- /**
- * @brief Determine if a floating point number is actually an integer, and
- * actually fits in the 64-bit integer type that we use for JSON Integer.
- */
- inline bool fits_in_integer(double number) {
- static constexpr double g_int_max = std::numeric_limits<int64_t>::max();
- static constexpr double g_int_min = std::numeric_limits<int64_t>::min();
- return is_json_integer(number) && number <= g_int_max && number >= g_int_min;
- }
- /**
- * @brief Determine if an unsigned integer fits into a signed integer
- */
- inline bool fits_in_integer(uint64_t number) { return (number & 0x8000'0000'0000'0000) == 0; }
- template <std::integral I> I from_str(std::string_view str, int base = 10) {
- I rval;
- auto [end, ec] = std::from_chars(str.begin(), str.end(), rval, base);
- if (ec != std::errc{}) {
- throw std::runtime_error(std::make_error_code(ec).message());
- }
- if (end != str.end()) {
- throw std::runtime_error("NaN: " + std::string(str));
- }
- return rval;
- }
- }
|