blob: cd644330b3a775ac8b69dd05cfd126843330ba09 [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"
Howard Hinnante9df0a52013-08-01 18:17:34 +000016#ifdef _LIBCPP_MSVCRT
Howard Hinnant6cd05ee2011-09-23 16:11:27 +000017#include "support/win32/support.h"
Howard Hinnante9df0a52013-08-01 18:17:34 +000018#endif // _LIBCPP_MSVCRT
Howard Hinnanted14a762013-07-23 16:05:56 +000019#include <stdio.h>
Howard Hinnanta6a062d2010-06-02 18:20:39 +000020
21_LIBCPP_BEGIN_NAMESPACE_STD
22
Shoaib Meenaie5cbce42016-09-19 18:29:07 +000023template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS __basic_string_common<true>;
Howard Hinnanta6a062d2010-06-02 18:20:39 +000024
Shoaib Meenaie5cbce42016-09-19 18:29:07 +000025template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS basic_string<char>;
26template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS basic_string<wchar_t>;
Howard Hinnanta6a062d2010-06-02 18:20:39 +000027
Howard Hinnanta6a062d2010-06-02 18:20:39 +000028template
29 string
30 operator+<char, char_traits<char>, allocator<char> >(char const*, string const&);
31
Howard Hinnant9e98b342013-05-16 17:13:40 +000032namespace
33{
34
35template<typename T>
36inline
37void throw_helper( const string& msg )
38{
39#ifndef _LIBCPP_NO_EXCEPTIONS
40 throw T( msg );
41#else
Ed Schoutene5a356a2015-03-10 07:57:43 +000042 fprintf(stderr, "%s\n", msg.c_str());
Marshall Clow14c09a22016-08-25 15:09:01 +000043 _VSTD::abort();
Howard Hinnant9e98b342013-05-16 17:13:40 +000044#endif
45}
46
47inline
48void throw_from_string_out_of_range( const string& func )
49{
50 throw_helper<out_of_range>(func + ": out of range");
51}
52
53inline
54void throw_from_string_invalid_arg( const string& func )
55{
56 throw_helper<invalid_argument>(func + ": no conversion");
57}
58
59// as_integer
60
61template<typename V, typename S, typename F>
62inline
63V
64as_integer_helper(const string& func, const S& str, size_t* idx, int base, F f)
65{
Eric Fiselier017e1aa2014-11-14 22:23:57 +000066 typename S::value_type* ptr = nullptr;
Howard Hinnant9e98b342013-05-16 17:13:40 +000067 const typename S::value_type* const p = str.c_str();
68 typename remove_reference<decltype(errno)>::type errno_save = errno;
69 errno = 0;
70 V r = f(p, &ptr, base);
71 swap(errno, errno_save);
72 if (errno_save == ERANGE)
73 throw_from_string_out_of_range(func);
74 if (ptr == p)
75 throw_from_string_invalid_arg(func);
76 if (idx)
77 *idx = static_cast<size_t>(ptr - p);
78 return r;
79}
80
81template<typename V, typename S>
82inline
83V
84as_integer(const string& func, const S& s, size_t* idx, int base);
85
86// string
87template<>
88inline
89int
90as_integer(const string& func, const string& s, size_t* idx, int base )
91{
Joerg Sonnenbergerbd64f142013-09-17 08:46:53 +000092 // Use long as no Standard string to integer exists.
Howard Hinnant9e98b342013-05-16 17:13:40 +000093 long r = as_integer_helper<long>( func, s, idx, base, strtol );
94 if (r < numeric_limits<int>::min() || numeric_limits<int>::max() < r)
95 throw_from_string_out_of_range(func);
96 return static_cast<int>(r);
97}
98
99template<>
100inline
101long
102as_integer(const string& func, const string& s, size_t* idx, int base )
103{
104 return as_integer_helper<long>( func, s, idx, base, strtol );
105}
106
107template<>
108inline
109unsigned long
110as_integer( const string& func, const string& s, size_t* idx, int base )
111{
112 return as_integer_helper<unsigned long>( func, s, idx, base, strtoul );
113}
114
115template<>
116inline
117long long
118as_integer( const string& func, const string& s, size_t* idx, int base )
119{
120 return as_integer_helper<long long>( func, s, idx, base, strtoll );
121}
122
123template<>
124inline
125unsigned long long
126as_integer( const string& func, const string& s, size_t* idx, int base )
127{
128 return as_integer_helper<unsigned long long>( func, s, idx, base, strtoull );
129}
130
131// wstring
132template<>
133inline
134int
135as_integer( const string& func, const wstring& s, size_t* idx, int base )
136{
137 // Use long as no Stantard string to integer exists.
138 long r = as_integer_helper<long>( func, s, idx, base, wcstol );
139 if (r < numeric_limits<int>::min() || numeric_limits<int>::max() < r)
140 throw_from_string_out_of_range(func);
141 return static_cast<int>(r);
142}
143
144template<>
145inline
146long
147as_integer( const string& func, const wstring& s, size_t* idx, int base )
148{
149 return as_integer_helper<long>( func, s, idx, base, wcstol );
150}
151
152template<>
153inline
154unsigned long
155as_integer( const string& func, const wstring& s, size_t* idx, int base )
156{
157 return as_integer_helper<unsigned long>( func, s, idx, base, wcstoul );
158}
159
160template<>
161inline
162long long
163as_integer( const string& func, const wstring& s, size_t* idx, int base )
164{
165 return as_integer_helper<long long>( func, s, idx, base, wcstoll );
166}
167
168template<>
169inline
170unsigned long long
171as_integer( const string& func, const wstring& s, size_t* idx, int base )
172{
173 return as_integer_helper<unsigned long long>( func, s, idx, base, wcstoull );
174}
175
176// as_float
177
178template<typename V, typename S, typename F>
179inline
180V
181as_float_helper(const string& func, const S& str, size_t* idx, F f )
182{
Eric Fiselier017e1aa2014-11-14 22:23:57 +0000183 typename S::value_type* ptr = nullptr;
Howard Hinnant9e98b342013-05-16 17:13:40 +0000184 const typename S::value_type* const p = str.c_str();
185 typename remove_reference<decltype(errno)>::type errno_save = errno;
186 errno = 0;
187 V r = f(p, &ptr);
188 swap(errno, errno_save);
189 if (errno_save == ERANGE)
190 throw_from_string_out_of_range(func);
191 if (ptr == p)
192 throw_from_string_invalid_arg(func);
193 if (idx)
194 *idx = static_cast<size_t>(ptr - p);
195 return r;
196}
197
198template<typename V, typename S>
199inline
200V as_float( const string& func, const S& s, size_t* idx = nullptr );
201
202template<>
203inline
204float
205as_float( const string& func, const string& s, size_t* idx )
206{
207 return as_float_helper<float>( func, s, idx, strtof );
208}
209
210template<>
211inline
212double
213as_float(const string& func, const string& s, size_t* idx )
214{
215 return as_float_helper<double>( func, s, idx, strtod );
216}
217
218template<>
219inline
220long double
221as_float( const string& func, const string& s, size_t* idx )
222{
223 return as_float_helper<long double>( func, s, idx, strtold );
224}
225
226template<>
227inline
228float
229as_float( const string& func, const wstring& s, size_t* idx )
230{
231 return as_float_helper<float>( func, s, idx, wcstof );
232}
233
234template<>
235inline
236double
237as_float( const string& func, const wstring& s, size_t* idx )
238{
239 return as_float_helper<double>( func, s, idx, wcstod );
240}
241
242template<>
243inline
244long double
245as_float( const string& func, const wstring& s, size_t* idx )
246{
247 return as_float_helper<long double>( func, s, idx, wcstold );
248}
249
250} // unnamed namespace
251
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000252int
253stoi(const string& str, size_t* idx, int base)
254{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000255 return as_integer<int>( "stoi", str, idx, base );
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000256}
257
258int
259stoi(const wstring& str, size_t* idx, int base)
260{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000261 return as_integer<int>( "stoi", str, idx, base );
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000262}
263
264long
265stol(const string& str, size_t* idx, int base)
266{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000267 return as_integer<long>( "stol", str, idx, base );
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000268}
269
270long
271stol(const wstring& str, size_t* idx, int base)
272{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000273 return as_integer<long>( "stol", str, idx, base );
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000274}
275
276unsigned long
277stoul(const string& str, size_t* idx, int base)
278{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000279 return as_integer<unsigned long>( "stoul", str, idx, base );
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000280}
281
282unsigned long
283stoul(const wstring& str, size_t* idx, int base)
284{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000285 return as_integer<unsigned long>( "stoul", str, idx, base );
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000286}
287
288long long
289stoll(const string& str, size_t* idx, int base)
290{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000291 return as_integer<long long>( "stoll", str, idx, base );
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000292}
293
294long long
295stoll(const wstring& str, size_t* idx, int base)
296{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000297 return as_integer<long long>( "stoll", str, idx, base );
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000298}
299
300unsigned long long
301stoull(const string& str, size_t* idx, int base)
302{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000303 return as_integer<unsigned long long>( "stoull", str, idx, base );
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000304}
305
306unsigned long long
307stoull(const wstring& str, size_t* idx, int base)
308{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000309 return as_integer<unsigned long long>( "stoull", str, idx, base );
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000310}
311
312float
313stof(const string& str, size_t* idx)
314{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000315 return as_float<float>( "stof", str, idx );
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000316}
317
318float
319stof(const wstring& str, size_t* idx)
320{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000321 return as_float<float>( "stof", str, idx );
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000322}
323
324double
325stod(const string& str, size_t* idx)
326{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000327 return as_float<double>( "stod", str, idx );
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000328}
329
330double
331stod(const wstring& str, size_t* idx)
332{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000333 return as_float<double>( "stod", str, idx );
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000334}
335
336long double
337stold(const string& str, size_t* idx)
338{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000339 return as_float<long double>( "stold", str, idx );
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000340}
341
342long double
343stold(const wstring& str, size_t* idx)
344{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000345 return as_float<long double>( "stold", str, idx );
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000346}
347
Howard Hinnant9e98b342013-05-16 17:13:40 +0000348// to_string
349
350namespace
351{
352
353// as_string
354
355template<typename S, typename P, typename V >
356inline
357S
358as_string(P sprintf_like, S s, const typename S::value_type* fmt, V a)
359{
360 typedef typename S::size_type size_type;
361 size_type available = s.size();
362 while (true)
363 {
364 int status = sprintf_like(&s[0], available + 1, fmt, a);
365 if ( status >= 0 )
366 {
367 size_type used = static_cast<size_type>(status);
368 if ( used <= available )
369 {
370 s.resize( used );
371 break;
372 }
373 available = used; // Assume this is advice of how much space we need.
374 }
375 else
376 available = available * 2 + 1;
377 s.resize(available);
378 }
379 return s;
380}
381
382template <class S, class V, bool = is_floating_point<V>::value>
383struct initial_string;
384
385template <class V, bool b>
386struct initial_string<string, V, b>
387{
388 string
389 operator()() const
390 {
391 string s;
392 s.resize(s.capacity());
393 return s;
394 }
395};
396
397template <class V>
398struct initial_string<wstring, V, false>
399{
400 wstring
401 operator()() const
402 {
403 const size_t n = (numeric_limits<unsigned long long>::digits / 3)
404 + ((numeric_limits<unsigned long long>::digits % 3) != 0)
405 + 1;
406 wstring s(n, wchar_t());
407 s.resize(s.capacity());
408 return s;
409 }
410};
411
412template <class V>
413struct initial_string<wstring, V, true>
414{
415 wstring
416 operator()() const
417 {
418 wstring s(20, wchar_t());
419 s.resize(s.capacity());
420 return s;
421 }
422};
423
424typedef int (*wide_printf)(wchar_t* __restrict, size_t, const wchar_t*__restrict, ...);
425
426inline
427wide_printf
428get_swprintf()
429{
Howard Hinnante9df0a52013-08-01 18:17:34 +0000430#ifndef _LIBCPP_MSVCRT
Howard Hinnant9e98b342013-05-16 17:13:40 +0000431 return swprintf;
432#else
433 return static_cast<int (__cdecl*)(wchar_t* __restrict, size_t, const wchar_t*__restrict, ...)>(swprintf);
434#endif
435}
436
437} // unnamed namespace
438
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000439string to_string(int val)
440{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000441 return as_string(snprintf, initial_string<string, int>()(), "%d", val);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000442}
443
444string to_string(unsigned val)
445{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000446 return as_string(snprintf, initial_string<string, unsigned>()(), "%u", val);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000447}
448
449string to_string(long val)
450{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000451 return as_string(snprintf, initial_string<string, long>()(), "%ld", val);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000452}
453
454string to_string(unsigned long val)
455{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000456 return as_string(snprintf, initial_string<string, unsigned long>()(), "%lu", val);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000457}
458
459string to_string(long long val)
460{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000461 return as_string(snprintf, initial_string<string, long long>()(), "%lld", val);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000462}
463
464string to_string(unsigned long long val)
465{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000466 return as_string(snprintf, initial_string<string, unsigned long long>()(), "%llu", val);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000467}
468
469string to_string(float val)
470{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000471 return as_string(snprintf, initial_string<string, float>()(), "%f", val);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000472}
473
474string to_string(double val)
475{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000476 return as_string(snprintf, initial_string<string, double>()(), "%f", val);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000477}
478
479string to_string(long double val)
480{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000481 return as_string(snprintf, initial_string<string, long double>()(), "%Lf", val);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000482}
483
484wstring to_wstring(int val)
485{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000486 return as_string(get_swprintf(), initial_string<wstring, int>()(), L"%d", val);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000487}
488
489wstring to_wstring(unsigned val)
490{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000491 return as_string(get_swprintf(), initial_string<wstring, unsigned>()(), L"%u", val);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000492}
493
494wstring to_wstring(long val)
495{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000496 return as_string(get_swprintf(), initial_string<wstring, long>()(), L"%ld", val);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000497}
498
499wstring to_wstring(unsigned long val)
500{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000501 return as_string(get_swprintf(), initial_string<wstring, unsigned long>()(), L"%lu", val);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000502}
503
504wstring to_wstring(long long val)
505{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000506 return as_string(get_swprintf(), initial_string<wstring, long long>()(), L"%lld", val);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000507}
508
509wstring to_wstring(unsigned long long val)
510{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000511 return as_string(get_swprintf(), initial_string<wstring, unsigned long long>()(), L"%llu", val);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000512}
513
514wstring to_wstring(float val)
515{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000516 return as_string(get_swprintf(), initial_string<wstring, float>()(), L"%f", val);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000517}
518
519wstring to_wstring(double val)
520{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000521 return as_string(get_swprintf(), initial_string<wstring, double>()(), L"%f", val);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000522}
523
524wstring to_wstring(long double val)
525{
Howard Hinnant9e98b342013-05-16 17:13:40 +0000526 return as_string(get_swprintf(), initial_string<wstring, long double>()(), L"%Lf", val);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000527}
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000528_LIBCPP_END_NAMESPACE_STD