mirror of
https://github.com/fastfloat/fast_float.git
synced 2025-12-06 16:56:57 +08:00
Compare commits
5 Commits
c553930104
...
358c4d7b71
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
358c4d7b71 | ||
|
|
f8c573d741 | ||
|
|
9d78a01ff7 | ||
|
|
409d6215b4 | ||
|
|
e6867f54b9 |
24
README.md
24
README.md
@ -58,32 +58,36 @@ Example:
|
|||||||
#include "fast_float/fast_float.h"
|
#include "fast_float/fast_float.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <system_error>
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
std::string input = "3.1416 xyz ";
|
const std::string input = "3.1416 xyz ";
|
||||||
double result;
|
double result;
|
||||||
auto answer = fast_float::from_chars(input.data(), input.data() + input.size(), 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()) {
|
||||||
std::cout << "parsed the number " << result << std::endl;
|
std::cerr << "parsing failure\n";
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
std::cout << "parsed the number " << result << '\n';
|
||||||
return EXIT_SUCCESS;
|
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:
|
Prior to C++26, checking for a successful `std::from_chars` conversion requires comparing the `from_chars_result::ec` member to `std::errc()`. As an extension `fast_float::from_chars` supports the improved C++26 API that allows checking the result by converting it to `bool`, like so:
|
||||||
|
|
||||||
```cpp
|
```C++
|
||||||
#include "fast_float/fast_float.h"
|
#include "fast_float/fast_float.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
std::string input = "3.1416 xyz ";
|
const std::string input = "3.1416 xyz ";
|
||||||
double result;
|
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;
|
std::cout << "parsed the number " << result << '\n';
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
std::cerr << "failed to parse " << result << std::endl;
|
std::cerr << "parsing failure\n";
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@ -91,7 +95,7 @@ int main() {
|
|||||||
You can parse delimited numbers:
|
You can parse delimited numbers:
|
||||||
|
|
||||||
```C++
|
```C++
|
||||||
std::string input = "234532.3426362,7869234.9823,324562.645";
|
const std::string input = "234532.3426362,7869234.9823,324562.645";
|
||||||
double result;
|
double result;
|
||||||
auto answer = fast_float::from_chars(input.data(), input.data() + input.size(), result);
|
auto answer = fast_float::from_chars(input.data(), input.data() + input.size(), result);
|
||||||
if (answer.ec != std::errc()) {
|
if (answer.ec != std::errc()) {
|
||||||
|
|||||||
@ -406,8 +406,8 @@ full_multiplication(uint64_t a, uint64_t b) {
|
|||||||
// But MinGW on ARM64 doesn't have native support for 64-bit multiplications
|
// But MinGW on ARM64 doesn't have native support for 64-bit multiplications
|
||||||
answer.high = __umulh(a, b);
|
answer.high = __umulh(a, b);
|
||||||
answer.low = a * b;
|
answer.low = a * b;
|
||||||
#elif defined(FASTFLOAT_32BIT) || \
|
#elif defined(FASTFLOAT_32BIT) || (defined(_WIN64) && !defined(__clang__) && \
|
||||||
(defined(_WIN64) && !defined(__clang__) && !defined(_M_ARM64))
|
!defined(_M_ARM64) && !defined(__GNUC__))
|
||||||
answer.low = _umul128(a, b, &answer.high); // _umul128 not available on ARM64
|
answer.low = _umul128(a, b, &answer.high); // _umul128 not available on ARM64
|
||||||
#elif defined(FASTFLOAT_64BIT) && defined(__SIZEOF_INT128__)
|
#elif defined(FASTFLOAT_64BIT) && defined(__SIZEOF_INT128__)
|
||||||
__uint128_t r = ((__uint128_t)a) * b;
|
__uint128_t r = ((__uint128_t)a) * b;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user