diff options
| author | Michael Jones <michaelrj@google.com> | 2023-02-17 11:14:37 -0800 |
|---|---|---|
| committer | Michael Jones <michaelrj@google.com> | 2023-02-28 10:50:43 -0800 |
| commit | cb3c41c285fc76f21162c67503de32f83f278fae (patch) | |
| tree | 56036d2c3c36e133206d2ede980d1474b9f94444 /libc/src/stdlib/strtod.cpp | |
| parent | 003078b62d8d40fc000462a97c3b70e01cbe4458 (diff) | |
[libc] Refactor string to float return values
The internal implementation of the string to float function previously
used pointer arguments for returning several values. Additionally it
set errno in several unexpected places. Now all of that goes through
return structs. For readability I also moved the function away from raw
pointer arithmetic towards proper indexing. I also added support for
rounding modes.
Reviewed By: sivachandra
Differential Revision: https://reviews.llvm.org/D144597
Diffstat (limited to 'libc/src/stdlib/strtod.cpp')
| -rw-r--r-- | libc/src/stdlib/strtod.cpp | 10 |
1 files changed, 9 insertions, 1 deletions
diff --git a/libc/src/stdlib/strtod.cpp b/libc/src/stdlib/strtod.cpp index 754918c36789..0690cfdec1e9 100644 --- a/libc/src/stdlib/strtod.cpp +++ b/libc/src/stdlib/strtod.cpp @@ -9,12 +9,20 @@ #include "src/stdlib/strtod.h" #include "src/__support/common.h" #include "src/__support/str_to_float.h" +#include <errno.h> namespace __llvm_libc { LLVM_LIBC_FUNCTION(double, strtod, (const char *__restrict str, char **__restrict str_end)) { - return internal::strtofloatingpoint<double>(str, str_end); + auto result = internal::strtofloatingpoint<double>(str); + if (result.has_error()) + errno = result.error; + + if (str_end != NULL) + *str_end = const_cast<char *>(str + result.parsed_len); + + return result.value; } } // namespace __llvm_libc |
