Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 1 | //===------------------------- string.cpp ---------------------------------===// |
| 2 | // |
Chandler Carruth | 57b08b0 | 2019-01-19 10:56:40 | [diff] [blame^] | 3 | // 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 Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "string" |
| 10 | #include "cstdlib" |
| 11 | #include "cwchar" |
| 12 | #include "cerrno" |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 13 | #include "limits" |
| 14 | #include "stdexcept" |
Howard Hinnant | 1468d0c | 2013-07-23 16:05:56 | [diff] [blame] | 15 | #include <stdio.h> |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 16 | |
| 17 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 18 | |
Shoaib Meenai | 190994e | 2016-09-19 18:29:07 | [diff] [blame] | 19 | template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS __basic_string_common<true>; |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 20 | |
Shoaib Meenai | 190994e | 2016-09-19 18:29:07 | [diff] [blame] | 21 | template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS basic_string<char>; |
| 22 | template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS basic_string<wchar_t>; |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 23 | |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 24 | template |
| 25 | string |
| 26 | operator+<char, char_traits<char>, allocator<char> >(char const*, string const&); |
| 27 | |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 28 | namespace |
| 29 | { |
| 30 | |
| 31 | template<typename T> |
| 32 | inline |
| 33 | void throw_helper( const string& msg ) |
| 34 | { |
| 35 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 36 | throw T( msg ); |
| 37 | #else |
Ed Schouten | c19393c | 2015-03-10 07:57:43 | [diff] [blame] | 38 | fprintf(stderr, "%s\n", msg.c_str()); |
Marshall Clow | d437fa5 | 2016-08-25 15:09:01 | [diff] [blame] | 39 | _VSTD::abort(); |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 40 | #endif |
| 41 | } |
| 42 | |
| 43 | inline |
| 44 | void throw_from_string_out_of_range( const string& func ) |
| 45 | { |
| 46 | throw_helper<out_of_range>(func + ": out of range"); |
| 47 | } |
| 48 | |
| 49 | inline |
| 50 | void throw_from_string_invalid_arg( const string& func ) |
| 51 | { |
| 52 | throw_helper<invalid_argument>(func + ": no conversion"); |
| 53 | } |
| 54 | |
| 55 | // as_integer |
| 56 | |
| 57 | template<typename V, typename S, typename F> |
| 58 | inline |
| 59 | V |
| 60 | as_integer_helper(const string& func, const S& str, size_t* idx, int base, F f) |
| 61 | { |
Eric Fiselier | d6bd7bf | 2014-11-14 22:23:57 | [diff] [blame] | 62 | typename S::value_type* ptr = nullptr; |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 63 | const typename S::value_type* const p = str.c_str(); |
| 64 | typename remove_reference<decltype(errno)>::type errno_save = errno; |
| 65 | errno = 0; |
| 66 | V r = f(p, &ptr, base); |
| 67 | swap(errno, errno_save); |
| 68 | if (errno_save == ERANGE) |
| 69 | throw_from_string_out_of_range(func); |
| 70 | if (ptr == p) |
| 71 | throw_from_string_invalid_arg(func); |
| 72 | if (idx) |
| 73 | *idx = static_cast<size_t>(ptr - p); |
| 74 | return r; |
| 75 | } |
| 76 | |
| 77 | template<typename V, typename S> |
| 78 | inline |
| 79 | V |
| 80 | as_integer(const string& func, const S& s, size_t* idx, int base); |
| 81 | |
| 82 | // string |
| 83 | template<> |
| 84 | inline |
| 85 | int |
| 86 | as_integer(const string& func, const string& s, size_t* idx, int base ) |
| 87 | { |
Joerg Sonnenberger | 8092c95 | 2013-09-17 08:46:53 | [diff] [blame] | 88 | // Use long as no Standard string to integer exists. |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 89 | long r = as_integer_helper<long>( func, s, idx, base, strtol ); |
| 90 | if (r < numeric_limits<int>::min() || numeric_limits<int>::max() < r) |
| 91 | throw_from_string_out_of_range(func); |
| 92 | return static_cast<int>(r); |
| 93 | } |
| 94 | |
| 95 | template<> |
| 96 | inline |
| 97 | long |
| 98 | as_integer(const string& func, const string& s, size_t* idx, int base ) |
| 99 | { |
| 100 | return as_integer_helper<long>( func, s, idx, base, strtol ); |
| 101 | } |
| 102 | |
| 103 | template<> |
| 104 | inline |
| 105 | unsigned long |
| 106 | as_integer( const string& func, const string& s, size_t* idx, int base ) |
| 107 | { |
| 108 | return as_integer_helper<unsigned long>( func, s, idx, base, strtoul ); |
| 109 | } |
| 110 | |
| 111 | template<> |
| 112 | inline |
| 113 | long long |
| 114 | as_integer( const string& func, const string& s, size_t* idx, int base ) |
| 115 | { |
| 116 | return as_integer_helper<long long>( func, s, idx, base, strtoll ); |
| 117 | } |
| 118 | |
| 119 | template<> |
| 120 | inline |
| 121 | unsigned long long |
| 122 | as_integer( const string& func, const string& s, size_t* idx, int base ) |
| 123 | { |
| 124 | return as_integer_helper<unsigned long long>( func, s, idx, base, strtoull ); |
| 125 | } |
| 126 | |
| 127 | // wstring |
| 128 | template<> |
| 129 | inline |
| 130 | int |
| 131 | as_integer( const string& func, const wstring& s, size_t* idx, int base ) |
| 132 | { |
| 133 | // Use long as no Stantard string to integer exists. |
| 134 | long r = as_integer_helper<long>( func, s, idx, base, wcstol ); |
| 135 | if (r < numeric_limits<int>::min() || numeric_limits<int>::max() < r) |
| 136 | throw_from_string_out_of_range(func); |
| 137 | return static_cast<int>(r); |
| 138 | } |
| 139 | |
| 140 | template<> |
| 141 | inline |
| 142 | long |
| 143 | as_integer( const string& func, const wstring& s, size_t* idx, int base ) |
| 144 | { |
| 145 | return as_integer_helper<long>( func, s, idx, base, wcstol ); |
| 146 | } |
| 147 | |
| 148 | template<> |
| 149 | inline |
| 150 | unsigned long |
| 151 | as_integer( const string& func, const wstring& s, size_t* idx, int base ) |
| 152 | { |
| 153 | return as_integer_helper<unsigned long>( func, s, idx, base, wcstoul ); |
| 154 | } |
| 155 | |
| 156 | template<> |
| 157 | inline |
| 158 | long long |
| 159 | as_integer( const string& func, const wstring& s, size_t* idx, int base ) |
| 160 | { |
| 161 | return as_integer_helper<long long>( func, s, idx, base, wcstoll ); |
| 162 | } |
| 163 | |
| 164 | template<> |
| 165 | inline |
| 166 | unsigned long long |
| 167 | as_integer( const string& func, const wstring& s, size_t* idx, int base ) |
| 168 | { |
| 169 | return as_integer_helper<unsigned long long>( func, s, idx, base, wcstoull ); |
| 170 | } |
| 171 | |
| 172 | // as_float |
| 173 | |
| 174 | template<typename V, typename S, typename F> |
| 175 | inline |
| 176 | V |
| 177 | as_float_helper(const string& func, const S& str, size_t* idx, F f ) |
| 178 | { |
Eric Fiselier | d6bd7bf | 2014-11-14 22:23:57 | [diff] [blame] | 179 | typename S::value_type* ptr = nullptr; |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 180 | const typename S::value_type* const p = str.c_str(); |
| 181 | typename remove_reference<decltype(errno)>::type errno_save = errno; |
| 182 | errno = 0; |
| 183 | V r = f(p, &ptr); |
| 184 | swap(errno, errno_save); |
| 185 | if (errno_save == ERANGE) |
| 186 | throw_from_string_out_of_range(func); |
| 187 | if (ptr == p) |
| 188 | throw_from_string_invalid_arg(func); |
| 189 | if (idx) |
| 190 | *idx = static_cast<size_t>(ptr - p); |
| 191 | return r; |
| 192 | } |
| 193 | |
| 194 | template<typename V, typename S> |
| 195 | inline |
| 196 | V as_float( const string& func, const S& s, size_t* idx = nullptr ); |
| 197 | |
| 198 | template<> |
| 199 | inline |
| 200 | float |
| 201 | as_float( const string& func, const string& s, size_t* idx ) |
| 202 | { |
| 203 | return as_float_helper<float>( func, s, idx, strtof ); |
| 204 | } |
| 205 | |
| 206 | template<> |
| 207 | inline |
| 208 | double |
| 209 | as_float(const string& func, const string& s, size_t* idx ) |
| 210 | { |
| 211 | return as_float_helper<double>( func, s, idx, strtod ); |
| 212 | } |
| 213 | |
| 214 | template<> |
| 215 | inline |
| 216 | long double |
| 217 | as_float( const string& func, const string& s, size_t* idx ) |
| 218 | { |
| 219 | return as_float_helper<long double>( func, s, idx, strtold ); |
| 220 | } |
| 221 | |
| 222 | template<> |
| 223 | inline |
| 224 | float |
| 225 | as_float( const string& func, const wstring& s, size_t* idx ) |
| 226 | { |
| 227 | return as_float_helper<float>( func, s, idx, wcstof ); |
| 228 | } |
| 229 | |
| 230 | template<> |
| 231 | inline |
| 232 | double |
| 233 | as_float( const string& func, const wstring& s, size_t* idx ) |
| 234 | { |
| 235 | return as_float_helper<double>( func, s, idx, wcstod ); |
| 236 | } |
| 237 | |
| 238 | template<> |
| 239 | inline |
| 240 | long double |
| 241 | as_float( const string& func, const wstring& s, size_t* idx ) |
| 242 | { |
| 243 | return as_float_helper<long double>( func, s, idx, wcstold ); |
| 244 | } |
| 245 | |
| 246 | } // unnamed namespace |
| 247 | |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 248 | int |
| 249 | stoi(const string& str, size_t* idx, int base) |
| 250 | { |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 251 | return as_integer<int>( "stoi", str, idx, base ); |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | int |
| 255 | stoi(const wstring& str, size_t* idx, int base) |
| 256 | { |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 257 | return as_integer<int>( "stoi", str, idx, base ); |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | long |
| 261 | stol(const string& str, size_t* idx, int base) |
| 262 | { |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 263 | return as_integer<long>( "stol", str, idx, base ); |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | long |
| 267 | stol(const wstring& str, size_t* idx, int base) |
| 268 | { |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 269 | return as_integer<long>( "stol", str, idx, base ); |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | unsigned long |
| 273 | stoul(const string& str, size_t* idx, int base) |
| 274 | { |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 275 | return as_integer<unsigned long>( "stoul", str, idx, base ); |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | unsigned long |
| 279 | stoul(const wstring& str, size_t* idx, int base) |
| 280 | { |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 281 | return as_integer<unsigned long>( "stoul", str, idx, base ); |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | long long |
| 285 | stoll(const string& str, size_t* idx, int base) |
| 286 | { |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 287 | return as_integer<long long>( "stoll", str, idx, base ); |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | long long |
| 291 | stoll(const wstring& str, size_t* idx, int base) |
| 292 | { |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 293 | return as_integer<long long>( "stoll", str, idx, base ); |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | unsigned long long |
| 297 | stoull(const string& str, size_t* idx, int base) |
| 298 | { |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 299 | return as_integer<unsigned long long>( "stoull", str, idx, base ); |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | unsigned long long |
| 303 | stoull(const wstring& str, size_t* idx, int base) |
| 304 | { |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 305 | return as_integer<unsigned long long>( "stoull", str, idx, base ); |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | float |
| 309 | stof(const string& str, size_t* idx) |
| 310 | { |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 311 | return as_float<float>( "stof", str, idx ); |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | float |
| 315 | stof(const wstring& str, size_t* idx) |
| 316 | { |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 317 | return as_float<float>( "stof", str, idx ); |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | double |
| 321 | stod(const string& str, size_t* idx) |
| 322 | { |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 323 | return as_float<double>( "stod", str, idx ); |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | double |
| 327 | stod(const wstring& str, size_t* idx) |
| 328 | { |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 329 | return as_float<double>( "stod", str, idx ); |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | long double |
| 333 | stold(const string& str, size_t* idx) |
| 334 | { |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 335 | return as_float<long double>( "stold", str, idx ); |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | long double |
| 339 | stold(const wstring& str, size_t* idx) |
| 340 | { |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 341 | return as_float<long double>( "stold", str, idx ); |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 342 | } |
| 343 | |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 344 | // to_string |
| 345 | |
| 346 | namespace |
| 347 | { |
| 348 | |
| 349 | // as_string |
| 350 | |
| 351 | template<typename S, typename P, typename V > |
| 352 | inline |
| 353 | S |
| 354 | as_string(P sprintf_like, S s, const typename S::value_type* fmt, V a) |
| 355 | { |
| 356 | typedef typename S::size_type size_type; |
| 357 | size_type available = s.size(); |
| 358 | while (true) |
| 359 | { |
| 360 | int status = sprintf_like(&s[0], available + 1, fmt, a); |
| 361 | if ( status >= 0 ) |
| 362 | { |
| 363 | size_type used = static_cast<size_type>(status); |
| 364 | if ( used <= available ) |
| 365 | { |
| 366 | s.resize( used ); |
| 367 | break; |
| 368 | } |
| 369 | available = used; // Assume this is advice of how much space we need. |
| 370 | } |
| 371 | else |
| 372 | available = available * 2 + 1; |
| 373 | s.resize(available); |
| 374 | } |
| 375 | return s; |
| 376 | } |
| 377 | |
| 378 | template <class S, class V, bool = is_floating_point<V>::value> |
| 379 | struct initial_string; |
| 380 | |
| 381 | template <class V, bool b> |
| 382 | struct initial_string<string, V, b> |
| 383 | { |
| 384 | string |
| 385 | operator()() const |
| 386 | { |
| 387 | string s; |
| 388 | s.resize(s.capacity()); |
| 389 | return s; |
| 390 | } |
| 391 | }; |
| 392 | |
| 393 | template <class V> |
| 394 | struct initial_string<wstring, V, false> |
| 395 | { |
| 396 | wstring |
| 397 | operator()() const |
| 398 | { |
| 399 | const size_t n = (numeric_limits<unsigned long long>::digits / 3) |
| 400 | + ((numeric_limits<unsigned long long>::digits % 3) != 0) |
| 401 | + 1; |
| 402 | wstring s(n, wchar_t()); |
| 403 | s.resize(s.capacity()); |
| 404 | return s; |
| 405 | } |
| 406 | }; |
| 407 | |
| 408 | template <class V> |
| 409 | struct initial_string<wstring, V, true> |
| 410 | { |
| 411 | wstring |
| 412 | operator()() const |
| 413 | { |
| 414 | wstring s(20, wchar_t()); |
| 415 | s.resize(s.capacity()); |
| 416 | return s; |
| 417 | } |
| 418 | }; |
| 419 | |
| 420 | typedef int (*wide_printf)(wchar_t* __restrict, size_t, const wchar_t*__restrict, ...); |
| 421 | |
| 422 | inline |
| 423 | wide_printf |
| 424 | get_swprintf() |
| 425 | { |
Howard Hinnant | 0be8f64 | 2013-08-01 18:17:34 | [diff] [blame] | 426 | #ifndef _LIBCPP_MSVCRT |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 427 | return swprintf; |
| 428 | #else |
Eric Fiselier | 5d50aa3 | 2017-05-10 20:57:45 | [diff] [blame] | 429 | return static_cast<int (__cdecl*)(wchar_t* __restrict, size_t, const wchar_t*__restrict, ...)>(_snwprintf); |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 430 | #endif |
| 431 | } |
| 432 | |
| 433 | } // unnamed namespace |
| 434 | |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 435 | string to_string(int val) |
| 436 | { |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 437 | return as_string(snprintf, initial_string<string, int>()(), "%d", val); |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | string to_string(unsigned val) |
| 441 | { |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 442 | return as_string(snprintf, initial_string<string, unsigned>()(), "%u", val); |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | string to_string(long val) |
| 446 | { |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 447 | return as_string(snprintf, initial_string<string, long>()(), "%ld", val); |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | string to_string(unsigned long val) |
| 451 | { |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 452 | return as_string(snprintf, initial_string<string, unsigned long>()(), "%lu", val); |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | string to_string(long long val) |
| 456 | { |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 457 | return as_string(snprintf, initial_string<string, long long>()(), "%lld", val); |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | string to_string(unsigned long long val) |
| 461 | { |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 462 | return as_string(snprintf, initial_string<string, unsigned long long>()(), "%llu", val); |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | string to_string(float val) |
| 466 | { |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 467 | return as_string(snprintf, initial_string<string, float>()(), "%f", val); |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | string to_string(double val) |
| 471 | { |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 472 | return as_string(snprintf, initial_string<string, double>()(), "%f", val); |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | string to_string(long double val) |
| 476 | { |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 477 | return as_string(snprintf, initial_string<string, long double>()(), "%Lf", val); |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | wstring to_wstring(int val) |
| 481 | { |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 482 | return as_string(get_swprintf(), initial_string<wstring, int>()(), L"%d", val); |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | wstring to_wstring(unsigned val) |
| 486 | { |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 487 | return as_string(get_swprintf(), initial_string<wstring, unsigned>()(), L"%u", val); |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | wstring to_wstring(long val) |
| 491 | { |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 492 | return as_string(get_swprintf(), initial_string<wstring, long>()(), L"%ld", val); |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | wstring to_wstring(unsigned long val) |
| 496 | { |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 497 | return as_string(get_swprintf(), initial_string<wstring, unsigned long>()(), L"%lu", val); |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | wstring to_wstring(long long val) |
| 501 | { |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 502 | return as_string(get_swprintf(), initial_string<wstring, long long>()(), L"%lld", val); |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | wstring to_wstring(unsigned long long val) |
| 506 | { |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 507 | return as_string(get_swprintf(), initial_string<wstring, unsigned long long>()(), L"%llu", val); |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | wstring to_wstring(float val) |
| 511 | { |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 512 | return as_string(get_swprintf(), initial_string<wstring, float>()(), L"%f", val); |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | wstring to_wstring(double val) |
| 516 | { |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 517 | return as_string(get_swprintf(), initial_string<wstring, double>()(), L"%f", val); |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | wstring to_wstring(long double val) |
| 521 | { |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 522 | return as_string(get_swprintf(), initial_string<wstring, long double>()(), L"%Lf", val); |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 523 | } |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 524 | _LIBCPP_END_NAMESPACE_STD |