Gemmi C++ API
Loading...
Searching...
No Matches
atof.hpp
Go to the documentation of this file.
1// Copyright 2020 Global Phasing Ltd.
2//
3// Functions that convert string to floating-point number ignoring locale.
4// Simple wrappers around fastfloat::from_chars().
5
6#ifndef GEMMI_ATOF_HPP_
7#define GEMMI_ATOF_HPP_
8
9#include "atox.hpp" // for is_space
10#include "third_party/fast_float.h"
11
12namespace gemmi {
13
14using fast_float::from_chars_result;
15
16inline from_chars_result fast_from_chars(const char* start, const char* end, double& d) {
17 while (start < end && is_space(*start))
18 ++start;
19 if (start < end && *start == '+')
20 ++start;
21 return fast_float::from_chars(start, end, d);
22}
23
24inline from_chars_result fast_from_chars(const char* start, double& d) {
25 while (is_space(*start))
26 ++start;
27 if (*start == '+')
28 ++start;
29 return fast_float::from_chars(start, start + std::strlen(start), d);
30}
31
32inline double fast_atof(const char* p, const char** endptr=nullptr) {
33 double d = 0;
34 auto result = fast_from_chars(p, d);
35 if (endptr)
36 *endptr = result.ptr;
37 return d;
38}
39
40} // namespace gemmi
41#endif
double fast_atof(const char *p, const char **endptr=nullptr)
Definition atof.hpp:32
from_chars_result fast_from_chars(const char *start, const char *end, double &d)
Definition atof.hpp:16
bool is_space(char c)
Definition atox.hpp:23