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 iostreams with imbued locales to print INFs correctly #3877

Merged
merged 6 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 4 additions & 4 deletions stl/inc/xlocnum
Original file line number Diff line number Diff line change
Expand Up @@ -1376,7 +1376,7 @@ protected:
const auto _Ngen = static_cast<size_t>(_CSTD sprintf_s(
&_Buf[0], _Buf.size(), _Ffmt(_Fmt, 0, _Iosbase.flags()), static_cast<int>(_Precision), _Val));

return _Fput_v2(_Dest, _Iosbase, _Fill, _Buf.c_str(), _Ngen, (_STD isnan)(_Val));
return _Fput_v2(_Dest, _Iosbase, _Fill, _Buf.c_str(), _Ngen, (_STD isnan)(_Val) || (_STD isinf)(_Val));
StephanTLavavej marked this conversation as resolved.
Show resolved Hide resolved
}

virtual _OutIt __CLR_OR_THIS_CALL do_put(
Expand All @@ -1400,7 +1400,7 @@ protected:
const auto _Ngen = static_cast<size_t>(_CSTD sprintf_s(
&_Buf[0], _Buf.size(), _Ffmt(_Fmt, 'L', _Iosbase.flags()), static_cast<int>(_Precision), _Val));

return _Fput_v2(_Dest, _Iosbase, _Fill, _Buf.c_str(), _Ngen, (_STD isnan)(_Val));
return _Fput_v2(_Dest, _Iosbase, _Fill, _Buf.c_str(), _Ngen, (_STD isnan)(_Val) || (_STD isinf)(_Val));
}
#pragma warning(pop)

Expand Down Expand Up @@ -1468,7 +1468,7 @@ private:

template <int = 0> // TRANSITION, ABI
_OutIt _Fput_v2(_OutIt _Dest, ios_base& _Iosbase, _Elem _Fill, const char* _Buf, size_t _Count,
bool _Is_nan_val) const { // put formatted floating-point to _Dest
bool _Is_nan_or_inf_val) const { // put formatted floating-point to _Dest
auto _Prefix = static_cast<size_t>(0 < _Count && (*_Buf == '+' || *_Buf == '-'));
const char* _Exps;
if ((_Iosbase.flags() & ios_base::floatfield) != ios_base::hexfloat) {
Expand Down Expand Up @@ -1497,7 +1497,7 @@ private:
_Groupstring[_Poff] = _Punct_fac.decimal_point();
}

if (!_Is_nan_val) {
if (!_Is_nan_or_inf_val) {
size_t _Off = _Poff == _Count ? _Eoff : _Poff;
const char* _Pg = &_Grouping[0];
while (*_Pg != CHAR_MAX && '\0' < *_Pg && static_cast<size_t>(*_Pg) < _Off - _Prefix) {
Expand Down
24 changes: 20 additions & 4 deletions tests/std/tests/GH_003867_output_nan/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,32 @@
#include <limits>
#include <locale>
#include <sstream>
#include <string>

using namespace std;

struct groups_of_1 : numpunct<char> {
// The standard char specialization of std::numpunct::do_thousands_sep returns ','
string do_grouping() const {
StephanTLavavej marked this conversation as resolved.
Show resolved Hide resolved
return "\1";
} // groups of 1 digit
};

template <typename T>
void test_gh_3867() {
// GH-3867 Writing NaN to the output stream with a set locale results in a weird output
ostringstream s;
s.imbue(locale("en-US"));
s << -numeric_limits<T>::quiet_NaN();
assert(s.str() == "-nan(ind)");
{
ostringstream s;
s.imbue(locale("en-US"));
s << -numeric_limits<T>::quiet_NaN();
assert(s.str() == "-nan(ind)");
}
{
ostringstream s;
s.imbue(locale(s.getloc(), new groups_of_1));
s << -numeric_limits<T>::infinity();
assert(s.str() == "-inf");
}
}

int main() {
Expand Down