blob: d7ebdd3e5c9a1e464779659397fa5c7d2a26b474 [file] [log] [blame]
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00001//===------------------------- string.cpp ---------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Howard Hinnantee11c312010-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 Hinnanta5f4f8e2010-06-02 18:20:39 +00007//
8//===----------------------------------------------------------------------===//
9
10#include "string"
11#include "cstdlib"
12#include "cwchar"
13#include "cerrno"
Howard Hinnantae2378a2013-05-16 17:13:40 +000014#include "limits"
15#include "stdexcept"
Howard Hinnant84f697e2013-07-23 16:05:56 +000016#include <stdio.h>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +000017
18_LIBCPP_BEGIN_NAMESPACE_STD
19
Shoaib Meenai5ac10672016-09-19 18:29:07 +000020template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS __basic_string_common<true>;
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +000021
Shoaib Meenai5ac10672016-09-19 18:29:07 +000022template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS basic_string<char>;
23template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS basic_string<wchar_t>;
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +000024
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +000025template
26 string
27 operator+<char, char_traits<char>, allocator<char> >(char const*, string const&);
28
Howard Hinnantae2378a2013-05-16 17:13:40 +000029namespace
30{
31
32template<typename T>
33inline
34void throw_helper( const string& msg )
35{
36#ifndef _LIBCPP_NO_EXCEPTIONS
37 throw T( msg );
38#else
Ed Schouten7a441452015-03-10 07:57:43 +000039 fprintf(stderr, "%s\n", msg.c_str());
Marshall Clow8fea1612016-08-25 15:09:01 +000040 _VSTD::abort();
Howard Hinnantae2378a2013-05-16 17:13:40 +000041#endif
42}
43
44inline
45void throw_from_string_out_of_range( const string& func )
46{
47 throw_helper<out_of_range>(func + ": out of range");
48}
49
50inline
51void throw_from_string_invalid_arg( const string& func )
52{
53 throw_helper<invalid_argument>(func + ": no conversion");
54}
55
56// as_integer
57
58template<typename V, typename S, typename F>
59inline
60V
61as_integer_helper(const string& func, const S& str, size_t* idx, int base, F f)
62{
Eric Fiselier002dc672014-11-14 22:23:57 +000063 typename S::value_type* ptr = nullptr;
Howard Hinnantae2378a2013-05-16 17:13:40 +000064 const typename S::value_type* const p = str.c_str();
65 typename remove_reference<decltype(errno)>::type errno_save = errno;
66 errno = 0;
67 V r = f(p, &ptr, base);
68 swap(errno, errno_save);
69 if (errno_save == ERANGE)
70 throw_from_string_out_of_range(func);
71 if (ptr == p)
72 throw_from_string_invalid_arg(func);
73 if (idx)
74 *idx = static_cast<size_t>(ptr - p);
75 return r;
76}
77
78template<typename V, typename S>
79inline
80V
81as_integer(const string& func, const S& s, size_t* idx, int base);
82
83// string
84template<>
85inline
86int
87as_integer(const string& func, const string& s, size_t* idx, int base )
88{
Joerg Sonnenbergere211bc02013-09-17 08:46:53 +000089 // Use long as no Standard string to integer exists.
Howard Hinnantae2378a2013-05-16 17:13:40 +000090 long r = as_integer_helper<long>( func, s, idx, base, strtol );
91 if (r < numeric_limits<int>::min() || numeric_limits<int>::max() < r)
92 throw_from_string_out_of_range(func);
93 return static_cast<int>(r);
94}
95
96template<>
97inline
98long
99as_integer(const string& func, const string& s, size_t* idx, int base )
100{
101 return as_integer_helper<long>( func, s, idx, base, strtol );
102}
103
104template<>
105inline
106unsigned long
107as_integer( const string& func, const string& s, size_t* idx, int base )
108{
109 return as_integer_helper<unsigned long>( func, s, idx, base, strtoul );
110}
111
112template<>
113inline
114long long
115as_integer( const string& func, const string& s, size_t* idx, int base )
116{
117 return as_integer_helper<long long>( func, s, idx, base, strtoll );
118}
119
120template<>
121inline
122unsigned long long
123as_integer( const string& func, const string& s, size_t* idx, int base )
124{
125 return as_integer_helper<unsigned long long>( func, s, idx, base, strtoull );
126}
127
128// wstring
129template<>
130inline
131int
132as_integer( const string& func, const wstring& s, size_t* idx, int base )
133{
134 // Use long as no Stantard string to integer exists.
135 long r = as_integer_helper<long>( func, s, idx, base, wcstol );
136 if (r < numeric_limits<int>::min() || numeric_limits<int>::max() < r)
137 throw_from_string_out_of_range(func);
138 return static_cast<int>(r);
139}
140
141template<>
142inline
143long
144as_integer( const string& func, const wstring& s, size_t* idx, int base )
145{
146 return as_integer_helper<long>( func, s, idx, base, wcstol );
147}
148
149template<>
150inline
151unsigned long
152as_integer( const string& func, const wstring& s, size_t* idx, int base )
153{
154 return as_integer_helper<unsigned long>( func, s, idx, base, wcstoul );
155}
156
157template<>
158inline
159long long
160as_integer( const string& func, const wstring& s, size_t* idx, int base )
161{
162 return as_integer_helper<long long>( func, s, idx, base, wcstoll );
163}
164
165template<>
166inline
167unsigned long long
168as_integer( const string& func, const wstring& s, size_t* idx, int base )
169{
170 return as_integer_helper<unsigned long long>( func, s, idx, base, wcstoull );
171}
172
173// as_float
174
175template<typename V, typename S, typename F>
176inline
177V
178as_float_helper(const string& func, const S& str, size_t* idx, F f )
179{
Eric Fiselier002dc672014-11-14 22:23:57 +0000180 typename S::value_type* ptr = nullptr;
Howard Hinnantae2378a2013-05-16 17:13:40 +0000181 const typename S::value_type* const p = str.c_str();
182 typename remove_reference<decltype(errno)>::type errno_save = errno;
183 errno = 0;
184 V r = f(p, &ptr);
185 swap(errno, errno_save);
186 if (errno_save == ERANGE)
187 throw_from_string_out_of_range(func);
188 if (ptr == p)
189 throw_from_string_invalid_arg(func);
190 if (idx)
191 *idx = static_cast<size_t>(ptr - p);
192 return r;
193}
194
195template<typename V, typename S>
196inline
197V as_float( const string& func, const S& s, size_t* idx = nullptr );
198
199template<>
200inline
201float
202as_float( const string& func, const string& s, size_t* idx )
203{
204 return as_float_helper<float>( func, s, idx, strtof );
205}
206
207template<>
208inline
209double
210as_float(const string& func, const string& s, size_t* idx )
211{
212 return as_float_helper<double>( func, s, idx, strtod );
213}
214
215template<>
216inline
217long double
218as_float( const string& func, const string& s, size_t* idx )
219{
220 return as_float_helper<long double>( func, s, idx, strtold );
221}
222
223template<>
224inline
225float
226as_float( const string& func, const wstring& s, size_t* idx )
227{
228 return as_float_helper<float>( func, s, idx, wcstof );
229}
230
231template<>
232inline
233double
234as_float( const string& func, const wstring& s, size_t* idx )
235{
236 return as_float_helper<double>( func, s, idx, wcstod );
237}
238
239template<>
240inline
241long double
242as_float( const string& func, const wstring& s, size_t* idx )
243{
244 return as_float_helper<long double>( func, s, idx, wcstold );
245}
246
247} // unnamed namespace
248
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000249int
250stoi(const string& str, size_t* idx, int base)
251{
Howard Hinnantae2378a2013-05-16 17:13:40 +0000252 return as_integer<int>( "stoi", str, idx, base );
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000253}
254
255int
256stoi(const wstring& str, size_t* idx, int base)
257{
Howard Hinnantae2378a2013-05-16 17:13:40 +0000258 return as_integer<int>( "stoi", str, idx, base );
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000259}
260
261long
262stol(const string& str, size_t* idx, int base)
263{
Howard Hinnantae2378a2013-05-16 17:13:40 +0000264 return as_integer<long>( "stol", str, idx, base );
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000265}
266
267long
268stol(const wstring& str, size_t* idx, int base)
269{
Howard Hinnantae2378a2013-05-16 17:13:40 +0000270 return as_integer<long>( "stol", str, idx, base );
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000271}
272
273unsigned long
274stoul(const string& str, size_t* idx, int base)
275{
Howard Hinnantae2378a2013-05-16 17:13:40 +0000276 return as_integer<unsigned long>( "stoul", str, idx, base );
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000277}
278
279unsigned long
280stoul(const wstring& str, size_t* idx, int base)
281{
Howard Hinnantae2378a2013-05-16 17:13:40 +0000282 return as_integer<unsigned long>( "stoul", str, idx, base );
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000283}
284
285long long
286stoll(const string& str, size_t* idx, int base)
287{
Howard Hinnantae2378a2013-05-16 17:13:40 +0000288 return as_integer<long long>( "stoll", str, idx, base );
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000289}
290
291long long
292stoll(const wstring& str, size_t* idx, int base)
293{
Howard Hinnantae2378a2013-05-16 17:13:40 +0000294 return as_integer<long long>( "stoll", str, idx, base );
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000295}
296
297unsigned long long
298stoull(const string& str, size_t* idx, int base)
299{
Howard Hinnantae2378a2013-05-16 17:13:40 +0000300 return as_integer<unsigned long long>( "stoull", str, idx, base );
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000301}
302
303unsigned long long
304stoull(const wstring& str, size_t* idx, int base)
305{
Howard Hinnantae2378a2013-05-16 17:13:40 +0000306 return as_integer<unsigned long long>( "stoull", str, idx, base );
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000307}
308
309float
310stof(const string& str, size_t* idx)
311{
Howard Hinnantae2378a2013-05-16 17:13:40 +0000312 return as_float<float>( "stof", str, idx );
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000313}
314
315float
316stof(const wstring& str, size_t* idx)
317{
Howard Hinnantae2378a2013-05-16 17:13:40 +0000318 return as_float<float>( "stof", str, idx );
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000319}
320
321double
322stod(const string& str, size_t* idx)
323{
Howard Hinnantae2378a2013-05-16 17:13:40 +0000324 return as_float<double>( "stod", str, idx );
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000325}
326
327double
328stod(const wstring& str, size_t* idx)
329{
Howard Hinnantae2378a2013-05-16 17:13:40 +0000330 return as_float<double>( "stod", str, idx );
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000331}
332
333long double
334stold(const string& str, size_t* idx)
335{
Howard Hinnantae2378a2013-05-16 17:13:40 +0000336 return as_float<long double>( "stold", str, idx );
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000337}
338
339long double
340stold(const wstring& str, size_t* idx)
341{
Howard Hinnantae2378a2013-05-16 17:13:40 +0000342 return as_float<long double>( "stold", str, idx );
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000343}
344
Howard Hinnantae2378a2013-05-16 17:13:40 +0000345// to_string
346
347namespace
348{
349
350// as_string
351
352template<typename S, typename P, typename V >
353inline
354S
355as_string(P sprintf_like, S s, const typename S::value_type* fmt, V a)
356{
357 typedef typename S::size_type size_type;
358 size_type available = s.size();
359 while (true)
360 {
361 int status = sprintf_like(&s[0], available + 1, fmt, a);
362 if ( status >= 0 )
363 {
364 size_type used = static_cast<size_type>(status);
365 if ( used <= available )
366 {
367 s.resize( used );
368 break;
369 }
370 available = used; // Assume this is advice of how much space we need.
371 }
372 else
373 available = available * 2 + 1;
374 s.resize(available);
375 }
376 return s;
377}
378
379template <class S, class V, bool = is_floating_point<V>::value>
380struct initial_string;
381
382template <class V, bool b>
383struct initial_string<string, V, b>
384{
385 string
386 operator()() const
387 {
388 string s;
389 s.resize(s.capacity());
390 return s;
391 }
392};
393
394template <class V>
395struct initial_string<wstring, V, false>
396{
397 wstring
398 operator()() const
399 {
400 const size_t n = (numeric_limits<unsigned long long>::digits / 3)
401 + ((numeric_limits<unsigned long long>::digits % 3) != 0)
402 + 1;
403 wstring s(n, wchar_t());
404 s.resize(s.capacity());
405 return s;
406 }
407};
408
409template <class V>
410struct initial_string<wstring, V, true>
411{
412 wstring
413 operator()() const
414 {
415 wstring s(20, wchar_t());
416 s.resize(s.capacity());
417 return s;
418 }
419};
420
421typedef int (*wide_printf)(wchar_t* __restrict, size_t, const wchar_t*__restrict, ...);
422
423inline
424wide_printf
425get_swprintf()
426{
Howard Hinnant13d8bc12013-08-01 18:17:34 +0000427#ifndef _LIBCPP_MSVCRT
Howard Hinnantae2378a2013-05-16 17:13:40 +0000428 return swprintf;
429#else
Eric Fiselier4a984f02017-05-10 20:57:45 +0000430 return static_cast<int (__cdecl*)(wchar_t* __restrict, size_t, const wchar_t*__restrict, ...)>(_snwprintf);
Howard Hinnantae2378a2013-05-16 17:13:40 +0000431#endif
432}
433
434} // unnamed namespace
435
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000436string to_string(int val)
437{
Howard Hinnantae2378a2013-05-16 17:13:40 +0000438 return as_string(snprintf, initial_string<string, int>()(), "%d", val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000439}
440
441string to_string(unsigned val)
442{
Howard Hinnantae2378a2013-05-16 17:13:40 +0000443 return as_string(snprintf, initial_string<string, unsigned>()(), "%u", val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000444}
445
446string to_string(long val)
447{
Howard Hinnantae2378a2013-05-16 17:13:40 +0000448 return as_string(snprintf, initial_string<string, long>()(), "%ld", val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000449}
450
451string to_string(unsigned long val)
452{
Howard Hinnantae2378a2013-05-16 17:13:40 +0000453 return as_string(snprintf, initial_string<string, unsigned long>()(), "%lu", val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000454}
455
456string to_string(long long val)
457{
Howard Hinnantae2378a2013-05-16 17:13:40 +0000458 return as_string(snprintf, initial_string<string, long long>()(), "%lld", val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000459}
460
461string to_string(unsigned long long val)
462{
Howard Hinnantae2378a2013-05-16 17:13:40 +0000463 return as_string(snprintf, initial_string<string, unsigned long long>()(), "%llu", val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000464}
465
466string to_string(float val)
467{
Howard Hinnantae2378a2013-05-16 17:13:40 +0000468 return as_string(snprintf, initial_string<string, float>()(), "%f", val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000469}
470
471string to_string(double val)
472{
Howard Hinnantae2378a2013-05-16 17:13:40 +0000473 return as_string(snprintf, initial_string<string, double>()(), "%f", val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000474}
475
476string to_string(long double val)
477{
Howard Hinnantae2378a2013-05-16 17:13:40 +0000478 return as_string(snprintf, initial_string<string, long double>()(), "%Lf", val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000479}
480
481wstring to_wstring(int val)
482{
Howard Hinnantae2378a2013-05-16 17:13:40 +0000483 return as_string(get_swprintf(), initial_string<wstring, int>()(), L"%d", val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000484}
485
486wstring to_wstring(unsigned val)
487{
Howard Hinnantae2378a2013-05-16 17:13:40 +0000488 return as_string(get_swprintf(), initial_string<wstring, unsigned>()(), L"%u", val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000489}
490
491wstring to_wstring(long val)
492{
Howard Hinnantae2378a2013-05-16 17:13:40 +0000493 return as_string(get_swprintf(), initial_string<wstring, long>()(), L"%ld", val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000494}
495
496wstring to_wstring(unsigned long val)
497{
Howard Hinnantae2378a2013-05-16 17:13:40 +0000498 return as_string(get_swprintf(), initial_string<wstring, unsigned long>()(), L"%lu", val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000499}
500
501wstring to_wstring(long long val)
502{
Howard Hinnantae2378a2013-05-16 17:13:40 +0000503 return as_string(get_swprintf(), initial_string<wstring, long long>()(), L"%lld", val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000504}
505
506wstring to_wstring(unsigned long long val)
507{
Howard Hinnantae2378a2013-05-16 17:13:40 +0000508 return as_string(get_swprintf(), initial_string<wstring, unsigned long long>()(), L"%llu", val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000509}
510
511wstring to_wstring(float val)
512{
Howard Hinnantae2378a2013-05-16 17:13:40 +0000513 return as_string(get_swprintf(), initial_string<wstring, float>()(), L"%f", val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000514}
515
516wstring to_wstring(double val)
517{
Howard Hinnantae2378a2013-05-16 17:13:40 +0000518 return as_string(get_swprintf(), initial_string<wstring, double>()(), L"%f", val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000519}
520
521wstring to_wstring(long double val)
522{
Howard Hinnantae2378a2013-05-16 17:13:40 +0000523 return as_string(get_swprintf(), initial_string<wstring, long double>()(), L"%Lf", val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000524}
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000525_LIBCPP_END_NAMESPACE_STD