From 81b8306c5f42de6b2b33fc45db5a0244296d8354 Mon Sep 17 00:00:00 2001 From: Daniel Lemire Date: Mon, 19 May 2025 18:08:36 -0400 Subject: [PATCH 1/5] implementation of https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p2497r0.html --- CMakeLists.txt | 2 +- include/fast_float/float_common.h | 5 +++++ tests/CMakeLists.txt | 21 ++++++--------------- 3 files changed, 12 insertions(+), 16 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8a1c9a32..fadc27b6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.9) +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/include/fast_float/float_common.h b/include/fast_float/float_common.h index 8fd05602..1d249c52 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 c4e43b21..3ffa8ffe 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -10,7 +10,7 @@ option(FASTFLOAT_SUPPLEMENTAL_TESTS "Run supplemental tests" ON) if (NOT SYSTEM_DOCTEST) FetchContent_Declare(doctest GIT_REPOSITORY https://github.com/onqtam/doctest.git - GIT_TAG v2.4.11) + GIT_TAG v2.4.12) else () find_package(doctest REQUIRED) endif() @@ -23,24 +23,15 @@ 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_Populate(doctest) - add_subdirectory(${doctest_SOURCE_DIR} ${doctest_BINARY_DIR}) - endif() + FetchContent_MakeAvailable(doctest) endif() add_library(supplemental-data INTERFACE) if (FASTFLOAT_SUPPLEMENTAL_TESTS) - FetchContent_GetProperties(supplemental_test_files) - if(NOT supplemental_test_files_POPULATED) - message(STATUS "Supplemental tests enabled. Retrieving test files.") - FetchContent_Populate(supplemental_test_files) - message(STATUS "Supplemental test files retrieved.") - add_subdirectory(${supplemental_test_files_SOURCE_DIR} ${supplemental_test_files_BINARY_DIR}) - endif() + message(STATUS "Supplemental tests enabled. Retrieving test files.") + FetchContent_MakeAvailable(supplemental_test_files) + message(STATUS "Supplemental test files retrieved.") target_compile_definitions(supplemental-data INTERFACE SUPPLEMENTAL_TEST_DATA_DIR="${supplemental_test_files_BINARY_DIR}/data") endif() @@ -82,7 +73,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) From 0458c20061669b63c0d52cbc2d95bf9dfaed8a25 Mon Sep 17 00:00:00 2001 From: Daniel Lemire Date: Mon, 19 May 2025 18:09:34 -0400 Subject: [PATCH 2/5] adding missing file --- tests/p2497.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 tests/p2497.cpp diff --git a/tests/p2497.cpp b/tests/p2497.cpp new file mode 100644 index 00000000..7e085a59 --- /dev/null +++ b/tests/p2497.cpp @@ -0,0 +1,15 @@ +#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 From 447ee0bc822b48f2e3a4fcd407da6789ef58108c Mon Sep 17 00:00:00 2001 From: Daniel Lemire Date: Mon, 19 May 2025 18:12:35 -0400 Subject: [PATCH 3/5] updating documentation --- README.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/README.md b/README.md index 16dee531..e12fc104 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++ From a1e272f5151e6258d11e39d5a8d26c58a171df46 Mon Sep 17 00:00:00 2001 From: Daniel Lemire Date: Mon, 19 May 2025 18:16:14 -0400 Subject: [PATCH 4/5] lint --- .github/workflows/ubuntu20-cxx20.yml | 19 ------------------- .github/workflows/ubuntu20-fastmath.yml | 16 ---------------- .github/workflows/ubuntu20.yml | 21 --------------------- tests/p2497.cpp | 3 ++- 4 files changed, 2 insertions(+), 57 deletions(-) delete mode 100644 .github/workflows/ubuntu20-cxx20.yml delete mode 100644 .github/workflows/ubuntu20-fastmath.yml delete mode 100644 .github/workflows/ubuntu20.yml diff --git a/.github/workflows/ubuntu20-cxx20.yml b/.github/workflows/ubuntu20-cxx20.yml deleted file mode 100644 index ff226c7d..00000000 --- a/.github/workflows/ubuntu20-cxx20.yml +++ /dev/null @@ -1,19 +0,0 @@ -name: Ubuntu 20.04 CI (C++20) - -on: [push, pull_request] - -jobs: - ubuntu-build: - runs-on: ubuntu-20.04 - strategy: - fail-fast: false - steps: - - uses: actions/checkout@v4 - - name: Use cmake - run: | - mkdir build && - cd build && - cmake -DFASTFLOAT_CXX_STANDARD=20 -DFASTFLOAT_TEST=ON -DCMAKE_INSTALL_PREFIX:PATH=destination .. && - cmake --build . && - ctest --output-on-failure && - cmake --install . diff --git a/.github/workflows/ubuntu20-fastmath.yml b/.github/workflows/ubuntu20-fastmath.yml deleted file mode 100644 index a2d7b6db..00000000 --- a/.github/workflows/ubuntu20-fastmath.yml +++ /dev/null @@ -1,16 +0,0 @@ -name: Ubuntu 20.04 CI (GCC 9, fast-math) - -on: [push, pull_request] - -jobs: - ubuntu-build: - runs-on: ubuntu-20.04 - steps: - - uses: actions/checkout@v4 - - name: Use cmake - run: | - mkdir build && - cd build && - cmake -DCMAKE_CXX_FLAGS="-ffast-math" -DFASTFLOAT_TEST=ON .. && - cmake --build . && - ctest --output-on-failure diff --git a/.github/workflows/ubuntu20.yml b/.github/workflows/ubuntu20.yml deleted file mode 100644 index f0a8b6f3..00000000 --- a/.github/workflows/ubuntu20.yml +++ /dev/null @@ -1,21 +0,0 @@ -name: Ubuntu 20.04 CI (GCC 9) - -on: [push, pull_request] - -jobs: - ubuntu-build: - runs-on: ubuntu-20.04 - steps: - - uses: actions/checkout@v4 - - name: Use cmake - run: | - mkdir build && - cd build && - cmake ${{matrix.cxx}} ${{matrix.arch}} -DFASTFLOAT_TEST=ON -DCMAKE_INSTALL_PREFIX:PATH=destination .. && - cmake --build . && - ctest --output-on-failure && - cmake --install . && - cd ../tests/installation_tests/find && - mkdir build && cd build && cmake -DCMAKE_INSTALL_PREFIX:PATH=../../../build/destination .. && cmake --build . && - cd ../../issue72_installation && - mkdir build && cd build && cmake -DCMAKE_INSTALL_PREFIX:PATH=../../../build/destination .. && cmake --build . diff --git a/tests/p2497.cpp b/tests/p2497.cpp index 7e085a59..fec5133c 100644 --- a/tests/p2497.cpp +++ b/tests/p2497.cpp @@ -6,7 +6,8 @@ int main() { std::string input = "3.1416 xyz "; double result; - if(auto answer = fast_float::from_chars(input.data(), input.data() + input.size(), 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; } From e5d93c993e5739a8c11972bb5ddc1e3c44dd9950 Mon Sep 17 00:00:00 2001 From: Daniel Lemire Date: Mon, 19 May 2025 18:32:56 -0400 Subject: [PATCH 5/5] updating workflow --- .../{amalgamate-ubuntu20.yml => amalgamate-ubuntu24.yml} | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename .github/workflows/{amalgamate-ubuntu20.yml => amalgamate-ubuntu24.yml} (84%) diff --git a/.github/workflows/amalgamate-ubuntu20.yml b/.github/workflows/amalgamate-ubuntu24.yml similarity index 84% rename from .github/workflows/amalgamate-ubuntu20.yml rename to .github/workflows/amalgamate-ubuntu24.yml index af738691..f92b0933 100644 --- a/.github/workflows/amalgamate-ubuntu20.yml +++ b/.github/workflows/amalgamate-ubuntu24.yml @@ -1,10 +1,10 @@ -name: Amalgamate Ubuntu 20.04 CI (GCC 9) +name: Amalgamate Ubuntu 20.04 CI (GCC 13æ) on: [push, pull_request] jobs: ubuntu-build: - runs-on: ubuntu-20.04 + runs-on: ubuntu-24.04 steps: - uses: actions/checkout@v4 - name: Compile with amalgamation