-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumber_parse.cpp
More file actions
50 lines (42 loc) · 1.36 KB
/
Copy pathnumber_parse.cpp
File metadata and controls
50 lines (42 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "pj_base/number_parse.hpp"
#include <cerrno>
#include <cstdlib>
#include <string>
#include <system_error>
#include "fast_float/fast_float.h"
namespace PJ::detail {
namespace {
template <typename T>
[[nodiscard]] std::optional<T> parseWithFastFloat(std::string_view text) {
T out{};
const char* begin = text.data();
const char* end = begin + text.size();
const auto result = fast_float::from_chars(begin, end, out);
if (result.ec != std::errc{} || result.ptr != end) {
return std::nullopt;
}
return out;
}
} // namespace
std::optional<float> parseFloatImpl(std::string_view text) {
return parseWithFastFloat<float>(text);
}
std::optional<double> parseDoubleImpl(std::string_view text) {
return parseWithFastFloat<double>(text);
}
// fast_float does not implement long double, so this branch falls back to
// std::strtold. strtold is locale-dependent (LC_NUMERIC), but long double
// has no in-tree call sites today; the fallback exists only to keep the
// template's compile-time surface complete.
std::optional<long double> parseLongDoubleImpl(std::string_view text) {
const std::string buffer(text);
const char* begin = buffer.c_str();
char* last = nullptr;
errno = 0;
const long double out = std::strtold(begin, &last);
if (errno != 0 || last != begin + buffer.size()) {
return std::nullopt;
}
return out;
}
} // namespace PJ::detail