1 //===------------------------- string.cpp ---------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "string"
11 #include "cstdlib"
12 #include "cwchar"
13 #include "cerrno"
14 #include "limits"
15 #include "stdexcept"
16 #include <stdio.h>
17
18 _LIBCPP_BEGIN_NAMESPACE_STD
19
20 template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS __basic_string_common<true>;
21
22 template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS basic_string<char>;
23 template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS basic_string<wchar_t>;
24
25 template
26 string
27 operator+<char, char_traits<char>, allocator<char> >(char const*, string const&);
28
29 namespace
30 {
31
32 template<typename T>
33 inline
throw_helper(const string & msg)34 void throw_helper( const string& msg )
35 {
36 #ifndef _LIBCPP_NO_EXCEPTIONS
37 throw T( msg );
38 #else
39 fprintf(stderr, "%s\n", msg.c_str());
40 _VSTD::abort();
41 #endif
42 }
43
44 inline
throw_from_string_out_of_range(const string & func)45 void throw_from_string_out_of_range( const string& func )
46 {
47 throw_helper<out_of_range>(func + ": out of range");
48 }
49
50 inline
throw_from_string_invalid_arg(const string & func)51 void throw_from_string_invalid_arg( const string& func )
52 {
53 throw_helper<invalid_argument>(func + ": no conversion");
54 }
55
56 // as_integer
57
58 template<typename V, typename S, typename F>
59 inline
60 V
as_integer_helper(const string & func,const S & str,size_t * idx,int base,F f)61 as_integer_helper(const string& func, const S& str, size_t* idx, int base, F f)
62 {
63 typename S::value_type* ptr = nullptr;
64 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
78 template<typename V, typename S>
79 inline
80 V
81 as_integer(const string& func, const S& s, size_t* idx, int base);
82
83 // string
84 template<>
85 inline
86 int
as_integer(const string & func,const string & s,size_t * idx,int base)87 as_integer(const string& func, const string& s, size_t* idx, int base )
88 {
89 // Use long as no Standard string to integer exists.
90 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
96 template<>
97 inline
98 long
as_integer(const string & func,const string & s,size_t * idx,int base)99 as_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
104 template<>
105 inline
106 unsigned long
as_integer(const string & func,const string & s,size_t * idx,int base)107 as_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
112 template<>
113 inline
114 long long
as_integer(const string & func,const string & s,size_t * idx,int base)115 as_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
120 template<>
121 inline
122 unsigned long long
as_integer(const string & func,const string & s,size_t * idx,int base)123 as_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
129 template<>
130 inline
131 int
as_integer(const string & func,const wstring & s,size_t * idx,int base)132 as_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
141 template<>
142 inline
143 long
as_integer(const string & func,const wstring & s,size_t * idx,int base)144 as_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
149 template<>
150 inline
151 unsigned long
as_integer(const string & func,const wstring & s,size_t * idx,int base)152 as_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
157 template<>
158 inline
159 long long
as_integer(const string & func,const wstring & s,size_t * idx,int base)160 as_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
165 template<>
166 inline
167 unsigned long long
as_integer(const string & func,const wstring & s,size_t * idx,int base)168 as_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
175 template<typename V, typename S, typename F>
176 inline
177 V
as_float_helper(const string & func,const S & str,size_t * idx,F f)178 as_float_helper(const string& func, const S& str, size_t* idx, F f )
179 {
180 typename S::value_type* ptr = nullptr;
181 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
195 template<typename V, typename S>
196 inline
197 V as_float( const string& func, const S& s, size_t* idx = nullptr );
198
199 template<>
200 inline
201 float
as_float(const string & func,const string & s,size_t * idx)202 as_float( const string& func, const string& s, size_t* idx )
203 {
204 return as_float_helper<float>( func, s, idx, strtof );
205 }
206
207 template<>
208 inline
209 double
as_float(const string & func,const string & s,size_t * idx)210 as_float(const string& func, const string& s, size_t* idx )
211 {
212 return as_float_helper<double>( func, s, idx, strtod );
213 }
214
215 template<>
216 inline
217 long double
as_float(const string & func,const string & s,size_t * idx)218 as_float( const string& func, const string& s, size_t* idx )
219 {
220 return as_float_helper<long double>( func, s, idx, strtold );
221 }
222
223 template<>
224 inline
225 float
as_float(const string & func,const wstring & s,size_t * idx)226 as_float( const string& func, const wstring& s, size_t* idx )
227 {
228 return as_float_helper<float>( func, s, idx, wcstof );
229 }
230
231 template<>
232 inline
233 double
as_float(const string & func,const wstring & s,size_t * idx)234 as_float( const string& func, const wstring& s, size_t* idx )
235 {
236 return as_float_helper<double>( func, s, idx, wcstod );
237 }
238
239 template<>
240 inline
241 long double
as_float(const string & func,const wstring & s,size_t * idx)242 as_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
249 int
stoi(const string & str,size_t * idx,int base)250 stoi(const string& str, size_t* idx, int base)
251 {
252 return as_integer<int>( "stoi", str, idx, base );
253 }
254
255 int
stoi(const wstring & str,size_t * idx,int base)256 stoi(const wstring& str, size_t* idx, int base)
257 {
258 return as_integer<int>( "stoi", str, idx, base );
259 }
260
261 long
stol(const string & str,size_t * idx,int base)262 stol(const string& str, size_t* idx, int base)
263 {
264 return as_integer<long>( "stol", str, idx, base );
265 }
266
267 long
stol(const wstring & str,size_t * idx,int base)268 stol(const wstring& str, size_t* idx, int base)
269 {
270 return as_integer<long>( "stol", str, idx, base );
271 }
272
273 unsigned long
stoul(const string & str,size_t * idx,int base)274 stoul(const string& str, size_t* idx, int base)
275 {
276 return as_integer<unsigned long>( "stoul", str, idx, base );
277 }
278
279 unsigned long
stoul(const wstring & str,size_t * idx,int base)280 stoul(const wstring& str, size_t* idx, int base)
281 {
282 return as_integer<unsigned long>( "stoul", str, idx, base );
283 }
284
285 long long
stoll(const string & str,size_t * idx,int base)286 stoll(const string& str, size_t* idx, int base)
287 {
288 return as_integer<long long>( "stoll", str, idx, base );
289 }
290
291 long long
stoll(const wstring & str,size_t * idx,int base)292 stoll(const wstring& str, size_t* idx, int base)
293 {
294 return as_integer<long long>( "stoll", str, idx, base );
295 }
296
297 unsigned long long
stoull(const string & str,size_t * idx,int base)298 stoull(const string& str, size_t* idx, int base)
299 {
300 return as_integer<unsigned long long>( "stoull", str, idx, base );
301 }
302
303 unsigned long long
stoull(const wstring & str,size_t * idx,int base)304 stoull(const wstring& str, size_t* idx, int base)
305 {
306 return as_integer<unsigned long long>( "stoull", str, idx, base );
307 }
308
309 float
stof(const string & str,size_t * idx)310 stof(const string& str, size_t* idx)
311 {
312 return as_float<float>( "stof", str, idx );
313 }
314
315 float
stof(const wstring & str,size_t * idx)316 stof(const wstring& str, size_t* idx)
317 {
318 return as_float<float>( "stof", str, idx );
319 }
320
321 double
stod(const string & str,size_t * idx)322 stod(const string& str, size_t* idx)
323 {
324 return as_float<double>( "stod", str, idx );
325 }
326
327 double
stod(const wstring & str,size_t * idx)328 stod(const wstring& str, size_t* idx)
329 {
330 return as_float<double>( "stod", str, idx );
331 }
332
333 long double
stold(const string & str,size_t * idx)334 stold(const string& str, size_t* idx)
335 {
336 return as_float<long double>( "stold", str, idx );
337 }
338
339 long double
stold(const wstring & str,size_t * idx)340 stold(const wstring& str, size_t* idx)
341 {
342 return as_float<long double>( "stold", str, idx );
343 }
344
345 // to_string
346
347 namespace
348 {
349
350 // as_string
351
352 template<typename S, typename P, typename V >
353 inline
354 S
as_string(P sprintf_like,S s,const typename S::value_type * fmt,V a)355 as_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
379 template <class S, class V, bool = is_floating_point<V>::value>
380 struct initial_string;
381
382 template <class V, bool b>
383 struct initial_string<string, V, b>
384 {
385 string
operator ()__anon98ee7c860211::initial_string386 operator()() const
387 {
388 string s;
389 s.resize(s.capacity());
390 return s;
391 }
392 };
393
394 template <class V>
395 struct initial_string<wstring, V, false>
396 {
397 wstring
operator ()__anon98ee7c860211::initial_string398 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
409 template <class V>
410 struct initial_string<wstring, V, true>
411 {
412 wstring
operator ()__anon98ee7c860211::initial_string413 operator()() const
414 {
415 wstring s(20, wchar_t());
416 s.resize(s.capacity());
417 return s;
418 }
419 };
420
421 typedef int (*wide_printf)(wchar_t* __restrict, size_t, const wchar_t*__restrict, ...);
422
423 inline
424 wide_printf
get_swprintf()425 get_swprintf()
426 {
427 #ifndef _LIBCPP_MSVCRT
428 return swprintf;
429 #else
430 return static_cast<int (__cdecl*)(wchar_t* __restrict, size_t, const wchar_t*__restrict, ...)>(_snwprintf);
431 #endif
432 }
433
434 } // unnamed namespace
435
to_string(int val)436 string to_string(int val)
437 {
438 return as_string(snprintf, initial_string<string, int>()(), "%d", val);
439 }
440
to_string(unsigned val)441 string to_string(unsigned val)
442 {
443 return as_string(snprintf, initial_string<string, unsigned>()(), "%u", val);
444 }
445
to_string(long val)446 string to_string(long val)
447 {
448 return as_string(snprintf, initial_string<string, long>()(), "%ld", val);
449 }
450
to_string(unsigned long val)451 string to_string(unsigned long val)
452 {
453 return as_string(snprintf, initial_string<string, unsigned long>()(), "%lu", val);
454 }
455
to_string(long long val)456 string to_string(long long val)
457 {
458 return as_string(snprintf, initial_string<string, long long>()(), "%lld", val);
459 }
460
to_string(unsigned long long val)461 string to_string(unsigned long long val)
462 {
463 return as_string(snprintf, initial_string<string, unsigned long long>()(), "%llu", val);
464 }
465
to_string(float val)466 string to_string(float val)
467 {
468 return as_string(snprintf, initial_string<string, float>()(), "%f", val);
469 }
470
to_string(double val)471 string to_string(double val)
472 {
473 return as_string(snprintf, initial_string<string, double>()(), "%f", val);
474 }
475
to_string(long double val)476 string to_string(long double val)
477 {
478 return as_string(snprintf, initial_string<string, long double>()(), "%Lf", val);
479 }
480
to_wstring(int val)481 wstring to_wstring(int val)
482 {
483 return as_string(get_swprintf(), initial_string<wstring, int>()(), L"%d", val);
484 }
485
to_wstring(unsigned val)486 wstring to_wstring(unsigned val)
487 {
488 return as_string(get_swprintf(), initial_string<wstring, unsigned>()(), L"%u", val);
489 }
490
to_wstring(long val)491 wstring to_wstring(long val)
492 {
493 return as_string(get_swprintf(), initial_string<wstring, long>()(), L"%ld", val);
494 }
495
to_wstring(unsigned long val)496 wstring to_wstring(unsigned long val)
497 {
498 return as_string(get_swprintf(), initial_string<wstring, unsigned long>()(), L"%lu", val);
499 }
500
to_wstring(long long val)501 wstring to_wstring(long long val)
502 {
503 return as_string(get_swprintf(), initial_string<wstring, long long>()(), L"%lld", val);
504 }
505
to_wstring(unsigned long long val)506 wstring to_wstring(unsigned long long val)
507 {
508 return as_string(get_swprintf(), initial_string<wstring, unsigned long long>()(), L"%llu", val);
509 }
510
to_wstring(float val)511 wstring to_wstring(float val)
512 {
513 return as_string(get_swprintf(), initial_string<wstring, float>()(), L"%f", val);
514 }
515
to_wstring(double val)516 wstring to_wstring(double val)
517 {
518 return as_string(get_swprintf(), initial_string<wstring, double>()(), L"%f", val);
519 }
520
to_wstring(long double val)521 wstring to_wstring(long double val)
522 {
523 return as_string(get_swprintf(), initial_string<wstring, long double>()(), L"%Lf", val);
524 }
525 _LIBCPP_END_NAMESPACE_STD
526