blob: c6fe408ca1ca2feaa1d320290333c7be4ac530c0 [file] [log] [blame]
Howard Hinnanta6a062d2010-06-02 18:20:39 +00001//===------------------------- string.cpp ---------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00005// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
Howard Hinnanta6a062d2010-06-02 18:20:39 +00007//
8//===----------------------------------------------------------------------===//
9
10#include "string"
11#include "cstdlib"
12#include "cwchar"
13#include "cerrno"
Howard Hinnant9e98b342013-05-16 17:13:40 +000014#include "limits"
15#include "stdexcept"
Marshall Clowa22d2ad2013-03-18 17:04:29 +000016#ifdef _WIN32
Howard Hinnant6cd05ee2011-09-23 16:11:27 +000017#include "support/win32/support.h"
18#endif // _WIN32
Howard Hinnanta6a062d2010-06-02 18:20:39 +000019
20_LIBCPP_BEGIN_NAMESPACE_STD
21
22template class __basic_string_common<true>;
23
24template class basic_string<char>;
25template class basic_string<wchar_t>;
26
Howard Hinnanta6a062d2010-06-02 18:20:39 +000027template
28 string
29 operator+<char, char_traits<char>, allocator<char> >(char const*, string const&);
30
Howard Hinnant9e98b342013-05-16 17:13:40 +000031namespace
32{
33
34template<typename T>
35inline
36void throw_helper( const string& msg )
37{
38#ifndef _LIBCPP_NO_EXCEPTIONS
39 throw T( msg );
40#else
41 abort();
42#endif
43}
44
45inline
46void throw_from_string_out_of_range( const string& func )
47{
48 throw_helper<out_of_range>(func + ": out of range");
49}
50
51inline
52void throw_from_string_invalid_arg( const string& func )
53{
54 throw_helper<invalid_argument>(func + ": no conversion");
55}
56
57// as_integer
58
59template<typename V, typename S, typename F>
60inline
61V
62as_integer_helper(const string& func, const S& str, size_t* idx, int base, F f)
63{
64 typename S::value_type* ptr;
65 const typename S::value_type* const p = str.c_str();
66 typename remove_reference<decltype(errno)>::type errno_save = errno;
67 errno = 0;
68 V r = f(p, &ptr, base);
69 swap(errno, errno_save);
70 if (errno_save == ERANGE)
71 throw_from_string_out_of_range(func);
72 if (ptr == p)
73 throw_from_string_invalid_arg(func);
74 if (idx)
75 *idx = static_cast<size_t>(ptr - p);
76 return r;
77}
78
79template<typename V, typename S>
80inline
81V
82as_integer(const string& func, const S& s, size_t* idx, int base);
83
84// string
85template<>
86inline
87int
88as_integer(const string& func, const string& s, size_t* idx, int base )
89{
90 // Use long as no Stantard string to integer exists.
91 long r = as_integer_helper<long>( func, s, idx, base, strtol );
92 if (r < numeric_limits<int>::min() || numeric_limits<int>::max() < r)
93 throw_from_string_out_of_range(func);
94 return static_cast<int>(r);
95}
96
97template<>
98inline
99long
100as_integer(const string& func, const string& s, size_t* idx, int base )
101{
102 return as_integer_helper<long>( func, s, idx, base, strtol );
103}
104
105template<>
106inline
107unsigned long
108as_integer( const string& func, const string& s, size_t* idx, int base )
109{
110 return as_integer_helper<unsigned long>( func, s, idx, base, strtoul );
111}
112
113template<>
114inline
115long long
116as_integer( const string& func, const string& s, size_t* idx, int base )
117{
118 return as_integer_helper<long long>( func, s, idx, base, strtoll );
119}
120
121template<>
122inline
123unsigned long long
124as_integer( const string& func, const string& s, size_t* idx, int base )
125{
126 return as_integer_helper<unsigned long long>( func, s, idx, base, strtoull );
127}
128
129// wstring
130template<>
131inline
132int
133as_integer( const string& func, const wstring& s, size_t* idx, int base )
134{
135 // Use long as no Stantard string to integer exists.
136 long r = as_integer_helper<long>( func, s, idx, base, wcstol );
137 if (r < numeric_limits<int>::min() || numeric_limits<int>::max() < r)
138 throw_from_string_out_of_range(func);
139 return static_cast<int>(r);
140}
141
142template<>
143inline
144long
145as_integer( const string& func, const wstring& s, size_t* idx, int base )
146{
147 return as_integer_helper<long>( func, s, idx, base, wcstol );
148}
149
150template<>
151inline
152unsigned long
153as_integer( const string& func, const wstring& s, size_t* idx, int base )
154{
155 return as_integer_helper<unsigned long>( func, s, idx, base, wcstoul );
156}
157
158template<>
159inline
160long long
161as_integer( const string& func, const wstring& s, size_t* idx, int base )
162{
163 return as_integer_helper<long long>( func, s, idx, base, wcstoll );
164}
165
166template<>
167inline
168unsigned long long
169as_integer( const string& func, const wstring& s, size_t* idx, int base )
170{
171 return as_integer_helper<unsigned long long>( func, s, idx, base, wcstoull );
172}
173
174// as_float
175
176template<typename V, typename S, typename F>
177inline
178V
179as_float_helper(const string& func, const S& str, size_t* idx, F f )
180{
181 typename S::value_type* ptr;
182 const typename S::value_type* const p = str.c_str();
183 typename remove_reference<decltype(errno)>::type errno_save = errno;
184 errno = 0;
185 V r = f(p, &ptr);
186 swap(errno, errno_save);
187 if (errno_save == ERANGE)
188 throw_from_string_out_of_range(func);
189 if (ptr == p)
190 throw_from_string_invalid_arg(func);
191 if (idx)
192 *idx = static_cast<size_t>(ptr - p);
193 return r;
194}
195
196template<typename V, typename S>
197inline
198V as_float( const string& func, const S& s, size_t* idx = nullptr );
199
200template<>
201inline
202float
203as_float( const string& func, const string& s, size_t* idx )
204{
205 return as_float_helper<float>( func, s, idx, strtof );
206}
207
208template<>
209inline
210double
211as_float(const string& func, const string& s, size_t* idx )
212{
213 return as_float_helper<double>( func, s, idx, strtod );
214}
215
216template<>
217inline
218long double
219as_float( const string& func, const string& s, size_t* idx )
220{
221 return as_float_helper<long double>( func, s, idx, strtold );
222}
223
224template<>
225inline
226float
227as_float( const string& func, const wstring& s, size_t* idx )
228{
229 return as_float_helper<float>( func, s, idx, wcstof );
230}
231
232template<>
233inline
234double
235as_float( const string& func, const wstring& s, size_t* idx )
236{
237 return as_float_helper<double>( func, s, idx, wcstod );
238}
239
240template<>
241inline
242long double
243as_float( const string& func, const wstring& s, size_t* idx )
244{
245 return as_float_helper<long double>( func, s, idx, wcstold );
246}
247
248} // unnamed namespace
249
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000250int
251stoi(const string& str, size_t* idx, int base)
252{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000253 return as_integer<int>( "stoi", str, idx, base );
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000254}
255
256int
257stoi(const wstring& str, size_t* idx, int base)
258{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000259 return as_integer<int>( "stoi", str, idx, base );
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000260}
261
262long
263stol(const string& str, size_t* idx, int base)
264{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000265 return as_integer<long>( "stol", str, idx, base );
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000266}
267
268long
269stol(const wstring& str, size_t* idx, int base)
270{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000271 return as_integer<long>( "stol", str, idx, base );
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000272}
273
274unsigned long
275stoul(const string& str, size_t* idx, int base)
276{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000277 return as_integer<unsigned long>( "stoul", str, idx, base );
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000278}
279
280unsigned long
281stoul(const wstring& str, size_t* idx, int base)
282{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000283 return as_integer<unsigned long>( "stoul", str, idx, base );
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000284}
285
286long long
287stoll(const string& str, size_t* idx, int base)
288{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000289 return as_integer<long long>( "stoll", str, idx, base );
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000290}
291
292long long
293stoll(const wstring& str, size_t* idx, int base)
294{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000295 return as_integer<long long>( "stoll", str, idx, base );
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000296}
297
298unsigned long long
299stoull(const string& str, size_t* idx, int base)
300{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000301 return as_integer<unsigned long long>( "stoull", str, idx, base );
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000302}
303
304unsigned long long
305stoull(const wstring& str, size_t* idx, int base)
306{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000307 return as_integer<unsigned long long>( "stoull", str, idx, base );
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000308}
309
310float
311stof(const string& str, size_t* idx)
312{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000313 return as_float<float>( "stof", str, idx );
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000314}
315
316float
317stof(const wstring& str, size_t* idx)
318{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000319 return as_float<float>( "stof", str, idx );
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000320}
321
322double
323stod(const string& str, size_t* idx)
324{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000325 return as_float<double>( "stod", str, idx );
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000326}
327
328double
329stod(const wstring& str, size_t* idx)
330{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000331 return as_float<double>( "stod", str, idx );
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000332}
333
334long double
335stold(const string& str, size_t* idx)
336{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000337 return as_float<long double>( "stold", str, idx );
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000338}
339
340long double
341stold(const wstring& str, size_t* idx)
342{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000343 return as_float<long double>( "stold", str, idx );
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000344}
345
Howard Hinnant9e98b342013-05-16 17:13:40 +0000346// to_string
347
348namespace
349{
350
351// as_string
352
353template<typename S, typename P, typename V >
354inline
355S
356as_string(P sprintf_like, S s, const typename S::value_type* fmt, V a)
357{
358 typedef typename S::size_type size_type;
359 size_type available = s.size();
360 while (true)
361 {
362 int status = sprintf_like(&s[0], available + 1, fmt, a);
363 if ( status >= 0 )
364 {
365 size_type used = static_cast<size_type>(status);
366 if ( used <= available )
367 {
368 s.resize( used );
369 break;
370 }
371 available = used; // Assume this is advice of how much space we need.
372 }
373 else
374 available = available * 2 + 1;
375 s.resize(available);
376 }
377 return s;
378}
379
380template <class S, class V, bool = is_floating_point<V>::value>
381struct initial_string;
382
383template <class V, bool b>
384struct initial_string<string, V, b>
385{
386 string
387 operator()() const
388 {
389 string s;
390 s.resize(s.capacity());
391 return s;
392 }
393};
394
395template <class V>
396struct initial_string<wstring, V, false>
397{
398 wstring
399 operator()() const
400 {
401 const size_t n = (numeric_limits<unsigned long long>::digits / 3)
402 + ((numeric_limits<unsigned long long>::digits % 3) != 0)
403 + 1;
404 wstring s(n, wchar_t());
405 s.resize(s.capacity());
406 return s;
407 }
408};
409
410template <class V>
411struct initial_string<wstring, V, true>
412{
413 wstring
414 operator()() const
415 {
416 wstring s(20, wchar_t());
417 s.resize(s.capacity());
418 return s;
419 }
420};
421
422typedef int (*wide_printf)(wchar_t* __restrict, size_t, const wchar_t*__restrict, ...);
423
424inline
425wide_printf
426get_swprintf()
427{
428#ifndef _WIN32
429 return swprintf;
430#else
431 return static_cast<int (__cdecl*)(wchar_t* __restrict, size_t, const wchar_t*__restrict, ...)>(swprintf);
432#endif
433}
434
435} // unnamed namespace
436
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000437string to_string(int val)
438{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000439 return as_string(snprintf, initial_string<string, int>()(), "%d", val);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000440}
441
442string to_string(unsigned val)
443{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000444 return as_string(snprintf, initial_string<string, unsigned>()(), "%u", val);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000445}
446
447string to_string(long val)
448{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000449 return as_string(snprintf, initial_string<string, long>()(), "%ld", val);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000450}
451
452string to_string(unsigned long val)
453{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000454 return as_string(snprintf, initial_string<string, unsigned long>()(), "%lu", val);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000455}
456
457string to_string(long long val)
458{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000459 return as_string(snprintf, initial_string<string, long long>()(), "%lld", val);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000460}
461
462string to_string(unsigned long long val)
463{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000464 return as_string(snprintf, initial_string<string, unsigned long long>()(), "%llu", val);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000465}
466
467string to_string(float val)
468{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000469 return as_string(snprintf, initial_string<string, float>()(), "%f", val);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000470}
471
472string to_string(double val)
473{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000474 return as_string(snprintf, initial_string<string, double>()(), "%f", val);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000475}
476
477string to_string(long double val)
478{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000479 return as_string(snprintf, initial_string<string, long double>()(), "%Lf", val);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000480}
481
482wstring to_wstring(int val)
483{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000484 return as_string(get_swprintf(), initial_string<wstring, int>()(), L"%d", val);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000485}
486
487wstring to_wstring(unsigned val)
488{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000489 return as_string(get_swprintf(), initial_string<wstring, unsigned>()(), L"%u", val);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000490}
491
492wstring to_wstring(long val)
493{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000494 return as_string(get_swprintf(), initial_string<wstring, long>()(), L"%ld", val);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000495}
496
497wstring to_wstring(unsigned long val)
498{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000499 return as_string(get_swprintf(), initial_string<wstring, unsigned long>()(), L"%lu", val);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000500}
501
502wstring to_wstring(long long val)
503{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000504 return as_string(get_swprintf(), initial_string<wstring, long long>()(), L"%lld", val);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000505}
506
507wstring to_wstring(unsigned long long val)
508{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000509 return as_string(get_swprintf(), initial_string<wstring, unsigned long long>()(), L"%llu", val);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000510}
511
512wstring to_wstring(float val)
513{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000514 return as_string(get_swprintf(), initial_string<wstring, float>()(), L"%f", val);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000515}
516
517wstring to_wstring(double val)
518{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000519 return as_string(get_swprintf(), initial_string<wstring, double>()(), L"%f", val);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000520}
521
522wstring to_wstring(long double val)
523{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000524 return as_string(get_swprintf(), initial_string<wstring, long double>()(), L"%Lf", val);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000525}
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000526_LIBCPP_END_NAMESPACE_STD