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 //===----------------------------------------------------------------------===//
15 #include "support/win32/support.h"
18 _LIBCPP_BEGIN_NAMESPACE_STD
20 template class __basic_string_common<true>;
22 template class basic_string<char>;
23 template class basic_string<wchar_t>;
27 operator+<char, char_traits<char>, allocator<char> >(char const*, string const&);
30 stoi(const string& str, size_t* idx, int base)
33 const char* const p = str.c_str();
34 long r = strtol(p, &ptr, base);
35 if (r < numeric_limits<int>::min() || numeric_limits<int>::max() < r)
36 ptr = const_cast<char*>(p);
39 #ifndef _LIBCPP_NO_EXCEPTIONS
41 throw invalid_argument("stoi: no conversion");
42 throw out_of_range("stoi: out of range");
43 #endif // _LIBCPP_NO_EXCEPTIONS
46 *idx = static_cast<size_t>(ptr - p);
47 return static_cast<int>(r);
51 stoi(const wstring& str, size_t* idx, int base)
54 const wchar_t* const p = str.c_str();
55 long r = wcstol(p, &ptr, base);
56 if (r < numeric_limits<int>::min() || numeric_limits<int>::max() < r)
57 ptr = const_cast<wchar_t*>(p);
60 #ifndef _LIBCPP_NO_EXCEPTIONS
62 throw invalid_argument("stoi: no conversion");
63 throw out_of_range("stoi: out of range");
64 #endif // _LIBCPP_NO_EXCEPTIONS
67 *idx = static_cast<size_t>(ptr - p);
68 return static_cast<int>(r);
72 stol(const string& str, size_t* idx, int base)
75 const char* const p = str.c_str();
76 long r = strtol(p, &ptr, base);
79 #ifndef _LIBCPP_NO_EXCEPTIONS
81 throw invalid_argument("stol: no conversion");
82 throw out_of_range("stol: out of range");
83 #endif // _LIBCPP_NO_EXCEPTIONS
86 *idx = static_cast<size_t>(ptr - p);
91 stol(const wstring& str, size_t* idx, int base)
94 const wchar_t* const p = str.c_str();
95 long r = wcstol(p, &ptr, base);
98 #ifndef _LIBCPP_NO_EXCEPTIONS
100 throw invalid_argument("stol: no conversion");
101 throw out_of_range("stol: out of range");
102 #endif // _LIBCPP_NO_EXCEPTIONS
105 *idx = static_cast<size_t>(ptr - p);
110 stoul(const string& str, size_t* idx, int base)
113 const char* const p = str.c_str();
114 unsigned long r = strtoul(p, &ptr, base);
117 #ifndef _LIBCPP_NO_EXCEPTIONS
119 throw invalid_argument("stoul: no conversion");
120 throw out_of_range("stoul: out of range");
121 #endif // _LIBCPP_NO_EXCEPTIONS
124 *idx = static_cast<size_t>(ptr - p);
129 stoul(const wstring& str, size_t* idx, int base)
132 const wchar_t* const p = str.c_str();
133 unsigned long r = wcstoul(p, &ptr, base);
136 #ifndef _LIBCPP_NO_EXCEPTIONS
138 throw invalid_argument("stoul: no conversion");
139 throw out_of_range("stoul: out of range");
140 #endif // _LIBCPP_NO_EXCEPTIONS
143 *idx = static_cast<size_t>(ptr - p);
148 stoll(const string& str, size_t* idx, int base)
151 const char* const p = str.c_str();
152 long long r = strtoll(p, &ptr, base);
155 #ifndef _LIBCPP_NO_EXCEPTIONS
157 throw invalid_argument("stoll: no conversion");
158 throw out_of_range("stoll: out of range");
159 #endif // _LIBCPP_NO_EXCEPTIONS
162 *idx = static_cast<size_t>(ptr - p);
167 stoll(const wstring& str, size_t* idx, int base)
170 const wchar_t* const p = str.c_str();
171 long long r = wcstoll(p, &ptr, base);
174 #ifndef _LIBCPP_NO_EXCEPTIONS
176 throw invalid_argument("stoll: no conversion");
177 throw out_of_range("stoll: out of range");
178 #endif // _LIBCPP_NO_EXCEPTIONS
181 *idx = static_cast<size_t>(ptr - p);
186 stoull(const string& str, size_t* idx, int base)
189 const char* const p = str.c_str();
190 unsigned long long r = strtoull(p, &ptr, base);
193 #ifndef _LIBCPP_NO_EXCEPTIONS
195 throw invalid_argument("stoull: no conversion");
196 throw out_of_range("stoull: out of range");
197 #endif // _LIBCPP_NO_EXCEPTIONS
200 *idx = static_cast<size_t>(ptr - p);
205 stoull(const wstring& str, size_t* idx, int base)
208 const wchar_t* const p = str.c_str();
209 unsigned long long r = wcstoull(p, &ptr, base);
212 #ifndef _LIBCPP_NO_EXCEPTIONS
214 throw invalid_argument("stoull: no conversion");
215 throw out_of_range("stoull: out of range");
216 #endif // _LIBCPP_NO_EXCEPTIONS
219 *idx = static_cast<size_t>(ptr - p);
224 stof(const string& str, size_t* idx)
227 const char* const p = str.c_str();
228 int errno_save = errno;
230 double r = strtod(p, &ptr);
231 swap(errno, errno_save);
232 #ifndef _LIBCPP_NO_EXCEPTIONS
233 if (errno_save == ERANGE)
234 throw out_of_range("stof: out of range");
236 throw invalid_argument("stof: no conversion");
237 #endif // _LIBCPP_NO_EXCEPTIONS
239 *idx = static_cast<size_t>(ptr - p);
240 return static_cast<float>(r);
244 stof(const wstring& str, size_t* idx)
247 const wchar_t* const p = str.c_str();
248 int errno_save = errno;
250 double r = wcstod(p, &ptr);
251 swap(errno, errno_save);
252 #ifndef _LIBCPP_NO_EXCEPTIONS
253 if (errno_save == ERANGE)
254 throw out_of_range("stof: out of range");
256 throw invalid_argument("stof: no conversion");
257 #endif // _LIBCPP_NO_EXCEPTIONS
259 *idx = static_cast<size_t>(ptr - p);
260 return static_cast<float>(r);
264 stod(const string& str, size_t* idx)
267 const char* const p = str.c_str();
268 int errno_save = errno;
270 double r = strtod(p, &ptr);
271 swap(errno, errno_save);
272 #ifndef _LIBCPP_NO_EXCEPTIONS
273 if (errno_save == ERANGE)
274 throw out_of_range("stod: out of range");
276 throw invalid_argument("stod: no conversion");
277 #endif // _LIBCPP_NO_EXCEPTIONS
279 *idx = static_cast<size_t>(ptr - p);
284 stod(const wstring& str, size_t* idx)
287 const wchar_t* const p = str.c_str();
288 int errno_save = errno;
290 double r = wcstod(p, &ptr);
291 swap(errno, errno_save);
292 #ifndef _LIBCPP_NO_EXCEPTIONS
293 if (errno_save == ERANGE)
294 throw out_of_range("stod: out of range");
296 throw invalid_argument("stod: no conversion");
297 #endif // _LIBCPP_NO_EXCEPTIONS
299 *idx = static_cast<size_t>(ptr - p);
304 stold(const string& str, size_t* idx)
307 const char* const p = str.c_str();
308 int errno_save = errno;
310 long double r = strtold(p, &ptr);
311 swap(errno, errno_save);
312 #ifndef _LIBCPP_NO_EXCEPTIONS
313 if (errno_save == ERANGE)
314 throw out_of_range("stold: out of range");
316 throw invalid_argument("stold: no conversion");
317 #endif // _LIBCPP_NO_EXCEPTIONS
319 *idx = static_cast<size_t>(ptr - p);
324 stold(const wstring& str, size_t* idx)
327 const wchar_t* const p = str.c_str();
328 int errno_save = errno;
330 long double r = wcstold(p, &ptr);
331 swap(errno, errno_save);
332 #ifndef _LIBCPP_NO_EXCEPTIONS
333 if (errno_save == ERANGE)
334 throw out_of_range("stold: out of range");
336 throw invalid_argument("stold: no conversion");
337 #endif // _LIBCPP_NO_EXCEPTIONS
339 *idx = static_cast<size_t>(ptr - p);
343 string to_string(int val)
346 s.resize(s.capacity());
349 size_t n2 = static_cast<size_t>(snprintf(&s[0], s.size()+1, "%d", val));
360 string to_string(unsigned val)
363 s.resize(s.capacity());
366 size_t n2 = static_cast<size_t>(snprintf(&s[0], s.size()+1, "%u", val));
377 string to_string(long val)
380 s.resize(s.capacity());
383 size_t n2 = static_cast<size_t>(snprintf(&s[0], s.size()+1, "%ld", val));
394 string to_string(unsigned long val)
397 s.resize(s.capacity());
400 size_t n2 = static_cast<size_t>(snprintf(&s[0], s.size()+1, "%lu", val));
411 string to_string(long long val)
414 s.resize(s.capacity());
417 size_t n2 = static_cast<size_t>(snprintf(&s[0], s.size()+1, "%lld", val));
428 string to_string(unsigned long long val)
431 s.resize(s.capacity());
434 size_t n2 = static_cast<size_t>(snprintf(&s[0], s.size()+1, "%llu", val));
445 string to_string(float val)
448 s.resize(s.capacity());
451 size_t n2 = static_cast<size_t>(snprintf(&s[0], s.size()+1, "%f", val));
462 string to_string(double val)
465 s.resize(s.capacity());
468 size_t n2 = static_cast<size_t>(snprintf(&s[0], s.size()+1, "%f", val));
479 string to_string(long double val)
482 s.resize(s.capacity());
485 size_t n2 = static_cast<size_t>(snprintf(&s[0], s.size()+1, "%Lf", val));
496 wstring to_wstring(int val)
498 const size_t n = (numeric_limits<int>::digits / 3)
499 + ((numeric_limits<int>::digits % 3) != 0)
501 wstring s(n, wchar_t());
502 s.resize(s.capacity());
505 int n2 = swprintf(&s[0], s.size()+1, L"%d", val);
508 s.resize(static_cast<size_t>(n2));
511 s.resize(2*s.size());
512 s.resize(s.capacity());
517 wstring to_wstring(unsigned val)
519 const size_t n = (numeric_limits<unsigned>::digits / 3)
520 + ((numeric_limits<unsigned>::digits % 3) != 0)
522 wstring s(n, wchar_t());
523 s.resize(s.capacity());
526 int n2 = swprintf(&s[0], s.size()+1, L"%u", val);
529 s.resize(static_cast<size_t>(n2));
532 s.resize(2*s.size());
533 s.resize(s.capacity());
538 wstring to_wstring(long val)
540 const size_t n = (numeric_limits<long>::digits / 3)
541 + ((numeric_limits<long>::digits % 3) != 0)
543 wstring s(n, wchar_t());
544 s.resize(s.capacity());
547 int n2 = swprintf(&s[0], s.size()+1, L"%ld", val);
550 s.resize(static_cast<size_t>(n2));
553 s.resize(2*s.size());
554 s.resize(s.capacity());
559 wstring to_wstring(unsigned long val)
561 const size_t n = (numeric_limits<unsigned long>::digits / 3)
562 + ((numeric_limits<unsigned long>::digits % 3) != 0)
564 wstring s(n, wchar_t());
565 s.resize(s.capacity());
568 int n2 = swprintf(&s[0], s.size()+1, L"%lu", val);
571 s.resize(static_cast<size_t>(n2));
574 s.resize(2*s.size());
575 s.resize(s.capacity());
580 wstring to_wstring(long long val)
582 const size_t n = (numeric_limits<long long>::digits / 3)
583 + ((numeric_limits<long long>::digits % 3) != 0)
585 wstring s(n, wchar_t());
586 s.resize(s.capacity());
589 int n2 = swprintf(&s[0], s.size()+1, L"%lld", val);
592 s.resize(static_cast<size_t>(n2));
595 s.resize(2*s.size());
596 s.resize(s.capacity());
601 wstring to_wstring(unsigned long long val)
603 const size_t n = (numeric_limits<unsigned long long>::digits / 3)
604 + ((numeric_limits<unsigned long long>::digits % 3) != 0)
606 wstring s(n, wchar_t());
607 s.resize(s.capacity());
610 int n2 = swprintf(&s[0], s.size()+1, L"%llu", val);
613 s.resize(static_cast<size_t>(n2));
616 s.resize(2*s.size());
617 s.resize(s.capacity());
622 wstring to_wstring(float val)
625 wstring s(n, wchar_t());
626 s.resize(s.capacity());
629 int n2 = swprintf(&s[0], s.size()+1, L"%f", val);
632 s.resize(static_cast<size_t>(n2));
635 s.resize(2*s.size());
636 s.resize(s.capacity());
641 wstring to_wstring(double val)
644 wstring s(n, wchar_t());
645 s.resize(s.capacity());
648 int n2 = swprintf(&s[0], s.size()+1, L"%f", val);
651 s.resize(static_cast<size_t>(n2));
654 s.resize(2*s.size());
655 s.resize(s.capacity());
660 wstring to_wstring(long double val)
663 wstring s(n, wchar_t());
664 s.resize(s.capacity());
667 int n2 = swprintf(&s[0], s.size()+1, L"%Lf", val);
670 s.resize(static_cast<size_t>(n2));
673 s.resize(2*s.size());
674 s.resize(s.capacity());
679 _LIBCPP_END_NAMESPACE_STD