xref: /freebsd/contrib/llvm-project/libcxx/src/string.cpp (revision fe6060f10f634930ff71b7c50291ddc610da2475)
10b57cec5SDimitry Andric //===------------------------- string.cpp ---------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "string"
100b57cec5SDimitry Andric #include "charconv"
110b57cec5SDimitry Andric #include "cstdlib"
120b57cec5SDimitry Andric #include "cwchar"
130b57cec5SDimitry Andric #include "cerrno"
140b57cec5SDimitry Andric #include "limits"
150b57cec5SDimitry Andric #include "stdexcept"
160b57cec5SDimitry Andric #include <stdio.h>
170b57cec5SDimitry Andric #include "__debug"
180b57cec5SDimitry Andric 
190b57cec5SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
200b57cec5SDimitry Andric 
210b57cec5SDimitry Andric template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS __basic_string_common<true>;
220b57cec5SDimitry Andric 
23*fe6060f1SDimitry Andric #define _LIBCPP_EXTERN_TEMPLATE_DEFINE(...) template __VA_ARGS__;
245ffd83dbSDimitry Andric #ifdef _LIBCPP_ABI_STRING_OPTIMIZED_EXTERNAL_INSTANTIATION
255ffd83dbSDimitry Andric _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE_DEFINE, char)
265ffd83dbSDimitry Andric _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE_DEFINE, wchar_t)
275ffd83dbSDimitry Andric #else
285ffd83dbSDimitry Andric _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE_DEFINE, char)
295ffd83dbSDimitry Andric _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE_DEFINE, wchar_t)
305ffd83dbSDimitry Andric #endif
31*fe6060f1SDimitry Andric #undef _LIBCPP_EXTERN_TEMPLATE_DEFINE
320b57cec5SDimitry Andric 
33*fe6060f1SDimitry Andric template string operator+<char, char_traits<char>, allocator<char> >(char const*, string const&);
340b57cec5SDimitry Andric 
350b57cec5SDimitry Andric namespace
360b57cec5SDimitry Andric {
370b57cec5SDimitry Andric 
380b57cec5SDimitry Andric template<typename T>
390b57cec5SDimitry Andric inline
400b57cec5SDimitry Andric void throw_helper( const string& msg )
410b57cec5SDimitry Andric {
420b57cec5SDimitry Andric #ifndef _LIBCPP_NO_EXCEPTIONS
430b57cec5SDimitry Andric     throw T( msg );
440b57cec5SDimitry Andric #else
450b57cec5SDimitry Andric     fprintf(stderr, "%s\n", msg.c_str());
460b57cec5SDimitry Andric     _VSTD::abort();
470b57cec5SDimitry Andric #endif
480b57cec5SDimitry Andric }
490b57cec5SDimitry Andric 
500b57cec5SDimitry Andric inline
510b57cec5SDimitry Andric void throw_from_string_out_of_range( const string& func )
520b57cec5SDimitry Andric {
530b57cec5SDimitry Andric     throw_helper<out_of_range>(func + ": out of range");
540b57cec5SDimitry Andric }
550b57cec5SDimitry Andric 
560b57cec5SDimitry Andric inline
570b57cec5SDimitry Andric void throw_from_string_invalid_arg( const string& func )
580b57cec5SDimitry Andric {
590b57cec5SDimitry Andric     throw_helper<invalid_argument>(func + ": no conversion");
600b57cec5SDimitry Andric }
610b57cec5SDimitry Andric 
620b57cec5SDimitry Andric // as_integer
630b57cec5SDimitry Andric 
640b57cec5SDimitry Andric template<typename V, typename S, typename F>
650b57cec5SDimitry Andric inline
660b57cec5SDimitry Andric V
670b57cec5SDimitry Andric as_integer_helper(const string& func, const S& str, size_t* idx, int base, F f)
680b57cec5SDimitry Andric {
690b57cec5SDimitry Andric     typename S::value_type* ptr = nullptr;
700b57cec5SDimitry Andric     const typename S::value_type* const p = str.c_str();
710b57cec5SDimitry Andric     typename remove_reference<decltype(errno)>::type errno_save = errno;
720b57cec5SDimitry Andric     errno = 0;
730b57cec5SDimitry Andric     V r = f(p, &ptr, base);
740b57cec5SDimitry Andric     swap(errno, errno_save);
750b57cec5SDimitry Andric     if (errno_save == ERANGE)
760b57cec5SDimitry Andric         throw_from_string_out_of_range(func);
770b57cec5SDimitry Andric     if (ptr == p)
780b57cec5SDimitry Andric         throw_from_string_invalid_arg(func);
790b57cec5SDimitry Andric     if (idx)
800b57cec5SDimitry Andric         *idx = static_cast<size_t>(ptr - p);
810b57cec5SDimitry Andric     return r;
820b57cec5SDimitry Andric }
830b57cec5SDimitry Andric 
840b57cec5SDimitry Andric template<typename V, typename S>
850b57cec5SDimitry Andric inline
860b57cec5SDimitry Andric V
870b57cec5SDimitry Andric as_integer(const string& func, const S& s, size_t* idx, int base);
880b57cec5SDimitry Andric 
890b57cec5SDimitry Andric // string
900b57cec5SDimitry Andric template<>
910b57cec5SDimitry Andric inline
920b57cec5SDimitry Andric int
930b57cec5SDimitry Andric as_integer(const string& func, const string& s, size_t* idx, int base )
940b57cec5SDimitry Andric {
950b57cec5SDimitry Andric     // Use long as no Standard string to integer exists.
960b57cec5SDimitry Andric     long r = as_integer_helper<long>( func, s, idx, base, strtol );
970b57cec5SDimitry Andric     if (r < numeric_limits<int>::min() || numeric_limits<int>::max() < r)
980b57cec5SDimitry Andric         throw_from_string_out_of_range(func);
990b57cec5SDimitry Andric     return static_cast<int>(r);
1000b57cec5SDimitry Andric }
1010b57cec5SDimitry Andric 
1020b57cec5SDimitry Andric template<>
1030b57cec5SDimitry Andric inline
1040b57cec5SDimitry Andric long
1050b57cec5SDimitry Andric as_integer(const string& func, const string& s, size_t* idx, int base )
1060b57cec5SDimitry Andric {
1070b57cec5SDimitry Andric     return as_integer_helper<long>( func, s, idx, base, strtol );
1080b57cec5SDimitry Andric }
1090b57cec5SDimitry Andric 
1100b57cec5SDimitry Andric template<>
1110b57cec5SDimitry Andric inline
1120b57cec5SDimitry Andric unsigned long
1130b57cec5SDimitry Andric as_integer( const string& func, const string& s, size_t* idx, int base )
1140b57cec5SDimitry Andric {
1150b57cec5SDimitry Andric     return as_integer_helper<unsigned long>( func, s, idx, base, strtoul );
1160b57cec5SDimitry Andric }
1170b57cec5SDimitry Andric 
1180b57cec5SDimitry Andric template<>
1190b57cec5SDimitry Andric inline
1200b57cec5SDimitry Andric long long
1210b57cec5SDimitry Andric as_integer( const string& func, const string& s, size_t* idx, int base )
1220b57cec5SDimitry Andric {
1230b57cec5SDimitry Andric     return as_integer_helper<long long>( func, s, idx, base, strtoll );
1240b57cec5SDimitry Andric }
1250b57cec5SDimitry Andric 
1260b57cec5SDimitry Andric template<>
1270b57cec5SDimitry Andric inline
1280b57cec5SDimitry Andric unsigned long long
1290b57cec5SDimitry Andric as_integer( const string& func, const string& s, size_t* idx, int base )
1300b57cec5SDimitry Andric {
1310b57cec5SDimitry Andric     return as_integer_helper<unsigned long long>( func, s, idx, base, strtoull );
1320b57cec5SDimitry Andric }
1330b57cec5SDimitry Andric 
1340b57cec5SDimitry Andric // wstring
1350b57cec5SDimitry Andric template<>
1360b57cec5SDimitry Andric inline
1370b57cec5SDimitry Andric int
1380b57cec5SDimitry Andric as_integer( const string& func, const wstring& s, size_t* idx, int base )
1390b57cec5SDimitry Andric {
1400b57cec5SDimitry Andric     // Use long as no Stantard string to integer exists.
1410b57cec5SDimitry Andric     long r = as_integer_helper<long>( func, s, idx, base, wcstol );
1420b57cec5SDimitry Andric     if (r < numeric_limits<int>::min() || numeric_limits<int>::max() < r)
1430b57cec5SDimitry Andric         throw_from_string_out_of_range(func);
1440b57cec5SDimitry Andric     return static_cast<int>(r);
1450b57cec5SDimitry Andric }
1460b57cec5SDimitry Andric 
1470b57cec5SDimitry Andric template<>
1480b57cec5SDimitry Andric inline
1490b57cec5SDimitry Andric long
1500b57cec5SDimitry Andric as_integer( const string& func, const wstring& s, size_t* idx, int base )
1510b57cec5SDimitry Andric {
1520b57cec5SDimitry Andric     return as_integer_helper<long>( func, s, idx, base, wcstol );
1530b57cec5SDimitry Andric }
1540b57cec5SDimitry Andric 
1550b57cec5SDimitry Andric template<>
1560b57cec5SDimitry Andric inline
1570b57cec5SDimitry Andric unsigned long
1580b57cec5SDimitry Andric as_integer( const string& func, const wstring& s, size_t* idx, int base )
1590b57cec5SDimitry Andric {
1600b57cec5SDimitry Andric     return as_integer_helper<unsigned long>( func, s, idx, base, wcstoul );
1610b57cec5SDimitry Andric }
1620b57cec5SDimitry Andric 
1630b57cec5SDimitry Andric template<>
1640b57cec5SDimitry Andric inline
1650b57cec5SDimitry Andric long long
1660b57cec5SDimitry Andric as_integer( const string& func, const wstring& s, size_t* idx, int base )
1670b57cec5SDimitry Andric {
1680b57cec5SDimitry Andric     return as_integer_helper<long long>( func, s, idx, base, wcstoll );
1690b57cec5SDimitry Andric }
1700b57cec5SDimitry Andric 
1710b57cec5SDimitry Andric template<>
1720b57cec5SDimitry Andric inline
1730b57cec5SDimitry Andric unsigned long long
1740b57cec5SDimitry Andric as_integer( const string& func, const wstring& s, size_t* idx, int base )
1750b57cec5SDimitry Andric {
1760b57cec5SDimitry Andric     return as_integer_helper<unsigned long long>( func, s, idx, base, wcstoull );
1770b57cec5SDimitry Andric }
1780b57cec5SDimitry Andric 
1790b57cec5SDimitry Andric // as_float
1800b57cec5SDimitry Andric 
1810b57cec5SDimitry Andric template<typename V, typename S, typename F>
1820b57cec5SDimitry Andric inline
1830b57cec5SDimitry Andric V
1840b57cec5SDimitry Andric as_float_helper(const string& func, const S& str, size_t* idx, F f )
1850b57cec5SDimitry Andric {
1860b57cec5SDimitry Andric     typename S::value_type* ptr = nullptr;
1870b57cec5SDimitry Andric     const typename S::value_type* const p = str.c_str();
1880b57cec5SDimitry Andric     typename remove_reference<decltype(errno)>::type errno_save = errno;
1890b57cec5SDimitry Andric     errno = 0;
1900b57cec5SDimitry Andric     V r = f(p, &ptr);
1910b57cec5SDimitry Andric     swap(errno, errno_save);
1920b57cec5SDimitry Andric     if (errno_save == ERANGE)
1930b57cec5SDimitry Andric         throw_from_string_out_of_range(func);
1940b57cec5SDimitry Andric     if (ptr == p)
1950b57cec5SDimitry Andric         throw_from_string_invalid_arg(func);
1960b57cec5SDimitry Andric     if (idx)
1970b57cec5SDimitry Andric         *idx = static_cast<size_t>(ptr - p);
1980b57cec5SDimitry Andric     return r;
1990b57cec5SDimitry Andric }
2000b57cec5SDimitry Andric 
2010b57cec5SDimitry Andric template<typename V, typename S>
2020b57cec5SDimitry Andric inline
2030b57cec5SDimitry Andric V as_float( const string& func, const S& s, size_t* idx = nullptr );
2040b57cec5SDimitry Andric 
2050b57cec5SDimitry Andric template<>
2060b57cec5SDimitry Andric inline
2070b57cec5SDimitry Andric float
2080b57cec5SDimitry Andric as_float( const string& func, const string& s, size_t* idx )
2090b57cec5SDimitry Andric {
2100b57cec5SDimitry Andric     return as_float_helper<float>( func, s, idx, strtof );
2110b57cec5SDimitry Andric }
2120b57cec5SDimitry Andric 
2130b57cec5SDimitry Andric template<>
2140b57cec5SDimitry Andric inline
2150b57cec5SDimitry Andric double
2160b57cec5SDimitry Andric as_float(const string& func, const string& s, size_t* idx )
2170b57cec5SDimitry Andric {
2180b57cec5SDimitry Andric     return as_float_helper<double>( func, s, idx, strtod );
2190b57cec5SDimitry Andric }
2200b57cec5SDimitry Andric 
2210b57cec5SDimitry Andric template<>
2220b57cec5SDimitry Andric inline
2230b57cec5SDimitry Andric long double
2240b57cec5SDimitry Andric as_float( const string& func, const string& s, size_t* idx )
2250b57cec5SDimitry Andric {
2260b57cec5SDimitry Andric     return as_float_helper<long double>( func, s, idx, strtold );
2270b57cec5SDimitry Andric }
2280b57cec5SDimitry Andric 
2290b57cec5SDimitry Andric template<>
2300b57cec5SDimitry Andric inline
2310b57cec5SDimitry Andric float
2320b57cec5SDimitry Andric as_float( const string& func, const wstring& s, size_t* idx )
2330b57cec5SDimitry Andric {
2340b57cec5SDimitry Andric     return as_float_helper<float>( func, s, idx, wcstof );
2350b57cec5SDimitry Andric }
2360b57cec5SDimitry Andric 
2370b57cec5SDimitry Andric template<>
2380b57cec5SDimitry Andric inline
2390b57cec5SDimitry Andric double
2400b57cec5SDimitry Andric as_float( const string& func, const wstring& s, size_t* idx )
2410b57cec5SDimitry Andric {
2420b57cec5SDimitry Andric     return as_float_helper<double>( func, s, idx, wcstod );
2430b57cec5SDimitry Andric }
2440b57cec5SDimitry Andric 
2450b57cec5SDimitry Andric template<>
2460b57cec5SDimitry Andric inline
2470b57cec5SDimitry Andric long double
2480b57cec5SDimitry Andric as_float( const string& func, const wstring& s, size_t* idx )
2490b57cec5SDimitry Andric {
2500b57cec5SDimitry Andric     return as_float_helper<long double>( func, s, idx, wcstold );
2510b57cec5SDimitry Andric }
2520b57cec5SDimitry Andric 
2530b57cec5SDimitry Andric }  // unnamed namespace
2540b57cec5SDimitry Andric 
2550b57cec5SDimitry Andric int
2560b57cec5SDimitry Andric stoi(const string& str, size_t* idx, int base)
2570b57cec5SDimitry Andric {
2580b57cec5SDimitry Andric     return as_integer<int>( "stoi", str, idx, base );
2590b57cec5SDimitry Andric }
2600b57cec5SDimitry Andric 
2610b57cec5SDimitry Andric int
2620b57cec5SDimitry Andric stoi(const wstring& str, size_t* idx, int base)
2630b57cec5SDimitry Andric {
2640b57cec5SDimitry Andric     return as_integer<int>( "stoi", str, idx, base );
2650b57cec5SDimitry Andric }
2660b57cec5SDimitry Andric 
2670b57cec5SDimitry Andric long
2680b57cec5SDimitry Andric stol(const string& str, size_t* idx, int base)
2690b57cec5SDimitry Andric {
2700b57cec5SDimitry Andric     return as_integer<long>( "stol", str, idx, base );
2710b57cec5SDimitry Andric }
2720b57cec5SDimitry Andric 
2730b57cec5SDimitry Andric long
2740b57cec5SDimitry Andric stol(const wstring& str, size_t* idx, int base)
2750b57cec5SDimitry Andric {
2760b57cec5SDimitry Andric     return as_integer<long>( "stol", str, idx, base );
2770b57cec5SDimitry Andric }
2780b57cec5SDimitry Andric 
2790b57cec5SDimitry Andric unsigned long
2800b57cec5SDimitry Andric stoul(const string& str, size_t* idx, int base)
2810b57cec5SDimitry Andric {
2820b57cec5SDimitry Andric     return as_integer<unsigned long>( "stoul", str, idx, base );
2830b57cec5SDimitry Andric }
2840b57cec5SDimitry Andric 
2850b57cec5SDimitry Andric unsigned long
2860b57cec5SDimitry Andric stoul(const wstring& str, size_t* idx, int base)
2870b57cec5SDimitry Andric {
2880b57cec5SDimitry Andric     return as_integer<unsigned long>( "stoul", str, idx, base );
2890b57cec5SDimitry Andric }
2900b57cec5SDimitry Andric 
2910b57cec5SDimitry Andric long long
2920b57cec5SDimitry Andric stoll(const string& str, size_t* idx, int base)
2930b57cec5SDimitry Andric {
2940b57cec5SDimitry Andric     return as_integer<long long>( "stoll", str, idx, base );
2950b57cec5SDimitry Andric }
2960b57cec5SDimitry Andric 
2970b57cec5SDimitry Andric long long
2980b57cec5SDimitry Andric stoll(const wstring& str, size_t* idx, int base)
2990b57cec5SDimitry Andric {
3000b57cec5SDimitry Andric     return as_integer<long long>( "stoll", str, idx, base );
3010b57cec5SDimitry Andric }
3020b57cec5SDimitry Andric 
3030b57cec5SDimitry Andric unsigned long long
3040b57cec5SDimitry Andric stoull(const string& str, size_t* idx, int base)
3050b57cec5SDimitry Andric {
3060b57cec5SDimitry Andric     return as_integer<unsigned long long>( "stoull", str, idx, base );
3070b57cec5SDimitry Andric }
3080b57cec5SDimitry Andric 
3090b57cec5SDimitry Andric unsigned long long
3100b57cec5SDimitry Andric stoull(const wstring& str, size_t* idx, int base)
3110b57cec5SDimitry Andric {
3120b57cec5SDimitry Andric     return as_integer<unsigned long long>( "stoull", str, idx, base );
3130b57cec5SDimitry Andric }
3140b57cec5SDimitry Andric 
3150b57cec5SDimitry Andric float
3160b57cec5SDimitry Andric stof(const string& str, size_t* idx)
3170b57cec5SDimitry Andric {
3180b57cec5SDimitry Andric     return as_float<float>( "stof", str, idx );
3190b57cec5SDimitry Andric }
3200b57cec5SDimitry Andric 
3210b57cec5SDimitry Andric float
3220b57cec5SDimitry Andric stof(const wstring& str, size_t* idx)
3230b57cec5SDimitry Andric {
3240b57cec5SDimitry Andric     return as_float<float>( "stof", str, idx );
3250b57cec5SDimitry Andric }
3260b57cec5SDimitry Andric 
3270b57cec5SDimitry Andric double
3280b57cec5SDimitry Andric stod(const string& str, size_t* idx)
3290b57cec5SDimitry Andric {
3300b57cec5SDimitry Andric     return as_float<double>( "stod", str, idx );
3310b57cec5SDimitry Andric }
3320b57cec5SDimitry Andric 
3330b57cec5SDimitry Andric double
3340b57cec5SDimitry Andric stod(const wstring& str, size_t* idx)
3350b57cec5SDimitry Andric {
3360b57cec5SDimitry Andric     return as_float<double>( "stod", str, idx );
3370b57cec5SDimitry Andric }
3380b57cec5SDimitry Andric 
3390b57cec5SDimitry Andric long double
3400b57cec5SDimitry Andric stold(const string& str, size_t* idx)
3410b57cec5SDimitry Andric {
3420b57cec5SDimitry Andric     return as_float<long double>( "stold", str, idx );
3430b57cec5SDimitry Andric }
3440b57cec5SDimitry Andric 
3450b57cec5SDimitry Andric long double
3460b57cec5SDimitry Andric stold(const wstring& str, size_t* idx)
3470b57cec5SDimitry Andric {
3480b57cec5SDimitry Andric     return as_float<long double>( "stold", str, idx );
3490b57cec5SDimitry Andric }
3500b57cec5SDimitry Andric 
3510b57cec5SDimitry Andric // to_string
3520b57cec5SDimitry Andric 
3530b57cec5SDimitry Andric namespace
3540b57cec5SDimitry Andric {
3550b57cec5SDimitry Andric 
3560b57cec5SDimitry Andric // as_string
3570b57cec5SDimitry Andric 
3580b57cec5SDimitry Andric template<typename S, typename P, typename V >
3590b57cec5SDimitry Andric inline
3600b57cec5SDimitry Andric S
3610b57cec5SDimitry Andric as_string(P sprintf_like, S s, const typename S::value_type* fmt, V a)
3620b57cec5SDimitry Andric {
3630b57cec5SDimitry Andric     typedef typename S::size_type size_type;
3640b57cec5SDimitry Andric     size_type available = s.size();
3650b57cec5SDimitry Andric     while (true)
3660b57cec5SDimitry Andric     {
3670b57cec5SDimitry Andric         int status = sprintf_like(&s[0], available + 1, fmt, a);
3680b57cec5SDimitry Andric         if ( status >= 0 )
3690b57cec5SDimitry Andric         {
3700b57cec5SDimitry Andric             size_type used = static_cast<size_type>(status);
3710b57cec5SDimitry Andric             if ( used <= available )
3720b57cec5SDimitry Andric             {
3730b57cec5SDimitry Andric                 s.resize( used );
3740b57cec5SDimitry Andric                 break;
3750b57cec5SDimitry Andric             }
3760b57cec5SDimitry Andric             available = used; // Assume this is advice of how much space we need.
3770b57cec5SDimitry Andric         }
3780b57cec5SDimitry Andric         else
3790b57cec5SDimitry Andric             available = available * 2 + 1;
3800b57cec5SDimitry Andric         s.resize(available);
3810b57cec5SDimitry Andric     }
3820b57cec5SDimitry Andric     return s;
3830b57cec5SDimitry Andric }
3840b57cec5SDimitry Andric 
3850b57cec5SDimitry Andric template <class S>
3860b57cec5SDimitry Andric struct initial_string;
3870b57cec5SDimitry Andric 
3880b57cec5SDimitry Andric template <>
3890b57cec5SDimitry Andric struct initial_string<string>
3900b57cec5SDimitry Andric {
3910b57cec5SDimitry Andric     string
3920b57cec5SDimitry Andric     operator()() const
3930b57cec5SDimitry Andric     {
3940b57cec5SDimitry Andric         string s;
3950b57cec5SDimitry Andric         s.resize(s.capacity());
3960b57cec5SDimitry Andric         return s;
3970b57cec5SDimitry Andric     }
3980b57cec5SDimitry Andric };
3990b57cec5SDimitry Andric 
4000b57cec5SDimitry Andric template <>
4010b57cec5SDimitry Andric struct initial_string<wstring>
4020b57cec5SDimitry Andric {
4030b57cec5SDimitry Andric     wstring
4040b57cec5SDimitry Andric     operator()() const
4050b57cec5SDimitry Andric     {
4060b57cec5SDimitry Andric         wstring s(20, wchar_t());
4070b57cec5SDimitry Andric         s.resize(s.capacity());
4080b57cec5SDimitry Andric         return s;
4090b57cec5SDimitry Andric     }
4100b57cec5SDimitry Andric };
4110b57cec5SDimitry Andric 
4120b57cec5SDimitry Andric typedef int (*wide_printf)(wchar_t* __restrict, size_t, const wchar_t*__restrict, ...);
4130b57cec5SDimitry Andric 
4140b57cec5SDimitry Andric inline
4150b57cec5SDimitry Andric wide_printf
4160b57cec5SDimitry Andric get_swprintf()
4170b57cec5SDimitry Andric {
4180b57cec5SDimitry Andric #ifndef _LIBCPP_MSVCRT
4190b57cec5SDimitry Andric     return swprintf;
4200b57cec5SDimitry Andric #else
4210b57cec5SDimitry Andric     return static_cast<int (__cdecl*)(wchar_t* __restrict, size_t, const wchar_t*__restrict, ...)>(_snwprintf);
4220b57cec5SDimitry Andric #endif
4230b57cec5SDimitry Andric }
4240b57cec5SDimitry Andric 
4250b57cec5SDimitry Andric template <typename S, typename V>
426*fe6060f1SDimitry Andric S i_to_string(V v)
4270b57cec5SDimitry Andric {
4280b57cec5SDimitry Andric //  numeric_limits::digits10 returns value less on 1 than desired for unsigned numbers.
4290b57cec5SDimitry Andric //  For example, for 1-byte unsigned value digits10 is 2 (999 can not be represented),
4300b57cec5SDimitry Andric //  so we need +1 here.
4310b57cec5SDimitry Andric     constexpr size_t bufsize = numeric_limits<V>::digits10 + 2;  // +1 for minus, +1 for digits10
4320b57cec5SDimitry Andric     char buf[bufsize];
4330b57cec5SDimitry Andric     const auto res = to_chars(buf, buf + bufsize, v);
4340b57cec5SDimitry Andric     _LIBCPP_ASSERT(res.ec == errc(), "bufsize must be large enough to accomodate the value");
4350b57cec5SDimitry Andric     return S(buf, res.ptr);
4360b57cec5SDimitry Andric }
4370b57cec5SDimitry Andric 
4380b57cec5SDimitry Andric }  // unnamed namespace
4390b57cec5SDimitry Andric 
4400b57cec5SDimitry Andric string  to_string (int val)                { return i_to_string< string>(val); }
4410b57cec5SDimitry Andric string  to_string (long val)               { return i_to_string< string>(val); }
4420b57cec5SDimitry Andric string  to_string (long long val)          { return i_to_string< string>(val); }
4430b57cec5SDimitry Andric string  to_string (unsigned val)           { return i_to_string< string>(val); }
4440b57cec5SDimitry Andric string  to_string (unsigned long val)      { return i_to_string< string>(val); }
4450b57cec5SDimitry Andric string  to_string (unsigned long long val) { return i_to_string< string>(val); }
4460b57cec5SDimitry Andric 
4470b57cec5SDimitry Andric wstring to_wstring(int val)                { return i_to_string<wstring>(val); }
4480b57cec5SDimitry Andric wstring to_wstring(long val)               { return i_to_string<wstring>(val); }
4490b57cec5SDimitry Andric wstring to_wstring(long long val)          { return i_to_string<wstring>(val); }
4500b57cec5SDimitry Andric wstring to_wstring(unsigned val)           { return i_to_string<wstring>(val); }
4510b57cec5SDimitry Andric wstring to_wstring(unsigned long val)      { return i_to_string<wstring>(val); }
4520b57cec5SDimitry Andric wstring to_wstring(unsigned long long val) { return i_to_string<wstring>(val); }
4530b57cec5SDimitry Andric 
4540b57cec5SDimitry Andric 
4550b57cec5SDimitry Andric string  to_string (float val)       { return as_string(snprintf,       initial_string< string>()(),   "%f", val); }
4560b57cec5SDimitry Andric string  to_string (double val)      { return as_string(snprintf,       initial_string< string>()(),   "%f", val); }
4570b57cec5SDimitry Andric string  to_string (long double val) { return as_string(snprintf,       initial_string< string>()(),  "%Lf", val); }
4580b57cec5SDimitry Andric 
4590b57cec5SDimitry Andric wstring to_wstring(float val)       { return as_string(get_swprintf(), initial_string<wstring>()(),  L"%f", val); }
4600b57cec5SDimitry Andric wstring to_wstring(double val)      { return as_string(get_swprintf(), initial_string<wstring>()(),  L"%f", val); }
4610b57cec5SDimitry Andric wstring to_wstring(long double val) { return as_string(get_swprintf(), initial_string<wstring>()(), L"%Lf", val); }
4620b57cec5SDimitry Andric 
4630b57cec5SDimitry Andric _LIBCPP_END_NAMESPACE_STD
464