blob: 8db129520ee501d8757cd927875fb7245eff43f0 [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 Dionne63251842022-05-24 15:31:3159template 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>
Louis Dionne63251842022-05-24 15:31:3165inline void throw_helper(const string& msg) {
Howard Hinnant9daaf572013-05-16 17:13:4066#ifndef _LIBCPP_NO_EXCEPTIONS
Louis Dionne63251842022-05-24 15:31:3167 throw T(msg);
Howard Hinnant9daaf572013-05-16 17:13:4068#else
Ed Schoutenc19393c2015-03-10 07:57:4369 fprintf(stderr, "%s\n", msg.c_str());
Marshall Clowd437fa52016-08-25 15:09:0170 _VSTD::abort();
Howard Hinnant9daaf572013-05-16 17:13:4071#endif
72}
73
Louis Dionne63251842022-05-24 15:31:3174inline void throw_from_string_out_of_range(const string& func) {
Howard Hinnant9daaf572013-05-16 17:13:4075 throw_helper<out_of_range>(func + ": out of range");
76}
77
Louis Dionne63251842022-05-24 15:31:3178inline void throw_from_string_invalid_arg(const string& func) {
Howard Hinnant9daaf572013-05-16 17:13:4079 throw_helper<invalid_argument>(func + ": no conversion");
80}
81
82// as_integer
83
84template<typename V, typename S, typename F>
Louis Dionne63251842022-05-24 15:31:3185inline V as_integer_helper(const string& func, const S& str, size_t* idx, int base, F f) {
Eric Fiselierd6bd7bf2014-11-14 22:23:5786 typename S::value_type* ptr = nullptr;
Howard Hinnant9daaf572013-05-16 17:13:4087 const typename S::value_type* const p = str.c_str();
88 typename remove_reference<decltype(errno)>::type errno_save = errno;
89 errno = 0;
90 V r = f(p, &ptr, base);
91 swap(errno, errno_save);
92 if (errno_save == ERANGE)
93 throw_from_string_out_of_range(func);
94 if (ptr == p)
95 throw_from_string_invalid_arg(func);
96 if (idx)
97 *idx = static_cast<size_t>(ptr - p);
98 return r;
99}
100
101template<typename V, typename S>
Louis Dionne63251842022-05-24 15:31:31102inline V as_integer(const string& func, const S& s, size_t* idx, int base);
Howard Hinnant9daaf572013-05-16 17:13:40103
104// string
105template<>
Louis Dionne63251842022-05-24 15:31:31106inline int as_integer(const string& func, const string& s, size_t* idx, int base) {
Joerg Sonnenberger8092c952013-09-17 08:46:53107 // Use long as no Standard string to integer exists.
Louis Dionne63251842022-05-24 15:31:31108 long r = as_integer_helper<long>(func, s, idx, base, strtol);
Howard Hinnant9daaf572013-05-16 17:13:40109 if (r < numeric_limits<int>::min() || numeric_limits<int>::max() < r)
110 throw_from_string_out_of_range(func);
111 return static_cast<int>(r);
112}
113
114template<>
Louis Dionne63251842022-05-24 15:31:31115inline long as_integer(const string& func, const string& s, size_t* idx, int base) {
116 return as_integer_helper<long>(func, s, idx, base, strtol);
Howard Hinnant9daaf572013-05-16 17:13:40117}
118
119template<>
Louis Dionne63251842022-05-24 15:31:31120inline unsigned long as_integer(const string& func, const string& s, size_t* idx, int base) {
121 return as_integer_helper<unsigned long>(func, s, idx, base, strtoul);
Howard Hinnant9daaf572013-05-16 17:13:40122}
123
124template<>
Louis Dionne63251842022-05-24 15:31:31125inline long long as_integer(const string& func, const string& s, size_t* idx, int base) {
126 return as_integer_helper<long long>(func, s, idx, base, strtoll);
Howard Hinnant9daaf572013-05-16 17:13:40127}
128
129template<>
Louis Dionne63251842022-05-24 15:31:31130inline unsigned long long as_integer(const string& func, const string& s, size_t* idx, int base) {
131 return as_integer_helper<unsigned long long>(func, s, idx, base, strtoull);
Howard Hinnant9daaf572013-05-16 17:13:40132}
133
Louis Dionnef4c12582021-08-23 19:32:36134#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnant9daaf572013-05-16 17:13:40135// wstring
136template<>
Louis Dionne63251842022-05-24 15:31:31137inline int as_integer(const string& func, const wstring& s, size_t* idx, int base) {
Howard Hinnant9daaf572013-05-16 17:13:40138 // Use long as no Stantard string to integer exists.
Louis Dionne63251842022-05-24 15:31:31139 long r = as_integer_helper<long>(func, s, idx, base, wcstol);
Howard Hinnant9daaf572013-05-16 17:13:40140 if (r < numeric_limits<int>::min() || numeric_limits<int>::max() < r)
141 throw_from_string_out_of_range(func);
142 return static_cast<int>(r);
143}
144
145template<>
Louis Dionne63251842022-05-24 15:31:31146inline long as_integer(const string& func, const wstring& s, size_t* idx, int base) {
147 return as_integer_helper<long>(func, s, idx, base, wcstol);
Howard Hinnant9daaf572013-05-16 17:13:40148}
149
150template<>
151inline
152unsigned long
Louis Dionne63251842022-05-24 15:31:31153as_integer(const string& func, const wstring& s, size_t* idx, int base)
Howard Hinnant9daaf572013-05-16 17:13:40154{
Louis Dionne63251842022-05-24 15:31:31155 return as_integer_helper<unsigned long>(func, s, idx, base, wcstoul);
Howard Hinnant9daaf572013-05-16 17:13:40156}
157
158template<>
Louis Dionne63251842022-05-24 15:31:31159inline long long as_integer(const string& func, const wstring& s, size_t* idx, int base) {
160 return as_integer_helper<long long>(func, s, idx, base, wcstoll);
Howard Hinnant9daaf572013-05-16 17:13:40161}
162
163template<>
Louis Dionne63251842022-05-24 15:31:31164inline unsigned long long as_integer(const string& func, const wstring& s, size_t* idx, int base) {
165 return as_integer_helper<unsigned long long>(func, s, idx, base, wcstoull);
Howard Hinnant9daaf572013-05-16 17:13:40166}
Louis Dionnef4c12582021-08-23 19:32:36167#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnant9daaf572013-05-16 17:13:40168
169// as_float
170
Marshall Clow141c2b72019-06-10 23:20:01171template<typename V, typename S, typename F>
Louis Dionne63251842022-05-24 15:31:31172inline V as_float_helper(const string& func, const S& str, size_t* idx, F f) {
Eric Fiselierd6bd7bf2014-11-14 22:23:57173 typename S::value_type* ptr = nullptr;
Howard Hinnant9daaf572013-05-16 17:13:40174 const typename S::value_type* const p = str.c_str();
175 typename remove_reference<decltype(errno)>::type errno_save = errno;
176 errno = 0;
177 V r = f(p, &ptr);
178 swap(errno, errno_save);
179 if (errno_save == ERANGE)
180 throw_from_string_out_of_range(func);
181 if (ptr == p)
182 throw_from_string_invalid_arg(func);
183 if (idx)
184 *idx = static_cast<size_t>(ptr - p);
185 return r;
186}
187
188template<typename V, typename S>
Louis Dionne63251842022-05-24 15:31:31189inline V as_float(const string& func, const S& s, size_t* idx = nullptr);
Howard Hinnant9daaf572013-05-16 17:13:40190
191template<>
Louis Dionne63251842022-05-24 15:31:31192inline float as_float(const string& func, const string& s, size_t* idx) {
193 return as_float_helper<float>(func, s, idx, strtof);
Howard Hinnant9daaf572013-05-16 17:13:40194}
195
196template<>
Louis Dionne63251842022-05-24 15:31:31197inline double as_float(const string& func, const string& s, size_t* idx) {
198 return as_float_helper<double>(func, s, idx, strtod);
Howard Hinnant9daaf572013-05-16 17:13:40199}
200
201template<>
Louis Dionne63251842022-05-24 15:31:31202inline long double as_float(const string& func, const string& s, size_t* idx) {
203 return as_float_helper<long double>(func, s, idx, strtold);
Howard Hinnant9daaf572013-05-16 17:13:40204}
205
Louis Dionnef4c12582021-08-23 19:32:36206#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnant9daaf572013-05-16 17:13:40207template<>
Louis Dionne63251842022-05-24 15:31:31208inline float as_float(const string& func, const wstring& s, size_t* idx) {
209 return as_float_helper<float>(func, s, idx, wcstof);
Howard Hinnant9daaf572013-05-16 17:13:40210}
211
212template<>
Louis Dionne63251842022-05-24 15:31:31213inline double as_float(const string& func, const wstring& s, size_t* idx) {
214 return as_float_helper<double>(func, s, idx, wcstod);
Howard Hinnant9daaf572013-05-16 17:13:40215}
216
217template<>
Louis Dionne63251842022-05-24 15:31:31218inline long double as_float(const string& func, const wstring& s, size_t* idx) {
219 return as_float_helper<long double>(func, s, idx, wcstold);
Howard Hinnant9daaf572013-05-16 17:13:40220}
Louis Dionnef4c12582021-08-23 19:32:36221#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnant9daaf572013-05-16 17:13:40222
223} // unnamed namespace
224
Louis Dionne63251842022-05-24 15:31:31225int stoi(const string& str, size_t* idx, int base) {
226 return as_integer<int>("stoi", str, idx, base);
Howard Hinnantcbbf6332010-06-02 18:20:39227}
228
Louis Dionne63251842022-05-24 15:31:31229long stol(const string& str, size_t* idx, int base) {
230 return as_integer<long>("stol", str, idx, base);
Howard Hinnantcbbf6332010-06-02 18:20:39231}
232
Louis Dionne63251842022-05-24 15:31:31233unsigned long stoul(const string& str, size_t* idx, int base) {
234 return as_integer<unsigned long>("stoul", str, idx, base);
Howard Hinnantcbbf6332010-06-02 18:20:39235}
236
Louis Dionne63251842022-05-24 15:31:31237long long stoll(const string& str, size_t* idx, int base) {
238 return as_integer<long long>("stoll", str, idx, base);
Howard Hinnantcbbf6332010-06-02 18:20:39239}
240
Louis Dionne63251842022-05-24 15:31:31241unsigned long long stoull(const string& str, size_t* idx, int base) {
242 return as_integer<unsigned long long>("stoull", str, idx, base);
Howard Hinnantcbbf6332010-06-02 18:20:39243}
244
Louis Dionne63251842022-05-24 15:31:31245float stof(const string& str, size_t* idx) {
246 return as_float<float>("stof", str, idx);
Howard Hinnantcbbf6332010-06-02 18:20:39247}
248
Louis Dionne63251842022-05-24 15:31:31249double stod(const string& str, size_t* idx) {
250 return as_float<double>("stod", str, idx);
Howard Hinnantcbbf6332010-06-02 18:20:39251}
252
Louis Dionne63251842022-05-24 15:31:31253long double stold(const string& str, size_t* idx) {
254 return as_float<long double>("stold", str, idx);
Howard Hinnantcbbf6332010-06-02 18:20:39255}
256
Louis Dionnef4c12582021-08-23 19:32:36257#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Louis Dionne63251842022-05-24 15:31:31258int stoi(const wstring& str, size_t* idx, int base) {
259 return as_integer<int>("stoi", str, idx, base);
Louis Dionnedc8a9a02022-05-24 15:22:43260}
261
Louis Dionne63251842022-05-24 15:31:31262long stol(const wstring& str, size_t* idx, int base) {
263 return as_integer<long>("stol", str, idx, base);
Louis Dionnedc8a9a02022-05-24 15:22:43264}
265
Louis Dionne63251842022-05-24 15:31:31266unsigned long stoul(const wstring& str, size_t* idx, int base) {
267 return as_integer<unsigned long>("stoul", str, idx, base);
Louis Dionnedc8a9a02022-05-24 15:22:43268}
269
Louis Dionne63251842022-05-24 15:31:31270long long stoll(const wstring& str, size_t* idx, int base) {
271 return as_integer<long long>("stoll", str, idx, base);
Louis Dionnedc8a9a02022-05-24 15:22:43272}
273
Louis Dionne63251842022-05-24 15:31:31274unsigned long long stoull(const wstring& str, size_t* idx, int base) {
275 return as_integer<unsigned long long>("stoull", str, idx, base);
Louis Dionnedc8a9a02022-05-24 15:22:43276}
277
Louis Dionne63251842022-05-24 15:31:31278float stof(const wstring& str, size_t* idx) {
279 return as_float<float>("stof", str, idx);
Louis Dionnedc8a9a02022-05-24 15:22:43280}
281
Louis Dionne63251842022-05-24 15:31:31282double stod(const wstring& str, size_t* idx) {
283 return as_float<double>("stod", str, idx);
Louis Dionnedc8a9a02022-05-24 15:22:43284}
285
Louis Dionne63251842022-05-24 15:31:31286long double stold(const wstring& str, size_t* idx) {
287 return as_float<long double>("stold", str, idx);
Howard Hinnantcbbf6332010-06-02 18:20:39288}
Louis Dionnedc8a9a02022-05-24 15:22:43289#endif // !_LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnantcbbf6332010-06-02 18:20:39290
Howard Hinnant9daaf572013-05-16 17:13:40291// to_string
292
293namespace
294{
295
296// as_string
297
298template<typename S, typename P, typename V >
Louis Dionne63251842022-05-24 15:31:31299inline S as_string(P sprintf_like, S s, const typename S::value_type* fmt, V a) {
Howard Hinnant9daaf572013-05-16 17:13:40300 typedef typename S::size_type size_type;
301 size_type available = s.size();
Louis Dionne63251842022-05-24 15:31:31302 while (true) {
Howard Hinnant9daaf572013-05-16 17:13:40303 int status = sprintf_like(&s[0], available + 1, fmt, a);
Louis Dionne63251842022-05-24 15:31:31304 if (status >= 0) {
Howard Hinnant9daaf572013-05-16 17:13:40305 size_type used = static_cast<size_type>(status);
Louis Dionne63251842022-05-24 15:31:31306 if (used <= available) {
307 s.resize(used);
Howard Hinnant9daaf572013-05-16 17:13:40308 break;
309 }
310 available = used; // Assume this is advice of how much space we need.
311 }
312 else
313 available = available * 2 + 1;
314 s.resize(available);
315 }
316 return s;
317}
318
Marshall Clow141c2b72019-06-10 23:20:01319template <class S>
Howard Hinnant9daaf572013-05-16 17:13:40320struct initial_string;
321
Marshall Clow141c2b72019-06-10 23:20:01322template <>
Louis Dionne63251842022-05-24 15:31:31323struct initial_string<string> {
324 string operator()() const {
Howard Hinnant9daaf572013-05-16 17:13:40325 string s;
326 s.resize(s.capacity());
327 return s;
328 }
329};
330
Louis Dionnef4c12582021-08-23 19:32:36331#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Marshall Clow141c2b72019-06-10 23:20:01332template <>
Louis Dionne63251842022-05-24 15:31:31333struct initial_string<wstring> {
334 wstring operator()() const {
Howard Hinnant9daaf572013-05-16 17:13:40335 wstring s(20, wchar_t());
336 s.resize(s.capacity());
337 return s;
338 }
339};
340
341typedef int (*wide_printf)(wchar_t* __restrict, size_t, const wchar_t*__restrict, ...);
342
Louis Dionne63251842022-05-24 15:31:31343inline wide_printf get_swprintf() {
Howard Hinnant0be8f642013-08-01 18:17:34344#ifndef _LIBCPP_MSVCRT
Howard Hinnant9daaf572013-05-16 17:13:40345 return swprintf;
346#else
Eric Fiselier5d50aa32017-05-10 20:57:45347 return static_cast<int (__cdecl*)(wchar_t* __restrict, size_t, const wchar_t*__restrict, ...)>(_snwprintf);
Howard Hinnant9daaf572013-05-16 17:13:40348#endif
349}
Louis Dionnef4c12582021-08-23 19:32:36350#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnant9daaf572013-05-16 17:13:40351
Marshall Clow141c2b72019-06-10 23:20:01352template <typename S, typename V>
Louis Dionne63251842022-05-24 15:31:31353S i_to_string(V v) {
Marshall Clow141c2b72019-06-10 23:20:01354// numeric_limits::digits10 returns value less on 1 than desired for unsigned numbers.
355// For example, for 1-byte unsigned value digits10 is 2 (999 can not be represented),
356// so we need +1 here.
357 constexpr size_t bufsize = numeric_limits<V>::digits10 + 2; // +1 for minus, +1 for digits10
358 char buf[bufsize];
359 const auto res = to_chars(buf, buf + bufsize, v);
360 _LIBCPP_ASSERT(res.ec == errc(), "bufsize must be large enough to accomodate the value");
361 return S(buf, res.ptr);
362}
363
Howard Hinnant9daaf572013-05-16 17:13:40364} // unnamed namespace
365
Marshall Clow141c2b72019-06-10 23:20:01366string to_string (int val) { return i_to_string< string>(val); }
367string to_string (long val) { return i_to_string< string>(val); }
368string to_string (long long val) { return i_to_string< string>(val); }
369string to_string (unsigned val) { return i_to_string< string>(val); }
370string to_string (unsigned long val) { return i_to_string< string>(val); }
371string to_string (unsigned long long val) { return i_to_string< string>(val); }
Howard Hinnantcbbf6332010-06-02 18:20:39372
Louis Dionnef4c12582021-08-23 19:32:36373#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Marshall Clow141c2b72019-06-10 23:20:01374wstring to_wstring(int val) { return i_to_string<wstring>(val); }
375wstring to_wstring(long val) { return i_to_string<wstring>(val); }
376wstring to_wstring(long long val) { return i_to_string<wstring>(val); }
377wstring to_wstring(unsigned val) { return i_to_string<wstring>(val); }
378wstring to_wstring(unsigned long val) { return i_to_string<wstring>(val); }
379wstring to_wstring(unsigned long long val) { return i_to_string<wstring>(val); }
Louis Dionnef4c12582021-08-23 19:32:36380#endif
Howard Hinnantcbbf6332010-06-02 18:20:39381
Marshall Clow141c2b72019-06-10 23:20:01382string to_string (float val) { return as_string(snprintf, initial_string< string>()(), "%f", val); }
383string to_string (double val) { return as_string(snprintf, initial_string< string>()(), "%f", val); }
384string to_string (long double val) { return as_string(snprintf, initial_string< string>()(), "%Lf", val); }
Howard Hinnantcbbf6332010-06-02 18:20:39385
Louis Dionnef4c12582021-08-23 19:32:36386#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Marshall Clow141c2b72019-06-10 23:20:01387wstring to_wstring(float val) { return as_string(get_swprintf(), initial_string<wstring>()(), L"%f", val); }
388wstring to_wstring(double val) { return as_string(get_swprintf(), initial_string<wstring>()(), L"%f", val); }
389wstring to_wstring(long double val) { return as_string(get_swprintf(), initial_string<wstring>()(), L"%Lf", val); }
Louis Dionnef4c12582021-08-23 19:32:36390#endif
Howard Hinnantcbbf6332010-06-02 18:20:39391
Howard Hinnantcbbf6332010-06-02 18:20:39392_LIBCPP_END_NAMESPACE_STD