This commit is contained in:
Daniel Lemire 2026-02-24 10:56:35 -05:00
parent df5db2cfbf
commit 6eb3dcdcd3
2 changed files with 16 additions and 17 deletions

View File

@ -2,13 +2,12 @@
#define __FAST_FLOAT_STRTOD_H__
#if defined(__cplusplus)
extern "C"
{
extern "C" {
#endif
/**
* @brief Convert a string to a double using the fast_float library. This is
* a C-compatible wrapper around the fast_float parsing functionality, designed to
* mimic the behavior of the standard strtod function.
* a C-compatible wrapper around the fast_float parsing functionality, designed
* to mimic the behavior of the standard strtod function.
*
* This function parses the initial portion of the null-terminated string `nptr`
* and converts it to a `double`, similar to the standard `strtod` function but
@ -23,7 +22,7 @@ extern "C"
* to the beginning of the string.
* @return The converted double value on success, or 0.0 on failure.
*/
double fast_float_strtod(const char *in, char **out);
double fast_float_strtod(const char *in, char **out);
#if defined(__cplusplus)
}

View File

@ -5,20 +5,20 @@
#include <system_error>
extern "C" double fast_float_strtod(const char *nptr, char **endptr) {
double result = 0.0;
double result = 0.0;
// Parse the string using fast_float's from_chars function
auto parse_result = fast_float::from_chars(nptr, nptr + strlen(nptr), result);
// Parse the string using fast_float's from_chars function
auto parse_result = fast_float::from_chars(nptr, nptr + strlen(nptr), result);
// Check if parsing encountered an error
if (parse_result.ec != std::errc{}) {
errno = EINVAL;
}
// Check if parsing encountered an error
if (parse_result.ec != std::errc{}) {
errno = EINVAL;
}
// Update endptr if provided
if (endptr != nullptr) {
*endptr = const_cast<char*>(parse_result.ptr);
}
// Update endptr if provided
if (endptr != nullptr) {
*endptr = const_cast<char *>(parse_result.ptr);
}
return result;
return result;
}