blob: 9d1de0cf6ca1a6c0e27b438201402e3df9225686 [file] [log] [blame]
Louis Dionneeb8650a2021-11-17 21:25:011//===----------------------------------------------------------------------===//
Howard Hinnantcbbf6332010-06-02 18:20:392//
Chandler Carruth57b08b02019-01-19 10:56:403// 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 Hinnantcbbf6332010-06-02 18:20:396//
7//===----------------------------------------------------------------------===//
8
Louis Dionnef87aa192022-02-14 18:41:099#include <__assert>
Arthur O'Dwyerbbb0f2c2022-02-11 18:00:3910#include <cerrno>
11#include <charconv>
12#include <cstdlib>
13#include <limits>
14#include <stdexcept>
Howard Hinnant1468d0c2013-07-23 16:05:5615#include <stdio.h>
Arthur O'Dwyerbbb0f2c2022-02-11 18:00:3916#include <string>
Howard Hinnantcbbf6332010-06-02 18:20:3917
Louis Dionnef4c12582021-08-23 19:32:3618#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Arthur O'Dwyerbbb0f2c2022-02-11 18:00:3919# include <cwchar>
Louis Dionnef4c12582021-08-23 19:32:3620#endif
21
Howard Hinnantcbbf6332010-06-02 18:20:3922_LIBCPP_BEGIN_NAMESPACE_STD
23
Nikolas Klauser5173f432022-02-02 19:15:4024#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 Dionne84b0b522021-08-19 16:21:0636void __basic_string_common<true>::__throw_length_error() const {
Nikolas Klauser5173f432022-02-02 19:15:4037 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 Dionne84b0b522021-08-19 16:21:0641}
42
Nikolas Klauser5173f432022-02-02 19:15:4043#endif // _LIBCPP_ABI_DO_NOT_EXPORT_BASIC_STRING_COMMON
Howard Hinnantcbbf6332010-06-02 18:20:3944
Louis Dionneb648c612021-06-09 13:41:2745#define _LIBCPP_EXTERN_TEMPLATE_DEFINE(...) template __VA_ARGS__;
Martijn Vels67532642020-03-02 15:11:3546#ifdef _LIBCPP_ABI_STRING_OPTIMIZED_EXTERNAL_INSTANTIATION
Louis Dionnef4c12582021-08-23 19:32:3647 _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 Velsd8969a12020-02-19 21:27:5051#else
Louis Dionnef4c12582021-08-23 19:32:3652 _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 Velsd8969a12020-02-19 21:27:5056#endif
Louis Dionneb648c612021-06-09 13:41:2757#undef _LIBCPP_EXTERN_TEMPLATE_DEFINE
Howard Hinnantcbbf6332010-06-02 18:20:3958
Louis Dionneb648c612021-06-09 13:41:2759template string operator+<char, char_traits<char>, allocator<char> >(char const*, string const&);
Howard Hinnantcbbf6332010-06-02 18:20:3960
Howard Hinnant9daaf572013-05-16 17:13:4061namespace
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 Schoutenc19393c2015-03-10 07:57:4371 fprintf(stderr, "%s\n", msg.c_str());
Marshall Clowd437fa52016-08-25 15:09:0172 _VSTD::abort();
Howard Hinnant9daaf572013-05-16 17:13:4073#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 Fiselierd6bd7bf2014-11-14 22:23:5795 typename S::value_type* ptr = nullptr;
Howard Hinnant9daaf572013-05-16 17:13:4096 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 Sonnenberger8092c952013-09-17 08:46:53121 // Use long as no Standard string to integer exists.
Howard Hinnant9daaf572013-05-16 17:13:40122 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 Dionnef4c12582021-08-23 19:32:36160#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnant9daaf572013-05-16 17:13:40161// 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 Dionnef4c12582021-08-23 19:32:36205#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnant9daaf572013-05-16 17:13:40206
207// as_float
208
Marshall Clow141c2b72019-06-10 23:20:01209template<typename V, typename S, typename F>
Howard Hinnant9daaf572013-05-16 17:13:40210inline
211V
212as_float_helper(const string& func, const S& str, size_t* idx, F f )
213{
Eric Fiselierd6bd7bf2014-11-14 22:23:57214 typename S::value_type* ptr = nullptr;
Howard Hinnant9daaf572013-05-16 17:13:40215 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 Dionnef4c12582021-08-23 19:32:36257#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnant9daaf572013-05-16 17:13:40258template<>
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 Dionnef4c12582021-08-23 19:32:36281#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnant9daaf572013-05-16 17:13:40282
283} // unnamed namespace
284
Howard Hinnantcbbf6332010-06-02 18:20:39285int
286stoi(const string& str, size_t* idx, int base)
287{
Howard Hinnant9daaf572013-05-16 17:13:40288 return as_integer<int>( "stoi", str, idx, base );
Howard Hinnantcbbf6332010-06-02 18:20:39289}
290
Louis Dionnef4c12582021-08-23 19:32:36291#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnantcbbf6332010-06-02 18:20:39292int
293stoi(const wstring& str, size_t* idx, int base)
294{
Howard Hinnant9daaf572013-05-16 17:13:40295 return as_integer<int>( "stoi", str, idx, base );
Howard Hinnantcbbf6332010-06-02 18:20:39296}
Louis Dionnef4c12582021-08-23 19:32:36297#endif
Howard Hinnantcbbf6332010-06-02 18:20:39298
299long
300stol(const string& str, size_t* idx, int base)
301{
Howard Hinnant9daaf572013-05-16 17:13:40302 return as_integer<long>( "stol", str, idx, base );
Howard Hinnantcbbf6332010-06-02 18:20:39303}
304
Louis Dionnef4c12582021-08-23 19:32:36305#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnantcbbf6332010-06-02 18:20:39306long
307stol(const wstring& str, size_t* idx, int base)
308{
Howard Hinnant9daaf572013-05-16 17:13:40309 return as_integer<long>( "stol", str, idx, base );
Howard Hinnantcbbf6332010-06-02 18:20:39310}
Louis Dionnef4c12582021-08-23 19:32:36311#endif
Howard Hinnantcbbf6332010-06-02 18:20:39312
313unsigned long
314stoul(const string& str, size_t* idx, int base)
315{
Howard Hinnant9daaf572013-05-16 17:13:40316 return as_integer<unsigned long>( "stoul", str, idx, base );
Howard Hinnantcbbf6332010-06-02 18:20:39317}
318
Louis Dionnef4c12582021-08-23 19:32:36319#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnantcbbf6332010-06-02 18:20:39320unsigned long
321stoul(const wstring& str, size_t* idx, int base)
322{
Howard Hinnant9daaf572013-05-16 17:13:40323 return as_integer<unsigned long>( "stoul", str, idx, base );
Howard Hinnantcbbf6332010-06-02 18:20:39324}
Louis Dionnef4c12582021-08-23 19:32:36325#endif
Howard Hinnantcbbf6332010-06-02 18:20:39326
327long long
328stoll(const string& str, size_t* idx, int base)
329{
Howard Hinnant9daaf572013-05-16 17:13:40330 return as_integer<long long>( "stoll", str, idx, base );
Howard Hinnantcbbf6332010-06-02 18:20:39331}
332
Louis Dionnef4c12582021-08-23 19:32:36333#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnantcbbf6332010-06-02 18:20:39334long long
335stoll(const wstring& str, size_t* idx, int base)
336{
Howard Hinnant9daaf572013-05-16 17:13:40337 return as_integer<long long>( "stoll", str, idx, base );
Howard Hinnantcbbf6332010-06-02 18:20:39338}
Louis Dionnef4c12582021-08-23 19:32:36339#endif
Howard Hinnantcbbf6332010-06-02 18:20:39340
341unsigned long long
342stoull(const string& str, size_t* idx, int base)
343{
Howard Hinnant9daaf572013-05-16 17:13:40344 return as_integer<unsigned long long>( "stoull", str, idx, base );
Howard Hinnantcbbf6332010-06-02 18:20:39345}
346
Louis Dionnef4c12582021-08-23 19:32:36347#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnantcbbf6332010-06-02 18:20:39348unsigned long long
349stoull(const wstring& str, size_t* idx, int base)
350{
Howard Hinnant9daaf572013-05-16 17:13:40351 return as_integer<unsigned long long>( "stoull", str, idx, base );
Howard Hinnantcbbf6332010-06-02 18:20:39352}
Louis Dionnef4c12582021-08-23 19:32:36353#endif
Howard Hinnantcbbf6332010-06-02 18:20:39354
355float
356stof(const string& str, size_t* idx)
357{
Howard Hinnant9daaf572013-05-16 17:13:40358 return as_float<float>( "stof", str, idx );
Howard Hinnantcbbf6332010-06-02 18:20:39359}
360
Louis Dionnef4c12582021-08-23 19:32:36361#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnantcbbf6332010-06-02 18:20:39362float
363stof(const wstring& str, size_t* idx)
364{
Howard Hinnant9daaf572013-05-16 17:13:40365 return as_float<float>( "stof", str, idx );
Howard Hinnantcbbf6332010-06-02 18:20:39366}
Louis Dionnef4c12582021-08-23 19:32:36367#endif
Howard Hinnantcbbf6332010-06-02 18:20:39368
369double
370stod(const string& str, size_t* idx)
371{
Howard Hinnant9daaf572013-05-16 17:13:40372 return as_float<double>( "stod", str, idx );
Howard Hinnantcbbf6332010-06-02 18:20:39373}
374
Louis Dionnef4c12582021-08-23 19:32:36375#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnantcbbf6332010-06-02 18:20:39376double
377stod(const wstring& str, size_t* idx)
378{
Howard Hinnant9daaf572013-05-16 17:13:40379 return as_float<double>( "stod", str, idx );
Howard Hinnantcbbf6332010-06-02 18:20:39380}
Louis Dionnef4c12582021-08-23 19:32:36381#endif
Howard Hinnantcbbf6332010-06-02 18:20:39382
383long double
384stold(const string& str, size_t* idx)
385{
Howard Hinnant9daaf572013-05-16 17:13:40386 return as_float<long double>( "stold", str, idx );
Howard Hinnantcbbf6332010-06-02 18:20:39387}
388
Louis Dionnef4c12582021-08-23 19:32:36389#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnantcbbf6332010-06-02 18:20:39390long double
391stold(const wstring& str, size_t* idx)
392{
Howard Hinnant9daaf572013-05-16 17:13:40393 return as_float<long double>( "stold", str, idx );
Howard Hinnantcbbf6332010-06-02 18:20:39394}
Louis Dionnef4c12582021-08-23 19:32:36395#endif
Howard Hinnantcbbf6332010-06-02 18:20:39396
Howard Hinnant9daaf572013-05-16 17:13:40397// to_string
398
399namespace
400{
401
402// as_string
403
404template<typename S, typename P, typename V >
405inline
406S
407as_string(P sprintf_like, S s, const typename S::value_type* fmt, V a)
408{
409 typedef typename S::size_type size_type;
410 size_type available = s.size();
411 while (true)
412 {
413 int status = sprintf_like(&s[0], available + 1, fmt, a);
414 if ( status >= 0 )
415 {
416 size_type used = static_cast<size_type>(status);
417 if ( used <= available )
418 {
419 s.resize( used );
420 break;
421 }
422 available = used; // Assume this is advice of how much space we need.
423 }
424 else
425 available = available * 2 + 1;
426 s.resize(available);
427 }
428 return s;
429}
430
Marshall Clow141c2b72019-06-10 23:20:01431template <class S>
Howard Hinnant9daaf572013-05-16 17:13:40432struct initial_string;
433
Marshall Clow141c2b72019-06-10 23:20:01434template <>
435struct initial_string<string>
Howard Hinnant9daaf572013-05-16 17:13:40436{
437 string
438 operator()() const
439 {
440 string s;
441 s.resize(s.capacity());
442 return s;
443 }
444};
445
Louis Dionnef4c12582021-08-23 19:32:36446#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Marshall Clow141c2b72019-06-10 23:20:01447template <>
448struct initial_string<wstring>
Howard Hinnant9daaf572013-05-16 17:13:40449{
450 wstring
451 operator()() const
452 {
453 wstring s(20, wchar_t());
454 s.resize(s.capacity());
455 return s;
456 }
457};
458
459typedef int (*wide_printf)(wchar_t* __restrict, size_t, const wchar_t*__restrict, ...);
460
461inline
462wide_printf
463get_swprintf()
464{
Howard Hinnant0be8f642013-08-01 18:17:34465#ifndef _LIBCPP_MSVCRT
Howard Hinnant9daaf572013-05-16 17:13:40466 return swprintf;
467#else
Eric Fiselier5d50aa32017-05-10 20:57:45468 return static_cast<int (__cdecl*)(wchar_t* __restrict, size_t, const wchar_t*__restrict, ...)>(_snwprintf);
Howard Hinnant9daaf572013-05-16 17:13:40469#endif
470}
Louis Dionnef4c12582021-08-23 19:32:36471#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnant9daaf572013-05-16 17:13:40472
Marshall Clow141c2b72019-06-10 23:20:01473template <typename S, typename V>
Louis Dionnea5628532021-06-29 17:52:26474S i_to_string(V v)
Marshall Clow141c2b72019-06-10 23:20:01475{
476// numeric_limits::digits10 returns value less on 1 than desired for unsigned numbers.
477// For example, for 1-byte unsigned value digits10 is 2 (999 can not be represented),
478// so we need +1 here.
479 constexpr size_t bufsize = numeric_limits<V>::digits10 + 2; // +1 for minus, +1 for digits10
480 char buf[bufsize];
481 const auto res = to_chars(buf, buf + bufsize, v);
482 _LIBCPP_ASSERT(res.ec == errc(), "bufsize must be large enough to accomodate the value");
483 return S(buf, res.ptr);
484}
485
Howard Hinnant9daaf572013-05-16 17:13:40486} // unnamed namespace
487
Marshall Clow141c2b72019-06-10 23:20:01488string to_string (int val) { return i_to_string< string>(val); }
489string to_string (long val) { return i_to_string< string>(val); }
490string to_string (long long val) { return i_to_string< string>(val); }
491string to_string (unsigned val) { return i_to_string< string>(val); }
492string to_string (unsigned long val) { return i_to_string< string>(val); }
493string to_string (unsigned long long val) { return i_to_string< string>(val); }
Howard Hinnantcbbf6332010-06-02 18:20:39494
Louis Dionnef4c12582021-08-23 19:32:36495#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Marshall Clow141c2b72019-06-10 23:20:01496wstring to_wstring(int val) { return i_to_string<wstring>(val); }
497wstring to_wstring(long val) { return i_to_string<wstring>(val); }
498wstring to_wstring(long long val) { return i_to_string<wstring>(val); }
499wstring to_wstring(unsigned val) { return i_to_string<wstring>(val); }
500wstring to_wstring(unsigned long val) { return i_to_string<wstring>(val); }
501wstring to_wstring(unsigned long long val) { return i_to_string<wstring>(val); }
Louis Dionnef4c12582021-08-23 19:32:36502#endif
Howard Hinnantcbbf6332010-06-02 18:20:39503
Marshall Clow141c2b72019-06-10 23:20:01504string to_string (float val) { return as_string(snprintf, initial_string< string>()(), "%f", val); }
505string to_string (double val) { return as_string(snprintf, initial_string< string>()(), "%f", val); }
506string to_string (long double val) { return as_string(snprintf, initial_string< string>()(), "%Lf", val); }
Howard Hinnantcbbf6332010-06-02 18:20:39507
Louis Dionnef4c12582021-08-23 19:32:36508#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Marshall Clow141c2b72019-06-10 23:20:01509wstring to_wstring(float val) { return as_string(get_swprintf(), initial_string<wstring>()(), L"%f", val); }
510wstring to_wstring(double val) { return as_string(get_swprintf(), initial_string<wstring>()(), L"%f", val); }
511wstring to_wstring(long double val) { return as_string(get_swprintf(), initial_string<wstring>()(), L"%Lf", val); }
Louis Dionnef4c12582021-08-23 19:32:36512#endif
Howard Hinnantcbbf6332010-06-02 18:20:39513
Howard Hinnantcbbf6332010-06-02 18:20:39514_LIBCPP_END_NAMESPACE_STD