blob: 012b2ea0895858082cc71bf46b2d737ef770257d [file] [log] [blame]
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00001//===------------------------- string.cpp ---------------------------------===//
2//
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
9#include "string"
Marshall Clow54e41492019-06-10 23:20:01 +000010#include "charconv"
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +000011#include "cstdlib"
12#include "cwchar"
13#include "cerrno"
Howard Hinnantae2378a2013-05-16 17:13:40 +000014#include "limits"
15#include "stdexcept"
Howard Hinnant84f697e2013-07-23 16:05:56 +000016#include <stdio.h>
Marshall Clow54e41492019-06-10 23:20:01 +000017#include "__debug"
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +000018
19_LIBCPP_BEGIN_NAMESPACE_STD
20
Louis Dionneb6aabd92021-08-19 12:21:06 -040021void __basic_string_common<true>::__throw_length_error() const {
22 _VSTD::__throw_length_error("basic_string");
23}
24
25void __basic_string_common<true>::__throw_out_of_range() const {
26 _VSTD::__throw_out_of_range("basic_string");
27}
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +000028
Louis Dionne732192e2021-06-09 09:41:27 -040029#define _LIBCPP_EXTERN_TEMPLATE_DEFINE(...) template __VA_ARGS__;
Martijn Velsb17d5802020-03-02 10:11:35 -050030#ifdef _LIBCPP_ABI_STRING_OPTIMIZED_EXTERNAL_INSTANTIATION
Martijn Vels1e63ba02020-02-19 16:27:50 -050031_LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE_DEFINE, char)
32_LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE_DEFINE, wchar_t)
33#else
34_LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE_DEFINE, char)
35_LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE_DEFINE, wchar_t)
36#endif
Louis Dionne732192e2021-06-09 09:41:27 -040037#undef _LIBCPP_EXTERN_TEMPLATE_DEFINE
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +000038
Louis Dionne732192e2021-06-09 09:41:27 -040039template string operator+<char, char_traits<char>, allocator<char> >(char const*, string const&);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +000040
Howard Hinnantae2378a2013-05-16 17:13:40 +000041namespace
42{
43
44template<typename T>
45inline
46void throw_helper( const string& msg )
47{
48#ifndef _LIBCPP_NO_EXCEPTIONS
49 throw T( msg );
50#else
Ed Schouten7a441452015-03-10 07:57:43 +000051 fprintf(stderr, "%s\n", msg.c_str());
Marshall Clow8fea1612016-08-25 15:09:01 +000052 _VSTD::abort();
Howard Hinnantae2378a2013-05-16 17:13:40 +000053#endif
54}
55
56inline
57void throw_from_string_out_of_range( const string& func )
58{
59 throw_helper<out_of_range>(func + ": out of range");
60}
61
62inline
63void throw_from_string_invalid_arg( const string& func )
64{
65 throw_helper<invalid_argument>(func + ": no conversion");
66}
67
68// as_integer
69
70template<typename V, typename S, typename F>
71inline
72V
73as_integer_helper(const string& func, const S& str, size_t* idx, int base, F f)
74{
Eric Fiselier002dc672014-11-14 22:23:57 +000075 typename S::value_type* ptr = nullptr;
Howard Hinnantae2378a2013-05-16 17:13:40 +000076 const typename S::value_type* const p = str.c_str();
77 typename remove_reference<decltype(errno)>::type errno_save = errno;
78 errno = 0;
79 V r = f(p, &ptr, base);
80 swap(errno, errno_save);
81 if (errno_save == ERANGE)
82 throw_from_string_out_of_range(func);
83 if (ptr == p)
84 throw_from_string_invalid_arg(func);
85 if (idx)
86 *idx = static_cast<size_t>(ptr - p);
87 return r;
88}
89
90template<typename V, typename S>
91inline
92V
93as_integer(const string& func, const S& s, size_t* idx, int base);
94
95// string
96template<>
97inline
98int
99as_integer(const string& func, const string& s, size_t* idx, int base )
100{
Joerg Sonnenbergere211bc02013-09-17 08:46:53 +0000101 // Use long as no Standard string to integer exists.
Howard Hinnantae2378a2013-05-16 17:13:40 +0000102 long r = as_integer_helper<long>( func, s, idx, base, strtol );
103 if (r < numeric_limits<int>::min() || numeric_limits<int>::max() < r)
104 throw_from_string_out_of_range(func);
105 return static_cast<int>(r);
106}
107
108template<>
109inline
110long
111as_integer(const string& func, const string& s, size_t* idx, int base )
112{
113 return as_integer_helper<long>( func, s, idx, base, strtol );
114}
115
116template<>
117inline
118unsigned long
119as_integer( const string& func, const string& s, size_t* idx, int base )
120{
121 return as_integer_helper<unsigned long>( func, s, idx, base, strtoul );
122}
123
124template<>
125inline
126long long
127as_integer( const string& func, const string& s, size_t* idx, int base )
128{
129 return as_integer_helper<long long>( func, s, idx, base, strtoll );
130}
131
132template<>
133inline
134unsigned long long
135as_integer( const string& func, const string& s, size_t* idx, int base )
136{
137 return as_integer_helper<unsigned long long>( func, s, idx, base, strtoull );
138}
139
140// wstring
141template<>
142inline
143int
144as_integer( const string& func, const wstring& s, size_t* idx, int base )
145{
146 // Use long as no Stantard string to integer exists.
147 long r = as_integer_helper<long>( func, s, idx, base, wcstol );
148 if (r < numeric_limits<int>::min() || numeric_limits<int>::max() < r)
149 throw_from_string_out_of_range(func);
150 return static_cast<int>(r);
151}
152
153template<>
154inline
155long
156as_integer( const string& func, const wstring& s, size_t* idx, int base )
157{
158 return as_integer_helper<long>( func, s, idx, base, wcstol );
159}
160
161template<>
162inline
163unsigned long
164as_integer( const string& func, const wstring& s, size_t* idx, int base )
165{
166 return as_integer_helper<unsigned long>( func, s, idx, base, wcstoul );
167}
168
169template<>
170inline
171long long
172as_integer( const string& func, const wstring& s, size_t* idx, int base )
173{
174 return as_integer_helper<long long>( func, s, idx, base, wcstoll );
175}
176
177template<>
178inline
179unsigned long long
180as_integer( const string& func, const wstring& s, size_t* idx, int base )
181{
182 return as_integer_helper<unsigned long long>( func, s, idx, base, wcstoull );
183}
184
185// as_float
186
Marshall Clow54e41492019-06-10 23:20:01 +0000187template<typename V, typename S, typename F>
Howard Hinnantae2378a2013-05-16 17:13:40 +0000188inline
189V
190as_float_helper(const string& func, const S& str, size_t* idx, F f )
191{
Eric Fiselier002dc672014-11-14 22:23:57 +0000192 typename S::value_type* ptr = nullptr;
Howard Hinnantae2378a2013-05-16 17:13:40 +0000193 const typename S::value_type* const p = str.c_str();
194 typename remove_reference<decltype(errno)>::type errno_save = errno;
195 errno = 0;
196 V r = f(p, &ptr);
197 swap(errno, errno_save);
198 if (errno_save == ERANGE)
199 throw_from_string_out_of_range(func);
200 if (ptr == p)
201 throw_from_string_invalid_arg(func);
202 if (idx)
203 *idx = static_cast<size_t>(ptr - p);
204 return r;
205}
206
207template<typename V, typename S>
208inline
209V as_float( const string& func, const S& s, size_t* idx = nullptr );
210
211template<>
212inline
213float
214as_float( const string& func, const string& s, size_t* idx )
215{
216 return as_float_helper<float>( func, s, idx, strtof );
217}
218
219template<>
220inline
221double
222as_float(const string& func, const string& s, size_t* idx )
223{
224 return as_float_helper<double>( func, s, idx, strtod );
225}
226
227template<>
228inline
229long double
230as_float( const string& func, const string& s, size_t* idx )
231{
232 return as_float_helper<long double>( func, s, idx, strtold );
233}
234
235template<>
236inline
237float
238as_float( const string& func, const wstring& s, size_t* idx )
239{
240 return as_float_helper<float>( func, s, idx, wcstof );
241}
242
243template<>
244inline
245double
246as_float( const string& func, const wstring& s, size_t* idx )
247{
248 return as_float_helper<double>( func, s, idx, wcstod );
249}
250
251template<>
252inline
253long double
254as_float( const string& func, const wstring& s, size_t* idx )
255{
256 return as_float_helper<long double>( func, s, idx, wcstold );
257}
258
259} // unnamed namespace
260
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000261int
262stoi(const string& str, size_t* idx, int base)
263{
Howard Hinnantae2378a2013-05-16 17:13:40 +0000264 return as_integer<int>( "stoi", str, idx, base );
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000265}
266
267int
268stoi(const wstring& str, size_t* idx, int base)
269{
Howard Hinnantae2378a2013-05-16 17:13:40 +0000270 return as_integer<int>( "stoi", str, idx, base );
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000271}
272
273long
274stol(const string& str, size_t* idx, int base)
275{
Howard Hinnantae2378a2013-05-16 17:13:40 +0000276 return as_integer<long>( "stol", str, idx, base );
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000277}
278
279long
280stol(const wstring& str, size_t* idx, int base)
281{
Howard Hinnantae2378a2013-05-16 17:13:40 +0000282 return as_integer<long>( "stol", str, idx, base );
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000283}
284
285unsigned long
286stoul(const string& str, size_t* idx, int base)
287{
Howard Hinnantae2378a2013-05-16 17:13:40 +0000288 return as_integer<unsigned long>( "stoul", str, idx, base );
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000289}
290
291unsigned long
292stoul(const wstring& str, size_t* idx, int base)
293{
Howard Hinnantae2378a2013-05-16 17:13:40 +0000294 return as_integer<unsigned long>( "stoul", str, idx, base );
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000295}
296
297long long
298stoll(const string& str, size_t* idx, int base)
299{
Howard Hinnantae2378a2013-05-16 17:13:40 +0000300 return as_integer<long long>( "stoll", str, idx, base );
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000301}
302
303long long
304stoll(const wstring& 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
309unsigned 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
315unsigned long long
316stoull(const wstring& str, size_t* idx, int base)
317{
Howard Hinnantae2378a2013-05-16 17:13:40 +0000318 return as_integer<unsigned long long>( "stoull", str, idx, base );
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000319}
320
321float
322stof(const string& str, size_t* idx)
323{
Howard Hinnantae2378a2013-05-16 17:13:40 +0000324 return as_float<float>( "stof", str, idx );
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000325}
326
327float
328stof(const wstring& str, size_t* idx)
329{
Howard Hinnantae2378a2013-05-16 17:13:40 +0000330 return as_float<float>( "stof", str, idx );
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000331}
332
333double
334stod(const string& str, size_t* idx)
335{
Howard Hinnantae2378a2013-05-16 17:13:40 +0000336 return as_float<double>( "stod", str, idx );
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000337}
338
339double
340stod(const wstring& str, size_t* idx)
341{
Howard Hinnantae2378a2013-05-16 17:13:40 +0000342 return as_float<double>( "stod", str, idx );
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000343}
344
345long double
346stold(const string& str, size_t* idx)
347{
Howard Hinnantae2378a2013-05-16 17:13:40 +0000348 return as_float<long double>( "stold", str, idx );
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000349}
350
351long double
352stold(const wstring& str, size_t* idx)
353{
Howard Hinnantae2378a2013-05-16 17:13:40 +0000354 return as_float<long double>( "stold", str, idx );
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000355}
356
Howard Hinnantae2378a2013-05-16 17:13:40 +0000357// to_string
358
359namespace
360{
361
362// as_string
363
364template<typename S, typename P, typename V >
365inline
366S
367as_string(P sprintf_like, S s, const typename S::value_type* fmt, V a)
368{
369 typedef typename S::size_type size_type;
370 size_type available = s.size();
371 while (true)
372 {
373 int status = sprintf_like(&s[0], available + 1, fmt, a);
374 if ( status >= 0 )
375 {
376 size_type used = static_cast<size_type>(status);
377 if ( used <= available )
378 {
379 s.resize( used );
380 break;
381 }
382 available = used; // Assume this is advice of how much space we need.
383 }
384 else
385 available = available * 2 + 1;
386 s.resize(available);
387 }
388 return s;
389}
390
Marshall Clow54e41492019-06-10 23:20:01 +0000391template <class S>
Howard Hinnantae2378a2013-05-16 17:13:40 +0000392struct initial_string;
393
Marshall Clow54e41492019-06-10 23:20:01 +0000394template <>
395struct initial_string<string>
Howard Hinnantae2378a2013-05-16 17:13:40 +0000396{
397 string
398 operator()() const
399 {
400 string s;
401 s.resize(s.capacity());
402 return s;
403 }
404};
405
Marshall Clow54e41492019-06-10 23:20:01 +0000406template <>
407struct initial_string<wstring>
Howard Hinnantae2378a2013-05-16 17:13:40 +0000408{
409 wstring
410 operator()() const
411 {
412 wstring s(20, wchar_t());
413 s.resize(s.capacity());
414 return s;
415 }
416};
417
418typedef int (*wide_printf)(wchar_t* __restrict, size_t, const wchar_t*__restrict, ...);
419
420inline
421wide_printf
422get_swprintf()
423{
Howard Hinnant13d8bc12013-08-01 18:17:34 +0000424#ifndef _LIBCPP_MSVCRT
Howard Hinnantae2378a2013-05-16 17:13:40 +0000425 return swprintf;
426#else
Eric Fiselier4a984f02017-05-10 20:57:45 +0000427 return static_cast<int (__cdecl*)(wchar_t* __restrict, size_t, const wchar_t*__restrict, ...)>(_snwprintf);
Howard Hinnantae2378a2013-05-16 17:13:40 +0000428#endif
429}
430
Marshall Clow54e41492019-06-10 23:20:01 +0000431template <typename S, typename V>
Louis Dionned9f528e2021-06-29 13:52:26 -0400432S i_to_string(V v)
Marshall Clow54e41492019-06-10 23:20:01 +0000433{
434// numeric_limits::digits10 returns value less on 1 than desired for unsigned numbers.
435// For example, for 1-byte unsigned value digits10 is 2 (999 can not be represented),
436// so we need +1 here.
437 constexpr size_t bufsize = numeric_limits<V>::digits10 + 2; // +1 for minus, +1 for digits10
438 char buf[bufsize];
439 const auto res = to_chars(buf, buf + bufsize, v);
440 _LIBCPP_ASSERT(res.ec == errc(), "bufsize must be large enough to accomodate the value");
441 return S(buf, res.ptr);
442}
443
Howard Hinnantae2378a2013-05-16 17:13:40 +0000444} // unnamed namespace
445
Marshall Clow54e41492019-06-10 23:20:01 +0000446string to_string (int val) { return i_to_string< string>(val); }
447string to_string (long val) { return i_to_string< string>(val); }
448string to_string (long long val) { return i_to_string< string>(val); }
449string to_string (unsigned val) { return i_to_string< string>(val); }
450string to_string (unsigned long val) { return i_to_string< string>(val); }
451string to_string (unsigned long long val) { return i_to_string< string>(val); }
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000452
Marshall Clow54e41492019-06-10 23:20:01 +0000453wstring to_wstring(int val) { return i_to_string<wstring>(val); }
454wstring to_wstring(long val) { return i_to_string<wstring>(val); }
455wstring to_wstring(long long val) { return i_to_string<wstring>(val); }
456wstring to_wstring(unsigned val) { return i_to_string<wstring>(val); }
457wstring to_wstring(unsigned long val) { return i_to_string<wstring>(val); }
458wstring to_wstring(unsigned long long val) { return i_to_string<wstring>(val); }
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000459
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000460
Marshall Clow54e41492019-06-10 23:20:01 +0000461string to_string (float val) { return as_string(snprintf, initial_string< string>()(), "%f", val); }
462string to_string (double val) { return as_string(snprintf, initial_string< string>()(), "%f", val); }
463string to_string (long double val) { return as_string(snprintf, initial_string< string>()(), "%Lf", val); }
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000464
Marshall Clow54e41492019-06-10 23:20:01 +0000465wstring to_wstring(float val) { return as_string(get_swprintf(), initial_string<wstring>()(), L"%f", val); }
466wstring to_wstring(double val) { return as_string(get_swprintf(), initial_string<wstring>()(), L"%f", val); }
467wstring to_wstring(long double val) { return as_string(get_swprintf(), initial_string<wstring>()(), L"%Lf", val); }
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000468
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000469_LIBCPP_END_NAMESPACE_STD