From f70b64543602aa076b19a3deaacb484f9830be9c Mon Sep 17 00:00:00 2001 From: Daniel Lemire Date: Tue, 3 Aug 2021 09:22:40 -0400 Subject: [PATCH] Candidate release. --- CMakeLists.txt | 2 +- README.md | 26 ++++++++++++++++++++++++++ tests/CMakeLists.txt | 1 + tests/example_comma_test.cpp | 13 +++++++++++++ tests/example_test.cpp | 2 +- 5 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 tests/example_comma_test.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 163a6554b4..eebae48af8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.9) -project(fast_float VERSION 1.0.0 LANGUAGES CXX) +project(fast_float VERSION 2.0.0 LANGUAGES CXX) option(FASTFLOAT_TEST "Enable tests" OFF) set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED ON) diff --git a/README.md b/README.md index ab6578ab6f..eb4fa42e48 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,32 @@ The library seeks to follow the C++17 (see 20.19.3.(7.1)) specification. In par We support Visual Studio, macOS, Linux, freeBSD. We support big and little endian. We support 32-bit and 64-bit systems. +## Using commas as decimal separator + + +The C++ standard stipulate that `from_chars` has to be locale-independent. In +particular, the decimal separator has to be the period (`.`). However, +some users still want to use the `fast_float` library with in a locale-dependent +manner. Using a separate function called `from_chars_advanced`, we allow the users +to pass a `parse_options` instance which contains a custom decimal separator (e.g., +the comma). You may use it as follows. + +```C++ +#include "fast_float/fast_float.h" +#include + +int main() { + const std::string input = "3,1416 xyz "; + double result; + fast_float::parse_options options{fast_float::chars_format::general, ','}; + auto answer = fast_float::from_chars_advanced(input.data(), input.data()+input.size(), result, options); + if((answer.ec != std::errc()) || ((result != 3.1416))) { std::cerr << "parsing failure\n"; return EXIT_FAILURE; } + std::cout << "parsed the number " << result << std::endl; + return EXIT_SUCCESS; +} +``` + + ## Reference - Daniel Lemire, [Number Parsing at a Gigabyte per Second](https://arxiv.org/abs/2101.11408), Software: Pratice and Experience 51 (8), 2021. diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 24101e3089..852a0c46a8 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -54,6 +54,7 @@ endfunction(fast_float_add_cpp_test) fast_float_add_cpp_test(example_test) +fast_float_add_cpp_test(example_comma_test) fast_float_add_cpp_test(basictest) diff --git a/tests/example_comma_test.cpp b/tests/example_comma_test.cpp new file mode 100644 index 0000000000..93f7f8b41e --- /dev/null +++ b/tests/example_comma_test.cpp @@ -0,0 +1,13 @@ + +#include "fast_float/fast_float.h" +#include + +int main() { + const std::string input = "3,1416 xyz "; + double result; + fast_float::parse_options options{fast_float::chars_format::general, ','}; + auto answer = fast_float::from_chars_advanced(input.data(), input.data()+input.size(), result, options); + if((answer.ec != std::errc()) || ((result != 3.1416))) { std::cerr << "parsing failure\n"; return EXIT_FAILURE; } + std::cout << "parsed the number " << result << std::endl; + return EXIT_SUCCESS; +} diff --git a/tests/example_test.cpp b/tests/example_test.cpp index 3502ce81bb..7d97bb92bc 100644 --- a/tests/example_test.cpp +++ b/tests/example_test.cpp @@ -6,7 +6,7 @@ int main() { const std::string input = "3.1416 xyz "; double result; auto answer = fast_float::from_chars(input.data(), input.data()+input.size(), result); - if(answer.ec != std::errc()) { std::cerr << "parsing failure\n"; return EXIT_FAILURE; } + if((answer.ec != std::errc()) || ((result != 3.1416))) { std::cerr << "parsing failure\n"; return EXIT_FAILURE; } std::cout << "parsed the number " << result << std::endl; return EXIT_SUCCESS; }