| 133 | |
| 134 | template<typename T> |
| 135 | inline T parse(std::string_view input) |
| 136 | { |
| 137 | static_assert(!std::is_same_v<T, float> && !std::is_same_v<T, double>, "Float support is currently unsupported"); |
| 138 | |
| 139 | if (input.empty()) |
| 140 | { |
| 141 | throw std::invalid_argument("Input is empty"); |
| 142 | } |
| 143 | T result; |
| 144 | auto [ptr, ec] = std::from_chars(input.data(), input.data() + input.size(), result); |
| 145 | if (ec == std::errc::invalid_argument) |
| 146 | { |
| 147 | throw std::invalid_argument("Invalid argument in conversion"); |
| 148 | } |
| 149 | if (ec == std::errc::result_out_of_range) |
| 150 | { |
| 151 | throw std::out_of_range("Result out of range"); |
| 152 | } |
| 153 | if (ec != std::errc()) |
| 154 | { |
| 155 | throw std::runtime_error("Conversion error"); |
| 156 | } |
| 157 | if (ptr != input.data() + input.size()) |
| 158 | { |
| 159 | throw std::invalid_argument("Trailing characters after number"); |
| 160 | } |
| 161 | return result; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Returns string representation of a hexadecimal input, such as SHA256 hash |
no test coverage detected