1*38fd1498Szrj // String Conversions -*- C++ -*-
2*38fd1498Szrj
3*38fd1498Szrj // Copyright (C) 2008-2018 Free Software Foundation, Inc.
4*38fd1498Szrj //
5*38fd1498Szrj // This file is part of the GNU ISO C++ Library. This library is free
6*38fd1498Szrj // software; you can redistribute it and/or modify it under the
7*38fd1498Szrj // terms of the GNU General Public License as published by the
8*38fd1498Szrj // Free Software Foundation; either version 3, or (at your option)
9*38fd1498Szrj // any later version.
10*38fd1498Szrj
11*38fd1498Szrj // This library is distributed in the hope that it will be useful,
12*38fd1498Szrj // but WITHOUT ANY WARRANTY; without even the implied warranty of
13*38fd1498Szrj // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14*38fd1498Szrj // GNU General Public License for more details.
15*38fd1498Szrj
16*38fd1498Szrj // Under Section 7 of GPL version 3, you are granted additional
17*38fd1498Szrj // permissions described in the GCC Runtime Library Exception, version
18*38fd1498Szrj // 3.1, as published by the Free Software Foundation.
19*38fd1498Szrj
20*38fd1498Szrj // You should have received a copy of the GNU General Public License and
21*38fd1498Szrj // a copy of the GCC Runtime Library Exception along with this program;
22*38fd1498Szrj // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23*38fd1498Szrj // <http://www.gnu.org/licenses/>.
24*38fd1498Szrj
25*38fd1498Szrj /** @file ext/string_conversions.h
26*38fd1498Szrj * This file is a GNU extension to the Standard C++ Library.
27*38fd1498Szrj */
28*38fd1498Szrj
29*38fd1498Szrj #ifndef _STRING_CONVERSIONS_H
30*38fd1498Szrj #define _STRING_CONVERSIONS_H 1
31*38fd1498Szrj
32*38fd1498Szrj #pragma GCC system_header
33*38fd1498Szrj
34*38fd1498Szrj #if __cplusplus < 201103L
35*38fd1498Szrj # include <bits/c++0x_warning.h>
36*38fd1498Szrj #else
37*38fd1498Szrj
38*38fd1498Szrj #include <bits/c++config.h>
39*38fd1498Szrj #include <ext/numeric_traits.h>
40*38fd1498Szrj #include <bits/functexcept.h>
41*38fd1498Szrj #include <cstdlib>
42*38fd1498Szrj #include <cwchar>
43*38fd1498Szrj #include <cstdio>
44*38fd1498Szrj #include <cerrno>
45*38fd1498Szrj
_GLIBCXX_VISIBILITY(default)46*38fd1498Szrj namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
47*38fd1498Szrj {
48*38fd1498Szrj _GLIBCXX_BEGIN_NAMESPACE_VERSION
49*38fd1498Szrj
50*38fd1498Szrj // Helper for all the sto* functions.
51*38fd1498Szrj template<typename _TRet, typename _Ret = _TRet, typename _CharT,
52*38fd1498Szrj typename... _Base>
53*38fd1498Szrj _Ret
54*38fd1498Szrj __stoa(_TRet (*__convf) (const _CharT*, _CharT**, _Base...),
55*38fd1498Szrj const char* __name, const _CharT* __str, std::size_t* __idx,
56*38fd1498Szrj _Base... __base)
57*38fd1498Szrj {
58*38fd1498Szrj _Ret __ret;
59*38fd1498Szrj
60*38fd1498Szrj _CharT* __endptr;
61*38fd1498Szrj
62*38fd1498Szrj struct _Save_errno {
63*38fd1498Szrj _Save_errno() : _M_errno(errno) { errno = 0; }
64*38fd1498Szrj ~_Save_errno() { if (errno == 0) errno = _M_errno; }
65*38fd1498Szrj int _M_errno;
66*38fd1498Szrj } const __save_errno;
67*38fd1498Szrj
68*38fd1498Szrj struct _Range_chk {
69*38fd1498Szrj static bool
70*38fd1498Szrj _S_chk(_TRet, std::false_type) { return false; }
71*38fd1498Szrj
72*38fd1498Szrj static bool
73*38fd1498Szrj _S_chk(_TRet __val, std::true_type) // only called when _Ret is int
74*38fd1498Szrj {
75*38fd1498Szrj return __val < _TRet(__numeric_traits<int>::__min)
76*38fd1498Szrj || __val > _TRet(__numeric_traits<int>::__max);
77*38fd1498Szrj }
78*38fd1498Szrj };
79*38fd1498Szrj
80*38fd1498Szrj const _TRet __tmp = __convf(__str, &__endptr, __base...);
81*38fd1498Szrj
82*38fd1498Szrj if (__endptr == __str)
83*38fd1498Szrj std::__throw_invalid_argument(__name);
84*38fd1498Szrj else if (errno == ERANGE
85*38fd1498Szrj || _Range_chk::_S_chk(__tmp, std::is_same<_Ret, int>{}))
86*38fd1498Szrj std::__throw_out_of_range(__name);
87*38fd1498Szrj else
88*38fd1498Szrj __ret = __tmp;
89*38fd1498Szrj
90*38fd1498Szrj if (__idx)
91*38fd1498Szrj *__idx = __endptr - __str;
92*38fd1498Szrj
93*38fd1498Szrj return __ret;
94*38fd1498Szrj }
95*38fd1498Szrj
96*38fd1498Szrj // Helper for the to_string / to_wstring functions.
97*38fd1498Szrj template<typename _String, typename _CharT = typename _String::value_type>
98*38fd1498Szrj _String
99*38fd1498Szrj __to_xstring(int (*__convf) (_CharT*, std::size_t, const _CharT*,
100*38fd1498Szrj __builtin_va_list), std::size_t __n,
101*38fd1498Szrj const _CharT* __fmt, ...)
102*38fd1498Szrj {
103*38fd1498Szrj // XXX Eventually the result should be constructed in-place in
104*38fd1498Szrj // the __cxx11 string, likely with the help of internal hooks.
105*38fd1498Szrj _CharT* __s = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
106*38fd1498Szrj * __n));
107*38fd1498Szrj
108*38fd1498Szrj __builtin_va_list __args;
109*38fd1498Szrj __builtin_va_start(__args, __fmt);
110*38fd1498Szrj
111*38fd1498Szrj const int __len = __convf(__s, __n, __fmt, __args);
112*38fd1498Szrj
113*38fd1498Szrj __builtin_va_end(__args);
114*38fd1498Szrj
115*38fd1498Szrj return _String(__s, __s + __len);
116*38fd1498Szrj }
117*38fd1498Szrj
118*38fd1498Szrj _GLIBCXX_END_NAMESPACE_VERSION
119*38fd1498Szrj } // namespace
120*38fd1498Szrj
121*38fd1498Szrj #endif // C++11
122*38fd1498Szrj
123*38fd1498Szrj #endif // _STRING_CONVERSIONS_H
124