diff --git a/CMakeLists.txt b/CMakeLists.txt index 329ff0d..a86dfc2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,5 @@ -cmake_minimum_required(VERSION 3.10) +cmake_minimum_required(VERSION 3.14) + project(fast_float VERSION 8.0.2 LANGUAGES CXX) set(FASTFLOAT_CXX_STANDARD 11 CACHE STRING "the C++ standard to use for fastfloat") diff --git a/README.md b/README.md index 8770ac4..cf0d9f3 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,7 @@ Example: ```C++ #include "fast_float/fast_float.h" #include +#include int main() { std::string input = "3.1416 xyz "; @@ -68,6 +69,25 @@ int main() { } ``` +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: + +```cpp +#include "fast_float/fast_float.h" +#include +#include + +int main() { + std::string input = "3.1416 xyz "; + double result; + if(auto answer = fast_float::from_chars(input.data(), input.data() + input.size(), result)) { + std::cout << "parsed the number " << result << std::endl; + return EXIT_SUCCESS; + } + std::cerr << "failed to parse " << result << std::endl; + return EXIT_FAILURE; +} +``` + You can parse delimited numbers: ```C++ diff --git a/include/fast_float/float_common.h b/include/fast_float/float_common.h index 8fd0560..1d249c5 100644 --- a/include/fast_float/float_common.h +++ b/include/fast_float/float_common.h @@ -58,6 +58,11 @@ enum class chars_format : uint64_t { template struct from_chars_result_t { UC const *ptr; std::errc ec; + + // https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p2497r0.html + constexpr explicit operator bool() const noexcept { + return ec == std::errc(); + } }; using from_chars_result = from_chars_result_t; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 939385f..36925f0 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -22,12 +22,8 @@ endif() # FetchContent_MakeAvailable() was only introduced in 3.14 # https://cmake.org/cmake/help/v3.14/release/3.14.html#modules -# FetchContent_MakeAvailable(doctest) if (NOT SYSTEM_DOCTEST) - FetchContent_GetProperties(doctest) - if(NOT doctest_POPULATED) - FetchContent_MakeAvailable(doctest) - endif() + FetchContent_MakeAvailable(doctest) endif() add_library(supplemental-data INTERFACE) @@ -76,7 +72,7 @@ endif() if (FASTFLOAT_SUPPLEMENTAL_TESTS) target_compile_definitions(basictest PRIVATE FASTFLOAT_SUPPLEMENTAL_TESTS) endif() - +fast_float_add_cpp_test(p2497) fast_float_add_cpp_test(long_test) fast_float_add_cpp_test(powersoffive_hardround) fast_float_add_cpp_test(string_test) diff --git a/tests/p2497.cpp b/tests/p2497.cpp new file mode 100644 index 0000000..fec5133 --- /dev/null +++ b/tests/p2497.cpp @@ -0,0 +1,16 @@ +#include "fast_float/fast_float.h" + +#include +#include + +int main() { + std::string input = "3.1416 xyz "; + double result; + if (auto answer = fast_float::from_chars( + input.data(), input.data() + input.size(), result)) { + std::cout << "parsed the number " << result << std::endl; + return EXIT_SUCCESS; + } + std::cerr << "failed to parse " << result << std::endl; + return EXIT_FAILURE; +} \ No newline at end of file