Allow fast_float to parse strings accepted by the Fortran internal read

function.
This commit is contained in:
allen.barnett@paperbirchsoftware.com 2023-08-07 08:39:06 -04:00
parent 6a390f63e9
commit e22c027544
4 changed files with 69 additions and 3 deletions

View File

@ -328,9 +328,17 @@ parsed_number_string_t<UC> parse_number_string(UC const *p, UC const * pend, par
return answer; return answer;
} }
int64_t exp_number = 0; // explicit exponential part int64_t exp_number = 0; // explicit exponential part
if ((fmt & chars_format::scientific) && (p != pend) && ((UC('e') == *p) || (UC('E') == *p))) { if ( ((fmt & chars_format::scientific) &&
(p != pend) &&
((UC('e') == *p) || (UC('E') == *p)))
||
((fmt & chars_format::fortran) &&
(p != pend) &&
((UC('+') == *p) || (UC('-') == *p) || (UC('d') == *p) || (UC('D') == *p)))) {
UC const * location_of_e = p; UC const * location_of_e = p;
++p; if ((UC('e') == *p) || (UC('E') == *p) || (UC('d') == *p) || (UC('D') == *p)) {
++p;
}
bool neg_exp = false; bool neg_exp = false;
if ((p != pend) && (UC('-') == *p)) { if ((p != pend) && (UC('-') == *p)) {
neg_exp = true; neg_exp = true;

View File

@ -16,6 +16,7 @@ enum chars_format {
scientific = 1 << 0, scientific = 1 << 0,
fixed = 1 << 2, fixed = 1 << 2,
hex = 1 << 3, hex = 1 << 3,
fortran = 1 << 4 | fixed | scientific,
general = fixed | scientific general = fixed | scientific
}; };

View File

@ -72,7 +72,7 @@ fast_float_add_cpp_test(long_test)
fast_float_add_cpp_test(powersoffive_hardround) fast_float_add_cpp_test(powersoffive_hardround)
fast_float_add_cpp_test(string_test) fast_float_add_cpp_test(string_test)
fast_float_add_cpp_test(fortran)
option(FASTFLOAT_EXHAUSTIVE "Exhaustive tests" OFF) option(FASTFLOAT_EXHAUSTIVE "Exhaustive tests" OFF)

57
tests/fortran.cpp Normal file
View File

@ -0,0 +1,57 @@
/*
* Exercise the Fortran conversion option.
*/
#include <cstdlib>
#include <iostream>
#include <vector>
#define FASTFLOAT_ALLOWS_LEADING_PLUS
#include "fast_float/fast_float.h"
int main ()
{
const std::vector<double> expected{ 10000, 1000, 100, 10, 1, .1, .01, .001, .0001 };
const std::vector<std::string> fmt1{ "1+4", "1+3", "1+2", "1+1", "1+0", "1-1", "1-2",
"1-3", "1-4" };
const std::vector<std::string> fmt2{ "1d+4", "1d+3", "1d+2", "1d+1", "1d+0", "1d-1",
"1d-2", "1d-3", "1d-4" };
const std::vector<std::string> fmt3{ "+1+4", "+1+3", "+1+2", "+1+1", "+1+0", "+1-1",
"+1-2", "+1-3", "+1-4" };
const fast_float::parse_options options{ fast_float::chars_format::fortran };
for ( auto const& f : fmt1 ) {
auto d{ std::distance( &fmt1[0], &f ) };
double result;
auto answer{ fast_float::from_chars_advanced( f.data(), f.data()+f.size(), result,
options ) };
if ( answer.ec != std::errc() || result != expected[std::size_t(d)] ) {
std::cerr << "parsing failure on " << f << std::endl;
return EXIT_FAILURE;
}
}
for ( auto const& f : fmt2 ) {
auto d{ std::distance( &fmt2[0], &f ) };
double result;
auto answer{ fast_float::from_chars_advanced( f.data(), f.data()+f.size(), result,
options ) };
if ( answer.ec != std::errc() || result != expected[std::size_t(d)] ) {
std::cerr << "parsing failure on " << f << std::endl;
return EXIT_FAILURE;
}
}
for ( auto const& f : fmt3 ) {
auto d{ std::distance( &fmt3[0], &f ) };
double result;
auto answer{ fast_float::from_chars_advanced( f.data(), f.data()+f.size(), result,
options ) };
if ( answer.ec != std::errc() || result != expected[std::size_t(d)] ) {
std::cerr << "parsing failure on " << f << std::endl;
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}