From 45d95050217f32c1b26f3a7f046e7a7358e3ea97 Mon Sep 17 00:00:00 2001 From: Thomas Beutlich Date: Wed, 10 Sep 2025 15:18:53 +0200 Subject: [PATCH] Fix wording --- README.md | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index cf0d9f34..d3cbacb4 100644 --- a/README.md +++ b/README.md @@ -58,32 +58,36 @@ Example: #include "fast_float/fast_float.h" #include #include +#include int main() { - std::string input = "3.1416 xyz "; + 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; } - std::cout << "parsed the number " << result << std::endl; + if (answer.ec != std::errc()) { + std::cerr << "parsing failure\n"; + return EXIT_FAILURE; + } + std::cout << "parsed the number " << result << '\n'; return EXIT_SUCCESS; } ``` -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: +You can simplify the conversion success check by casting the result of `from_chars` to `bool`, as shown below: -```cpp +```C++ #include "fast_float/fast_float.h" #include #include int main() { - std::string input = "3.1416 xyz "; + const 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; + if (auto answer = fast_float::from_chars(input.data(), input.data() + input.size(), result)) { + std::cout << "parsed the number " << result << '\n'; return EXIT_SUCCESS; } - std::cerr << "failed to parse " << result << std::endl; + std::cerr << "parsing failure\n"; return EXIT_FAILURE; } ``` @@ -91,7 +95,7 @@ int main() { You can parse delimited numbers: ```C++ - std::string input = "234532.3426362,7869234.9823,324562.645"; + const std::string input = "234532.3426362,7869234.9823,324562.645"; double result; auto answer = fast_float::from_chars(input.data(), input.data() + input.size(), result); if (answer.ec != std::errc()) {