Skip to content

implementation of p2497 #314

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Expand Down
19 changes: 0 additions & 19 deletions .github/workflows/ubuntu20-cxx20.yml

This file was deleted.

16 changes: 0 additions & 16 deletions .github/workflows/ubuntu20-fastmath.yml

This file was deleted.

21 changes: 0 additions & 21 deletions .github/workflows/ubuntu20.yml

This file was deleted.

2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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")
Expand Down
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ Example:
```C++
#include "fast_float/fast_float.h"
#include <iostream>
#include <string>

int main() {
std::string input = "3.1416 xyz ";
Expand All @@ -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 <iostream>
#include <string>

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++
Expand Down
5 changes: 5 additions & 0 deletions include/fast_float/float_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ enum class chars_format : uint64_t {
template <typename UC> 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<char>;
Expand Down
21 changes: 6 additions & 15 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()

Expand Down Expand Up @@ -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)
Expand Down
16 changes: 16 additions & 0 deletions tests/p2497.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "fast_float/fast_float.h"

#include <iostream>
#include <string>

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;
}