blob: d847ad506845c292bec81fbcf2a85710980bc0f2 [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
Howard Hinnantcbbf6332010-06-02 18:20:39291long
292stol(const string& str, size_t* idx, int base)
293{
Howard Hinnant9daaf572013-05-16 17:13:40294 return as_integer<long>( "stol", str, idx, base );
Howard Hinnantcbbf6332010-06-02 18:20:39295}
296
Howard Hinnantcbbf6332010-06-02 18:20:39297unsigned long
298stoul(const string& str, size_t* idx, int base)
299{
Howard Hinnant9daaf572013-05-16 17:13:40300 return as_integer<unsigned long>( "stoul", str, idx, base );
Howard Hinnantcbbf6332010-06-02 18:20:39301}
302
Howard Hinnantcbbf6332010-06-02 18:20:39303long long
304stoll(const string& str, size_t* idx, int base)
305{
Howard Hinnant9daaf572013-05-16 17:13:40306 return as_integer<long long>( "stoll", str, idx, base );
Howard Hinnantcbbf6332010-06-02 18:20:39307}
308
Howard Hinnantcbbf6332010-06-02 18:20:39309unsigned long long
310stoull(const string& str, size_t* idx, int base)
311{
Howard Hinnant9daaf572013-05-16 17:13:40312 return as_integer<unsigned long long>( "stoull", str, idx, base );
Howard Hinnantcbbf6332010-06-02 18:20:39313}
314
Howard Hinnantcbbf6332010-06-02 18:20:39315float
316stof(const string& str, size_t* idx)
317{
Howard Hinnant9daaf572013-05-16 17:13:40318 return as_float<float>( "stof", str, idx );
Howard Hinnantcbbf6332010-06-02 18:20:39319}
320
Howard Hinnantcbbf6332010-06-02 18:20:39321double
322stod(const string& str, size_t* idx)
323{
Howard Hinnant9daaf572013-05-16 17:13:40324 return as_float<double>( "stod", str, idx );
Howard Hinnantcbbf6332010-06-02 18:20:39325}
326
Howard Hinnantcbbf6332010-06-02 18:20:39327long double
328stold(const string& str, size_t* idx)
329{
Howard Hinnant9daaf572013-05-16 17:13:40330 return as_float<long double>( "stold", str, idx );
Howard Hinnantcbbf6332010-06-02 18:20:39331}
332
Louis Dionnef4c12582021-08-23 19:32:36333#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Louis Dionnedc8a9a02022-05-24 15:22:43334int
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 Hinnantcbbf6332010-06-02 18:20:39376long double
377stold(const wstring& str, size_t* idx)
378{
Howard Hinnant9daaf572013-05-16 17:13:40379 return as_float<long double>( "stold", str, idx );
Howard Hinnantcbbf6332010-06-02 18:20:39380}
Louis Dionnedc8a9a02022-05-24 15:22:43381#endif // !_LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnantcbbf6332010-06-02 18:20:39382
Howard Hinnant9daaf572013-05-16 17:13:40383// 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 Clow141c2b72019-06-10 23:20:01417template <class S>
Howard Hinnant9daaf572013-05-16 17:13:40418struct initial_string;
419
Marshall Clow141c2b72019-06-10 23:20:01420template <>
421struct initial_string<string>
Howard Hinnant9daaf572013-05-16 17:13:40422{
423 string
424 operator()() const
425 {
426 string s;
427 s.resize(s.capacity());
428 return s;
429 }
430};
431
Louis Dionnef4c12582021-08-23 19:32:36432#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Marshall Clow141c2b72019-06-10 23:20:01433template <>
434struct initial_string<wstring>
Howard Hinnant9daaf572013-05-16 17:13:40435{
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 Hinnant0be8f642013-08-01 18:17:34451#ifndef _LIBCPP_MSVCRT
Howard Hinnant9daaf572013-05-16 17:13:40452 return swprintf;
453#else
Eric Fiselier5d50aa32017-05-10 20:57:45454 return static_cast<int (__cdecl*)(wchar_t* __restrict, size_t, const wchar_t*__restrict, ...)>(_snwprintf);
Howard Hinnant9daaf572013-05-16 17:13:40455#endif
456}
Louis Dionnef4c12582021-08-23 19:32:36457#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnant9daaf572013-05-16 17:13:40458
Marshall Clow141c2b72019-06-10 23:20:01459template <typename S, typename V>
Louis Dionnea5628532021-06-29 17:52:26460S i_to_string(V v)
Marshall Clow141c2b72019-06-10 23:20:01461{
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 Hinnant9daaf572013-05-16 17:13:40472} // unnamed namespace
473
Marshall Clow141c2b72019-06-10 23:20:01474string 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 Hinnantcbbf6332010-06-02 18:20:39480
Louis Dionnef4c12582021-08-23 19:32:36481#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Marshall Clow141c2b72019-06-10 23:20:01482wstring 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 Dionnef4c12582021-08-23 19:32:36488#endif
Howard Hinnantcbbf6332010-06-02 18:20:39489
Marshall Clow141c2b72019-06-10 23:20:01490string 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 Hinnantcbbf6332010-06-02 18:20:39493
Louis Dionnef4c12582021-08-23 19:32:36494#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Marshall Clow141c2b72019-06-10 23:20:01495wstring 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 Dionnef4c12582021-08-23 19:32:36498#endif
Howard Hinnantcbbf6332010-06-02 18:20:39499
Howard Hinnantcbbf6332010-06-02 18:20:39500_LIBCPP_END_NAMESPACE_STD