1*0b57cec5SDimitry Andric //===------------------------- string.cpp ---------------------------------===// 2*0b57cec5SDimitry Andric // 3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*0b57cec5SDimitry Andric // 7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 8*0b57cec5SDimitry Andric 9*0b57cec5SDimitry Andric #include "string" 10*0b57cec5SDimitry Andric #include "charconv" 11*0b57cec5SDimitry Andric #include "cstdlib" 12*0b57cec5SDimitry Andric #include "cwchar" 13*0b57cec5SDimitry Andric #include "cerrno" 14*0b57cec5SDimitry Andric #include "limits" 15*0b57cec5SDimitry Andric #include "stdexcept" 16*0b57cec5SDimitry Andric #include <stdio.h> 17*0b57cec5SDimitry Andric #include "__debug" 18*0b57cec5SDimitry Andric 19*0b57cec5SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD 20*0b57cec5SDimitry Andric 21*0b57cec5SDimitry Andric template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS __basic_string_common<true>; 22*0b57cec5SDimitry Andric 235ffd83dbSDimitry Andric #ifdef _LIBCPP_ABI_STRING_OPTIMIZED_EXTERNAL_INSTANTIATION 245ffd83dbSDimitry Andric _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE_DEFINE, char) 255ffd83dbSDimitry Andric _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE_DEFINE, wchar_t) 265ffd83dbSDimitry Andric #else 275ffd83dbSDimitry Andric _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE_DEFINE, char) 285ffd83dbSDimitry Andric _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE_DEFINE, wchar_t) 295ffd83dbSDimitry Andric #endif 30*0b57cec5SDimitry Andric 31*0b57cec5SDimitry Andric template 32*0b57cec5SDimitry Andric string 33*0b57cec5SDimitry Andric operator+<char, char_traits<char>, allocator<char> >(char const*, string const&); 34*0b57cec5SDimitry Andric 35*0b57cec5SDimitry Andric namespace 36*0b57cec5SDimitry Andric { 37*0b57cec5SDimitry Andric 38*0b57cec5SDimitry Andric template<typename T> 39*0b57cec5SDimitry Andric inline 40*0b57cec5SDimitry Andric void throw_helper( const string& msg ) 41*0b57cec5SDimitry Andric { 42*0b57cec5SDimitry Andric #ifndef _LIBCPP_NO_EXCEPTIONS 43*0b57cec5SDimitry Andric throw T( msg ); 44*0b57cec5SDimitry Andric #else 45*0b57cec5SDimitry Andric fprintf(stderr, "%s\n", msg.c_str()); 46*0b57cec5SDimitry Andric _VSTD::abort(); 47*0b57cec5SDimitry Andric #endif 48*0b57cec5SDimitry Andric } 49*0b57cec5SDimitry Andric 50*0b57cec5SDimitry Andric inline 51*0b57cec5SDimitry Andric void throw_from_string_out_of_range( const string& func ) 52*0b57cec5SDimitry Andric { 53*0b57cec5SDimitry Andric throw_helper<out_of_range>(func + ": out of range"); 54*0b57cec5SDimitry Andric } 55*0b57cec5SDimitry Andric 56*0b57cec5SDimitry Andric inline 57*0b57cec5SDimitry Andric void throw_from_string_invalid_arg( const string& func ) 58*0b57cec5SDimitry Andric { 59*0b57cec5SDimitry Andric throw_helper<invalid_argument>(func + ": no conversion"); 60*0b57cec5SDimitry Andric } 61*0b57cec5SDimitry Andric 62*0b57cec5SDimitry Andric // as_integer 63*0b57cec5SDimitry Andric 64*0b57cec5SDimitry Andric template<typename V, typename S, typename F> 65*0b57cec5SDimitry Andric inline 66*0b57cec5SDimitry Andric V 67*0b57cec5SDimitry Andric as_integer_helper(const string& func, const S& str, size_t* idx, int base, F f) 68*0b57cec5SDimitry Andric { 69*0b57cec5SDimitry Andric typename S::value_type* ptr = nullptr; 70*0b57cec5SDimitry Andric const typename S::value_type* const p = str.c_str(); 71*0b57cec5SDimitry Andric typename remove_reference<decltype(errno)>::type errno_save = errno; 72*0b57cec5SDimitry Andric errno = 0; 73*0b57cec5SDimitry Andric V r = f(p, &ptr, base); 74*0b57cec5SDimitry Andric swap(errno, errno_save); 75*0b57cec5SDimitry Andric if (errno_save == ERANGE) 76*0b57cec5SDimitry Andric throw_from_string_out_of_range(func); 77*0b57cec5SDimitry Andric if (ptr == p) 78*0b57cec5SDimitry Andric throw_from_string_invalid_arg(func); 79*0b57cec5SDimitry Andric if (idx) 80*0b57cec5SDimitry Andric *idx = static_cast<size_t>(ptr - p); 81*0b57cec5SDimitry Andric return r; 82*0b57cec5SDimitry Andric } 83*0b57cec5SDimitry Andric 84*0b57cec5SDimitry Andric template<typename V, typename S> 85*0b57cec5SDimitry Andric inline 86*0b57cec5SDimitry Andric V 87*0b57cec5SDimitry Andric as_integer(const string& func, const S& s, size_t* idx, int base); 88*0b57cec5SDimitry Andric 89*0b57cec5SDimitry Andric // string 90*0b57cec5SDimitry Andric template<> 91*0b57cec5SDimitry Andric inline 92*0b57cec5SDimitry Andric int 93*0b57cec5SDimitry Andric as_integer(const string& func, const string& s, size_t* idx, int base ) 94*0b57cec5SDimitry Andric { 95*0b57cec5SDimitry Andric // Use long as no Standard string to integer exists. 96*0b57cec5SDimitry Andric long r = as_integer_helper<long>( func, s, idx, base, strtol ); 97*0b57cec5SDimitry Andric if (r < numeric_limits<int>::min() || numeric_limits<int>::max() < r) 98*0b57cec5SDimitry Andric throw_from_string_out_of_range(func); 99*0b57cec5SDimitry Andric return static_cast<int>(r); 100*0b57cec5SDimitry Andric } 101*0b57cec5SDimitry Andric 102*0b57cec5SDimitry Andric template<> 103*0b57cec5SDimitry Andric inline 104*0b57cec5SDimitry Andric long 105*0b57cec5SDimitry Andric as_integer(const string& func, const string& s, size_t* idx, int base ) 106*0b57cec5SDimitry Andric { 107*0b57cec5SDimitry Andric return as_integer_helper<long>( func, s, idx, base, strtol ); 108*0b57cec5SDimitry Andric } 109*0b57cec5SDimitry Andric 110*0b57cec5SDimitry Andric template<> 111*0b57cec5SDimitry Andric inline 112*0b57cec5SDimitry Andric unsigned long 113*0b57cec5SDimitry Andric as_integer( const string& func, const string& s, size_t* idx, int base ) 114*0b57cec5SDimitry Andric { 115*0b57cec5SDimitry Andric return as_integer_helper<unsigned long>( func, s, idx, base, strtoul ); 116*0b57cec5SDimitry Andric } 117*0b57cec5SDimitry Andric 118*0b57cec5SDimitry Andric template<> 119*0b57cec5SDimitry Andric inline 120*0b57cec5SDimitry Andric long long 121*0b57cec5SDimitry Andric as_integer( const string& func, const string& s, size_t* idx, int base ) 122*0b57cec5SDimitry Andric { 123*0b57cec5SDimitry Andric return as_integer_helper<long long>( func, s, idx, base, strtoll ); 124*0b57cec5SDimitry Andric } 125*0b57cec5SDimitry Andric 126*0b57cec5SDimitry Andric template<> 127*0b57cec5SDimitry Andric inline 128*0b57cec5SDimitry Andric unsigned long long 129*0b57cec5SDimitry Andric as_integer( const string& func, const string& s, size_t* idx, int base ) 130*0b57cec5SDimitry Andric { 131*0b57cec5SDimitry Andric return as_integer_helper<unsigned long long>( func, s, idx, base, strtoull ); 132*0b57cec5SDimitry Andric } 133*0b57cec5SDimitry Andric 134*0b57cec5SDimitry Andric // wstring 135*0b57cec5SDimitry Andric template<> 136*0b57cec5SDimitry Andric inline 137*0b57cec5SDimitry Andric int 138*0b57cec5SDimitry Andric as_integer( const string& func, const wstring& s, size_t* idx, int base ) 139*0b57cec5SDimitry Andric { 140*0b57cec5SDimitry Andric // Use long as no Stantard string to integer exists. 141*0b57cec5SDimitry Andric long r = as_integer_helper<long>( func, s, idx, base, wcstol ); 142*0b57cec5SDimitry Andric if (r < numeric_limits<int>::min() || numeric_limits<int>::max() < r) 143*0b57cec5SDimitry Andric throw_from_string_out_of_range(func); 144*0b57cec5SDimitry Andric return static_cast<int>(r); 145*0b57cec5SDimitry Andric } 146*0b57cec5SDimitry Andric 147*0b57cec5SDimitry Andric template<> 148*0b57cec5SDimitry Andric inline 149*0b57cec5SDimitry Andric long 150*0b57cec5SDimitry Andric as_integer( const string& func, const wstring& s, size_t* idx, int base ) 151*0b57cec5SDimitry Andric { 152*0b57cec5SDimitry Andric return as_integer_helper<long>( func, s, idx, base, wcstol ); 153*0b57cec5SDimitry Andric } 154*0b57cec5SDimitry Andric 155*0b57cec5SDimitry Andric template<> 156*0b57cec5SDimitry Andric inline 157*0b57cec5SDimitry Andric unsigned long 158*0b57cec5SDimitry Andric as_integer( const string& func, const wstring& s, size_t* idx, int base ) 159*0b57cec5SDimitry Andric { 160*0b57cec5SDimitry Andric return as_integer_helper<unsigned long>( func, s, idx, base, wcstoul ); 161*0b57cec5SDimitry Andric } 162*0b57cec5SDimitry Andric 163*0b57cec5SDimitry Andric template<> 164*0b57cec5SDimitry Andric inline 165*0b57cec5SDimitry Andric long long 166*0b57cec5SDimitry Andric as_integer( const string& func, const wstring& s, size_t* idx, int base ) 167*0b57cec5SDimitry Andric { 168*0b57cec5SDimitry Andric return as_integer_helper<long long>( func, s, idx, base, wcstoll ); 169*0b57cec5SDimitry Andric } 170*0b57cec5SDimitry Andric 171*0b57cec5SDimitry Andric template<> 172*0b57cec5SDimitry Andric inline 173*0b57cec5SDimitry Andric unsigned long long 174*0b57cec5SDimitry Andric as_integer( const string& func, const wstring& s, size_t* idx, int base ) 175*0b57cec5SDimitry Andric { 176*0b57cec5SDimitry Andric return as_integer_helper<unsigned long long>( func, s, idx, base, wcstoull ); 177*0b57cec5SDimitry Andric } 178*0b57cec5SDimitry Andric 179*0b57cec5SDimitry Andric // as_float 180*0b57cec5SDimitry Andric 181*0b57cec5SDimitry Andric template<typename V, typename S, typename F> 182*0b57cec5SDimitry Andric inline 183*0b57cec5SDimitry Andric V 184*0b57cec5SDimitry Andric as_float_helper(const string& func, const S& str, size_t* idx, F f ) 185*0b57cec5SDimitry Andric { 186*0b57cec5SDimitry Andric typename S::value_type* ptr = nullptr; 187*0b57cec5SDimitry Andric const typename S::value_type* const p = str.c_str(); 188*0b57cec5SDimitry Andric typename remove_reference<decltype(errno)>::type errno_save = errno; 189*0b57cec5SDimitry Andric errno = 0; 190*0b57cec5SDimitry Andric V r = f(p, &ptr); 191*0b57cec5SDimitry Andric swap(errno, errno_save); 192*0b57cec5SDimitry Andric if (errno_save == ERANGE) 193*0b57cec5SDimitry Andric throw_from_string_out_of_range(func); 194*0b57cec5SDimitry Andric if (ptr == p) 195*0b57cec5SDimitry Andric throw_from_string_invalid_arg(func); 196*0b57cec5SDimitry Andric if (idx) 197*0b57cec5SDimitry Andric *idx = static_cast<size_t>(ptr - p); 198*0b57cec5SDimitry Andric return r; 199*0b57cec5SDimitry Andric } 200*0b57cec5SDimitry Andric 201*0b57cec5SDimitry Andric template<typename V, typename S> 202*0b57cec5SDimitry Andric inline 203*0b57cec5SDimitry Andric V as_float( const string& func, const S& s, size_t* idx = nullptr ); 204*0b57cec5SDimitry Andric 205*0b57cec5SDimitry Andric template<> 206*0b57cec5SDimitry Andric inline 207*0b57cec5SDimitry Andric float 208*0b57cec5SDimitry Andric as_float( const string& func, const string& s, size_t* idx ) 209*0b57cec5SDimitry Andric { 210*0b57cec5SDimitry Andric return as_float_helper<float>( func, s, idx, strtof ); 211*0b57cec5SDimitry Andric } 212*0b57cec5SDimitry Andric 213*0b57cec5SDimitry Andric template<> 214*0b57cec5SDimitry Andric inline 215*0b57cec5SDimitry Andric double 216*0b57cec5SDimitry Andric as_float(const string& func, const string& s, size_t* idx ) 217*0b57cec5SDimitry Andric { 218*0b57cec5SDimitry Andric return as_float_helper<double>( func, s, idx, strtod ); 219*0b57cec5SDimitry Andric } 220*0b57cec5SDimitry Andric 221*0b57cec5SDimitry Andric template<> 222*0b57cec5SDimitry Andric inline 223*0b57cec5SDimitry Andric long double 224*0b57cec5SDimitry Andric as_float( const string& func, const string& s, size_t* idx ) 225*0b57cec5SDimitry Andric { 226*0b57cec5SDimitry Andric return as_float_helper<long double>( func, s, idx, strtold ); 227*0b57cec5SDimitry Andric } 228*0b57cec5SDimitry Andric 229*0b57cec5SDimitry Andric template<> 230*0b57cec5SDimitry Andric inline 231*0b57cec5SDimitry Andric float 232*0b57cec5SDimitry Andric as_float( const string& func, const wstring& s, size_t* idx ) 233*0b57cec5SDimitry Andric { 234*0b57cec5SDimitry Andric return as_float_helper<float>( func, s, idx, wcstof ); 235*0b57cec5SDimitry Andric } 236*0b57cec5SDimitry Andric 237*0b57cec5SDimitry Andric template<> 238*0b57cec5SDimitry Andric inline 239*0b57cec5SDimitry Andric double 240*0b57cec5SDimitry Andric as_float( const string& func, const wstring& s, size_t* idx ) 241*0b57cec5SDimitry Andric { 242*0b57cec5SDimitry Andric return as_float_helper<double>( func, s, idx, wcstod ); 243*0b57cec5SDimitry Andric } 244*0b57cec5SDimitry Andric 245*0b57cec5SDimitry Andric template<> 246*0b57cec5SDimitry Andric inline 247*0b57cec5SDimitry Andric long double 248*0b57cec5SDimitry Andric as_float( const string& func, const wstring& s, size_t* idx ) 249*0b57cec5SDimitry Andric { 250*0b57cec5SDimitry Andric return as_float_helper<long double>( func, s, idx, wcstold ); 251*0b57cec5SDimitry Andric } 252*0b57cec5SDimitry Andric 253*0b57cec5SDimitry Andric } // unnamed namespace 254*0b57cec5SDimitry Andric 255*0b57cec5SDimitry Andric int 256*0b57cec5SDimitry Andric stoi(const string& str, size_t* idx, int base) 257*0b57cec5SDimitry Andric { 258*0b57cec5SDimitry Andric return as_integer<int>( "stoi", str, idx, base ); 259*0b57cec5SDimitry Andric } 260*0b57cec5SDimitry Andric 261*0b57cec5SDimitry Andric int 262*0b57cec5SDimitry Andric stoi(const wstring& str, size_t* idx, int base) 263*0b57cec5SDimitry Andric { 264*0b57cec5SDimitry Andric return as_integer<int>( "stoi", str, idx, base ); 265*0b57cec5SDimitry Andric } 266*0b57cec5SDimitry Andric 267*0b57cec5SDimitry Andric long 268*0b57cec5SDimitry Andric stol(const string& str, size_t* idx, int base) 269*0b57cec5SDimitry Andric { 270*0b57cec5SDimitry Andric return as_integer<long>( "stol", str, idx, base ); 271*0b57cec5SDimitry Andric } 272*0b57cec5SDimitry Andric 273*0b57cec5SDimitry Andric long 274*0b57cec5SDimitry Andric stol(const wstring& str, size_t* idx, int base) 275*0b57cec5SDimitry Andric { 276*0b57cec5SDimitry Andric return as_integer<long>( "stol", str, idx, base ); 277*0b57cec5SDimitry Andric } 278*0b57cec5SDimitry Andric 279*0b57cec5SDimitry Andric unsigned long 280*0b57cec5SDimitry Andric stoul(const string& str, size_t* idx, int base) 281*0b57cec5SDimitry Andric { 282*0b57cec5SDimitry Andric return as_integer<unsigned long>( "stoul", str, idx, base ); 283*0b57cec5SDimitry Andric } 284*0b57cec5SDimitry Andric 285*0b57cec5SDimitry Andric unsigned long 286*0b57cec5SDimitry Andric stoul(const wstring& str, size_t* idx, int base) 287*0b57cec5SDimitry Andric { 288*0b57cec5SDimitry Andric return as_integer<unsigned long>( "stoul", str, idx, base ); 289*0b57cec5SDimitry Andric } 290*0b57cec5SDimitry Andric 291*0b57cec5SDimitry Andric long long 292*0b57cec5SDimitry Andric stoll(const string& str, size_t* idx, int base) 293*0b57cec5SDimitry Andric { 294*0b57cec5SDimitry Andric return as_integer<long long>( "stoll", str, idx, base ); 295*0b57cec5SDimitry Andric } 296*0b57cec5SDimitry Andric 297*0b57cec5SDimitry Andric long long 298*0b57cec5SDimitry Andric stoll(const wstring& str, size_t* idx, int base) 299*0b57cec5SDimitry Andric { 300*0b57cec5SDimitry Andric return as_integer<long long>( "stoll", str, idx, base ); 301*0b57cec5SDimitry Andric } 302*0b57cec5SDimitry Andric 303*0b57cec5SDimitry Andric unsigned long long 304*0b57cec5SDimitry Andric stoull(const string& str, size_t* idx, int base) 305*0b57cec5SDimitry Andric { 306*0b57cec5SDimitry Andric return as_integer<unsigned long long>( "stoull", str, idx, base ); 307*0b57cec5SDimitry Andric } 308*0b57cec5SDimitry Andric 309*0b57cec5SDimitry Andric unsigned long long 310*0b57cec5SDimitry Andric stoull(const wstring& str, size_t* idx, int base) 311*0b57cec5SDimitry Andric { 312*0b57cec5SDimitry Andric return as_integer<unsigned long long>( "stoull", str, idx, base ); 313*0b57cec5SDimitry Andric } 314*0b57cec5SDimitry Andric 315*0b57cec5SDimitry Andric float 316*0b57cec5SDimitry Andric stof(const string& str, size_t* idx) 317*0b57cec5SDimitry Andric { 318*0b57cec5SDimitry Andric return as_float<float>( "stof", str, idx ); 319*0b57cec5SDimitry Andric } 320*0b57cec5SDimitry Andric 321*0b57cec5SDimitry Andric float 322*0b57cec5SDimitry Andric stof(const wstring& str, size_t* idx) 323*0b57cec5SDimitry Andric { 324*0b57cec5SDimitry Andric return as_float<float>( "stof", str, idx ); 325*0b57cec5SDimitry Andric } 326*0b57cec5SDimitry Andric 327*0b57cec5SDimitry Andric double 328*0b57cec5SDimitry Andric stod(const string& str, size_t* idx) 329*0b57cec5SDimitry Andric { 330*0b57cec5SDimitry Andric return as_float<double>( "stod", str, idx ); 331*0b57cec5SDimitry Andric } 332*0b57cec5SDimitry Andric 333*0b57cec5SDimitry Andric double 334*0b57cec5SDimitry Andric stod(const wstring& str, size_t* idx) 335*0b57cec5SDimitry Andric { 336*0b57cec5SDimitry Andric return as_float<double>( "stod", str, idx ); 337*0b57cec5SDimitry Andric } 338*0b57cec5SDimitry Andric 339*0b57cec5SDimitry Andric long double 340*0b57cec5SDimitry Andric stold(const string& str, size_t* idx) 341*0b57cec5SDimitry Andric { 342*0b57cec5SDimitry Andric return as_float<long double>( "stold", str, idx ); 343*0b57cec5SDimitry Andric } 344*0b57cec5SDimitry Andric 345*0b57cec5SDimitry Andric long double 346*0b57cec5SDimitry Andric stold(const wstring& str, size_t* idx) 347*0b57cec5SDimitry Andric { 348*0b57cec5SDimitry Andric return as_float<long double>( "stold", str, idx ); 349*0b57cec5SDimitry Andric } 350*0b57cec5SDimitry Andric 351*0b57cec5SDimitry Andric // to_string 352*0b57cec5SDimitry Andric 353*0b57cec5SDimitry Andric namespace 354*0b57cec5SDimitry Andric { 355*0b57cec5SDimitry Andric 356*0b57cec5SDimitry Andric // as_string 357*0b57cec5SDimitry Andric 358*0b57cec5SDimitry Andric template<typename S, typename P, typename V > 359*0b57cec5SDimitry Andric inline 360*0b57cec5SDimitry Andric S 361*0b57cec5SDimitry Andric as_string(P sprintf_like, S s, const typename S::value_type* fmt, V a) 362*0b57cec5SDimitry Andric { 363*0b57cec5SDimitry Andric typedef typename S::size_type size_type; 364*0b57cec5SDimitry Andric size_type available = s.size(); 365*0b57cec5SDimitry Andric while (true) 366*0b57cec5SDimitry Andric { 367*0b57cec5SDimitry Andric int status = sprintf_like(&s[0], available + 1, fmt, a); 368*0b57cec5SDimitry Andric if ( status >= 0 ) 369*0b57cec5SDimitry Andric { 370*0b57cec5SDimitry Andric size_type used = static_cast<size_type>(status); 371*0b57cec5SDimitry Andric if ( used <= available ) 372*0b57cec5SDimitry Andric { 373*0b57cec5SDimitry Andric s.resize( used ); 374*0b57cec5SDimitry Andric break; 375*0b57cec5SDimitry Andric } 376*0b57cec5SDimitry Andric available = used; // Assume this is advice of how much space we need. 377*0b57cec5SDimitry Andric } 378*0b57cec5SDimitry Andric else 379*0b57cec5SDimitry Andric available = available * 2 + 1; 380*0b57cec5SDimitry Andric s.resize(available); 381*0b57cec5SDimitry Andric } 382*0b57cec5SDimitry Andric return s; 383*0b57cec5SDimitry Andric } 384*0b57cec5SDimitry Andric 385*0b57cec5SDimitry Andric template <class S> 386*0b57cec5SDimitry Andric struct initial_string; 387*0b57cec5SDimitry Andric 388*0b57cec5SDimitry Andric template <> 389*0b57cec5SDimitry Andric struct initial_string<string> 390*0b57cec5SDimitry Andric { 391*0b57cec5SDimitry Andric string 392*0b57cec5SDimitry Andric operator()() const 393*0b57cec5SDimitry Andric { 394*0b57cec5SDimitry Andric string s; 395*0b57cec5SDimitry Andric s.resize(s.capacity()); 396*0b57cec5SDimitry Andric return s; 397*0b57cec5SDimitry Andric } 398*0b57cec5SDimitry Andric }; 399*0b57cec5SDimitry Andric 400*0b57cec5SDimitry Andric template <> 401*0b57cec5SDimitry Andric struct initial_string<wstring> 402*0b57cec5SDimitry Andric { 403*0b57cec5SDimitry Andric wstring 404*0b57cec5SDimitry Andric operator()() const 405*0b57cec5SDimitry Andric { 406*0b57cec5SDimitry Andric wstring s(20, wchar_t()); 407*0b57cec5SDimitry Andric s.resize(s.capacity()); 408*0b57cec5SDimitry Andric return s; 409*0b57cec5SDimitry Andric } 410*0b57cec5SDimitry Andric }; 411*0b57cec5SDimitry Andric 412*0b57cec5SDimitry Andric typedef int (*wide_printf)(wchar_t* __restrict, size_t, const wchar_t*__restrict, ...); 413*0b57cec5SDimitry Andric 414*0b57cec5SDimitry Andric inline 415*0b57cec5SDimitry Andric wide_printf 416*0b57cec5SDimitry Andric get_swprintf() 417*0b57cec5SDimitry Andric { 418*0b57cec5SDimitry Andric #ifndef _LIBCPP_MSVCRT 419*0b57cec5SDimitry Andric return swprintf; 420*0b57cec5SDimitry Andric #else 421*0b57cec5SDimitry Andric return static_cast<int (__cdecl*)(wchar_t* __restrict, size_t, const wchar_t*__restrict, ...)>(_snwprintf); 422*0b57cec5SDimitry Andric #endif 423*0b57cec5SDimitry Andric } 424*0b57cec5SDimitry Andric 425*0b57cec5SDimitry Andric template <typename S, typename V> 426*0b57cec5SDimitry Andric S i_to_string(const V v) 427*0b57cec5SDimitry Andric { 428*0b57cec5SDimitry Andric // numeric_limits::digits10 returns value less on 1 than desired for unsigned numbers. 429*0b57cec5SDimitry Andric // For example, for 1-byte unsigned value digits10 is 2 (999 can not be represented), 430*0b57cec5SDimitry Andric // so we need +1 here. 431*0b57cec5SDimitry Andric constexpr size_t bufsize = numeric_limits<V>::digits10 + 2; // +1 for minus, +1 for digits10 432*0b57cec5SDimitry Andric char buf[bufsize]; 433*0b57cec5SDimitry Andric const auto res = to_chars(buf, buf + bufsize, v); 434*0b57cec5SDimitry Andric _LIBCPP_ASSERT(res.ec == errc(), "bufsize must be large enough to accomodate the value"); 435*0b57cec5SDimitry Andric return S(buf, res.ptr); 436*0b57cec5SDimitry Andric } 437*0b57cec5SDimitry Andric 438*0b57cec5SDimitry Andric } // unnamed namespace 439*0b57cec5SDimitry Andric 440*0b57cec5SDimitry Andric string to_string (int val) { return i_to_string< string>(val); } 441*0b57cec5SDimitry Andric string to_string (long val) { return i_to_string< string>(val); } 442*0b57cec5SDimitry Andric string to_string (long long val) { return i_to_string< string>(val); } 443*0b57cec5SDimitry Andric string to_string (unsigned val) { return i_to_string< string>(val); } 444*0b57cec5SDimitry Andric string to_string (unsigned long val) { return i_to_string< string>(val); } 445*0b57cec5SDimitry Andric string to_string (unsigned long long val) { return i_to_string< string>(val); } 446*0b57cec5SDimitry Andric 447*0b57cec5SDimitry Andric wstring to_wstring(int val) { return i_to_string<wstring>(val); } 448*0b57cec5SDimitry Andric wstring to_wstring(long val) { return i_to_string<wstring>(val); } 449*0b57cec5SDimitry Andric wstring to_wstring(long long val) { return i_to_string<wstring>(val); } 450*0b57cec5SDimitry Andric wstring to_wstring(unsigned val) { return i_to_string<wstring>(val); } 451*0b57cec5SDimitry Andric wstring to_wstring(unsigned long val) { return i_to_string<wstring>(val); } 452*0b57cec5SDimitry Andric wstring to_wstring(unsigned long long val) { return i_to_string<wstring>(val); } 453*0b57cec5SDimitry Andric 454*0b57cec5SDimitry Andric 455*0b57cec5SDimitry Andric string to_string (float val) { return as_string(snprintf, initial_string< string>()(), "%f", val); } 456*0b57cec5SDimitry Andric string to_string (double val) { return as_string(snprintf, initial_string< string>()(), "%f", val); } 457*0b57cec5SDimitry Andric string to_string (long double val) { return as_string(snprintf, initial_string< string>()(), "%Lf", val); } 458*0b57cec5SDimitry Andric 459*0b57cec5SDimitry Andric wstring to_wstring(float val) { return as_string(get_swprintf(), initial_string<wstring>()(), L"%f", val); } 460*0b57cec5SDimitry Andric wstring to_wstring(double val) { return as_string(get_swprintf(), initial_string<wstring>()(), L"%f", val); } 461*0b57cec5SDimitry Andric wstring to_wstring(long double val) { return as_string(get_swprintf(), initial_string<wstring>()(), L"%Lf", val); } 462*0b57cec5SDimitry Andric 463*0b57cec5SDimitry Andric _LIBCPP_END_NAMESPACE_STD 464