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 typename remove_reference<decltype(errno)>::type errno_save = errno;
36 long r = strtol(p, &ptr, base);
37 swap(errno, errno_save);
38 #ifndef _LIBCPP_NO_EXCEPTIONS
39 if (errno_save == ERANGE || r < numeric_limits<int>::min() ||
40 numeric_limits<int>::max() < r)
41 throw out_of_range("stoi: out of range");
43 throw invalid_argument("stoi: no conversion");
44 #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 typename remove_reference<decltype(errno)>::type errno_save = errno;
57 long r = wcstol(p, &ptr, base);
58 swap(errno, errno_save);
59 #ifndef _LIBCPP_NO_EXCEPTIONS
60 if (errno_save == ERANGE || r < numeric_limits<int>::min() ||
61 numeric_limits<int>::max() < r)
62 throw out_of_range("stoi: out of range");
64 throw invalid_argument("stoi: no conversion");
65 #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 typename remove_reference<decltype(errno)>::type errno_save = errno;
78 long r = strtol(p, &ptr, base);
79 swap(errno, errno_save);
80 #ifndef _LIBCPP_NO_EXCEPTIONS
81 if (errno_save == ERANGE)
82 throw out_of_range("stol: out of range");
84 throw invalid_argument("stol: no conversion");
85 #endif // _LIBCPP_NO_EXCEPTIONS
87 *idx = static_cast<size_t>(ptr - p);
92 stol(const wstring& str, size_t* idx, int base)
95 const wchar_t* const p = str.c_str();
96 typename remove_reference<decltype(errno)>::type errno_save = errno;
98 long r = wcstol(p, &ptr, base);
99 swap(errno, errno_save);
100 #ifndef _LIBCPP_NO_EXCEPTIONS
101 if (errno_save == ERANGE)
102 throw out_of_range("stol: out of range");
104 throw invalid_argument("stol: no conversion");
105 #endif // _LIBCPP_NO_EXCEPTIONS
107 *idx = static_cast<size_t>(ptr - p);
112 stoul(const string& str, size_t* idx, int base)
115 const char* const p = str.c_str();
116 typename remove_reference<decltype(errno)>::type errno_save = errno;
118 unsigned long r = strtoul(p, &ptr, base);
119 swap(errno, errno_save);
120 #ifndef _LIBCPP_NO_EXCEPTIONS
121 if (errno_save == ERANGE)
122 throw out_of_range("stoul: out of range");
124 throw invalid_argument("stoul: no conversion");
125 #endif // _LIBCPP_NO_EXCEPTIONS
127 *idx = static_cast<size_t>(ptr - p);
132 stoul(const wstring& str, size_t* idx, int base)
135 const wchar_t* const p = str.c_str();
136 typename remove_reference<decltype(errno)>::type errno_save = errno;
138 unsigned long r = wcstoul(p, &ptr, base);
139 swap(errno, errno_save);
140 #ifndef _LIBCPP_NO_EXCEPTIONS
141 if (errno_save == ERANGE)
142 throw out_of_range("stoul: out of range");
144 throw invalid_argument("stoul: no conversion");
145 #endif // _LIBCPP_NO_EXCEPTIONS
147 *idx = static_cast<size_t>(ptr - p);
152 stoll(const string& str, size_t* idx, int base)
155 const char* const p = str.c_str();
156 typename remove_reference<decltype(errno)>::type errno_save = errno;
158 long long r = strtoll(p, &ptr, base);
159 swap(errno, errno_save);
160 #ifndef _LIBCPP_NO_EXCEPTIONS
161 if (errno_save == ERANGE)
162 throw out_of_range("stoll: out of range");
164 throw invalid_argument("stoll: no conversion");
165 #endif // _LIBCPP_NO_EXCEPTIONS
167 *idx = static_cast<size_t>(ptr - p);
172 stoll(const wstring& str, size_t* idx, int base)
175 const wchar_t* const p = str.c_str();
176 typename remove_reference<decltype(errno)>::type errno_save = errno;
178 long long r = wcstoll(p, &ptr, base);
179 swap(errno, errno_save);
180 #ifndef _LIBCPP_NO_EXCEPTIONS
181 if (errno_save == ERANGE)
182 throw out_of_range("stoll: out of range");
184 throw invalid_argument("stoll: no conversion");
185 #endif // _LIBCPP_NO_EXCEPTIONS
187 *idx = static_cast<size_t>(ptr - p);
192 stoull(const string& str, size_t* idx, int base)
195 const char* const p = str.c_str();
196 typename remove_reference<decltype(errno)>::type errno_save = errno;
198 unsigned long long r = strtoull(p, &ptr, base);
199 swap(errno, errno_save);
200 #ifndef _LIBCPP_NO_EXCEPTIONS
201 if (errno_save == ERANGE)
202 throw out_of_range("stoull: out of range");
204 throw invalid_argument("stoull: no conversion");
205 #endif // _LIBCPP_NO_EXCEPTIONS
207 *idx = static_cast<size_t>(ptr - p);
212 stoull(const wstring& str, size_t* idx, int base)
215 const wchar_t* const p = str.c_str();
216 typename remove_reference<decltype(errno)>::type errno_save = errno;
218 unsigned long long r = wcstoull(p, &ptr, base);
219 swap(errno, errno_save);
220 #ifndef _LIBCPP_NO_EXCEPTIONS
221 if (errno_save == ERANGE)
222 throw out_of_range("stoull: out of range");
224 throw invalid_argument("stoull: no conversion");
225 #endif // _LIBCPP_NO_EXCEPTIONS
227 *idx = static_cast<size_t>(ptr - p);
232 stof(const string& str, size_t* idx)
235 const char* const p = str.c_str();
236 typename remove_reference<decltype(errno)>::type errno_save = errno;
238 float r = strtof(p, &ptr);
239 swap(errno, errno_save);
240 #ifndef _LIBCPP_NO_EXCEPTIONS
241 if (errno_save == ERANGE)
242 throw out_of_range("stof: out of range");
244 throw invalid_argument("stof: no conversion");
245 #endif // _LIBCPP_NO_EXCEPTIONS
247 *idx = static_cast<size_t>(ptr - p);
252 stof(const wstring& str, size_t* idx)
255 const wchar_t* const p = str.c_str();
256 typename remove_reference<decltype(errno)>::type errno_save = errno;
258 float r = wcstof(p, &ptr);
259 swap(errno, errno_save);
260 #ifndef _LIBCPP_NO_EXCEPTIONS
261 if (errno_save == ERANGE)
262 throw out_of_range("stof: out of range");
264 throw invalid_argument("stof: no conversion");
265 #endif // _LIBCPP_NO_EXCEPTIONS
267 *idx = static_cast<size_t>(ptr - p);
272 stod(const string& str, size_t* idx)
275 const char* const p = str.c_str();
276 typename remove_reference<decltype(errno)>::type errno_save = errno;
278 double r = strtod(p, &ptr);
279 swap(errno, errno_save);
280 #ifndef _LIBCPP_NO_EXCEPTIONS
281 if (errno_save == ERANGE)
282 throw out_of_range("stod: out of range");
284 throw invalid_argument("stod: no conversion");
285 #endif // _LIBCPP_NO_EXCEPTIONS
287 *idx = static_cast<size_t>(ptr - p);
292 stod(const wstring& str, size_t* idx)
295 const wchar_t* const p = str.c_str();
296 typename remove_reference<decltype(errno)>::type errno_save = errno;
298 double r = wcstod(p, &ptr);
299 swap(errno, errno_save);
300 #ifndef _LIBCPP_NO_EXCEPTIONS
301 if (errno_save == ERANGE)
302 throw out_of_range("stod: out of range");
304 throw invalid_argument("stod: no conversion");
305 #endif // _LIBCPP_NO_EXCEPTIONS
307 *idx = static_cast<size_t>(ptr - p);
312 stold(const string& str, size_t* idx)
315 const char* const p = str.c_str();
316 typename remove_reference<decltype(errno)>::type errno_save = errno;
318 long double r = strtold(p, &ptr);
319 swap(errno, errno_save);
320 #ifndef _LIBCPP_NO_EXCEPTIONS
321 if (errno_save == ERANGE)
322 throw out_of_range("stold: out of range");
324 throw invalid_argument("stold: no conversion");
325 #endif // _LIBCPP_NO_EXCEPTIONS
327 *idx = static_cast<size_t>(ptr - p);
332 stold(const wstring& str, size_t* idx)
335 const wchar_t* const p = str.c_str();
336 typename remove_reference<decltype(errno)>::type errno_save = errno;
338 long double r = wcstold(p, &ptr);
339 swap(errno, errno_save);
340 #ifndef _LIBCPP_NO_EXCEPTIONS
341 if (errno_save == ERANGE)
342 throw out_of_range("stold: out of range");
344 throw invalid_argument("stold: no conversion");
345 #endif // _LIBCPP_NO_EXCEPTIONS
347 *idx = static_cast<size_t>(ptr - p);
351 string to_string(int val)
354 s.resize(s.capacity());
357 size_t n2 = static_cast<size_t>(snprintf(&s[0], s.size()+1, "%d", val));
368 string to_string(unsigned val)
371 s.resize(s.capacity());
374 size_t n2 = static_cast<size_t>(snprintf(&s[0], s.size()+1, "%u", val));
385 string to_string(long val)
388 s.resize(s.capacity());
391 size_t n2 = static_cast<size_t>(snprintf(&s[0], s.size()+1, "%ld", val));
402 string to_string(unsigned long val)
405 s.resize(s.capacity());
408 size_t n2 = static_cast<size_t>(snprintf(&s[0], s.size()+1, "%lu", val));
419 string to_string(long long val)
422 s.resize(s.capacity());
425 size_t n2 = static_cast<size_t>(snprintf(&s[0], s.size()+1, "%lld", val));
436 string to_string(unsigned long long val)
439 s.resize(s.capacity());
442 size_t n2 = static_cast<size_t>(snprintf(&s[0], s.size()+1, "%llu", val));
453 string to_string(float val)
456 s.resize(s.capacity());
459 size_t n2 = static_cast<size_t>(snprintf(&s[0], s.size()+1, "%f", val));
470 string to_string(double val)
473 s.resize(s.capacity());
476 size_t n2 = static_cast<size_t>(snprintf(&s[0], s.size()+1, "%f", val));
487 string to_string(long double val)
490 s.resize(s.capacity());
493 size_t n2 = static_cast<size_t>(snprintf(&s[0], s.size()+1, "%Lf", val));
504 wstring to_wstring(int val)
506 const size_t n = (numeric_limits<int>::digits / 3)
507 + ((numeric_limits<int>::digits % 3) != 0)
509 wstring s(n, wchar_t());
510 s.resize(s.capacity());
513 int n2 = swprintf(&s[0], s.size()+1, L"%d", val);
516 s.resize(static_cast<size_t>(n2));
519 s.resize(2*s.size());
520 s.resize(s.capacity());
525 wstring to_wstring(unsigned val)
527 const size_t n = (numeric_limits<unsigned>::digits / 3)
528 + ((numeric_limits<unsigned>::digits % 3) != 0)
530 wstring s(n, wchar_t());
531 s.resize(s.capacity());
534 int n2 = swprintf(&s[0], s.size()+1, L"%u", val);
537 s.resize(static_cast<size_t>(n2));
540 s.resize(2*s.size());
541 s.resize(s.capacity());
546 wstring to_wstring(long val)
548 const size_t n = (numeric_limits<long>::digits / 3)
549 + ((numeric_limits<long>::digits % 3) != 0)
551 wstring s(n, wchar_t());
552 s.resize(s.capacity());
555 int n2 = swprintf(&s[0], s.size()+1, L"%ld", val);
558 s.resize(static_cast<size_t>(n2));
561 s.resize(2*s.size());
562 s.resize(s.capacity());
567 wstring to_wstring(unsigned long val)
569 const size_t n = (numeric_limits<unsigned long>::digits / 3)
570 + ((numeric_limits<unsigned long>::digits % 3) != 0)
572 wstring s(n, wchar_t());
573 s.resize(s.capacity());
576 int n2 = swprintf(&s[0], s.size()+1, L"%lu", val);
579 s.resize(static_cast<size_t>(n2));
582 s.resize(2*s.size());
583 s.resize(s.capacity());
588 wstring to_wstring(long long val)
590 const size_t n = (numeric_limits<long long>::digits / 3)
591 + ((numeric_limits<long long>::digits % 3) != 0)
593 wstring s(n, wchar_t());
594 s.resize(s.capacity());
597 int n2 = swprintf(&s[0], s.size()+1, L"%lld", val);
600 s.resize(static_cast<size_t>(n2));
603 s.resize(2*s.size());
604 s.resize(s.capacity());
609 wstring to_wstring(unsigned long long val)
611 const size_t n = (numeric_limits<unsigned long long>::digits / 3)
612 + ((numeric_limits<unsigned long long>::digits % 3) != 0)
614 wstring s(n, wchar_t());
615 s.resize(s.capacity());
618 int n2 = swprintf(&s[0], s.size()+1, L"%llu", val);
621 s.resize(static_cast<size_t>(n2));
624 s.resize(2*s.size());
625 s.resize(s.capacity());
630 wstring to_wstring(float val)
633 wstring s(n, wchar_t());
634 s.resize(s.capacity());
637 int n2 = swprintf(&s[0], s.size()+1, L"%f", val);
640 s.resize(static_cast<size_t>(n2));
643 s.resize(2*s.size());
644 s.resize(s.capacity());
649 wstring to_wstring(double val)
652 wstring s(n, wchar_t());
653 s.resize(s.capacity());
656 int n2 = swprintf(&s[0], s.size()+1, L"%f", val);
659 s.resize(static_cast<size_t>(n2));
662 s.resize(2*s.size());
663 s.resize(s.capacity());
668 wstring to_wstring(long double val)
671 wstring s(n, wchar_t());
672 s.resize(s.capacity());
675 int n2 = swprintf(&s[0], s.size()+1, L"%Lf", val);
678 s.resize(static_cast<size_t>(n2));
681 s.resize(2*s.size());
682 s.resize(s.capacity());
687 _LIBCPP_END_NAMESPACE_STD