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 //===----------------------------------------------------------------------===//
18 _LIBCPP_BEGIN_NAMESPACE_STD
20 template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS __basic_string_common<true>;
22 template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS basic_string<char>;
23 template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS basic_string<wchar_t>;
27 operator+<char, char_traits<char>, allocator<char> >(char const*, string const&);
34 void throw_helper( const string& msg )
36 #ifndef _LIBCPP_NO_EXCEPTIONS
39 fprintf(stderr, "%s\n", msg.c_str());
45 void throw_from_string_out_of_range( const string& func )
47 throw_helper<out_of_range>(func + ": out of range");
51 void throw_from_string_invalid_arg( const string& func )
53 throw_helper<invalid_argument>(func + ": no conversion");
58 template<typename V, typename S, typename F>
61 as_integer_helper(const string& func, const S& str, size_t* idx, int base, F f)
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;
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);
72 throw_from_string_invalid_arg(func);
74 *idx = static_cast<size_t>(ptr - p);
78 template<typename V, typename S>
81 as_integer(const string& func, const S& s, size_t* idx, int base);
87 as_integer(const string& func, const string& s, size_t* idx, int base )
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);
99 as_integer(const string& func, const string& s, size_t* idx, int base )
101 return as_integer_helper<long>( func, s, idx, base, strtol );
107 as_integer( const string& func, const string& s, size_t* idx, int base )
109 return as_integer_helper<unsigned long>( func, s, idx, base, strtoul );
115 as_integer( const string& func, const string& s, size_t* idx, int base )
117 return as_integer_helper<long long>( func, s, idx, base, strtoll );
123 as_integer( const string& func, const string& s, size_t* idx, int base )
125 return as_integer_helper<unsigned long long>( func, s, idx, base, strtoull );
132 as_integer( const string& func, const wstring& s, size_t* idx, int base )
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);
144 as_integer( const string& func, const wstring& s, size_t* idx, int base )
146 return as_integer_helper<long>( func, s, idx, base, wcstol );
152 as_integer( const string& func, const wstring& s, size_t* idx, int base )
154 return as_integer_helper<unsigned long>( func, s, idx, base, wcstoul );
160 as_integer( const string& func, const wstring& s, size_t* idx, int base )
162 return as_integer_helper<long long>( func, s, idx, base, wcstoll );
168 as_integer( const string& func, const wstring& s, size_t* idx, int base )
170 return as_integer_helper<unsigned long long>( func, s, idx, base, wcstoull );
175 template<typename V, typename S, typename F>
178 as_float_helper(const string& func, const S& str, size_t* idx, F f )
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;
185 swap(errno, errno_save);
186 if (errno_save == ERANGE)
187 throw_from_string_out_of_range(func);
189 throw_from_string_invalid_arg(func);
191 *idx = static_cast<size_t>(ptr - p);
195 template<typename V, typename S>
197 V as_float( const string& func, const S& s, size_t* idx = nullptr );
202 as_float( const string& func, const string& s, size_t* idx )
204 return as_float_helper<float>( func, s, idx, strtof );
210 as_float(const string& func, const string& s, size_t* idx )
212 return as_float_helper<double>( func, s, idx, strtod );
218 as_float( const string& func, const string& s, size_t* idx )
220 return as_float_helper<long double>( func, s, idx, strtold );
226 as_float( const string& func, const wstring& s, size_t* idx )
228 return as_float_helper<float>( func, s, idx, wcstof );
234 as_float( const string& func, const wstring& s, size_t* idx )
236 return as_float_helper<double>( func, s, idx, wcstod );
242 as_float( const string& func, const wstring& s, size_t* idx )
244 return as_float_helper<long double>( func, s, idx, wcstold );
247 } // unnamed namespace
250 stoi(const string& str, size_t* idx, int base)
252 return as_integer<int>( "stoi", str, idx, base );
256 stoi(const wstring& str, size_t* idx, int base)
258 return as_integer<int>( "stoi", str, idx, base );
262 stol(const string& str, size_t* idx, int base)
264 return as_integer<long>( "stol", str, idx, base );
268 stol(const wstring& str, size_t* idx, int base)
270 return as_integer<long>( "stol", str, idx, base );
274 stoul(const string& str, size_t* idx, int base)
276 return as_integer<unsigned long>( "stoul", str, idx, base );
280 stoul(const wstring& str, size_t* idx, int base)
282 return as_integer<unsigned long>( "stoul", str, idx, base );
286 stoll(const string& str, size_t* idx, int base)
288 return as_integer<long long>( "stoll", str, idx, base );
292 stoll(const wstring& str, size_t* idx, int base)
294 return as_integer<long long>( "stoll", str, idx, base );
298 stoull(const string& str, size_t* idx, int base)
300 return as_integer<unsigned long long>( "stoull", str, idx, base );
304 stoull(const wstring& str, size_t* idx, int base)
306 return as_integer<unsigned long long>( "stoull", str, idx, base );
310 stof(const string& str, size_t* idx)
312 return as_float<float>( "stof", str, idx );
316 stof(const wstring& str, size_t* idx)
318 return as_float<float>( "stof", str, idx );
322 stod(const string& str, size_t* idx)
324 return as_float<double>( "stod", str, idx );
328 stod(const wstring& str, size_t* idx)
330 return as_float<double>( "stod", str, idx );
334 stold(const string& str, size_t* idx)
336 return as_float<long double>( "stold", str, idx );
340 stold(const wstring& str, size_t* idx)
342 return as_float<long double>( "stold", str, idx );
352 template<typename S, typename P, typename V >
355 as_string(P sprintf_like, S s, const typename S::value_type* fmt, V a)
357 typedef typename S::size_type size_type;
358 size_type available = s.size();
361 int status = sprintf_like(&s[0], available + 1, fmt, a);
364 size_type used = static_cast<size_type>(status);
365 if ( used <= available )
370 available = used; // Assume this is advice of how much space we need.
373 available = available * 2 + 1;
379 template <class S, class V, bool = is_floating_point<V>::value>
380 struct initial_string;
382 template <class V, bool b>
383 struct initial_string<string, V, b>
389 s.resize(s.capacity());
395 struct initial_string<wstring, V, false>
400 const size_t n = (numeric_limits<unsigned long long>::digits / 3)
401 + ((numeric_limits<unsigned long long>::digits % 3) != 0)
403 wstring s(n, wchar_t());
404 s.resize(s.capacity());
410 struct initial_string<wstring, V, true>
415 wstring s(20, wchar_t());
416 s.resize(s.capacity());
421 typedef int (*wide_printf)(wchar_t* __restrict, size_t, const wchar_t*__restrict, ...);
427 #ifndef _LIBCPP_MSVCRT
430 return static_cast<int (__cdecl*)(wchar_t* __restrict, size_t, const wchar_t*__restrict, ...)>(_snwprintf);
434 } // unnamed namespace
436 string to_string(int val)
438 return as_string(snprintf, initial_string<string, int>()(), "%d", val);
441 string to_string(unsigned val)
443 return as_string(snprintf, initial_string<string, unsigned>()(), "%u", val);
446 string to_string(long val)
448 return as_string(snprintf, initial_string<string, long>()(), "%ld", val);
451 string to_string(unsigned long val)
453 return as_string(snprintf, initial_string<string, unsigned long>()(), "%lu", val);
456 string to_string(long long val)
458 return as_string(snprintf, initial_string<string, long long>()(), "%lld", val);
461 string to_string(unsigned long long val)
463 return as_string(snprintf, initial_string<string, unsigned long long>()(), "%llu", val);
466 string to_string(float val)
468 return as_string(snprintf, initial_string<string, float>()(), "%f", val);
471 string to_string(double val)
473 return as_string(snprintf, initial_string<string, double>()(), "%f", val);
476 string to_string(long double val)
478 return as_string(snprintf, initial_string<string, long double>()(), "%Lf", val);
481 wstring to_wstring(int val)
483 return as_string(get_swprintf(), initial_string<wstring, int>()(), L"%d", val);
486 wstring to_wstring(unsigned val)
488 return as_string(get_swprintf(), initial_string<wstring, unsigned>()(), L"%u", val);
491 wstring to_wstring(long val)
493 return as_string(get_swprintf(), initial_string<wstring, long>()(), L"%ld", val);
496 wstring to_wstring(unsigned long val)
498 return as_string(get_swprintf(), initial_string<wstring, unsigned long>()(), L"%lu", val);
501 wstring to_wstring(long long val)
503 return as_string(get_swprintf(), initial_string<wstring, long long>()(), L"%lld", val);
506 wstring to_wstring(unsigned long long val)
508 return as_string(get_swprintf(), initial_string<wstring, unsigned long long>()(), L"%llu", val);
511 wstring to_wstring(float val)
513 return as_string(get_swprintf(), initial_string<wstring, float>()(), L"%f", val);
516 wstring to_wstring(double val)
518 return as_string(get_swprintf(), initial_string<wstring, double>()(), L"%f", val);
521 wstring to_wstring(long double val)
523 return as_string(get_swprintf(), initial_string<wstring, long double>()(), L"%Lf", val);
525 _LIBCPP_END_NAMESPACE_STD