Skip to content

Allow fast_float to parse strings accepted by the Fortran internal read function. #219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions include/fast_float/ascii_number.h
Original file line number Diff line number Diff line change
Expand Up @@ -328,9 +328,17 @@ parsed_number_string_t<UC> parse_number_string(UC const *p, UC const * pend, par
return answer;
}
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) &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line would match chars_format::general as well, allowing 'd' and 'D' even in non-fortran mode.
eg. general = 0b101, fortran = 0b10101, general & fortran = 0b101 which evaluates to true.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm… that’s not good. We need to fix this mistake.

(p != pend) &&
((UC('+') == *p) || (UC('-') == *p) || (UC('d') == *p) || (UC('D') == *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;
if ((p != pend) && (UC('-') == *p)) {
neg_exp = true;
Expand Down
1 change: 1 addition & 0 deletions include/fast_float/float_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ enum chars_format {
scientific = 1 << 0,
fixed = 1 << 2,
hex = 1 << 3,
fortran = 1 << 4 | fixed | scientific,
general = fixed | scientific
};

Expand Down
2 changes: 1 addition & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ fast_float_add_cpp_test(long_test)
fast_float_add_cpp_test(powersoffive_hardround)
fast_float_add_cpp_test(string_test)


fast_float_add_cpp_test(fortran)

option(FASTFLOAT_EXHAUSTIVE "Exhaustive tests" OFF)

Expand Down
57 changes: 57 additions & 0 deletions tests/fortran.cpp
Original file line number Diff line number Diff line change
@@ -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;
}