1 //===------------------------- string.cpp ---------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
17 #include "support/win32/support.h"
20 _LIBCPP_BEGIN_NAMESPACE_STD
22 template class __basic_string_common<true>;
24 template class basic_string<char>;
25 template class basic_string<wchar_t>;
29 operator+<char, char_traits<char>, allocator<char> >(char const*, string const&);
36 void throw_helper( const string& msg )
38 #ifndef _LIBCPP_NO_EXCEPTIONS
46 void throw_from_string_out_of_range( const string& func )
48 throw_helper<out_of_range>(func + ": out of range");
52 void throw_from_string_invalid_arg( const string& func )
54 throw_helper<invalid_argument>(func + ": no conversion");
59 template<typename V, typename S, typename F>
62 as_integer_helper(const string& func, const S& str, size_t* idx, int base, F f)
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;
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);
73 throw_from_string_invalid_arg(func);
75 *idx = static_cast<size_t>(ptr - p);
79 template<typename V, typename S>
82 as_integer(const string& func, const S& s, size_t* idx, int base);
88 as_integer(const string& func, const string& s, size_t* idx, int base )
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);
100 as_integer(const string& func, const string& s, size_t* idx, int base )
102 return as_integer_helper<long>( func, s, idx, base, strtol );
108 as_integer( const string& func, const string& s, size_t* idx, int base )
110 return as_integer_helper<unsigned long>( func, s, idx, base, strtoul );
116 as_integer( const string& func, const string& s, size_t* idx, int base )
118 return as_integer_helper<long long>( func, s, idx, base, strtoll );
124 as_integer( const string& func, const string& s, size_t* idx, int base )
126 return as_integer_helper<unsigned long long>( func, s, idx, base, strtoull );
133 as_integer( const string& func, const wstring& s, size_t* idx, int base )
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);
145 as_integer( const string& func, const wstring& s, size_t* idx, int base )
147 return as_integer_helper<long>( func, s, idx, base, wcstol );
153 as_integer( const string& func, const wstring& s, size_t* idx, int base )
155 return as_integer_helper<unsigned long>( func, s, idx, base, wcstoul );
161 as_integer( const string& func, const wstring& s, size_t* idx, int base )
163 return as_integer_helper<long long>( func, s, idx, base, wcstoll );
169 as_integer( const string& func, const wstring& s, size_t* idx, int base )
171 return as_integer_helper<unsigned long long>( func, s, idx, base, wcstoull );
176 template<typename V, typename S, typename F>
179 as_float_helper(const string& func, const S& str, size_t* idx, F f )
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;
186 swap(errno, errno_save);
187 if (errno_save == ERANGE)
188 throw_from_string_out_of_range(func);
190 throw_from_string_invalid_arg(func);
192 *idx = static_cast<size_t>(ptr - p);
196 template<typename V, typename S>
198 V as_float( const string& func, const S& s, size_t* idx = nullptr );
203 as_float( const string& func, const string& s, size_t* idx )
205 return as_float_helper<float>( func, s, idx, strtof );
211 as_float(const string& func, const string& s, size_t* idx )
213 return as_float_helper<double>( func, s, idx, strtod );
219 as_float( const string& func, const string& s, size_t* idx )
221 return as_float_helper<long double>( func, s, idx, strtold );
227 as_float( const string& func, const wstring& s, size_t* idx )
229 return as_float_helper<float>( func, s, idx, wcstof );
235 as_float( const string& func, const wstring& s, size_t* idx )
237 return as_float_helper<double>( func, s, idx, wcstod );
243 as_float( const string& func, const wstring& s, size_t* idx )
245 return as_float_helper<long double>( func, s, idx, wcstold );
248 } // unnamed namespace
251 stoi(const string& str, size_t* idx, int base)
253 return as_integer<int>( "stoi", str, idx, base );
257 stoi(const wstring& str, size_t* idx, int base)
259 return as_integer<int>( "stoi", str, idx, base );
263 stol(const string& str, size_t* idx, int base)
265 return as_integer<long>( "stol", str, idx, base );
269 stol(const wstring& str, size_t* idx, int base)
271 return as_integer<long>( "stol", str, idx, base );
275 stoul(const string& str, size_t* idx, int base)
277 return as_integer<unsigned long>( "stoul", str, idx, base );
281 stoul(const wstring& str, size_t* idx, int base)
283 return as_integer<unsigned long>( "stoul", str, idx, base );
287 stoll(const string& str, size_t* idx, int base)
289 return as_integer<long long>( "stoll", str, idx, base );
293 stoll(const wstring& str, size_t* idx, int base)
295 return as_integer<long long>( "stoll", str, idx, base );
299 stoull(const string& str, size_t* idx, int base)
301 return as_integer<unsigned long long>( "stoull", str, idx, base );
305 stoull(const wstring& str, size_t* idx, int base)
307 return as_integer<unsigned long long>( "stoull", str, idx, base );
311 stof(const string& str, size_t* idx)
313 return as_float<float>( "stof", str, idx );
317 stof(const wstring& str, size_t* idx)
319 return as_float<float>( "stof", str, idx );
323 stod(const string& str, size_t* idx)
325 return as_float<double>( "stod", str, idx );
329 stod(const wstring& str, size_t* idx)
331 return as_float<double>( "stod", str, idx );
335 stold(const string& str, size_t* idx)
337 return as_float<long double>( "stold", str, idx );
341 stold(const wstring& str, size_t* idx)
343 return as_float<long double>( "stold", str, idx );
353 template<typename S, typename P, typename V >
356 as_string(P sprintf_like, S s, const typename S::value_type* fmt, V a)
358 typedef typename S::size_type size_type;
359 size_type available = s.size();
362 int status = sprintf_like(&s[0], available + 1, fmt, a);
365 size_type used = static_cast<size_type>(status);
366 if ( used <= available )
371 available = used; // Assume this is advice of how much space we need.
374 available = available * 2 + 1;
380 template <class S, class V, bool = is_floating_point<V>::value>
381 struct initial_string;
383 template <class V, bool b>
384 struct initial_string<string, V, b>
390 s.resize(s.capacity());
396 struct initial_string<wstring, V, false>
401 const size_t n = (numeric_limits<unsigned long long>::digits / 3)
402 + ((numeric_limits<unsigned long long>::digits % 3) != 0)
404 wstring s(n, wchar_t());
405 s.resize(s.capacity());
411 struct initial_string<wstring, V, true>
416 wstring s(20, wchar_t());
417 s.resize(s.capacity());
422 typedef int (*wide_printf)(wchar_t* __restrict, size_t, const wchar_t*__restrict, ...);
431 return static_cast<int (__cdecl*)(wchar_t* __restrict, size_t, const wchar_t*__restrict, ...)>(swprintf);
435 } // unnamed namespace
437 string to_string(int val)
439 return as_string(snprintf, initial_string<string, int>()(), "%d", val);
442 string to_string(unsigned val)
444 return as_string(snprintf, initial_string<string, unsigned>()(), "%u", val);
447 string to_string(long val)
449 return as_string(snprintf, initial_string<string, long>()(), "%ld", val);
452 string to_string(unsigned long val)
454 return as_string(snprintf, initial_string<string, unsigned long>()(), "%lu", val);
457 string to_string(long long val)
459 return as_string(snprintf, initial_string<string, long long>()(), "%lld", val);
462 string to_string(unsigned long long val)
464 return as_string(snprintf, initial_string<string, unsigned long long>()(), "%llu", val);
467 string to_string(float val)
469 return as_string(snprintf, initial_string<string, float>()(), "%f", val);
472 string to_string(double val)
474 return as_string(snprintf, initial_string<string, double>()(), "%f", val);
477 string to_string(long double val)
479 return as_string(snprintf, initial_string<string, long double>()(), "%Lf", val);
482 wstring to_wstring(int val)
484 return as_string(get_swprintf(), initial_string<wstring, int>()(), L"%d", val);
487 wstring to_wstring(unsigned val)
489 return as_string(get_swprintf(), initial_string<wstring, unsigned>()(), L"%u", val);
492 wstring to_wstring(long val)
494 return as_string(get_swprintf(), initial_string<wstring, long>()(), L"%ld", val);
497 wstring to_wstring(unsigned long val)
499 return as_string(get_swprintf(), initial_string<wstring, unsigned long>()(), L"%lu", val);
502 wstring to_wstring(long long val)
504 return as_string(get_swprintf(), initial_string<wstring, long long>()(), L"%lld", val);
507 wstring to_wstring(unsigned long long val)
509 return as_string(get_swprintf(), initial_string<wstring, unsigned long long>()(), L"%llu", val);
512 wstring to_wstring(float val)
514 return as_string(get_swprintf(), initial_string<wstring, float>()(), L"%f", val);
517 wstring to_wstring(double val)
519 return as_string(get_swprintf(), initial_string<wstring, double>()(), L"%f", val);
522 wstring to_wstring(long double val)
524 return as_string(get_swprintf(), initial_string<wstring, long double>()(), L"%Lf", val);
526 _LIBCPP_END_NAMESPACE_STD