libstdc++
string_conversions.h
Go to the documentation of this file.
00001 // String Conversions -*- C++ -*-
00002 
00003 // Copyright (C) 2008-2019 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 3, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 /** @file ext/string_conversions.h
00026  *  This file is a GNU extension to the Standard C++ Library.
00027  */
00028 
00029 #ifndef _STRING_CONVERSIONS_H
00030 #define _STRING_CONVERSIONS_H 1
00031 
00032 #pragma GCC system_header
00033 
00034 #if __cplusplus < 201103L
00035 # include <bits/c++0x_warning.h>
00036 #else
00037 
00038 #include <bits/c++config.h>
00039 #include <ext/numeric_traits.h>
00040 #include <bits/functexcept.h>
00041 #include <cstdlib>
00042 #include <cwchar>
00043 #include <cstdio>
00044 #include <cerrno>
00045 
00046 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
00047 {
00048 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00049 
00050   // Helper for all the sto* functions.
00051   template<typename _TRet, typename _Ret = _TRet, typename _CharT,
00052            typename... _Base>
00053     _Ret
00054     __stoa(_TRet (*__convf) (const _CharT*, _CharT**, _Base...),
00055            const char* __name, const _CharT* __str, std::size_t* __idx,
00056            _Base... __base)
00057     {
00058       _Ret __ret;
00059 
00060       _CharT* __endptr;
00061 
00062       struct _Save_errno {
00063         _Save_errno() : _M_errno(errno) { errno = 0; }
00064         ~_Save_errno() { if (errno == 0) errno = _M_errno; }
00065         int _M_errno;
00066       } const __save_errno;
00067 
00068       struct _Range_chk {
00069           static bool
00070           _S_chk(_TRet, std::false_type) { return false; }
00071 
00072           static bool
00073           _S_chk(_TRet __val, std::true_type) // only called when _Ret is int
00074           {
00075             return __val < _TRet(__numeric_traits<int>::__min)
00076               || __val > _TRet(__numeric_traits<int>::__max);
00077           }
00078       };
00079 
00080       const _TRet __tmp = __convf(__str, &__endptr, __base...);
00081 
00082       if (__endptr == __str)
00083         std::__throw_invalid_argument(__name);
00084       else if (errno == ERANGE
00085           || _Range_chk::_S_chk(__tmp, std::is_same<_Ret, int>{}))
00086         std::__throw_out_of_range(__name);
00087       else
00088         __ret = __tmp;
00089 
00090       if (__idx)
00091         *__idx = __endptr - __str;
00092 
00093       return __ret;
00094     }
00095 
00096   // Helper for the to_string / to_wstring functions.
00097   template<typename _String, typename _CharT = typename _String::value_type>
00098     _String
00099     __to_xstring(int (*__convf) (_CharT*, std::size_t, const _CharT*,
00100                                  __builtin_va_list), std::size_t __n,
00101                  const _CharT* __fmt, ...)
00102     {
00103       // XXX Eventually the result should be constructed in-place in
00104       // the __cxx11 string, likely with the help of internal hooks.
00105       _CharT* __s = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
00106                                                           * __n));
00107 
00108       __builtin_va_list __args;
00109       __builtin_va_start(__args, __fmt);
00110 
00111       const int __len = __convf(__s, __n, __fmt, __args);
00112 
00113       __builtin_va_end(__args);
00114 
00115       return _String(__s, __s + __len);
00116     }
00117 
00118 _GLIBCXX_END_NAMESPACE_VERSION
00119 } // namespace
00120 
00121 #endif // C++11
00122 
00123 #endif // _STRING_CONVERSIONS_H