This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[v3] Implement resolution of DR 1261


Hi,

tested x86_64-linux, committed to mainline.

Paolo.

/////////////////////
2009-11-11  Paolo Carlini  <paolo.carlini@oracle.com>

	* include/bits/basic_string.h (to_string(int), to_string(unsigned),
	to_string(long), to_string(unsigned long), to_string(float),
	to_string(double), to_wstring(int), to_wstring(unsigned),
	to_wstring(long), to_wstring(unsigned long), to_wstring(float),
	to_wstring(double)): Add, per resolution of DR 1261.
	* include/ext/vstring.h: Likewise.
	* testsuite/21_strings/basic_string/numeric_conversions/char/
	dr1261.cc: Add.
	* testsuite/21_strings/basic_string/numeric_conversions/wchar_t/
	dr1261.cc: Likewise.
Index: include/ext/vstring.h
===================================================================
--- include/ext/vstring.h	(revision 154097)
+++ include/ext/vstring.h	(working copy)
@@ -2464,7 +2464,33 @@
   { return __gnu_cxx::__stoa(&std::strtold, "stold", __str.c_str(), __idx); }
 
   // NB: (v)snprintf vs sprintf.
+
+  // DR 1261.
   inline __vstring
+  to_string(int __val)
+  { return __gnu_cxx::__to_xstring<__vstring>(&std::vsnprintf, 4 * sizeof(int),
+					      "%d", __val); }
+
+  inline __vstring
+  to_string(unsigned __val)
+  { return __gnu_cxx::__to_xstring<__vstring>(&std::vsnprintf,
+					      4 * sizeof(unsigned),
+					      "%u", __val); }
+
+  inline __vstring
+  to_string(long __val)
+  { return __gnu_cxx::__to_xstring<__vstring>(&std::vsnprintf,
+					      4 * sizeof(long),
+					      "%ld", __val); }
+
+  inline __vstring
+  to_string(unsigned long __val)
+  { return __gnu_cxx::__to_xstring<__vstring>(&std::vsnprintf,
+					      4 * sizeof(unsigned long),
+					      "%lu", __val); }
+
+
+  inline __vstring
   to_string(long long __val)
   { return __gnu_cxx::__to_xstring<__vstring>(&std::vsnprintf,
 					      4 * sizeof(long long),
@@ -2477,6 +2503,22 @@
 					      "%llu", __val); }
 
   inline __vstring
+  to_string(float __val)
+  {
+    const int __n = __numeric_traits<float>::__max_exponent10 + 20;
+    return __gnu_cxx::__to_xstring<__vstring>(&std::vsnprintf, __n,
+					      "%f", __val);
+  }
+
+  inline __vstring
+  to_string(double __val)
+  {
+    const int __n = __numeric_traits<double>::__max_exponent10 + 20;
+    return __gnu_cxx::__to_xstring<__vstring>(&std::vsnprintf, __n,
+					      "%f", __val);
+  }
+
+  inline __vstring
   to_string(long double __val)
   {
     const int __n = __numeric_traits<long double>::__max_exponent10 + 20;
@@ -2524,7 +2566,32 @@
   { return __gnu_cxx::__stoa(&std::wcstold, "stold", __str.c_str(), __idx); }
 
 #ifndef _GLIBCXX_HAVE_BROKEN_VSWPRINTF
+  // DR 1261.
   inline __wvstring
+  to_wstring(int __val)
+  { return __gnu_cxx::__to_xstring<__wvstring>(&std::vswprintf,
+					       4 * sizeof(int),
+					       L"%d", __val); }
+
+  inline __wvstring
+  to_wstring(unsigned __val)
+  { return __gnu_cxx::__to_xstring<__wvstring>(&std::vswprintf,
+					       4 * sizeof(unsigned),
+					       L"%u", __val); }
+
+  inline __wvstring
+  to_wstring(long __val)
+  { return __gnu_cxx::__to_xstring<__wvstring>(&std::vswprintf,
+					       4 * sizeof(long),
+					       L"%ld", __val); }
+
+  inline __wvstring
+  to_wstring(unsigned long __val)
+  { return __gnu_cxx::__to_xstring<__wvstring>(&std::vswprintf,
+					       4 * sizeof(unsigned long),
+					       L"%lu", __val); }
+
+  inline __wvstring
   to_wstring(long long __val)
   { return __gnu_cxx::__to_xstring<__wvstring>(&std::vswprintf,
 					       4 * sizeof(long long),
@@ -2537,6 +2604,22 @@
 					       L"%llu", __val); }
 
   inline __wvstring
+  to_wstring(float __val)
+  {
+    const int __n = __numeric_traits<float>::__max_exponent10 + 20;
+    return __gnu_cxx::__to_xstring<__wvstring>(&std::vswprintf, __n,
+					       L"%f", __val);
+  }
+
+  inline __wvstring
+  to_wstring(double __val)
+  {
+    const int __n = __numeric_traits<double>::__max_exponent10 + 20;
+    return __gnu_cxx::__to_xstring<__wvstring>(&std::vswprintf, __n,
+					       L"%f", __val);
+  }
+
+  inline __wvstring
   to_wstring(long double __val)
   {
     const int __n = __numeric_traits<long double>::__max_exponent10 + 20;
Index: include/bits/basic_string.h
===================================================================
--- include/bits/basic_string.h	(revision 154097)
+++ include/bits/basic_string.h	(working copy)
@@ -2639,7 +2639,31 @@
   { return __gnu_cxx::__stoa(&std::strtold, "stold", __str.c_str(), __idx); }
 
   // NB: (v)snprintf vs sprintf.
+
+  // DR 1261.
   inline string
+  to_string(int __val)
+  { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, 4 * sizeof(int),
+					   "%d", __val); }
+
+  inline string
+  to_string(unsigned __val)
+  { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf,
+					   4 * sizeof(unsigned),
+					   "%u", __val); }
+
+  inline string
+  to_string(long __val)
+  { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, 4 * sizeof(long),
+					   "%ld", __val); }
+
+  inline string
+  to_string(unsigned long __val)
+  { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf,
+					   4 * sizeof(unsigned long),
+					   "%lu", __val); }
+
+  inline string
   to_string(long long __val)
   { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf,
 					   4 * sizeof(long long),
@@ -2652,6 +2676,24 @@
 					   "%llu", __val); }
 
   inline string
+  to_string(float __val)
+  {
+    const int __n = 
+      __gnu_cxx::__numeric_traits<float>::__max_exponent10 + 20;
+    return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
+					   "%f", __val);
+  }
+
+  inline string
+  to_string(double __val)
+  {
+    const int __n = 
+      __gnu_cxx::__numeric_traits<double>::__max_exponent10 + 20;
+    return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
+					   "%f", __val);
+  }
+
+  inline string
   to_string(long double __val)
   {
     const int __n = 
@@ -2699,7 +2741,30 @@
   stold(const wstring& __str, size_t* __idx = 0)
   { return __gnu_cxx::__stoa(&std::wcstold, "stold", __str.c_str(), __idx); }
 
+  // DR 1261.
   inline wstring
+  to_wstring(int __val)
+  { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, 4 * sizeof(int),
+					    L"%d", __val); }
+
+  inline wstring
+  to_wstring(unsigned __val)
+  { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
+					    4 * sizeof(unsigned),
+					    L"%u", __val); }
+
+  inline wstring
+  to_wstring(long __val)
+  { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, 4 * sizeof(long),
+					    L"%ld", __val); }
+
+  inline wstring
+  to_wstring(unsigned long __val)
+  { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
+					    4 * sizeof(unsigned long),
+					    L"%lu", __val); }
+
+  inline wstring
   to_wstring(long long __val)
   { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
 					    4 * sizeof(long long),
@@ -2712,6 +2777,24 @@
 					    L"%llu", __val); }
 
   inline wstring
+  to_wstring(float __val)
+  {
+    const int __n =
+      __gnu_cxx::__numeric_traits<float>::__max_exponent10 + 20;
+    return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n,
+					    L"%f", __val);
+  }
+
+  inline wstring
+  to_wstring(double __val)
+  {
+    const int __n =
+      __gnu_cxx::__numeric_traits<double>::__max_exponent10 + 20;
+    return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n,
+					    L"%f", __val);
+  }
+
+  inline wstring
   to_wstring(long double __val)
   {
     const int __n =
Index: testsuite/21_strings/basic_string/numeric_conversions/wchar_t/dr1261.cc
===================================================================
--- testsuite/21_strings/basic_string/numeric_conversions/wchar_t/dr1261.cc	(revision 0)
+++ testsuite/21_strings/basic_string/numeric_conversions/wchar_t/dr1261.cc	(revision 0)
@@ -0,0 +1,65 @@
+// { dg-options "-std=gnu++0x" }
+// { dg-require-string-conversions "" }
+
+// 2009-11-11  Paolo Carlini  <paolo.carlini@oracle.com>
+
+// Copyright (C) 2009 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 2, 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 COPYING.  If not, write to the Free
+// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+// USA.
+
+#include <string>
+#include <testsuite_hooks.h>
+
+// DR 1261. Insufficient overloads for to_string / to_wstring
+void test01()
+{
+  bool test __attribute__((unused)) = true;
+  using namespace std;
+
+  const wstring one(to_wstring(-2));
+  VERIFY( one == L"-2" );
+
+  const wstring two(to_wstring(10u));
+  VERIFY( two == L"10" );
+
+  const wstring three(to_wstring(2l));
+  VERIFY( three == L"2" );
+
+  const wstring four(to_wstring(3000ul));
+  VERIFY( four == L"3000" );
+
+  const wstring five(to_wstring(7ll));
+  VERIFY( five == L"7" );
+
+  const wstring six(to_wstring(400ull));
+  VERIFY( six == L"400" );
+
+  const wstring seven(to_wstring(-1.0F));
+  VERIFY( seven == L"-1.000000" );
+
+  const wstring eight(to_wstring(2.0));
+  VERIFY( eight == L"2.000000" );
+
+  const wstring nine(to_wstring(-4.0L));
+  VERIFY( nine == L"-4.000000" );
+}
+
+int main()
+{
+  test01();
+  return 0;
+}
Index: testsuite/21_strings/basic_string/numeric_conversions/char/dr1261.cc
===================================================================
--- testsuite/21_strings/basic_string/numeric_conversions/char/dr1261.cc	(revision 0)
+++ testsuite/21_strings/basic_string/numeric_conversions/char/dr1261.cc	(revision 0)
@@ -0,0 +1,65 @@
+// { dg-options "-std=gnu++0x" }
+// { dg-require-string-conversions "" }
+
+// 2009-11-11  Paolo Carlini  <paolo.carlini@oracle.com>
+
+// Copyright (C) 2009 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 2, 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 COPYING.  If not, write to the Free
+// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+// USA.
+
+#include <string>
+#include <testsuite_hooks.h>
+
+// DR 1261. Insufficient overloads for to_string / to_wstring
+void test01()
+{
+  bool test __attribute__((unused)) = true;
+  using namespace std;
+
+  const string one(to_string(-2));
+  VERIFY( one == "-2" );
+
+  const string two(to_string(10u));
+  VERIFY( two == "10" );
+
+  const string three(to_string(2l));
+  VERIFY( three == "2" );
+
+  const string four(to_string(3000ul));
+  VERIFY( four == "3000" );
+
+  const string five(to_string(7ll));
+  VERIFY( five == "7" );
+
+  const string six(to_string(400ull));
+  VERIFY( six == "400" );
+
+  const string seven(to_string(-1.0F));
+  VERIFY( seven == "-1.000000" );
+
+  const string eight(to_string(2.0));
+  VERIFY( eight == "2.000000" );
+
+  const string nine(to_string(-4.0L));
+  VERIFY( nine == "-4.000000" );
+}
+
+int main()
+{
+  test01();
+  return 0;
+}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]