From c4b64f5bcac6f7d5e1ae7d3026a3e228fb8adbbd Mon Sep 17 00:00:00 2001 From: =?utf8?q?R=C3=BCdiger=20Sonderfeld?= Date: Mon, 6 Oct 2014 15:55:53 +0000 Subject: [PATCH] re PR libstdc++/59987 ([C++11]: Missing ios_base::hexfloat format specifier) MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 2014-10-06 Rüdiger Sonderfeld Jonathan Wakely PR libstdc++/59987 * doc/xml/manual/status_cxx2011.xml: Remove hexfloat from notes. * doc/html/manual/status.html: Regenerate. * include/bits/ios_base.h (hexfloat): New function. (defaultfloat): New function. * src/c++98/locale_facets.cc (__num_base::_S_format_float): Support hexadecimal floating point format. * testsuite/27_io/basic_ostream/inserters_arithmetic/char/hexfloat.cc: New file. Co-Authored-By: Jonathan Wakely From-SVN: r215952 --- libstdc++-v3/ChangeLog | 13 ++ libstdc++-v3/doc/html/manual/status.html | 1 - .../doc/xml/manual/status_cxx2011.xml | 1 - libstdc++-v3/include/bits/ios_base.h | 21 +++ libstdc++-v3/src/c++98/locale_facets.cc | 17 +- .../inserters_arithmetic/char/hexfloat.cc | 152 ++++++++++++++++++ 6 files changed, 198 insertions(+), 7 deletions(-) create mode 100644 libstdc++-v3/testsuite/27_io/basic_ostream/inserters_arithmetic/char/hexfloat.cc diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 9ebe1f7f4fe..6120d26ac6b 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,16 @@ +2014-10-06 Rüdiger Sonderfeld + Jonathan Wakely + + PR libstdc++/59987 + * doc/xml/manual/status_cxx2011.xml: Remove hexfloat from notes. + * doc/html/manual/status.html: Regenerate. + * include/bits/ios_base.h (hexfloat): New function. + (defaultfloat): New function. + * src/c++98/locale_facets.cc (__num_base::_S_format_float): Support + hexadecimal floating point format. + * testsuite/27_io/basic_ostream/inserters_arithmetic/char/hexfloat.cc: + New file. + 2014-10-06 Jonathan Wakely * include/Makefile.am: Add new header. diff --git a/libstdc++-v3/doc/html/manual/status.html b/libstdc++-v3/doc/html/manual/status.html index a90b435b581..232bdef8185 100644 --- a/libstdc++-v3/doc/html/manual/status.html +++ b/libstdc++-v3/doc/html/manual/status.html @@ -215,7 +215,6 @@ particular release. 27.1GeneralY 27.2Iostreams requirementsY 27.2.1Imbue LimitationsY 27.2.2Positioning Type LimitationsY 27.2.3Thread safetyPartial 27.3Forward declarationsY 27.4Standard iostream objectsY 27.4.1OverviewY 27.4.2Narrow stream objectsY 27.4.3Wide stream objectsY 27.5Iostreams base classesPartial Missing io_errc and iostream_category. ios_base::failure is not derived from system_error. - Missing ios_base::hexfloat. 27.6Stream buffersY 27.7Formatting and manipulatorsPartial Missing get_time and put_time manipulators. 27.8String-based streamsY 27.9File-based streamsY  diff --git a/libstdc++-v3/doc/xml/manual/status_cxx2011.xml b/libstdc++-v3/doc/xml/manual/status_cxx2011.xml index 5b365565617..108de369087 100644 --- a/libstdc++-v3/doc/xml/manual/status_cxx2011.xml +++ b/libstdc++-v3/doc/xml/manual/status_cxx2011.xml @@ -2131,7 +2131,6 @@ particular release. Missing io_errc and iostream_category. ios_base::failure is not derived from system_error. - Missing ios_base::hexfloat. diff --git a/libstdc++-v3/include/bits/ios_base.h b/libstdc++-v3/include/bits/ios_base.h index fb448fd1091..5e33b812fe3 100644 --- a/libstdc++-v3/include/bits/ios_base.h +++ b/libstdc++-v3/include/bits/ios_base.h @@ -984,6 +984,27 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return __base; } +#if __cplusplus >= 201103L + // New C++11 floatfield manipulators + + /// Calls + /// base.setf(ios_base::fixed|ios_base::scientific, ios_base::floatfield) + inline ios_base& + hexfloat(ios_base& __base) + { + __base.setf(ios_base::fixed | ios_base::scientific, ios_base::floatfield); + return __base; + } + + /// Calls @c base.unsetf(ios_base::floatfield) + inline ios_base& + defaultfloat(ios_base& __base) + { + __base.unsetf(ios_base::floatfield); + return __base; + } +#endif + _GLIBCXX_END_NAMESPACE_VERSION } // namespace diff --git a/libstdc++-v3/src/c++98/locale_facets.cc b/libstdc++-v3/src/c++98/locale_facets.cc index 3669acb0ca8..7ed04e613e2 100644 --- a/libstdc++-v3/src/c++98/locale_facets.cc +++ b/libstdc++-v3/src/c++98/locale_facets.cc @@ -69,19 +69,26 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION if (__flags & ios_base::showpoint) *__fptr++ = '#'; - // As per DR 231: _always_, not only when - // __flags & ios_base::fixed || __prec > 0 - *__fptr++ = '.'; - *__fptr++ = '*'; + ios_base::fmtflags __fltfield = __flags & ios_base::floatfield; + + if (__fltfield != (ios_base::fixed | ios_base::scientific)) + { + // As per DR 231: not only when __flags & ios_base::fixed || __prec > 0 + *__fptr++ = '.'; + *__fptr++ = '*'; + } if (__mod) *__fptr++ = __mod; - ios_base::fmtflags __fltfield = __flags & ios_base::floatfield; // [22.2.2.2.2] Table 58 if (__fltfield == ios_base::fixed) *__fptr++ = 'f'; else if (__fltfield == ios_base::scientific) *__fptr++ = (__flags & ios_base::uppercase) ? 'E' : 'e'; +#ifdef _GLIBCXX_USE_C99 + else if (__fltfield == (ios_base::fixed | ios_base::scientific)) + *__fptr++ = (__flags & ios_base::uppercase) ? 'A' : 'a'; +#endif else *__fptr++ = (__flags & ios_base::uppercase) ? 'G' : 'g'; *__fptr = '\0'; diff --git a/libstdc++-v3/testsuite/27_io/basic_ostream/inserters_arithmetic/char/hexfloat.cc b/libstdc++-v3/testsuite/27_io/basic_ostream/inserters_arithmetic/char/hexfloat.cc new file mode 100644 index 00000000000..485a485f100 --- /dev/null +++ b/libstdc++-v3/testsuite/27_io/basic_ostream/inserters_arithmetic/char/hexfloat.cc @@ -0,0 +1,152 @@ +// { dg-options "-std=gnu++11" } + +// 2014-03-27 Rüdiger Sonderfeld +// test the hexadecimal floating point inserters (facet num_put) + +// Copyright (C) 2014 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +#include +#include +#include +#include + +#ifndef _GLIBCXX_ASSERT +# define TEST_NUMPUT_VERBOSE 1 +#endif + +#ifdef TEST_NUMPUT_VERBOSE +# include +#endif + +using namespace std; + +void +test01() +{ + ostringstream os; + double d = 272.; // 0x1.1p+8; +#ifdef TEST_NUMPUT_VERBOSE + cout << os.precision() << endl; +#endif + os << hexfloat << setprecision(1); + os << d; +#ifdef TEST_NUMPUT_VERBOSE + cout << "got: " << os.str() << endl; +#endif + VERIFY( os && std::stod(os.str()) == d ); + VERIFY( os.str().substr(0, 2) == "0x" ); + VERIFY( os.str().find('p') != std::string::npos ); + + os.str(""); + os << uppercase << d; +#ifdef TEST_NUMPUT_VERBOSE + cout << "got: " << os.str() << endl; +#endif + VERIFY( os && std::stod(os.str()) == d ); + VERIFY( os.str().substr(0, 2) == "0X" ); + VERIFY( os.str().find('P') != std::string::npos ); + + os << nouppercase; + os.str(""); + os << defaultfloat << setprecision(6); + os << d; +#ifdef TEST_NUMPUT_VERBOSE + cout << "got: " << os.str() << endl; +#endif + VERIFY( os && os.str() == "272" ); + + os.str(""); + d = 15.; //0x1.ep+3; + os << hexfloat << setprecision(1); + os << d; +#ifdef TEST_NUMPUT_VERBOSE + cout << "got: " << os.str() << endl; +#endif + VERIFY( os && std::stod(os.str()) == d ); + os.str(""); + os << uppercase << d; +#ifdef TEST_NUMPUT_VERBOSE + cout << "got: " << os.str() << endl; +#endif + VERIFY( os && std::stod(os.str()) == d ); + os << nouppercase; + os.str(""); + os << defaultfloat << setprecision(6); + os << d; +#ifdef TEST_NUMPUT_VERBOSE + cout << "got: " << os.str() << endl; +#endif + VERIFY( os && os.str() == "15" ); +} + +void +test02() +{ + ostringstream os; + long double d = 272.L; // 0x1.1p+8L; + os << hexfloat << setprecision(1); + os << d; +#ifdef TEST_NUMPUT_VERBOSE + cout << "got: " << os.str() << endl; +#endif + VERIFY( os && std::stold(os.str()) == d ); + os.str(""); + os << uppercase << d; +#ifdef TEST_NUMPUT_VERBOSE + cout << "got: " << os.str() << endl; +#endif + VERIFY( os && std::stold(os.str()) == d ); + os << nouppercase; + os.str(""); + os << defaultfloat << setprecision(6); + os << d; +#ifdef TEST_NUMPUT_VERBOSE + cout << "got: " << os.str() << endl; +#endif + VERIFY( os && os.str() == "272" ); + + os.str(""); + os << hexfloat << setprecision(1); + d = 15.; //0x1.ep+3; + os << d; +#ifdef TEST_NUMPUT_VERBOSE + cout << "got: " << os.str() << endl; +#endif + VERIFY( os && std::stold(os.str()) == d ); + os.str(""); + os << uppercase << d; +#ifdef TEST_NUMPUT_VERBOSE + cout << "got: " << os.str() << endl; +#endif + VERIFY( os && std::stold(os.str()) == d ); + os << nouppercase; + os.str(""); + os << defaultfloat << setprecision(6); + os << d; +#ifdef TEST_NUMPUT_VERBOSE + cout << "got: " << os.str() << endl; +#endif + VERIFY( os && os.str() == "15" ); +} + +int +main() +{ + test01(); + test02(); +} -- 2.30.2