OpenSWMM Engine  6.0.0-alpha.1
Data-oriented, plugin-extensible SWMM Engine (6.0.0-alpha.1)
Loading...
Searching...
No Matches
charconv_compat.hpp
Go to the documentation of this file.
1
12#ifndef OPENSWMM_CHARCONV_COMPAT_HPP
13#define OPENSWMM_CHARCONV_COMPAT_HPP
14
15#include <charconv>
16#include <cstdlib>
17#include <system_error>
18#include <string>
19
20namespace openswmm {
21
29inline std::from_chars_result from_chars_double(const char* first,
30 const char* last,
31 double& value) noexcept {
32#if defined(__cpp_lib_to_chars) && __cpp_lib_to_chars >= 201611L
33 return std::from_chars(first, last, value);
34#else
35 if (first == last) {
36 return {first, std::errc::invalid_argument};
37 }
38 // std::strtod needs a null-terminated string
39 std::string tmp(first, last);
40 char* end = nullptr;
41 double v = std::strtod(tmp.c_str(), &end);
42 if (end == tmp.c_str()) {
43 return {first, std::errc::invalid_argument};
44 }
45 value = v;
46 return {first + (end - tmp.c_str()), std::errc{}};
47#endif
48}
49
50} // namespace openswmm
51
52#endif // OPENSWMM_CHARCONV_COMPAT_HPP
Definition Controls.cpp:24
std::from_chars_result from_chars_double(const char *first, const char *last, double &value) noexcept
Locale-independent parse of a double from a character range.
Definition charconv_compat.hpp:29