Add from_chars floating point docs

This commit is contained in:
Matt Borland
2023-05-01 16:20:10 +02:00
parent ba51e5928c
commit b09cd1f50a
2 changed files with 38 additions and 8 deletions

View File

@@ -23,6 +23,9 @@ template <typename Integral>
BOOST_CXX14_CONSTEXPR from_chars_result from_chars(const char* first, const char* last, Integral& value, int base = 10) noexcept;
BOOST_CXX14_CONSTEXPR from_chars_result from_chars<bool>(const char* first, const char* last, bool& value, int base) = delete;
template <typename Real>
from_chars_result from_chars(const char* first, const char* last, Real& value, boost::charconv::chars_format fmt = boost::charconv::chars_format::general);
----
== from_chars_result
@@ -37,18 +40,22 @@ BOOST_CXX14_CONSTEXPR from_chars_result from_chars<bool>(const char* first, cons
== from_chars
* first, last - valid range to parse
* value - where the output is stored upon successful parsing
* base - the integer base to use. Must be between 2 and 36 inclusive
* base (integer only) - the integer base to use. Must be between 2 and 36 inclusive
* fmt (floating point only) - The format of the buffer. See xref:chars_format.adoc[chars_format overview] for description.
=== from_chars for integral types
* All built-in integral types are allowed except bool which is deleted
* These functions have been tested to support `__int128` and `unsigned __int128`
* from_chars for integral types is constexpr when compiled using `-std=c++14` or newer
** One known exception is GCC 5 which does not support constexpr comparison of const char*.
== from_chars for integral types
* All built-in integral types are allowed except bool which is deleted
* These functions have been tested to support `__int128` and `unsigned __int128` when compiling with `-std=gnu++11` or newer
=== from_chars for floating point types
== Examples
=== Basic usage
[source, c++]
==== Integral
----
const char* buffer = "42";
int v = 0;
@@ -56,9 +63,18 @@ from_chars_result r = boost::charconv::from_chars(buffer, buffer + std::strlen(b
assert(r.ec == 0);
assert(v == 42);
----
==== Floating Point
----
const char* buffer = "1.2345"
double v = 0;
auto r = boost::charconv::from_chars(buffer, buffer + std::strlen(buffer), v);
assert(r.ec == 0);
assert(v == 1.2345);
----
=== Hexadecimal
[source, c++]
==== Integral
----
const char* buffer = "2a";
unsigned v = 0;
@@ -66,18 +82,31 @@ auto r = boost::charconv::from_chars(buffer, buffer + std::strlen(buffer), v, 16
assert(r.ec == 0);
assert(v == 42);
----
==== Floating Point
----
const char* buffer = "1.3a2bp-10";
double v = 0;
auto r = boost::charconv::from_chars(buffer, buffer + std::strlen(buffer), v, boost::charconv::chars_format::hex);
assert(r.ec == 0);
assert(v == 8.0427e-18);
----
=== EINVAL
[source, c++]
----
const char* buffer = "-123";
unsigned v = 0;
auto r = boost::charconv::from_chars(buffer, buffer + std::strlen(buffer), v);
assert(r.ec, EINVAL);
assert(v == 0);
assert(r.ec == EINVAL);
----
----
const char* buffer = "-1.573e-3";
double v = 0;
auto r = boost::charconv::from_chars(buffer, buffer + std::strlen(buffer), v, boost::charconv::chars_format::fixed);
assert(r.ec == EINVAL);
----
In the event of EINVAL v is not set by `from_chars`
=== ERANGE
[source, c++]
----

View File

@@ -42,7 +42,8 @@ to_chars_result(char* first, char* last, Real value, boost::charconv::chars_form
* first, last - pointers to the character buffer
* value - the value to be paresed into the buffer
* base (integer only) - the integer base to use. Must be between 2 and 36 inclusive
* fmt (float only) - the floating point format
* fmt (float only) - the floating point format to use.
See xref:chars_format.adoc[chars_format overview] for description.
* precision (float only) - the number of decimal places required
=== to_chars for integral types