xref: /netbsd-src/external/gpl3/gcc.old/dist/libstdc++-v3/include/ext/string_conversions.h (revision 8feb0f0b7eaff0608f8350bbfa3098827b4bb91b)
136ac495dSmrg // String Conversions -*- C++ -*-
236ac495dSmrg 
3*8feb0f0bSmrg // Copyright (C) 2008-2020 Free Software Foundation, Inc.
436ac495dSmrg //
536ac495dSmrg // This file is part of the GNU ISO C++ Library.  This library is free
636ac495dSmrg // software; you can redistribute it and/or modify it under the
736ac495dSmrg // terms of the GNU General Public License as published by the
836ac495dSmrg // Free Software Foundation; either version 3, or (at your option)
936ac495dSmrg // any later version.
1036ac495dSmrg 
1136ac495dSmrg // This library is distributed in the hope that it will be useful,
1236ac495dSmrg // but WITHOUT ANY WARRANTY; without even the implied warranty of
1336ac495dSmrg // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1436ac495dSmrg // GNU General Public License for more details.
1536ac495dSmrg 
1636ac495dSmrg // Under Section 7 of GPL version 3, you are granted additional
1736ac495dSmrg // permissions described in the GCC Runtime Library Exception, version
1836ac495dSmrg // 3.1, as published by the Free Software Foundation.
1936ac495dSmrg 
2036ac495dSmrg // You should have received a copy of the GNU General Public License and
2136ac495dSmrg // a copy of the GCC Runtime Library Exception along with this program;
2236ac495dSmrg // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
2336ac495dSmrg // <http://www.gnu.org/licenses/>.
2436ac495dSmrg 
2536ac495dSmrg /** @file ext/string_conversions.h
2636ac495dSmrg  *  This file is a GNU extension to the Standard C++ Library.
2736ac495dSmrg  */
2836ac495dSmrg 
2936ac495dSmrg #ifndef _STRING_CONVERSIONS_H
3036ac495dSmrg #define _STRING_CONVERSIONS_H 1
3136ac495dSmrg 
3236ac495dSmrg #pragma GCC system_header
3336ac495dSmrg 
3436ac495dSmrg #if __cplusplus < 201103L
3536ac495dSmrg # include <bits/c++0x_warning.h>
3636ac495dSmrg #else
3736ac495dSmrg 
3836ac495dSmrg #include <bits/c++config.h>
3936ac495dSmrg #include <ext/numeric_traits.h>
4036ac495dSmrg #include <bits/functexcept.h>
4136ac495dSmrg #include <cstdlib>
4236ac495dSmrg #include <cwchar>
4336ac495dSmrg #include <cstdio>
4436ac495dSmrg #include <cerrno>
4536ac495dSmrg 
_GLIBCXX_VISIBILITY(default)4636ac495dSmrg namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
4736ac495dSmrg {
4836ac495dSmrg _GLIBCXX_BEGIN_NAMESPACE_VERSION
4936ac495dSmrg 
5036ac495dSmrg   // Helper for all the sto* functions.
5136ac495dSmrg   template<typename _TRet, typename _Ret = _TRet, typename _CharT,
5236ac495dSmrg 	   typename... _Base>
5336ac495dSmrg     _Ret
5436ac495dSmrg     __stoa(_TRet (*__convf) (const _CharT*, _CharT**, _Base...),
5536ac495dSmrg 	   const char* __name, const _CharT* __str, std::size_t* __idx,
5636ac495dSmrg 	   _Base... __base)
5736ac495dSmrg     {
5836ac495dSmrg       _Ret __ret;
5936ac495dSmrg 
6036ac495dSmrg       _CharT* __endptr;
6136ac495dSmrg 
6236ac495dSmrg       struct _Save_errno {
6336ac495dSmrg 	_Save_errno() : _M_errno(errno) { errno = 0; }
6436ac495dSmrg 	~_Save_errno() { if (errno == 0) errno = _M_errno; }
6536ac495dSmrg 	int _M_errno;
6636ac495dSmrg       } const __save_errno;
6736ac495dSmrg 
6836ac495dSmrg       struct _Range_chk {
6936ac495dSmrg 	  static bool
7036ac495dSmrg 	  _S_chk(_TRet, std::false_type) { return false; }
7136ac495dSmrg 
7236ac495dSmrg 	  static bool
7336ac495dSmrg 	  _S_chk(_TRet __val, std::true_type) // only called when _Ret is int
7436ac495dSmrg 	  {
7536ac495dSmrg 	    return __val < _TRet(__numeric_traits<int>::__min)
7636ac495dSmrg 	      || __val > _TRet(__numeric_traits<int>::__max);
7736ac495dSmrg 	  }
7836ac495dSmrg       };
7936ac495dSmrg 
8036ac495dSmrg       const _TRet __tmp = __convf(__str, &__endptr, __base...);
8136ac495dSmrg 
8236ac495dSmrg       if (__endptr == __str)
8336ac495dSmrg 	std::__throw_invalid_argument(__name);
8436ac495dSmrg       else if (errno == ERANGE
8536ac495dSmrg 	  || _Range_chk::_S_chk(__tmp, std::is_same<_Ret, int>{}))
8636ac495dSmrg 	std::__throw_out_of_range(__name);
8736ac495dSmrg       else
8836ac495dSmrg 	__ret = __tmp;
8936ac495dSmrg 
9036ac495dSmrg       if (__idx)
9136ac495dSmrg 	*__idx = __endptr - __str;
9236ac495dSmrg 
9336ac495dSmrg       return __ret;
9436ac495dSmrg     }
9536ac495dSmrg 
9636ac495dSmrg   // Helper for the to_string / to_wstring functions.
9736ac495dSmrg   template<typename _String, typename _CharT = typename _String::value_type>
9836ac495dSmrg     _String
9936ac495dSmrg     __to_xstring(int (*__convf) (_CharT*, std::size_t, const _CharT*,
10036ac495dSmrg 				 __builtin_va_list), std::size_t __n,
10136ac495dSmrg 		 const _CharT* __fmt, ...)
10236ac495dSmrg     {
10336ac495dSmrg       // XXX Eventually the result should be constructed in-place in
10436ac495dSmrg       // the __cxx11 string, likely with the help of internal hooks.
10536ac495dSmrg       _CharT* __s = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
10636ac495dSmrg 							  * __n));
10736ac495dSmrg 
10836ac495dSmrg       __builtin_va_list __args;
10936ac495dSmrg       __builtin_va_start(__args, __fmt);
11036ac495dSmrg 
11136ac495dSmrg       const int __len = __convf(__s, __n, __fmt, __args);
11236ac495dSmrg 
11336ac495dSmrg       __builtin_va_end(__args);
11436ac495dSmrg 
11536ac495dSmrg       return _String(__s, __s + __len);
11636ac495dSmrg     }
11736ac495dSmrg 
11836ac495dSmrg _GLIBCXX_END_NAMESPACE_VERSION
11936ac495dSmrg } // namespace
12036ac495dSmrg 
12136ac495dSmrg #endif // C++11
12236ac495dSmrg 
12336ac495dSmrg #endif // _STRING_CONVERSIONS_H
124