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