| 1 |
|
| 2 |
#include "fast_float/fast_float.h" |
| 3 |
#include <iostream> |
| 4 |
#include <string> |
| 5 |
#include <system_error> |
| 6 |
|
| 7 |
bool many() { |
| 8 |
std::string const input = "234532.3426362,7869234.9823,324562.645"; |
| 9 |
double result; |
| 10 |
auto answer = |
| 11 |
fast_float::from_chars(input.data(), input.data() + input.size(), result); |
| 12 |
if (answer.ec != std::errc()) { |
| 13 |
return false; |
| 14 |
} |
| 15 |
if (result != 234532.3426362) { |
| 16 |
return false; |
| 17 |
} |
| 18 |
if (answer.ptr[0] != ',') { |
| 19 |
return false; |
| 20 |
} |
| 21 |
answer = fast_float::from_chars(answer.ptr + 1, input.data() + input.size(), |
| 22 |
result); |
| 23 |
if (answer.ec != std::errc()) { |
| 24 |
return false; |
| 25 |
} |
| 26 |
if (result != 7869234.9823) { |
| 27 |
return false; |
| 28 |
} |
| 29 |
if (answer.ptr[0] != ',') { |
| 30 |
return false; |
| 31 |
} |
| 32 |
answer = fast_float::from_chars(answer.ptr + 1, input.data() + input.size(), |
| 33 |
result); |
| 34 |
if (answer.ec != std::errc()) { |
| 35 |
return false; |
| 36 |
} |
| 37 |
if (result != 324562.645) { |
| 38 |
return false; |
| 39 |
} |
| 40 |
return true; |
| 41 |
} |
| 42 |
|
| 43 |
void many_loop() { |
| 44 |
std::string const input = "234532.3426362,7869234.9823,324562.645"; |
| 45 |
double result; |
| 46 |
char const *pointer = input.data(); |
| 47 |
char const *end_pointer = input.data() + input.size(); |
| 48 |
|
| 49 |
while (pointer < end_pointer) { |
| 50 |
auto answer = fast_float::from_chars(pointer, end_pointer, result); |
| 51 |
if (answer.ec != std::errc()) { |
| 52 |
std::cerr << "error while parsing" << std::endl; |
| 53 |
break; |
| 54 |
} |
| 55 |
std::cout << "parsed: " << result << std::endl; |
| 56 |
pointer = answer.ptr; |
| 57 |
if ((answer.ptr < end_pointer) && (*pointer == ',')) { |
| 58 |
pointer++; |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
#if FASTFLOAT_IS_CONSTEXPR |
| 64 |
// consteval forces compile-time evaluation of the function in C++20. |
| 65 |
consteval double parse(std::string_view input) { |
| 66 |
double result; |
| 67 |
auto answer = |
| 68 |
fast_float::from_chars(input.data(), input.data() + input.size(), result); |
| 69 |
if (answer.ec != std::errc()) { |
| 70 |
return -1.0; |
| 71 |
} |
| 72 |
return result; |
| 73 |
} |
| 74 |
|
| 75 |
// This function should compile to a function which |
| 76 |
// merely returns 3.1415. |
| 77 |
constexpr double constexptest() { return parse("3.1415 input"); } |
| 78 |
#endif |
| 79 |
|
| 80 |
bool small() { |
| 81 |
double result = -1; |
| 82 |
std::string str = "3e-1000"; |
| 83 |
auto r = fast_float::from_chars(str.data(), str.data() + str.size(), result); |
| 84 |
if (r.ec != std::errc::result_out_of_range) { |
| 85 |
return false; |
| 86 |
} |
| 87 |
if (r.ptr != str.data() + 7) { |
| 88 |
return false; |
| 89 |
} |
| 90 |
if (result != 0) { |
| 91 |
return false; |
| 92 |
} |
| 93 |
printf("small values go to zero\n"); |
| 94 |
return true; |
| 95 |
} |
| 96 |
|
| 97 |
bool large() { |
| 98 |
double result = -1; |
| 99 |
std::string str = "3e1000"; |
| 100 |
auto r = fast_float::from_chars(str.data(), str.data() + str.size(), result); |
| 101 |
if (r.ec != std::errc::result_out_of_range) { |
| 102 |
return false; |
| 103 |
} |
| 104 |
if (r.ptr != str.data() + 6) { |
| 105 |
return false; |
| 106 |
} |
| 107 |
if (result != std::numeric_limits<double>::infinity()) { |
| 108 |
return false; |
| 109 |
} |
| 110 |
printf("large values go to infinity\n"); |
| 111 |
return true; |
| 112 |
} |
| 113 |
|
| 114 |
int main() { |
| 115 |
std::string input = "3.1416 xyz "; |
| 116 |
double result; |
| 117 |
auto answer = |
| 118 |
fast_float::from_chars(input.data(), input.data() + input.size(), result); |
| 119 |
if ((answer.ec != std::errc()) || ((result != 3.1416))) { |
| 120 |
std::cerr << "parsing failure\n"; |
| 121 |
return EXIT_FAILURE; |
| 122 |
} |
| 123 |
std::cout << "parsed the number " << result << std::endl; |
| 124 |
#ifdef __STDCPP_FLOAT16_T__ |
| 125 |
printf("16-bit float\n"); |
| 126 |
// Parse as 16-bit float |
| 127 |
std::float16_t parsed_16{}; |
| 128 |
input = "10000e-1452"; |
| 129 |
auto fast_float_r16 = fast_float::from_chars( |
| 130 |
input.data(), input.data() + input.size(), parsed_16); |
| 131 |
if (fast_float_r16.ec != std::errc() && |
| 132 |
fast_float_r16.ec != std::errc::result_out_of_range) { |
| 133 |
std::cerr << "16-bit fast_float parsing failure for: " + input + "\n"; |
| 134 |
return false; |
| 135 |
} |
| 136 |
std::cout << "parsed the 16-bit value " << float(parsed_16) << std::endl; |
| 137 |
#endif |
| 138 |
if (!small()) { |
| 139 |
printf("Bug\n"); |
| 140 |
return EXIT_FAILURE; |
| 141 |
} |
| 142 |
if (!large()) { |
| 143 |
printf("Bug\n"); |
| 144 |
return EXIT_FAILURE; |
| 145 |
} |
| 146 |
|
| 147 |
if (!many()) { |
| 148 |
printf("Bug\n"); |
| 149 |
return EXIT_FAILURE; |
| 150 |
} |
| 151 |
many_loop(); |
| 152 |
#if FASTFLOAT_IS_CONSTEXPR |
| 153 |
if constexpr (constexptest() != 3.1415) { |
| 154 |
return EXIT_FAILURE; |
| 155 |
} |
| 156 |
#endif |
| 157 |
return EXIT_SUCCESS; |
| 158 |
} |