1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP___CHARCONV_FROM_CHARS_FLOATING_POINT_H
11#define _LIBCPP___CHARCONV_FROM_CHARS_FLOATING_POINT_H
12
13#include <__assert>
14#include <__charconv/chars_format.h>
15#include <__charconv/from_chars_result.h>
16#include <__config>
17#include <__cstddef/ptrdiff_t.h>
18#include <__system_error/errc.h>
19
20#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
21# pragma GCC system_header
22#endif
23
24_LIBCPP_PUSH_MACROS
25#include <__undef_macros>
26
27_LIBCPP_BEGIN_NAMESPACE_STD
28
29#if _LIBCPP_STD_VER >= 17
30
31template <class _Fp>
32struct __from_chars_result {
33 _Fp __value;
34 ptrdiff_t __n;
35 errc __ec;
36};
37
38_LIBCPP_BEGIN_EXPLICIT_ABI_ANNOTATIONS
39template <class _Fp>
40_LIBCPP_EXPORTED_FROM_ABI __from_chars_result<_Fp> __from_chars_floating_point(
41 _LIBCPP_NOESCAPE const char* __first, _LIBCPP_NOESCAPE const char* __last, chars_format __fmt);
42
43extern template __from_chars_result<float> __from_chars_floating_point(
44 _LIBCPP_NOESCAPE const char* __first, _LIBCPP_NOESCAPE const char* __last, chars_format __fmt);
45
46extern template __from_chars_result<double> __from_chars_floating_point(
47 _LIBCPP_NOESCAPE const char* __first, _LIBCPP_NOESCAPE const char* __last, chars_format __fmt);
48_LIBCPP_END_EXPLICIT_ABI_ANNOTATIONS
49
50template <class _Fp>
51_LIBCPP_HIDE_FROM_ABI from_chars_result
52__from_chars(const char* __first, const char* __last, _Fp& __value, chars_format __fmt) {
53 __from_chars_result<_Fp> __r = std::__from_chars_floating_point<_Fp>(__first, __last, __fmt);
54 if (__r.__ec != errc::invalid_argument)
55 __value = __r.__value;
56 return {__first + __r.__n, __r.__ec};
57}
58
59_LIBCPP_AVAILABILITY_FROM_CHARS_FLOATING_POINT _LIBCPP_HIDE_FROM_ABI inline from_chars_result
60from_chars(const char* __first, const char* __last, float& __value, chars_format __fmt = chars_format::general) {
61 return std::__from_chars<float>(__first, __last, __value, __fmt);
62}
63
64_LIBCPP_AVAILABILITY_FROM_CHARS_FLOATING_POINT _LIBCPP_HIDE_FROM_ABI inline from_chars_result
65from_chars(const char* __first, const char* __last, double& __value, chars_format __fmt = chars_format::general) {
66 return std::__from_chars<double>(__first, __last, __value, __fmt);
67}
68
69#endif // _LIBCPP_STD_VER >= 17
70
71_LIBCPP_END_NAMESPACE_STD
72
73_LIBCPP_POP_MACROS
74
75#endif // _LIBCPP___CHARCONV_FROM_CHARS_FLOATING_POINT_H
76