blob: d847ad506845c292bec81fbcf2a85710980bc0f2 [file] [log] [blame]
Louis Dionne9bd93882021-11-17 16:25:01 -05001//===----------------------------------------------------------------------===//
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00002//
Chandler Carruthd2012102019-01-19 10:56:40 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00006//
7//===----------------------------------------------------------------------===//
8
Louis Dionne9bdbeb32022-02-14 13:41:09 -05009#include <__assert>
Arthur O'Dwyercf9bf392022-02-11 13:00:39 -050010#include <cerrno>
11#include <charconv>
12#include <cstdlib>
13#include <limits>
14#include <stdexcept>
Howard Hinnant84f697e2013-07-23 16:05:56 +000015#include <stdio.h>
Arthur O'Dwyercf9bf392022-02-11 13:00:39 -050016#include <string>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +000017
Louis Dionne89258142021-08-23 15:32:36 -040018#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Arthur O'Dwyercf9bf392022-02-11 13:00:39 -050019# include <cwchar>
Louis Dionne89258142021-08-23 15:32:36 -040020#endif
21
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +000022_LIBCPP_BEGIN_NAMESPACE_STD
23
Nikolas Klausera2453e82022-02-02 20:15:40 +010024#ifndef _LIBCPP_ABI_DO_NOT_EXPORT_BASIC_STRING_COMMON
25
26template <bool>
27struct __basic_string_common;
28
29// The struct isn't declared anymore in the headers. It's only here for ABI compatibility.
30template <>
31struct __basic_string_common<true> {
32 _LIBCPP_NORETURN _LIBCPP_EXPORTED_FROM_ABI void __throw_length_error() const;
33 _LIBCPP_NORETURN _LIBCPP_EXPORTED_FROM_ABI void __throw_out_of_range() const;
34};
35
Louis Dionneb6aabd92021-08-19 12:21:06 -040036void __basic_string_common<true>::__throw_length_error() const {
Nikolas Klausera2453e82022-02-02 20:15:40 +010037 std::__throw_length_error("basic_string");
38}
39void __basic_string_common<true>::__throw_out_of_range() const {
40 std::__throw_out_of_range("basic_string");
Louis Dionneb6aabd92021-08-19 12:21:06 -040041}
42
Nikolas Klausera2453e82022-02-02 20:15:40 +010043#endif // _LIBCPP_ABI_DO_NOT_EXPORT_BASIC_STRING_COMMON
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +000044
Louis Dionne732192e2021-06-09 09:41:27 -040045#define _LIBCPP_EXTERN_TEMPLATE_DEFINE(...) template __VA_ARGS__;
Martijn Velsb17d5802020-03-02 10:11:35 -050046#ifdef _LIBCPP_ABI_STRING_OPTIMIZED_EXTERNAL_INSTANTIATION
Louis Dionne89258142021-08-23 15:32:36 -040047 _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE_DEFINE, char)
48# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
49 _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE_DEFINE, wchar_t)
50# endif
Martijn Vels1e63ba02020-02-19 16:27:50 -050051#else
Louis Dionne89258142021-08-23 15:32:36 -040052 _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE_DEFINE, char)
53# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
54 _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE_DEFINE, wchar_t)
55# endif
Martijn Vels1e63ba02020-02-19 16:27:50 -050056#endif
Louis Dionne732192e2021-06-09 09:41:27 -040057#undef _LIBCPP_EXTERN_TEMPLATE_DEFINE
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +000058
Louis Dionne732192e2021-06-09 09:41:27 -040059template string operator+<char, char_traits<char>, allocator<char> >(char const*, string const&);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +000060
Howard Hinnantae2378a2013-05-16 17:13:40 +000061namespace
62{
63
64template<typename T>
65inline
66void throw_helper( const string& msg )
67{
68#ifndef _LIBCPP_NO_EXCEPTIONS
69 throw T( msg );
70#else
Ed Schouten7a441452015-03-10 07:57:43 +000071 fprintf(stderr, "%s\n", msg.c_str());
Marshall Clow8fea1612016-08-25 15:09:01 +000072 _VSTD::abort();
Howard Hinnantae2378a2013-05-16 17:13:40 +000073#endif
74}
75
76inline
77void throw_from_string_out_of_range( const string& func )
78{
79 throw_helper<out_of_range>(func + ": out of range");
80}
81
82inline
83void throw_from_string_invalid_arg( const string& func )
84{
85 throw_helper<invalid_argument>(func + ": no conversion");
86}
87
88// as_integer
89
90template<typename V, typename S, typename F>
91inline
92V
93as_integer_helper(const string& func, const S& str, size_t* idx, int base, F f)
94{
Eric Fiselier002dc672014-11-14 22:23:57 +000095 typename S::value_type* ptr = nullptr;
Howard Hinnantae2378a2013-05-16 17:13:40 +000096 const typename S::value_type* const p = str.c_str();
97 typename remove_reference<decltype(errno)>::type errno_save = errno;
98 errno = 0;
99 V r = f(p, &ptr, base);
100 swap(errno, errno_save);
101 if (errno_save == ERANGE)
102 throw_from_string_out_of_range(func);
103 if (ptr == p)
104 throw_from_string_invalid_arg(func);
105 if (idx)
106 *idx = static_cast<size_t>(ptr - p);
107 return r;
108}
109
110template<typename V, typename S>
111inline
112V
113as_integer(const string& func, const S& s, size_t* idx, int base);
114
115// string
116template<>
117inline
118int
119as_integer(const string& func, const string& s, size_t* idx, int base )
120{
Joerg Sonnenbergere211bc02013-09-17 08:46:53 +0000121 // Use long as no Standard string to integer exists.
Howard Hinnantae2378a2013-05-16 17:13:40 +0000122 long r = as_integer_helper<long>( func, s, idx, base, strtol );
123 if (r < numeric_limits<int>::min() || numeric_limits<int>::max() < r)
124 throw_from_string_out_of_range(func);
125 return static_cast<int>(r);
126}
127
128template<>
129inline
130long
131as_integer(const string& func, const string& s, size_t* idx, int base )
132{
133 return as_integer_helper<long>( func, s, idx, base, strtol );
134}
135
136template<>
137inline
138unsigned long
139as_integer( const string& func, const string& s, size_t* idx, int base )
140{
141 return as_integer_helper<unsigned long>( func, s, idx, base, strtoul );
142}
143
144template<>
145inline
146long long
147as_integer( const string& func, const string& s, size_t* idx, int base )
148{
149 return as_integer_helper<long long>( func, s, idx, base, strtoll );
150}
151
152template<>
153inline
154unsigned long long
155as_integer( const string& func, const string& s, size_t* idx, int base )
156{
157 return as_integer_helper<unsigned long long>( func, s, idx, base, strtoull );
158}
159
Louis Dionne89258142021-08-23 15:32:36 -0400160#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnantae2378a2013-05-16 17:13:40 +0000161// wstring
162template<>
163inline
164int
165as_integer( const string& func, const wstring& s, size_t* idx, int base )
166{
167 // Use long as no Stantard string to integer exists.
168 long r = as_integer_helper<long>( func, s, idx, base, wcstol );
169 if (r < numeric_limits<int>::min() || numeric_limits<int>::max() < r)
170 throw_from_string_out_of_range(func);
171 return static_cast<int>(r);
172}
173
174template<>
175inline
176long
177as_integer( const string& func, const wstring& s, size_t* idx, int base )
178{
179 return as_integer_helper<long>( func, s, idx, base, wcstol );
180}
181
182template<>
183inline
184unsigned long
185as_integer( const string& func, const wstring& s, size_t* idx, int base )
186{
187 return as_integer_helper<unsigned long>( func, s, idx, base, wcstoul );
188}
189
190template<>
191inline
192long long
193as_integer( const string& func, const wstring& s, size_t* idx, int base )
194{
195 return as_integer_helper<long long>( func, s, idx, base, wcstoll );
196}
197
198template<>
199inline
200unsigned long long
201as_integer( const string& func, const wstring& s, size_t* idx, int base )
202{
203 return as_integer_helper<unsigned long long>( func, s, idx, base, wcstoull );
204}
Louis Dionne89258142021-08-23 15:32:36 -0400205#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnantae2378a2013-05-16 17:13:40 +0000206
207// as_float
208
Marshall Clow54e41492019-06-10 23:20:01 +0000209template<typename V, typename S, typename F>
Howard Hinnantae2378a2013-05-16 17:13:40 +0000210inline
211V
212as_float_helper(const string& func, const S& str, size_t* idx, F f )
213{
Eric Fiselier002dc672014-11-14 22:23:57 +0000214 typename S::value_type* ptr = nullptr;
Howard Hinnantae2378a2013-05-16 17:13:40 +0000215 const typename S::value_type* const p = str.c_str();
216 typename remove_reference<decltype(errno)>::type errno_save = errno;
217 errno = 0;
218 V r = f(p, &ptr);
219 swap(errno, errno_save);
220 if (errno_save == ERANGE)
221 throw_from_string_out_of_range(func);
222 if (ptr == p)
223 throw_from_string_invalid_arg(func);
224 if (idx)
225 *idx = static_cast<size_t>(ptr - p);
226 return r;
227}
228
229template<typename V, typename S>
230inline
231V as_float( const string& func, const S& s, size_t* idx = nullptr );
232
233template<>
234inline
235float
236as_float( const string& func, const string& s, size_t* idx )
237{
238 return as_float_helper<float>( func, s, idx, strtof );
239}
240
241template<>
242inline
243double
244as_float(const string& func, const string& s, size_t* idx )
245{
246 return as_float_helper<double>( func, s, idx, strtod );
247}
248
249template<>
250inline
251long double
252as_float( const string& func, const string& s, size_t* idx )
253{
254 return as_float_helper<long double>( func, s, idx, strtold );
255}
256
Louis Dionne89258142021-08-23 15:32:36 -0400257#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnantae2378a2013-05-16 17:13:40 +0000258template<>
259inline
260float
261as_float( const string& func, const wstring& s, size_t* idx )
262{
263 return as_float_helper<float>( func, s, idx, wcstof );
264}
265
266template<>
267inline
268double
269as_float( const string& func, const wstring& s, size_t* idx )
270{
271 return as_float_helper<double>( func, s, idx, wcstod );
272}
273
274template<>
275inline
276long double
277as_float( const string& func, const wstring& s, size_t* idx )
278{
279 return as_float_helper<long double>( func, s, idx, wcstold );
280}
Louis Dionne89258142021-08-23 15:32:36 -0400281#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnantae2378a2013-05-16 17:13:40 +0000282
283} // unnamed namespace
284
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000285int
286stoi(const string& str, size_t* idx, int base)
287{
Howard Hinnantae2378a2013-05-16 17:13:40 +0000288 return as_integer<int>( "stoi", str, idx, base );
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000289}
290
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000291long
292stol(const string& str, size_t* idx, int base)
293{
Howard Hinnantae2378a2013-05-16 17:13:40 +0000294 return as_integer<long>( "stol", str, idx, base );
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000295}
296
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000297unsigned long
298stoul(const string& str, size_t* idx, int base)
299{
Howard Hinnantae2378a2013-05-16 17:13:40 +0000300 return as_integer<unsigned long>( "stoul", str, idx, base );
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000301}
302
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000303long long
304stoll(const string& str, size_t* idx, int base)
305{
Howard Hinnantae2378a2013-05-16 17:13:40 +0000306 return as_integer<long long>( "stoll", str, idx, base );
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000307}
308
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000309unsigned long long
310stoull(const string& str, size_t* idx, int base)
311{
Howard Hinnantae2378a2013-05-16 17:13:40 +0000312 return as_integer<unsigned long long>( "stoull", str, idx, base );
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000313}
314
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000315float
316stof(const string& str, size_t* idx)
317{
Howard Hinnantae2378a2013-05-16 17:13:40 +0000318 return as_float<float>( "stof", str, idx );
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000319}
320
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000321double
322stod(const string& str, size_t* idx)
323{
Howard Hinnantae2378a2013-05-16 17:13:40 +0000324 return as_float<double>( "stod", str, idx );
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000325}
326
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000327long double
328stold(const string& str, size_t* idx)
329{
Howard Hinnantae2378a2013-05-16 17:13:40 +0000330 return as_float<long double>( "stold", str, idx );
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000331}
332
Louis Dionne89258142021-08-23 15:32:36 -0400333#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Louis Dionne417986b2022-05-24 11:22:43 -0400334int
335stoi(const wstring& str, size_t* idx, int base)
336{
337 return as_integer<int>( "stoi", str, idx, base );
338}
339
340long
341stol(const wstring& str, size_t* idx, int base)
342{
343 return as_integer<long>( "stol", str, idx, base );
344}
345
346unsigned long
347stoul(const wstring& str, size_t* idx, int base)
348{
349 return as_integer<unsigned long>( "stoul", str, idx, base );
350}
351
352long long
353stoll(const wstring& str, size_t* idx, int base)
354{
355 return as_integer<long long>( "stoll", str, idx, base );
356}
357
358unsigned long long
359stoull(const wstring& str, size_t* idx, int base)
360{
361 return as_integer<unsigned long long>( "stoull", str, idx, base );
362}
363
364float
365stof(const wstring& str, size_t* idx)
366{
367 return as_float<float>( "stof", str, idx );
368}
369
370double
371stod(const wstring& str, size_t* idx)
372{
373 return as_float<double>( "stod", str, idx );
374}
375
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000376long double
377stold(const wstring& str, size_t* idx)
378{
Howard Hinnantae2378a2013-05-16 17:13:40 +0000379 return as_float<long double>( "stold", str, idx );
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000380}
Louis Dionne417986b2022-05-24 11:22:43 -0400381#endif // !_LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000382
Howard Hinnantae2378a2013-05-16 17:13:40 +0000383// to_string
384
385namespace
386{
387
388// as_string
389
390template<typename S, typename P, typename V >
391inline
392S
393as_string(P sprintf_like, S s, const typename S::value_type* fmt, V a)
394{
395 typedef typename S::size_type size_type;
396 size_type available = s.size();
397 while (true)
398 {
399 int status = sprintf_like(&s[0], available + 1, fmt, a);
400 if ( status >= 0 )
401 {
402 size_type used = static_cast<size_type>(status);
403 if ( used <= available )
404 {
405 s.resize( used );
406 break;
407 }
408 available = used; // Assume this is advice of how much space we need.
409 }
410 else
411 available = available * 2 + 1;
412 s.resize(available);
413 }
414 return s;
415}
416
Marshall Clow54e41492019-06-10 23:20:01 +0000417template <class S>
Howard Hinnantae2378a2013-05-16 17:13:40 +0000418struct initial_string;
419
Marshall Clow54e41492019-06-10 23:20:01 +0000420template <>
421struct initial_string<string>
Howard Hinnantae2378a2013-05-16 17:13:40 +0000422{
423 string
424 operator()() const
425 {
426 string s;
427 s.resize(s.capacity());
428 return s;
429 }
430};
431
Louis Dionne89258142021-08-23 15:32:36 -0400432#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Marshall Clow54e41492019-06-10 23:20:01 +0000433template <>
434struct initial_string<wstring>
Howard Hinnantae2378a2013-05-16 17:13:40 +0000435{
436 wstring
437 operator()() const
438 {
439 wstring s(20, wchar_t());
440 s.resize(s.capacity());
441 return s;
442 }
443};
444
445typedef int (*wide_printf)(wchar_t* __restrict, size_t, const wchar_t*__restrict, ...);
446
447inline
448wide_printf
449get_swprintf()
450{
Howard Hinnant13d8bc12013-08-01 18:17:34 +0000451#ifndef _LIBCPP_MSVCRT
Howard Hinnantae2378a2013-05-16 17:13:40 +0000452 return swprintf;
453#else
Eric Fiselier4a984f02017-05-10 20:57:45 +0000454 return static_cast<int (__cdecl*)(wchar_t* __restrict, size_t, const wchar_t*__restrict, ...)>(_snwprintf);
Howard Hinnantae2378a2013-05-16 17:13:40 +0000455#endif
456}
Louis Dionne89258142021-08-23 15:32:36 -0400457#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnantae2378a2013-05-16 17:13:40 +0000458
Marshall Clow54e41492019-06-10 23:20:01 +0000459template <typename S, typename V>
Louis Dionned9f528e2021-06-29 13:52:26 -0400460S i_to_string(V v)
Marshall Clow54e41492019-06-10 23:20:01 +0000461{
462// numeric_limits::digits10 returns value less on 1 than desired for unsigned numbers.
463// For example, for 1-byte unsigned value digits10 is 2 (999 can not be represented),
464// so we need +1 here.
465 constexpr size_t bufsize = numeric_limits<V>::digits10 + 2; // +1 for minus, +1 for digits10
466 char buf[bufsize];
467 const auto res = to_chars(buf, buf + bufsize, v);
468 _LIBCPP_ASSERT(res.ec == errc(), "bufsize must be large enough to accomodate the value");
469 return S(buf, res.ptr);
470}
471
Howard Hinnantae2378a2013-05-16 17:13:40 +0000472} // unnamed namespace
473
Marshall Clow54e41492019-06-10 23:20:01 +0000474string to_string (int val) { return i_to_string< string>(val); }
475string to_string (long val) { return i_to_string< string>(val); }
476string to_string (long long val) { return i_to_string< string>(val); }
477string to_string (unsigned val) { return i_to_string< string>(val); }
478string to_string (unsigned long val) { return i_to_string< string>(val); }
479string to_string (unsigned long long val) { return i_to_string< string>(val); }
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000480
Louis Dionne89258142021-08-23 15:32:36 -0400481#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Marshall Clow54e41492019-06-10 23:20:01 +0000482wstring to_wstring(int val) { return i_to_string<wstring>(val); }
483wstring to_wstring(long val) { return i_to_string<wstring>(val); }
484wstring to_wstring(long long val) { return i_to_string<wstring>(val); }
485wstring to_wstring(unsigned val) { return i_to_string<wstring>(val); }
486wstring to_wstring(unsigned long val) { return i_to_string<wstring>(val); }
487wstring to_wstring(unsigned long long val) { return i_to_string<wstring>(val); }
Louis Dionne89258142021-08-23 15:32:36 -0400488#endif
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000489
Marshall Clow54e41492019-06-10 23:20:01 +0000490string to_string (float val) { return as_string(snprintf, initial_string< string>()(), "%f", val); }
491string to_string (double val) { return as_string(snprintf, initial_string< string>()(), "%f", val); }
492string to_string (long double val) { return as_string(snprintf, initial_string< string>()(), "%Lf", val); }
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000493
Louis Dionne89258142021-08-23 15:32:36 -0400494#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Marshall Clow54e41492019-06-10 23:20:01 +0000495wstring to_wstring(float val) { return as_string(get_swprintf(), initial_string<wstring>()(), L"%f", val); }
496wstring to_wstring(double val) { return as_string(get_swprintf(), initial_string<wstring>()(), L"%f", val); }
497wstring to_wstring(long double val) { return as_string(get_swprintf(), initial_string<wstring>()(), L"%Lf", val); }
Louis Dionne89258142021-08-23 15:32:36 -0400498#endif
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000499
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000500_LIBCPP_END_NAMESPACE_STD