From patchwork Thu Mar 27 16:00:19 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?R=C3=BCdiger_Sonderfeld?= X-Patchwork-Id: 334384 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 34C9A14009B for ; Fri, 28 Mar 2014 03:00:45 +1100 (EST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding:content-type; q=dns; s= default; b=l3a4N4RJzfq3gA8JfbD1FiKONC8KjNpBIX3rIW8bFof27r9DU1UN4 ciLt2QUQCYuaCiKsrSXHnhf44KkuikfZuxoJM8V+SGZTRWET8mS7DWio4vZEk8Gd unUY8KwC8kU1jD6ihPV0JtiyoC73HLubStIryFsq2paN6ADpWUu5Q8= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding:content-type; s=default; bh=XEU2nuExspItsWmjcR/e9RXSD04=; b=O/WmRzoQsHYpKFSGlub7/iaw3b5J 4L22E82SfsKjpwhJ9EYghAhtKT+qln2lBCRvfuFp6eWw+Ay7IRjnMPrbVuJbqXx4 AIeCpdSlp5slb7+H7/aHbG5UGHi92aE6zJtzIH6LA2YrKnxu8ssMEilqymTogoA+ vfJ02dbViq6+xKQ= Received: (qmail 19830 invoked by alias); 27 Mar 2014 16:00:38 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 19804 invoked by uid 89); 27 Mar 2014 16:00:38 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-3.2 required=5.0 tests=AWL, BAYES_00, SPF_HELO_PASS autolearn=ham version=3.3.2 X-Spam-User: qpsmtpd, 2 recipients X-HELO: ptmx.org Received: from ptmx.org (HELO ptmx.org) (178.63.28.110) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-SHA encrypted) ESMTPS; Thu, 27 Mar 2014 16:00:35 +0000 Received: from localhost (localhost [127.0.0.1]) by ptmx.org (Postfix) with ESMTP id 9300428C2A; Thu, 27 Mar 2014 17:00:30 +0100 (CET) Received: from ptmx.org ([127.0.0.1]) by localhost (ptmx.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id MDY5jD5mavvS; Thu, 27 Mar 2014 17:00:28 +0100 (CET) Received: from descartes.localnet (chello080108246092.7.14.vie.surfer.at [80.108.246.92]) by ptmx.org (Postfix) with ESMTPSA id C583324FA9; Thu, 27 Mar 2014 17:00:26 +0100 (CET) From: =?ISO-8859-1?Q?R=FCdiger?= Sonderfeld To: Jonathan Wakely Cc: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCH v2] libstdc++: Add hexfloat/defaultfloat io manipulators. Date: Thu, 27 Mar 2014 17:00:19 +0100 Message-ID: <1901588.qodTErPe42@descartes> User-Agent: KMail/4.11.5 (Linux/3.11.0-17-generic; KDE/4.11.5; x86_64; ; ) In-Reply-To: <20140327133723.GA13599@redhat.com> References: <20140327133723.GA13599@redhat.com> MIME-Version: 1.0 Hello Jonathan, thanks for your comments. > N.B. patches to the ChangeLog rarely apply cleanly (because someone > else may have changed the ChangeLog since the patch was created) so > the convention is to send the ChangeLog entry in the email body, or as > a separate attachment, or by using 'git log -p @{u}..' so the commit > log shows it, rather than as part of the patch. Yes, ChangeLog's can be a bit of a pain. I removed the ChangeLog from the patch. But FYI there is a ChangeLog merge driver hidden in gnulib, which can be helpful when dealing with ChangeLog files in git (and potentially other version control systems) http://git.savannah.gnu.org/gitweb/?p=gnulib.git;a=blob;f=lib/git-merge-changelog.c > We could document that (fixed|scientific) has that effect in c++98 > mode. Where should it be documented? Regards, Rüdiger -- 8< ---------------------------------------------------------- >8 -- * 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. Signed-off-by: Rüdiger Sonderfeld --- libstdc++-v3/include/bits/ios_base.h | 21 +++ libstdc++-v3/src/c++98/locale_facets.cc | 4 + .../inserters_arithmetic/char/hexfloat.cc | 141 +++++++++++++++++++++ 3 files changed, 166 insertions(+) create mode 100644 libstdc++-v3/testsuite/27_io/basic_ostream/inserters_arithmetic/char/hexfloat.cc diff --git a/libstdc++-v3/include/bits/ios_base.h b/libstdc++-v3/include/bits/ios_base.h index ae856de..b7fae43 100644 --- a/libstdc++-v3/include/bits/ios_base.h +++ b/libstdc++-v3/include/bits/ios_base.h @@ -969,6 +969,27 @@ namespace std _GLIBCXX_VISIBILITY(default) 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 base.unsetf(ios_base::floatfield). + inline ios_base& + defaultfloat(ios_base& __base) + { + __base.unsetf(ios_base::floatfield); + return __base; + } +#endif // __cplusplus >= 201103L + _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 3669acb..a21d665 100644 --- a/libstdc++-v3/src/c++98/locale_facets.cc +++ b/libstdc++-v3/src/c++98/locale_facets.cc @@ -82,6 +82,10 @@ namespace std _GLIBCXX_VISIBILITY(default) *__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 0000000..55c46ad --- /dev/null +++ b/libstdc++-v3/testsuite/27_io/basic_ostream/inserters_arithmetic/char/hexfloat.cc @@ -0,0 +1,141 @@ +// { 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 +#include + +using namespace std; + +#ifndef _GLIBCXX_ASSERT +# define TEST_NUMPUT_VERBOSE 1 +#endif + +void +test01() +{ + { + ostringstream os; + double d = 272.; // 0x1.1p+8; + cout << os.precision() << endl; + os << hexfloat << setprecision(1); + os << d; +#ifdef TEST_NUMPUT_VERBOSE + cout << "got: " << os.str() << endl; +#endif + VERIFY( os && os.str() == "0x1.1p+8" ); + os.str(""); + os << uppercase << d; +#ifdef TEST_NUMPUT_VERBOSE + cout << "got: " << os.str() << endl; +#endif + VERIFY( os && os.str() == "0X1.1P+8" ); + 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 && os.str() == "0x1.ep+3" ); + os.str(""); + os << uppercase << d; +#ifdef TEST_NUMPUT_VERBOSE + cout << "got: " << os.str() << endl; +#endif + VERIFY( os && os.str() == "0X1.EP+3" ); + 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" ); + } + + { + 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 && os.str() == "0x8.8p+5" ); + os.str(""); + os << uppercase << d; +#ifdef TEST_NUMPUT_VERBOSE + cout << "got: " << os.str() << endl; +#endif + VERIFY( os && os.str() == "0X8.8P+5" ); + 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 && os.str() == "0xf.0p+0" ); + os.str(""); + os << uppercase << d; +#ifdef TEST_NUMPUT_VERBOSE + cout << "got: " << os.str() << endl; +#endif + VERIFY( os && os.str() == "0XF.0P+0" ); + 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(); + return 0; +}