| 163 | */ |
| 164 | template <typename T> |
| 165 | std::optional<T> ToIntegral(const std::string& str) |
| 166 | { |
| 167 | static_assert(std::is_integral<T>::value); |
| 168 | T result; |
| 169 | const auto [first_nonmatching, error_condition] = std::from_chars(str.data(), str.data() + str.size(), result); |
| 170 | if (first_nonmatching != str.data() + str.size() || error_condition != std::errc{}) { |
| 171 | return std::nullopt; |
| 172 | } |
| 173 | return result; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Convert string to signed 32-bit integer with strict parse error feedback. |