Skip to content
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

fix formatting floating-point values without type and precision provided #4327

Merged
merged 2 commits into from
Jan 25, 2024
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
10 changes: 9 additions & 1 deletion stl/inc/format
Original file line number Diff line number Diff line change
Expand Up @@ -2967,6 +2967,7 @@ _NODISCARD _OutputIt _Fmt_write(
auto _Format = chars_format::general;
auto _Exponent = 'e';
auto _Precision = _Specs._Precision;
auto _None_type = false;

switch (_Specs._Type) {
case 'A':
Expand Down Expand Up @@ -3003,6 +3004,9 @@ _NODISCARD _OutputIt _Fmt_write(
}
_Format = chars_format::general;
break;
default:
_None_type = true;
break;
}

// Consider the powers of 2 in decimal:
Expand Down Expand Up @@ -3043,7 +3047,11 @@ _NODISCARD _OutputIt _Fmt_write(
_Result.ptr += 3;
} else {
if (_Precision == -1) {
_Result = _STD to_chars(_Buffer, _STD end(_Buffer), _Value, _Format);
if (_None_type) {
_Result = _STD to_chars(_Buffer, _STD end(_Buffer), _Value);
} else {
_Result = _STD to_chars(_Buffer, _STD end(_Buffer), _Value, _Format);
}
} else {
_Result = _STD to_chars(_Buffer, _STD end(_Buffer), _Value, _Format, _Precision);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1506,6 +1506,12 @@ constexpr bool test_format_string() {
return true;
}

// Also test GH-4319: incorrect output for some floating-point values
template <class charT>
void test_gh_4319() {
assert(format(STR("{:}"), 12345678.0) == STR("12345678"));
}

void test() {
test_simple_formatting<char>();
test_simple_formatting<wchar_t>();
Expand Down Expand Up @@ -1582,6 +1588,9 @@ void test() {
test_localized_char<char, char>();
test_localized_char<wchar_t, char>();
test_localized_char<wchar_t, wchar_t>();

test_gh_4319<char>();
test_gh_4319<wchar_t>();
}

int main() {
Expand Down