diff options
author | Paolo Carlini <paolo.carlini@oracle.com> | 2008-06-16 09:58:54 +0000 |
---|---|---|
committer | Paolo Carlini <paolo@gcc.gnu.org> | 2008-06-16 09:58:54 +0000 |
commit | 7364f2868c8ac21e519725d716bc13f02b19684f (patch) | |
tree | 8685abf3ce961673fcc9761e7fded8aeef7e1b6a /libstdc++-v3/src/string_conversions.cc | |
parent | 1b4b3957d77c7d12df446508b42f82bdaf8da8c8 (diff) | |
download | gcc-7364f2868c8ac21e519725d716bc13f02b19684f.zip gcc-7364f2868c8ac21e519725d716bc13f02b19684f.tar.gz gcc-7364f2868c8ac21e519725d716bc13f02b19684f.tar.bz2 |
basic_string.h (stod, [...]): Declare in C++0x mode.
2008-06-16 Paolo Carlini <paolo.carlini@oracle.com>
* include/bits/basic_string.h (stod, stof, stoi, stol, stold, stoll,
stoul, stoull, to_string, to_wstring): Declare in C++0x mode.
* src/string_conversions.cc: Add.
* src/Makefile.am: Update.
* config/abi/pre/gnu.ver: Adjust exports.
* src/Makefile.in: Regenerate.
* testsuite/21_strings/basic_string/numeric_conversions/char/stod.cc:
New.
* testsuite/21_strings/basic_string/numeric_conversions/char/stof.cc:
Likewise.
* testsuite/21_strings/basic_string/numeric_conversions/char/stoi.cc:
Likewise.
* testsuite/21_strings/basic_string/numeric_conversions/char/stol.cc:
Likewise.
* testsuite/21_strings/basic_string/numeric_conversions/char/stold.cc:
Likewise.
* testsuite/21_strings/basic_string/numeric_conversions/char/stoll.cc:
Likewise.
* testsuite/21_strings/basic_string/numeric_conversions/char/stoul.cc:
Likewise.
* testsuite/21_strings/basic_string/numeric_conversions/char/stoull.cc:
Likewise.
* testsuite/21_strings/basic_string/numeric_conversions/char/
to_string.cc: Likewise.
* testsuite/21_strings/basic_string/numeric_conversions/wchar_t/
stod.cc: New.
* testsuite/21_strings/basic_string/numeric_conversions/wchar_t/
stof.cc: Likewise.
* testsuite/21_strings/basic_string/numeric_conversions/wchar_t/
stoi.cc: Likewise.
* testsuite/21_strings/basic_string/numeric_conversions/wchar_t/
stol.cc: Likewise.
* testsuite/21_strings/basic_string/numeric_conversions/wchar_t/
stold.cc: Likewise.
* testsuite/21_strings/basic_string/numeric_conversions/wchar_t/
stoll.cc: Likewise.
* testsuite/21_strings/basic_string/numeric_conversions/wchar_t/
stoul.cc: Likewise.
* testsuite/21_strings/basic_string/numeric_conversions/wchar_t/
stoull.cc: Likewise.
* testsuite/21_strings/basic_string/numeric_conversions/wchar_t/
to_wstring.cc: Likewise.
From-SVN: r136841
Diffstat (limited to 'libstdc++-v3/src/string_conversions.cc')
-rw-r--r-- | libstdc++-v3/src/string_conversions.cc | 195 |
1 files changed, 195 insertions, 0 deletions
diff --git a/libstdc++-v3/src/string_conversions.cc b/libstdc++-v3/src/string_conversions.cc new file mode 100644 index 0000000..627ddfa --- /dev/null +++ b/libstdc++-v3/src/string_conversions.cc @@ -0,0 +1,195 @@ +// String Conversions -*- C++ -*- + +// Copyright (C) 2008 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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, +// USA. + +// As a special exception, you may use this file as part of a free software +// library without restriction. Specifically, if other files instantiate +// templates or use macros or inline functions from this file, or you compile +// this file and link it with other files to produce an executable, this +// file does not by itself cause the resulting executable to be covered by +// the GNU General Public License. This exception does not however +// invalidate any other reasons why the executable file might be covered by +// the GNU General Public License. + +#include <string> +#include <limits> +#include <cerrno> +#include <cstdlib> + +#ifdef _GLIBCXX_USE_C99 + +_GLIBCXX_BEGIN_NAMESPACE(std) + + // Helper for all the sto* functions. + template<typename _TRet, typename _Ret = _TRet, typename _CharT, + typename... _Base> + inline _Ret + __stoa(_TRet (*__convf) (const _CharT*, _CharT**, _Base...), + const char* __name, const basic_string<_CharT>& __str, + size_t* __idx, _Base... __base) + { + _Ret __ret; + + _CharT* __endptr; + errno = 0; + const _TRet __tmp = __convf(__str.c_str(), &__endptr, __base...); + + if (__endptr == __str.c_str()) + __throw_invalid_argument(__name); + else if (errno == ERANGE + || (__are_same<_Ret, int>::__value + && (__tmp < numeric_limits<_Ret>::min() + || __tmp > numeric_limits<_Ret>::max()))) + __throw_out_of_range(__name); + else + __ret = __tmp; + + if (__idx) + *__idx = __endptr - __str.c_str(); + + return __ret; + } + + + int + stoi(const string& __str, size_t* __idx, int __base) + { return std::__stoa<long, int>(&std::strtol, "stoi", __str, + __idx, __base); } + + long + stol(const string& __str, size_t* __idx, int __base) + { return std::__stoa(&std::strtol, "stol", __str, __idx, __base); } + + unsigned long + stoul(const string& __str, size_t* __idx, int __base) + { return std::__stoa(&std::strtoul, "stoul", __str, __idx, __base); } + + long long + stoll(const string& __str, size_t* __idx, int __base) + { return std::__stoa(&std::strtoll, "stoll", __str, __idx, __base); } + + unsigned long long + stoull(const string& __str, size_t* __idx, int __base) + { return std::__stoa(&std::strtoull, "stoull", __str, __idx, __base); } + + // NB: strtof vs strtod. + float + stof(const string& __str, size_t* __idx) + { return std::__stoa(&std::strtof, "stof", __str, __idx); } + + double + stod(const string& __str, size_t* __idx) + { return std::__stoa(&std::strtod, "stod", __str, __idx); } + + long double + stold(const string& __str, size_t* __idx) + { return std::__stoa(&std::strtold, "stold", __str, __idx); } + + string + to_string(long long __val) + { + // XXX Eventually the result will be constructed in place in + // the C++0x string, likely with the help of internal hooks. + const int __n = 4 * sizeof(long long); + char* __s = static_cast<char*>(__builtin_alloca(__n)); + return string(__s, __s + std::snprintf(__s, __n, "%lld", __val)); + } + + string + to_string(unsigned long long __val) + { + const int __n = 4 * sizeof(unsigned long long); + char* __s = static_cast<char*>(__builtin_alloca(__n)); + return string(__s, __s + std::snprintf(__s, __n, "%llu", __val)); + } + + string + to_string(long double __val) + { + const int __n = numeric_limits<long double>::max_exponent10 + 20; + char* __s = static_cast<char*>(__builtin_alloca(__n)); + return string(__s, __s + std::snprintf(__s, __n, "%Lf", __val)); + } + +#ifdef _GLIBCXX_USE_WCHAR_T + int + stoi(const wstring& __str, size_t* __idx, int __base) + { return std::__stoa<long, int>(&std::wcstol, "stoi", __str, + __idx, __base); } + + long + stol(const wstring& __str, size_t* __idx, int __base) + { return std::__stoa(&std::wcstol, "stol", __str, __idx, __base); } + + unsigned long + stoul(const wstring& __str, size_t* __idx, int __base) + { return std::__stoa(&std::wcstoul, "stoul", __str, __idx, __base); } + + long long + stoll(const wstring& __str, size_t* __idx, int __base) + { return std::__stoa(&std::wcstoll, "stoll", __str, __idx, __base); } + + unsigned long long + stoull(const wstring& __str, size_t* __idx, int __base) + { return std::__stoa(&std::wcstoull, "stoull", __str, __idx, __base); } + + // NB: wcstof vs wcstod. + float + stof(const wstring& __str, size_t* __idx) + { return std::__stoa(&std::wcstof, "stof", __str, __idx); } + + double + stod(const wstring& __str, size_t* __idx) + { return std::__stoa(&std::wcstod, "stod", __str, __idx); } + + long double + stold(const wstring& __str, size_t* __idx) + { return std::__stoa(&std::wcstold, "stold", __str, __idx); } + + wstring + to_wstring(long long __val) + { + const int __n = 4 * sizeof(long long); + wchar_t* __s = static_cast<wchar_t*>(__builtin_alloca(sizeof(wchar_t) + * __n)); + return wstring(__s, __s + std::swprintf(__s, __n, L"%lld", __val)); + } + + wstring + to_wstring(unsigned long long __val) + { + const int __n = 4 * sizeof(unsigned long long); + wchar_t* __s = static_cast<wchar_t*>(__builtin_alloca(sizeof(wchar_t) + * __n)); + return wstring(__s, __s + std::swprintf(__s, __n, L"%llu", __val)); + } + + wstring + to_wstring(long double __val) + { + const int __n = numeric_limits<long double>::max_exponent10 + 20; + wchar_t* __s = static_cast<wchar_t*>(__builtin_alloca(sizeof(wchar_t) + * __n)); + return wstring(__s, __s + std::swprintf(__s, __n, L"%Lf", __val)); + } +#endif + +_GLIBCXX_END_NAMESPACE + +#endif |