14684ddb6SLionel Sambuc //===------------------------- string.cpp ---------------------------------===//
24684ddb6SLionel Sambuc //
34684ddb6SLionel Sambuc // The LLVM Compiler Infrastructure
44684ddb6SLionel Sambuc //
54684ddb6SLionel Sambuc // This file is dual licensed under the MIT and the University of Illinois Open
64684ddb6SLionel Sambuc // Source Licenses. See LICENSE.TXT for details.
74684ddb6SLionel Sambuc //
84684ddb6SLionel Sambuc //===----------------------------------------------------------------------===//
94684ddb6SLionel Sambuc
104684ddb6SLionel Sambuc #include "string"
114684ddb6SLionel Sambuc #include "cstdlib"
124684ddb6SLionel Sambuc #include "cwchar"
134684ddb6SLionel Sambuc #include "cerrno"
144684ddb6SLionel Sambuc #include "limits"
154684ddb6SLionel Sambuc #include "stdexcept"
164684ddb6SLionel Sambuc #ifdef _LIBCPP_MSVCRT
174684ddb6SLionel Sambuc #include "support/win32/support.h"
184684ddb6SLionel Sambuc #endif // _LIBCPP_MSVCRT
194684ddb6SLionel Sambuc #include <stdio.h>
204684ddb6SLionel Sambuc
214684ddb6SLionel Sambuc _LIBCPP_BEGIN_NAMESPACE_STD
224684ddb6SLionel Sambuc
234684ddb6SLionel Sambuc template class __basic_string_common<true>;
244684ddb6SLionel Sambuc
254684ddb6SLionel Sambuc template class basic_string<char>;
264684ddb6SLionel Sambuc template class basic_string<wchar_t>;
274684ddb6SLionel Sambuc
284684ddb6SLionel Sambuc template
294684ddb6SLionel Sambuc string
304684ddb6SLionel Sambuc operator+<char, char_traits<char>, allocator<char> >(char const*, string const&);
314684ddb6SLionel Sambuc
324684ddb6SLionel Sambuc namespace
334684ddb6SLionel Sambuc {
344684ddb6SLionel Sambuc
354684ddb6SLionel Sambuc template<typename T>
364684ddb6SLionel Sambuc inline
throw_helper(const string & msg)374684ddb6SLionel Sambuc void throw_helper( const string& msg )
384684ddb6SLionel Sambuc {
394684ddb6SLionel Sambuc #ifndef _LIBCPP_NO_EXCEPTIONS
404684ddb6SLionel Sambuc throw T( msg );
414684ddb6SLionel Sambuc #else
42*0a6a1f1dSLionel Sambuc fprintf(stderr, "%s\n", msg.c_str());
434684ddb6SLionel Sambuc abort();
444684ddb6SLionel Sambuc #endif
454684ddb6SLionel Sambuc }
464684ddb6SLionel Sambuc
474684ddb6SLionel Sambuc inline
throw_from_string_out_of_range(const string & func)484684ddb6SLionel Sambuc void throw_from_string_out_of_range( const string& func )
494684ddb6SLionel Sambuc {
504684ddb6SLionel Sambuc throw_helper<out_of_range>(func + ": out of range");
514684ddb6SLionel Sambuc }
524684ddb6SLionel Sambuc
534684ddb6SLionel Sambuc inline
throw_from_string_invalid_arg(const string & func)544684ddb6SLionel Sambuc void throw_from_string_invalid_arg( const string& func )
554684ddb6SLionel Sambuc {
564684ddb6SLionel Sambuc throw_helper<invalid_argument>(func + ": no conversion");
574684ddb6SLionel Sambuc }
584684ddb6SLionel Sambuc
594684ddb6SLionel Sambuc // as_integer
604684ddb6SLionel Sambuc
614684ddb6SLionel Sambuc template<typename V, typename S, typename F>
624684ddb6SLionel Sambuc inline
634684ddb6SLionel Sambuc V
as_integer_helper(const string & func,const S & str,size_t * idx,int base,F f)644684ddb6SLionel Sambuc as_integer_helper(const string& func, const S& str, size_t* idx, int base, F f)
654684ddb6SLionel Sambuc {
66*0a6a1f1dSLionel Sambuc typename S::value_type* ptr = nullptr;
674684ddb6SLionel Sambuc const typename S::value_type* const p = str.c_str();
684684ddb6SLionel Sambuc typename remove_reference<decltype(errno)>::type errno_save = errno;
694684ddb6SLionel Sambuc errno = 0;
704684ddb6SLionel Sambuc V r = f(p, &ptr, base);
714684ddb6SLionel Sambuc swap(errno, errno_save);
724684ddb6SLionel Sambuc if (errno_save == ERANGE)
734684ddb6SLionel Sambuc throw_from_string_out_of_range(func);
744684ddb6SLionel Sambuc if (ptr == p)
754684ddb6SLionel Sambuc throw_from_string_invalid_arg(func);
764684ddb6SLionel Sambuc if (idx)
774684ddb6SLionel Sambuc *idx = static_cast<size_t>(ptr - p);
784684ddb6SLionel Sambuc return r;
794684ddb6SLionel Sambuc }
804684ddb6SLionel Sambuc
814684ddb6SLionel Sambuc template<typename V, typename S>
824684ddb6SLionel Sambuc inline
834684ddb6SLionel Sambuc V
844684ddb6SLionel Sambuc as_integer(const string& func, const S& s, size_t* idx, int base);
854684ddb6SLionel Sambuc
864684ddb6SLionel Sambuc // string
874684ddb6SLionel Sambuc template<>
884684ddb6SLionel Sambuc inline
894684ddb6SLionel Sambuc int
as_integer(const string & func,const string & s,size_t * idx,int base)904684ddb6SLionel Sambuc as_integer(const string& func, const string& s, size_t* idx, int base )
914684ddb6SLionel Sambuc {
924684ddb6SLionel Sambuc // Use long as no Standard string to integer exists.
934684ddb6SLionel Sambuc long r = as_integer_helper<long>( func, s, idx, base, strtol );
944684ddb6SLionel Sambuc if (r < numeric_limits<int>::min() || numeric_limits<int>::max() < r)
954684ddb6SLionel Sambuc throw_from_string_out_of_range(func);
964684ddb6SLionel Sambuc return static_cast<int>(r);
974684ddb6SLionel Sambuc }
984684ddb6SLionel Sambuc
994684ddb6SLionel Sambuc template<>
1004684ddb6SLionel Sambuc inline
1014684ddb6SLionel Sambuc long
as_integer(const string & func,const string & s,size_t * idx,int base)1024684ddb6SLionel Sambuc as_integer(const string& func, const string& s, size_t* idx, int base )
1034684ddb6SLionel Sambuc {
1044684ddb6SLionel Sambuc return as_integer_helper<long>( func, s, idx, base, strtol );
1054684ddb6SLionel Sambuc }
1064684ddb6SLionel Sambuc
1074684ddb6SLionel Sambuc template<>
1084684ddb6SLionel Sambuc inline
1094684ddb6SLionel Sambuc unsigned long
as_integer(const string & func,const string & s,size_t * idx,int base)1104684ddb6SLionel Sambuc as_integer( const string& func, const string& s, size_t* idx, int base )
1114684ddb6SLionel Sambuc {
1124684ddb6SLionel Sambuc return as_integer_helper<unsigned long>( func, s, idx, base, strtoul );
1134684ddb6SLionel Sambuc }
1144684ddb6SLionel Sambuc
1154684ddb6SLionel Sambuc template<>
1164684ddb6SLionel Sambuc inline
1174684ddb6SLionel Sambuc long long
as_integer(const string & func,const string & s,size_t * idx,int base)1184684ddb6SLionel Sambuc as_integer( const string& func, const string& s, size_t* idx, int base )
1194684ddb6SLionel Sambuc {
1204684ddb6SLionel Sambuc return as_integer_helper<long long>( func, s, idx, base, strtoll );
1214684ddb6SLionel Sambuc }
1224684ddb6SLionel Sambuc
1234684ddb6SLionel Sambuc template<>
1244684ddb6SLionel Sambuc inline
1254684ddb6SLionel Sambuc unsigned long long
as_integer(const string & func,const string & s,size_t * idx,int base)1264684ddb6SLionel Sambuc as_integer( const string& func, const string& s, size_t* idx, int base )
1274684ddb6SLionel Sambuc {
1284684ddb6SLionel Sambuc return as_integer_helper<unsigned long long>( func, s, idx, base, strtoull );
1294684ddb6SLionel Sambuc }
1304684ddb6SLionel Sambuc
1314684ddb6SLionel Sambuc // wstring
1324684ddb6SLionel Sambuc template<>
1334684ddb6SLionel Sambuc inline
1344684ddb6SLionel Sambuc int
as_integer(const string & func,const wstring & s,size_t * idx,int base)1354684ddb6SLionel Sambuc as_integer( const string& func, const wstring& s, size_t* idx, int base )
1364684ddb6SLionel Sambuc {
1374684ddb6SLionel Sambuc // Use long as no Stantard string to integer exists.
1384684ddb6SLionel Sambuc long r = as_integer_helper<long>( func, s, idx, base, wcstol );
1394684ddb6SLionel Sambuc if (r < numeric_limits<int>::min() || numeric_limits<int>::max() < r)
1404684ddb6SLionel Sambuc throw_from_string_out_of_range(func);
1414684ddb6SLionel Sambuc return static_cast<int>(r);
1424684ddb6SLionel Sambuc }
1434684ddb6SLionel Sambuc
1444684ddb6SLionel Sambuc template<>
1454684ddb6SLionel Sambuc inline
1464684ddb6SLionel Sambuc long
as_integer(const string & func,const wstring & s,size_t * idx,int base)1474684ddb6SLionel Sambuc as_integer( const string& func, const wstring& s, size_t* idx, int base )
1484684ddb6SLionel Sambuc {
1494684ddb6SLionel Sambuc return as_integer_helper<long>( func, s, idx, base, wcstol );
1504684ddb6SLionel Sambuc }
1514684ddb6SLionel Sambuc
1524684ddb6SLionel Sambuc template<>
1534684ddb6SLionel Sambuc inline
1544684ddb6SLionel Sambuc unsigned long
as_integer(const string & func,const wstring & s,size_t * idx,int base)1554684ddb6SLionel Sambuc as_integer( const string& func, const wstring& s, size_t* idx, int base )
1564684ddb6SLionel Sambuc {
1574684ddb6SLionel Sambuc return as_integer_helper<unsigned long>( func, s, idx, base, wcstoul );
1584684ddb6SLionel Sambuc }
1594684ddb6SLionel Sambuc
1604684ddb6SLionel Sambuc template<>
1614684ddb6SLionel Sambuc inline
1624684ddb6SLionel Sambuc long long
as_integer(const string & func,const wstring & s,size_t * idx,int base)1634684ddb6SLionel Sambuc as_integer( const string& func, const wstring& s, size_t* idx, int base )
1644684ddb6SLionel Sambuc {
1654684ddb6SLionel Sambuc return as_integer_helper<long long>( func, s, idx, base, wcstoll );
1664684ddb6SLionel Sambuc }
1674684ddb6SLionel Sambuc
1684684ddb6SLionel Sambuc template<>
1694684ddb6SLionel Sambuc inline
1704684ddb6SLionel Sambuc unsigned long long
as_integer(const string & func,const wstring & s,size_t * idx,int base)1714684ddb6SLionel Sambuc as_integer( const string& func, const wstring& s, size_t* idx, int base )
1724684ddb6SLionel Sambuc {
1734684ddb6SLionel Sambuc return as_integer_helper<unsigned long long>( func, s, idx, base, wcstoull );
1744684ddb6SLionel Sambuc }
1754684ddb6SLionel Sambuc
1764684ddb6SLionel Sambuc // as_float
1774684ddb6SLionel Sambuc
1784684ddb6SLionel Sambuc template<typename V, typename S, typename F>
1794684ddb6SLionel Sambuc inline
1804684ddb6SLionel Sambuc V
as_float_helper(const string & func,const S & str,size_t * idx,F f)1814684ddb6SLionel Sambuc as_float_helper(const string& func, const S& str, size_t* idx, F f )
1824684ddb6SLionel Sambuc {
183*0a6a1f1dSLionel Sambuc typename S::value_type* ptr = nullptr;
1844684ddb6SLionel Sambuc const typename S::value_type* const p = str.c_str();
1854684ddb6SLionel Sambuc typename remove_reference<decltype(errno)>::type errno_save = errno;
1864684ddb6SLionel Sambuc errno = 0;
1874684ddb6SLionel Sambuc V r = f(p, &ptr);
1884684ddb6SLionel Sambuc swap(errno, errno_save);
1894684ddb6SLionel Sambuc if (errno_save == ERANGE)
1904684ddb6SLionel Sambuc throw_from_string_out_of_range(func);
1914684ddb6SLionel Sambuc if (ptr == p)
1924684ddb6SLionel Sambuc throw_from_string_invalid_arg(func);
1934684ddb6SLionel Sambuc if (idx)
1944684ddb6SLionel Sambuc *idx = static_cast<size_t>(ptr - p);
1954684ddb6SLionel Sambuc return r;
1964684ddb6SLionel Sambuc }
1974684ddb6SLionel Sambuc
1984684ddb6SLionel Sambuc template<typename V, typename S>
1994684ddb6SLionel Sambuc inline
2004684ddb6SLionel Sambuc V as_float( const string& func, const S& s, size_t* idx = nullptr );
2014684ddb6SLionel Sambuc
2024684ddb6SLionel Sambuc template<>
2034684ddb6SLionel Sambuc inline
2044684ddb6SLionel Sambuc float
as_float(const string & func,const string & s,size_t * idx)2054684ddb6SLionel Sambuc as_float( const string& func, const string& s, size_t* idx )
2064684ddb6SLionel Sambuc {
2074684ddb6SLionel Sambuc return as_float_helper<float>( func, s, idx, strtof );
2084684ddb6SLionel Sambuc }
2094684ddb6SLionel Sambuc
2104684ddb6SLionel Sambuc template<>
2114684ddb6SLionel Sambuc inline
2124684ddb6SLionel Sambuc double
as_float(const string & func,const string & s,size_t * idx)2134684ddb6SLionel Sambuc as_float(const string& func, const string& s, size_t* idx )
2144684ddb6SLionel Sambuc {
2154684ddb6SLionel Sambuc return as_float_helper<double>( func, s, idx, strtod );
2164684ddb6SLionel Sambuc }
2174684ddb6SLionel Sambuc
2184684ddb6SLionel Sambuc template<>
2194684ddb6SLionel Sambuc inline
2204684ddb6SLionel Sambuc long double
as_float(const string & func,const string & s,size_t * idx)2214684ddb6SLionel Sambuc as_float( const string& func, const string& s, size_t* idx )
2224684ddb6SLionel Sambuc {
2234684ddb6SLionel Sambuc return as_float_helper<long double>( func, s, idx, strtold );
2244684ddb6SLionel Sambuc }
2254684ddb6SLionel Sambuc
2264684ddb6SLionel Sambuc template<>
2274684ddb6SLionel Sambuc inline
2284684ddb6SLionel Sambuc float
as_float(const string & func,const wstring & s,size_t * idx)2294684ddb6SLionel Sambuc as_float( const string& func, const wstring& s, size_t* idx )
2304684ddb6SLionel Sambuc {
2314684ddb6SLionel Sambuc return as_float_helper<float>( func, s, idx, wcstof );
2324684ddb6SLionel Sambuc }
2334684ddb6SLionel Sambuc
2344684ddb6SLionel Sambuc template<>
2354684ddb6SLionel Sambuc inline
2364684ddb6SLionel Sambuc double
as_float(const string & func,const wstring & s,size_t * idx)2374684ddb6SLionel Sambuc as_float( const string& func, const wstring& s, size_t* idx )
2384684ddb6SLionel Sambuc {
2394684ddb6SLionel Sambuc return as_float_helper<double>( func, s, idx, wcstod );
2404684ddb6SLionel Sambuc }
2414684ddb6SLionel Sambuc
2424684ddb6SLionel Sambuc template<>
2434684ddb6SLionel Sambuc inline
2444684ddb6SLionel Sambuc long double
as_float(const string & func,const wstring & s,size_t * idx)2454684ddb6SLionel Sambuc as_float( const string& func, const wstring& s, size_t* idx )
2464684ddb6SLionel Sambuc {
2474684ddb6SLionel Sambuc return as_float_helper<long double>( func, s, idx, wcstold );
2484684ddb6SLionel Sambuc }
2494684ddb6SLionel Sambuc
2504684ddb6SLionel Sambuc } // unnamed namespace
2514684ddb6SLionel Sambuc
2524684ddb6SLionel Sambuc int
stoi(const string & str,size_t * idx,int base)2534684ddb6SLionel Sambuc stoi(const string& str, size_t* idx, int base)
2544684ddb6SLionel Sambuc {
2554684ddb6SLionel Sambuc return as_integer<int>( "stoi", str, idx, base );
2564684ddb6SLionel Sambuc }
2574684ddb6SLionel Sambuc
2584684ddb6SLionel Sambuc int
stoi(const wstring & str,size_t * idx,int base)2594684ddb6SLionel Sambuc stoi(const wstring& str, size_t* idx, int base)
2604684ddb6SLionel Sambuc {
2614684ddb6SLionel Sambuc return as_integer<int>( "stoi", str, idx, base );
2624684ddb6SLionel Sambuc }
2634684ddb6SLionel Sambuc
2644684ddb6SLionel Sambuc long
stol(const string & str,size_t * idx,int base)2654684ddb6SLionel Sambuc stol(const string& str, size_t* idx, int base)
2664684ddb6SLionel Sambuc {
2674684ddb6SLionel Sambuc return as_integer<long>( "stol", str, idx, base );
2684684ddb6SLionel Sambuc }
2694684ddb6SLionel Sambuc
2704684ddb6SLionel Sambuc long
stol(const wstring & str,size_t * idx,int base)2714684ddb6SLionel Sambuc stol(const wstring& str, size_t* idx, int base)
2724684ddb6SLionel Sambuc {
2734684ddb6SLionel Sambuc return as_integer<long>( "stol", str, idx, base );
2744684ddb6SLionel Sambuc }
2754684ddb6SLionel Sambuc
2764684ddb6SLionel Sambuc unsigned long
stoul(const string & str,size_t * idx,int base)2774684ddb6SLionel Sambuc stoul(const string& str, size_t* idx, int base)
2784684ddb6SLionel Sambuc {
2794684ddb6SLionel Sambuc return as_integer<unsigned long>( "stoul", str, idx, base );
2804684ddb6SLionel Sambuc }
2814684ddb6SLionel Sambuc
2824684ddb6SLionel Sambuc unsigned long
stoul(const wstring & str,size_t * idx,int base)2834684ddb6SLionel Sambuc stoul(const wstring& str, size_t* idx, int base)
2844684ddb6SLionel Sambuc {
2854684ddb6SLionel Sambuc return as_integer<unsigned long>( "stoul", str, idx, base );
2864684ddb6SLionel Sambuc }
2874684ddb6SLionel Sambuc
2884684ddb6SLionel Sambuc long long
stoll(const string & str,size_t * idx,int base)2894684ddb6SLionel Sambuc stoll(const string& str, size_t* idx, int base)
2904684ddb6SLionel Sambuc {
2914684ddb6SLionel Sambuc return as_integer<long long>( "stoll", str, idx, base );
2924684ddb6SLionel Sambuc }
2934684ddb6SLionel Sambuc
2944684ddb6SLionel Sambuc long long
stoll(const wstring & str,size_t * idx,int base)2954684ddb6SLionel Sambuc stoll(const wstring& str, size_t* idx, int base)
2964684ddb6SLionel Sambuc {
2974684ddb6SLionel Sambuc return as_integer<long long>( "stoll", str, idx, base );
2984684ddb6SLionel Sambuc }
2994684ddb6SLionel Sambuc
3004684ddb6SLionel Sambuc unsigned long long
stoull(const string & str,size_t * idx,int base)3014684ddb6SLionel Sambuc stoull(const string& str, size_t* idx, int base)
3024684ddb6SLionel Sambuc {
3034684ddb6SLionel Sambuc return as_integer<unsigned long long>( "stoull", str, idx, base );
3044684ddb6SLionel Sambuc }
3054684ddb6SLionel Sambuc
3064684ddb6SLionel Sambuc unsigned long long
stoull(const wstring & str,size_t * idx,int base)3074684ddb6SLionel Sambuc stoull(const wstring& str, size_t* idx, int base)
3084684ddb6SLionel Sambuc {
3094684ddb6SLionel Sambuc return as_integer<unsigned long long>( "stoull", str, idx, base );
3104684ddb6SLionel Sambuc }
3114684ddb6SLionel Sambuc
3124684ddb6SLionel Sambuc float
stof(const string & str,size_t * idx)3134684ddb6SLionel Sambuc stof(const string& str, size_t* idx)
3144684ddb6SLionel Sambuc {
3154684ddb6SLionel Sambuc return as_float<float>( "stof", str, idx );
3164684ddb6SLionel Sambuc }
3174684ddb6SLionel Sambuc
3184684ddb6SLionel Sambuc float
stof(const wstring & str,size_t * idx)3194684ddb6SLionel Sambuc stof(const wstring& str, size_t* idx)
3204684ddb6SLionel Sambuc {
3214684ddb6SLionel Sambuc return as_float<float>( "stof", str, idx );
3224684ddb6SLionel Sambuc }
3234684ddb6SLionel Sambuc
3244684ddb6SLionel Sambuc double
stod(const string & str,size_t * idx)3254684ddb6SLionel Sambuc stod(const string& str, size_t* idx)
3264684ddb6SLionel Sambuc {
3274684ddb6SLionel Sambuc return as_float<double>( "stod", str, idx );
3284684ddb6SLionel Sambuc }
3294684ddb6SLionel Sambuc
3304684ddb6SLionel Sambuc double
stod(const wstring & str,size_t * idx)3314684ddb6SLionel Sambuc stod(const wstring& str, size_t* idx)
3324684ddb6SLionel Sambuc {
3334684ddb6SLionel Sambuc return as_float<double>( "stod", str, idx );
3344684ddb6SLionel Sambuc }
3354684ddb6SLionel Sambuc
3364684ddb6SLionel Sambuc long double
stold(const string & str,size_t * idx)3374684ddb6SLionel Sambuc stold(const string& str, size_t* idx)
3384684ddb6SLionel Sambuc {
3394684ddb6SLionel Sambuc return as_float<long double>( "stold", str, idx );
3404684ddb6SLionel Sambuc }
3414684ddb6SLionel Sambuc
3424684ddb6SLionel Sambuc long double
stold(const wstring & str,size_t * idx)3434684ddb6SLionel Sambuc stold(const wstring& str, size_t* idx)
3444684ddb6SLionel Sambuc {
3454684ddb6SLionel Sambuc return as_float<long double>( "stold", str, idx );
3464684ddb6SLionel Sambuc }
3474684ddb6SLionel Sambuc
3484684ddb6SLionel Sambuc // to_string
3494684ddb6SLionel Sambuc
3504684ddb6SLionel Sambuc namespace
3514684ddb6SLionel Sambuc {
3524684ddb6SLionel Sambuc
3534684ddb6SLionel Sambuc // as_string
3544684ddb6SLionel Sambuc
3554684ddb6SLionel Sambuc template<typename S, typename P, typename V >
3564684ddb6SLionel Sambuc inline
3574684ddb6SLionel Sambuc S
as_string(P sprintf_like,S s,const typename S::value_type * fmt,V a)3584684ddb6SLionel Sambuc as_string(P sprintf_like, S s, const typename S::value_type* fmt, V a)
3594684ddb6SLionel Sambuc {
3604684ddb6SLionel Sambuc typedef typename S::size_type size_type;
3614684ddb6SLionel Sambuc size_type available = s.size();
3624684ddb6SLionel Sambuc while (true)
3634684ddb6SLionel Sambuc {
3644684ddb6SLionel Sambuc int status = sprintf_like(&s[0], available + 1, fmt, a);
3654684ddb6SLionel Sambuc if ( status >= 0 )
3664684ddb6SLionel Sambuc {
3674684ddb6SLionel Sambuc size_type used = static_cast<size_type>(status);
3684684ddb6SLionel Sambuc if ( used <= available )
3694684ddb6SLionel Sambuc {
3704684ddb6SLionel Sambuc s.resize( used );
3714684ddb6SLionel Sambuc break;
3724684ddb6SLionel Sambuc }
3734684ddb6SLionel Sambuc available = used; // Assume this is advice of how much space we need.
3744684ddb6SLionel Sambuc }
3754684ddb6SLionel Sambuc else
3764684ddb6SLionel Sambuc available = available * 2 + 1;
3774684ddb6SLionel Sambuc s.resize(available);
3784684ddb6SLionel Sambuc }
3794684ddb6SLionel Sambuc return s;
3804684ddb6SLionel Sambuc }
3814684ddb6SLionel Sambuc
3824684ddb6SLionel Sambuc template <class S, class V, bool = is_floating_point<V>::value>
3834684ddb6SLionel Sambuc struct initial_string;
3844684ddb6SLionel Sambuc
3854684ddb6SLionel Sambuc template <class V, bool b>
3864684ddb6SLionel Sambuc struct initial_string<string, V, b>
3874684ddb6SLionel Sambuc {
3884684ddb6SLionel Sambuc string
operator ()__anonf7aac1cf0211::initial_string3894684ddb6SLionel Sambuc operator()() const
3904684ddb6SLionel Sambuc {
3914684ddb6SLionel Sambuc string s;
3924684ddb6SLionel Sambuc s.resize(s.capacity());
3934684ddb6SLionel Sambuc return s;
3944684ddb6SLionel Sambuc }
3954684ddb6SLionel Sambuc };
3964684ddb6SLionel Sambuc
3974684ddb6SLionel Sambuc template <class V>
3984684ddb6SLionel Sambuc struct initial_string<wstring, V, false>
3994684ddb6SLionel Sambuc {
4004684ddb6SLionel Sambuc wstring
operator ()__anonf7aac1cf0211::initial_string4014684ddb6SLionel Sambuc operator()() const
4024684ddb6SLionel Sambuc {
4034684ddb6SLionel Sambuc const size_t n = (numeric_limits<unsigned long long>::digits / 3)
4044684ddb6SLionel Sambuc + ((numeric_limits<unsigned long long>::digits % 3) != 0)
4054684ddb6SLionel Sambuc + 1;
4064684ddb6SLionel Sambuc wstring s(n, wchar_t());
4074684ddb6SLionel Sambuc s.resize(s.capacity());
4084684ddb6SLionel Sambuc return s;
4094684ddb6SLionel Sambuc }
4104684ddb6SLionel Sambuc };
4114684ddb6SLionel Sambuc
4124684ddb6SLionel Sambuc template <class V>
4134684ddb6SLionel Sambuc struct initial_string<wstring, V, true>
4144684ddb6SLionel Sambuc {
4154684ddb6SLionel Sambuc wstring
operator ()__anonf7aac1cf0211::initial_string4164684ddb6SLionel Sambuc operator()() const
4174684ddb6SLionel Sambuc {
4184684ddb6SLionel Sambuc wstring s(20, wchar_t());
4194684ddb6SLionel Sambuc s.resize(s.capacity());
4204684ddb6SLionel Sambuc return s;
4214684ddb6SLionel Sambuc }
4224684ddb6SLionel Sambuc };
4234684ddb6SLionel Sambuc
4244684ddb6SLionel Sambuc typedef int (*wide_printf)(wchar_t* __restrict, size_t, const wchar_t*__restrict, ...);
4254684ddb6SLionel Sambuc
4264684ddb6SLionel Sambuc inline
4274684ddb6SLionel Sambuc wide_printf
get_swprintf()4284684ddb6SLionel Sambuc get_swprintf()
4294684ddb6SLionel Sambuc {
4304684ddb6SLionel Sambuc #ifndef _LIBCPP_MSVCRT
4314684ddb6SLionel Sambuc return swprintf;
4324684ddb6SLionel Sambuc #else
4334684ddb6SLionel Sambuc return static_cast<int (__cdecl*)(wchar_t* __restrict, size_t, const wchar_t*__restrict, ...)>(swprintf);
4344684ddb6SLionel Sambuc #endif
4354684ddb6SLionel Sambuc }
4364684ddb6SLionel Sambuc
4374684ddb6SLionel Sambuc } // unnamed namespace
4384684ddb6SLionel Sambuc
to_string(int val)4394684ddb6SLionel Sambuc string to_string(int val)
4404684ddb6SLionel Sambuc {
4414684ddb6SLionel Sambuc return as_string(snprintf, initial_string<string, int>()(), "%d", val);
4424684ddb6SLionel Sambuc }
4434684ddb6SLionel Sambuc
to_string(unsigned val)4444684ddb6SLionel Sambuc string to_string(unsigned val)
4454684ddb6SLionel Sambuc {
4464684ddb6SLionel Sambuc return as_string(snprintf, initial_string<string, unsigned>()(), "%u", val);
4474684ddb6SLionel Sambuc }
4484684ddb6SLionel Sambuc
to_string(long val)4494684ddb6SLionel Sambuc string to_string(long val)
4504684ddb6SLionel Sambuc {
4514684ddb6SLionel Sambuc return as_string(snprintf, initial_string<string, long>()(), "%ld", val);
4524684ddb6SLionel Sambuc }
4534684ddb6SLionel Sambuc
to_string(unsigned long val)4544684ddb6SLionel Sambuc string to_string(unsigned long val)
4554684ddb6SLionel Sambuc {
4564684ddb6SLionel Sambuc return as_string(snprintf, initial_string<string, unsigned long>()(), "%lu", val);
4574684ddb6SLionel Sambuc }
4584684ddb6SLionel Sambuc
to_string(long long val)4594684ddb6SLionel Sambuc string to_string(long long val)
4604684ddb6SLionel Sambuc {
4614684ddb6SLionel Sambuc return as_string(snprintf, initial_string<string, long long>()(), "%lld", val);
4624684ddb6SLionel Sambuc }
4634684ddb6SLionel Sambuc
to_string(unsigned long long val)4644684ddb6SLionel Sambuc string to_string(unsigned long long val)
4654684ddb6SLionel Sambuc {
4664684ddb6SLionel Sambuc return as_string(snprintf, initial_string<string, unsigned long long>()(), "%llu", val);
4674684ddb6SLionel Sambuc }
4684684ddb6SLionel Sambuc
to_string(float val)4694684ddb6SLionel Sambuc string to_string(float val)
4704684ddb6SLionel Sambuc {
4714684ddb6SLionel Sambuc return as_string(snprintf, initial_string<string, float>()(), "%f", val);
4724684ddb6SLionel Sambuc }
4734684ddb6SLionel Sambuc
to_string(double val)4744684ddb6SLionel Sambuc string to_string(double val)
4754684ddb6SLionel Sambuc {
4764684ddb6SLionel Sambuc return as_string(snprintf, initial_string<string, double>()(), "%f", val);
4774684ddb6SLionel Sambuc }
4784684ddb6SLionel Sambuc
to_string(long double val)4794684ddb6SLionel Sambuc string to_string(long double val)
4804684ddb6SLionel Sambuc {
4814684ddb6SLionel Sambuc return as_string(snprintf, initial_string<string, long double>()(), "%Lf", val);
4824684ddb6SLionel Sambuc }
4834684ddb6SLionel Sambuc
to_wstring(int val)4844684ddb6SLionel Sambuc wstring to_wstring(int val)
4854684ddb6SLionel Sambuc {
4864684ddb6SLionel Sambuc return as_string(get_swprintf(), initial_string<wstring, int>()(), L"%d", val);
4874684ddb6SLionel Sambuc }
4884684ddb6SLionel Sambuc
to_wstring(unsigned val)4894684ddb6SLionel Sambuc wstring to_wstring(unsigned val)
4904684ddb6SLionel Sambuc {
4914684ddb6SLionel Sambuc return as_string(get_swprintf(), initial_string<wstring, unsigned>()(), L"%u", val);
4924684ddb6SLionel Sambuc }
4934684ddb6SLionel Sambuc
to_wstring(long val)4944684ddb6SLionel Sambuc wstring to_wstring(long val)
4954684ddb6SLionel Sambuc {
4964684ddb6SLionel Sambuc return as_string(get_swprintf(), initial_string<wstring, long>()(), L"%ld", val);
4974684ddb6SLionel Sambuc }
4984684ddb6SLionel Sambuc
to_wstring(unsigned long val)4994684ddb6SLionel Sambuc wstring to_wstring(unsigned long val)
5004684ddb6SLionel Sambuc {
5014684ddb6SLionel Sambuc return as_string(get_swprintf(), initial_string<wstring, unsigned long>()(), L"%lu", val);
5024684ddb6SLionel Sambuc }
5034684ddb6SLionel Sambuc
to_wstring(long long val)5044684ddb6SLionel Sambuc wstring to_wstring(long long val)
5054684ddb6SLionel Sambuc {
5064684ddb6SLionel Sambuc return as_string(get_swprintf(), initial_string<wstring, long long>()(), L"%lld", val);
5074684ddb6SLionel Sambuc }
5084684ddb6SLionel Sambuc
to_wstring(unsigned long long val)5094684ddb6SLionel Sambuc wstring to_wstring(unsigned long long val)
5104684ddb6SLionel Sambuc {
5114684ddb6SLionel Sambuc return as_string(get_swprintf(), initial_string<wstring, unsigned long long>()(), L"%llu", val);
5124684ddb6SLionel Sambuc }
5134684ddb6SLionel Sambuc
to_wstring(float val)5144684ddb6SLionel Sambuc wstring to_wstring(float val)
5154684ddb6SLionel Sambuc {
5164684ddb6SLionel Sambuc return as_string(get_swprintf(), initial_string<wstring, float>()(), L"%f", val);
5174684ddb6SLionel Sambuc }
5184684ddb6SLionel Sambuc
to_wstring(double val)5194684ddb6SLionel Sambuc wstring to_wstring(double val)
5204684ddb6SLionel Sambuc {
5214684ddb6SLionel Sambuc return as_string(get_swprintf(), initial_string<wstring, double>()(), L"%f", val);
5224684ddb6SLionel Sambuc }
5234684ddb6SLionel Sambuc
to_wstring(long double val)5244684ddb6SLionel Sambuc wstring to_wstring(long double val)
5254684ddb6SLionel Sambuc {
5264684ddb6SLionel Sambuc return as_string(get_swprintf(), initial_string<wstring, long double>()(), L"%Lf", val);
5274684ddb6SLionel Sambuc }
5284684ddb6SLionel Sambuc _LIBCPP_END_NAMESPACE_STD
529