Skip to content

Commit 4bb6fd1

Browse files
authored
Merge pull request #314 from fastfloat/P2497R0
implementation of p2497
2 parents ce511cb + 42db9ac commit 4bb6fd1

File tree

5 files changed

+45
-7
lines changed

5 files changed

+45
-7
lines changed

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
cmake_minimum_required(VERSION 3.10)
1+
cmake_minimum_required(VERSION 3.14)
2+
23

34
project(fast_float VERSION 8.0.2 LANGUAGES CXX)
45
set(FASTFLOAT_CXX_STANDARD 11 CACHE STRING "the C++ standard to use for fastfloat")

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ Example:
5757
```C++
5858
#include "fast_float/fast_float.h"
5959
#include <iostream>
60+
#include <string>
6061
6162
int main() {
6263
std::string input = "3.1416 xyz ";
@@ -68,6 +69,25 @@ int main() {
6869
}
6970
```
7071

72+
Though the C++17 standard has you do a comparison with `std::errc()` to check whether the conversion worked, you can avoid it by casting the result to a `bool` like so:
73+
74+
```cpp
75+
#include "fast_float/fast_float.h"
76+
#include <iostream>
77+
#include <string>
78+
79+
int main() {
80+
std::string input = "3.1416 xyz ";
81+
double result;
82+
if(auto answer = fast_float::from_chars(input.data(), input.data() + input.size(), result)) {
83+
std::cout << "parsed the number " << result << std::endl;
84+
return EXIT_SUCCESS;
85+
}
86+
std::cerr << "failed to parse " << result << std::endl;
87+
return EXIT_FAILURE;
88+
}
89+
```
90+
7191
You can parse delimited numbers:
7292

7393
```C++

include/fast_float/float_common.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ enum class chars_format : uint64_t {
5858
template <typename UC> struct from_chars_result_t {
5959
UC const *ptr;
6060
std::errc ec;
61+
62+
// https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p2497r0.html
63+
constexpr explicit operator bool() const noexcept {
64+
return ec == std::errc();
65+
}
6166
};
6267

6368
using from_chars_result = from_chars_result_t<char>;

tests/CMakeLists.txt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,8 @@ endif()
2222

2323
# FetchContent_MakeAvailable() was only introduced in 3.14
2424
# https://cmake.org/cmake/help/v3.14/release/3.14.html#modules
25-
# FetchContent_MakeAvailable(doctest)
2625
if (NOT SYSTEM_DOCTEST)
27-
FetchContent_GetProperties(doctest)
28-
if(NOT doctest_POPULATED)
29-
FetchContent_MakeAvailable(doctest)
30-
endif()
26+
FetchContent_MakeAvailable(doctest)
3127
endif()
3228

3329
add_library(supplemental-data INTERFACE)
@@ -76,7 +72,7 @@ endif()
7672
if (FASTFLOAT_SUPPLEMENTAL_TESTS)
7773
target_compile_definitions(basictest PRIVATE FASTFLOAT_SUPPLEMENTAL_TESTS)
7874
endif()
79-
75+
fast_float_add_cpp_test(p2497)
8076
fast_float_add_cpp_test(long_test)
8177
fast_float_add_cpp_test(powersoffive_hardround)
8278
fast_float_add_cpp_test(string_test)

tests/p2497.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include "fast_float/fast_float.h"
2+
3+
#include <iostream>
4+
#include <string>
5+
6+
int main() {
7+
std::string input = "3.1416 xyz ";
8+
double result;
9+
if (auto answer = fast_float::from_chars(
10+
input.data(), input.data() + input.size(), result)) {
11+
std::cout << "parsed the number " << result << std::endl;
12+
return EXIT_SUCCESS;
13+
}
14+
std::cerr << "failed to parse " << result << std::endl;
15+
return EXIT_FAILURE;
16+
}

0 commit comments

Comments
 (0)