aboutsummaryrefslogtreecommitdiff
path: root/libstdc++-v3/src/string_conversions.cc
blob: 627ddfad4132db400a9d9c8a1ef9c1f105f296d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
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