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