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 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: 6 additions & 6 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_v3(_Dest, _Iosbase, _Fill, _Buf.c_str(), _Ngen, (_STD isfinite)(_Val));
}

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_v3(_Dest, _Iosbase, _Fill, _Buf.c_str(), _Ngen, (_STD isfinite)(_Val));
}
#pragma warning(pop)

Expand Down Expand Up @@ -1463,12 +1463,12 @@ private:

_OutIt __CLRCALL_OR_CDECL _Fput(_OutIt _Dest, ios_base& _Iosbase, _Elem _Fill, const char* _Buf,
size_t _Count) const { // TRANSITION, ABI: preserved for binary compatibility
return _Fput_v2(_Dest, _Iosbase, _Fill, _Buf, _Count, false);
return _Fput_v3(_Dest, _Iosbase, _Fill, _Buf, _Count, true);
}

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
_OutIt _Fput_v3(_OutIt _Dest, ios_base& _Iosbase, _Elem _Fill, const char* _Buf, size_t _Count,
bool _Is_finite_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_finite_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
1 change: 0 additions & 1 deletion tests/libcxx/expected_results.txt
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,6 @@ std/localization/locale.categories/category.monetary/locale.money.put/locale.mon
std/localization/locale.categories/category.monetary/locale.money.put/locale.money.put.members/put_string_en_US.pass.cpp FAIL
std/localization/locale.categories/category.monetary/locale.moneypunct/locale.moneypunct.members/decimal_point.pass.cpp FAIL
std/localization/locale.categories/category.monetary/locale.moneypunct/locale.moneypunct.members/thousands_sep.pass.cpp FAIL
std/localization/locale.categories/category.numeric/locale.nm.put/facet.num.put.members/put_long_double.pass.cpp FAIL
std/localization/locale.categories/category.time/locale.time.get/locale.time.get.members/get_monthname_wide.pass.cpp FAIL
std/localization/locale.categories/category.time/locale.time.get/locale.time.get.members/get_monthname.pass.cpp FAIL
std/localization/locale.categories/category.time/locale.time.get/locale.time.get.members/get_one.pass.cpp FAIL
Expand Down
29 changes: 18 additions & 11 deletions tests/std/tests/Dev11_0496153_locale_ctor/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,23 @@

using namespace std;

// The following monstrosity avoids activating the test code for _DLL compiles
// that use a non-default value of _ITERATOR_DEBUG_LEVEL (_ITERATOR_DEBUG_LEVEL
// defaults to "0" for release builds and "2" for _DEBUG).
#if defined(_DLL) && (_ITERATOR_DEBUG_LEVEL == 1 || (defined(_DEBUG) != (_ITERATOR_DEBUG_LEVEL == 2)))
#define ENABLE_TEST 0
// Because std::string crosses the DLL boundary via overridden virtual functions,
// we can test custom facets only when:
// * linking statically, or
// * linking dynamically with IDL set to its default value (so the user code and the DLL match).
#ifdef _DEBUG
#define DEFAULT_IDL 2
#else
#define ENABLE_TEST 1
#endif // defined(_DLL) && (_ITERATOR_DEBUG_LEVEL == 1 || (defined(_DEBUG) != (_ITERATOR_DEBUG_LEVEL == 2)))
#define DEFAULT_IDL 0
#endif

#if ENABLE_TEST
#if !defined(_DLL) || _ITERATOR_DEBUG_LEVEL == DEFAULT_IDL
#define TEST_CUSTOM_FACET 1
#else
#define TEST_CUSTOM_FACET 0
#endif

#if TEST_CUSTOM_FACET
void test_Dev11_496153_locale_ctor_should_not_throw() noexcept {
const locale loc(setlocale(LC_ALL, nullptr));

Expand All @@ -39,11 +46,11 @@ void test_VSO_159700_locale_should_support_user_defined_facets() {
str << 1.5f;
assert("locale didn't support user-defined facets" && str.str() == "1,5");
}
#endif // ENABLE_TEST
#endif // TEST_CUSTOM_FACET

int main() {
#if ENABLE_TEST
#if TEST_CUSTOM_FACET
test_Dev11_496153_locale_ctor_should_not_throw();
test_VSO_159700_locale_should_support_user_defined_facets();
#endif // ENABLE_TEST
#endif // TEST_CUSTOM_FACET
}
45 changes: 41 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,53 @@
#include <limits>
#include <locale>
#include <sstream>
#include <string>

using namespace std;

// Because std::string crosses the DLL boundary via overridden virtual functions,
// we can test custom facets only when:
// * linking statically, or
// * linking dynamically with IDL set to its default value (so the user code and the DLL match).
#ifdef _DEBUG
#define DEFAULT_IDL 2
#else
#define DEFAULT_IDL 0
#endif

#if !defined(_DLL) || _ITERATOR_DEBUG_LEVEL == DEFAULT_IDL
#define TEST_CUSTOM_FACET 1
#else
#define TEST_CUSTOM_FACET 0
#endif

#if TEST_CUSTOM_FACET
struct groups_of_1 : numpunct<char> {
// The standard char specialization of std::numpunct::do_thousands_sep returns ','
string do_grouping() const override {
return "\1";
} // groups of 1 digit
};
#endif // TEST_CUSTOM_FACET

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)");
}

#if TEST_CUSTOM_FACET
{
ostringstream s;
s.imbue(locale(s.getloc(), new groups_of_1));
s << -numeric_limits<T>::infinity();
assert(s.str() == "-inf");
}
#endif // TEST_CUSTOM_FACET
}

int main() {
Expand Down