From a4ecd144bb7cbad8f122f98b2db8a3b6de468361 Mon Sep 17 00:00:00 2001 From: Paolo Carlini Date: Wed, 11 Nov 2009 19:57:48 +0000 Subject: [PATCH] basic_string.h (to_string(int), [...]): Add, per resolution of DR 1261. 2009-11-11 Paolo Carlini * 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. From-SVN: r154102 --- libstdc++-v3/ChangeLog | 13 +++ libstdc++-v3/include/bits/basic_string.h | 83 +++++++++++++++++++ libstdc++-v3/include/ext/vstring.h | 83 +++++++++++++++++++ .../numeric_conversions/char/dr1261.cc | 65 +++++++++++++++ .../numeric_conversions/wchar_t/dr1261.cc | 65 +++++++++++++++ 5 files changed, 309 insertions(+) create mode 100644 libstdc++-v3/testsuite/21_strings/basic_string/numeric_conversions/char/dr1261.cc create mode 100644 libstdc++-v3/testsuite/21_strings/basic_string/numeric_conversions/wchar_t/dr1261.cc diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 154d4d5030df..b34122c4de56 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,16 @@ +2009-11-11 Paolo Carlini + + * 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. + 2009-11-09 Benjamin Kosnik * doc/doxygen/user.cfg.in: Regenerate, add files. diff --git a/libstdc++-v3/include/bits/basic_string.h b/libstdc++-v3/include/bits/basic_string.h index 9d44dc4fdfc9..269a75ed4681 100644 --- a/libstdc++-v3/include/bits/basic_string.h +++ b/libstdc++-v3/include/bits/basic_string.h @@ -2639,6 +2639,30 @@ _GLIBCXX_BEGIN_NAMESPACE(std) { 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(&std::vsnprintf, 4 * sizeof(int), + "%d", __val); } + + inline string + to_string(unsigned __val) + { return __gnu_cxx::__to_xstring(&std::vsnprintf, + 4 * sizeof(unsigned), + "%u", __val); } + + inline string + to_string(long __val) + { return __gnu_cxx::__to_xstring(&std::vsnprintf, 4 * sizeof(long), + "%ld", __val); } + + inline string + to_string(unsigned long __val) + { return __gnu_cxx::__to_xstring(&std::vsnprintf, + 4 * sizeof(unsigned long), + "%lu", __val); } + inline string to_string(long long __val) { return __gnu_cxx::__to_xstring(&std::vsnprintf, @@ -2651,6 +2675,24 @@ _GLIBCXX_BEGIN_NAMESPACE(std) 4 * sizeof(unsigned long long), "%llu", __val); } + inline string + to_string(float __val) + { + const int __n = + __gnu_cxx::__numeric_traits::__max_exponent10 + 20; + return __gnu_cxx::__to_xstring(&std::vsnprintf, __n, + "%f", __val); + } + + inline string + to_string(double __val) + { + const int __n = + __gnu_cxx::__numeric_traits::__max_exponent10 + 20; + return __gnu_cxx::__to_xstring(&std::vsnprintf, __n, + "%f", __val); + } + inline string to_string(long double __val) { @@ -2699,6 +2741,29 @@ _GLIBCXX_BEGIN_NAMESPACE(std) 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(&std::vswprintf, 4 * sizeof(int), + L"%d", __val); } + + inline wstring + to_wstring(unsigned __val) + { return __gnu_cxx::__to_xstring(&std::vswprintf, + 4 * sizeof(unsigned), + L"%u", __val); } + + inline wstring + to_wstring(long __val) + { return __gnu_cxx::__to_xstring(&std::vswprintf, 4 * sizeof(long), + L"%ld", __val); } + + inline wstring + to_wstring(unsigned long __val) + { return __gnu_cxx::__to_xstring(&std::vswprintf, + 4 * sizeof(unsigned long), + L"%lu", __val); } + inline wstring to_wstring(long long __val) { return __gnu_cxx::__to_xstring(&std::vswprintf, @@ -2711,6 +2776,24 @@ _GLIBCXX_BEGIN_NAMESPACE(std) 4 * sizeof(unsigned long long), L"%llu", __val); } + inline wstring + to_wstring(float __val) + { + const int __n = + __gnu_cxx::__numeric_traits::__max_exponent10 + 20; + return __gnu_cxx::__to_xstring(&std::vswprintf, __n, + L"%f", __val); + } + + inline wstring + to_wstring(double __val) + { + const int __n = + __gnu_cxx::__numeric_traits::__max_exponent10 + 20; + return __gnu_cxx::__to_xstring(&std::vswprintf, __n, + L"%f", __val); + } + inline wstring to_wstring(long double __val) { diff --git a/libstdc++-v3/include/ext/vstring.h b/libstdc++-v3/include/ext/vstring.h index 61e6ae49fea1..183d037a2d21 100644 --- a/libstdc++-v3/include/ext/vstring.h +++ b/libstdc++-v3/include/ext/vstring.h @@ -2464,6 +2464,32 @@ _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx) { 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, @@ -2476,6 +2502,22 @@ _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx) 4 * sizeof(unsigned long long), "%llu", __val); } + inline __vstring + to_string(float __val) + { + const int __n = __numeric_traits::__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::__max_exponent10 + 20; + return __gnu_cxx::__to_xstring<__vstring>(&std::vsnprintf, __n, + "%f", __val); + } + inline __vstring to_string(long double __val) { @@ -2524,6 +2566,31 @@ _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx) { 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, @@ -2536,6 +2603,22 @@ _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx) 4 * sizeof(unsigned long long), L"%llu", __val); } + inline __wvstring + to_wstring(float __val) + { + const int __n = __numeric_traits::__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::__max_exponent10 + 20; + return __gnu_cxx::__to_xstring<__wvstring>(&std::vswprintf, __n, + L"%f", __val); + } + inline __wvstring to_wstring(long double __val) { diff --git a/libstdc++-v3/testsuite/21_strings/basic_string/numeric_conversions/char/dr1261.cc b/libstdc++-v3/testsuite/21_strings/basic_string/numeric_conversions/char/dr1261.cc new file mode 100644 index 000000000000..739ceed3004d --- /dev/null +++ b/libstdc++-v3/testsuite/21_strings/basic_string/numeric_conversions/char/dr1261.cc @@ -0,0 +1,65 @@ +// { dg-options "-std=gnu++0x" } +// { dg-require-string-conversions "" } + +// 2009-11-11 Paolo Carlini + +// 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 +#include + +// 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; +} diff --git a/libstdc++-v3/testsuite/21_strings/basic_string/numeric_conversions/wchar_t/dr1261.cc b/libstdc++-v3/testsuite/21_strings/basic_string/numeric_conversions/wchar_t/dr1261.cc new file mode 100644 index 000000000000..7d754b30102e --- /dev/null +++ b/libstdc++-v3/testsuite/21_strings/basic_string/numeric_conversions/wchar_t/dr1261.cc @@ -0,0 +1,65 @@ +// { dg-options "-std=gnu++0x" } +// { dg-require-string-conversions "" } + +// 2009-11-11 Paolo Carlini + +// 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 +#include + +// 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; +}