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 | |
Louis Dionne | f4c1258 | 2021-08-23 19:32:36 | [diff] [blame] | 291 | #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 292 | int |
| 293 | stoi(const wstring& str, size_t* idx, int base) |
| 294 | { |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 295 | return as_integer<int>( "stoi", str, idx, base ); |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 296 | } |
Louis Dionne | f4c1258 | 2021-08-23 19:32:36 | [diff] [blame] | 297 | #endif |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 298 | |
| 299 | long |
| 300 | stol(const string& str, size_t* idx, int base) |
| 301 | { |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 302 | return as_integer<long>( "stol", str, idx, base ); |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 303 | } |
| 304 | |
Louis Dionne | f4c1258 | 2021-08-23 19:32:36 | [diff] [blame] | 305 | #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 306 | long |
| 307 | stol(const wstring& str, size_t* idx, int base) |
| 308 | { |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 309 | return as_integer<long>( "stol", str, idx, base ); |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 310 | } |
Louis Dionne | f4c1258 | 2021-08-23 19:32:36 | [diff] [blame] | 311 | #endif |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 312 | |
| 313 | unsigned long |
| 314 | stoul(const string& str, size_t* idx, int base) |
| 315 | { |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 316 | return as_integer<unsigned long>( "stoul", str, idx, base ); |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 317 | } |
| 318 | |
Louis Dionne | f4c1258 | 2021-08-23 19:32:36 | [diff] [blame] | 319 | #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 320 | unsigned long |
| 321 | stoul(const wstring& str, size_t* idx, int base) |
| 322 | { |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 323 | return as_integer<unsigned long>( "stoul", str, idx, base ); |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 324 | } |
Louis Dionne | f4c1258 | 2021-08-23 19:32:36 | [diff] [blame] | 325 | #endif |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 326 | |
| 327 | long long |
| 328 | stoll(const string& str, size_t* idx, int base) |
| 329 | { |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 330 | return as_integer<long long>( "stoll", str, idx, base ); |
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 |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 334 | long long |
| 335 | stoll(const wstring& str, size_t* idx, int base) |
| 336 | { |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 337 | return as_integer<long long>( "stoll", str, idx, base ); |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 338 | } |
Louis Dionne | f4c1258 | 2021-08-23 19:32:36 | [diff] [blame] | 339 | #endif |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 340 | |
| 341 | unsigned long long |
| 342 | stoull(const string& str, size_t* idx, int base) |
| 343 | { |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 344 | return as_integer<unsigned long long>( "stoull", str, idx, base ); |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 345 | } |
| 346 | |
Louis Dionne | f4c1258 | 2021-08-23 19:32:36 | [diff] [blame] | 347 | #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 348 | unsigned long long |
| 349 | stoull(const wstring& str, size_t* idx, int base) |
| 350 | { |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 351 | return as_integer<unsigned long long>( "stoull", str, idx, base ); |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 352 | } |
Louis Dionne | f4c1258 | 2021-08-23 19:32:36 | [diff] [blame] | 353 | #endif |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 354 | |
| 355 | float |
| 356 | stof(const string& str, size_t* idx) |
| 357 | { |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 358 | return as_float<float>( "stof", str, idx ); |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 359 | } |
| 360 | |
Louis Dionne | f4c1258 | 2021-08-23 19:32:36 | [diff] [blame] | 361 | #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 362 | float |
| 363 | stof(const wstring& str, size_t* idx) |
| 364 | { |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 365 | return as_float<float>( "stof", str, idx ); |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 366 | } |
Louis Dionne | f4c1258 | 2021-08-23 19:32:36 | [diff] [blame] | 367 | #endif |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 368 | |
| 369 | double |
| 370 | stod(const string& str, size_t* idx) |
| 371 | { |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 372 | return as_float<double>( "stod", str, idx ); |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 373 | } |
| 374 | |
Louis Dionne | f4c1258 | 2021-08-23 19:32:36 | [diff] [blame] | 375 | #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 376 | double |
| 377 | stod(const wstring& str, size_t* idx) |
| 378 | { |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 379 | return as_float<double>( "stod", str, idx ); |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 380 | } |
Louis Dionne | f4c1258 | 2021-08-23 19:32:36 | [diff] [blame] | 381 | #endif |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 382 | |
| 383 | long double |
| 384 | stold(const string& str, size_t* idx) |
| 385 | { |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 386 | return as_float<long double>( "stold", str, idx ); |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 387 | } |
| 388 | |
Louis Dionne | f4c1258 | 2021-08-23 19:32:36 | [diff] [blame] | 389 | #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 390 | long double |
| 391 | stold(const wstring& str, size_t* idx) |
| 392 | { |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 393 | return as_float<long double>( "stold", str, idx ); |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 394 | } |
Louis Dionne | f4c1258 | 2021-08-23 19:32:36 | [diff] [blame] | 395 | #endif |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 396 | |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 397 | // to_string |
| 398 | |
| 399 | namespace |
| 400 | { |
| 401 | |
| 402 | // as_string |
| 403 | |
| 404 | template<typename S, typename P, typename V > |
| 405 | inline |
| 406 | S |
| 407 | as_string(P sprintf_like, S s, const typename S::value_type* fmt, V a) |
| 408 | { |
| 409 | typedef typename S::size_type size_type; |
| 410 | size_type available = s.size(); |
| 411 | while (true) |
| 412 | { |
| 413 | int status = sprintf_like(&s[0], available + 1, fmt, a); |
| 414 | if ( status >= 0 ) |
| 415 | { |
| 416 | size_type used = static_cast<size_type>(status); |
| 417 | if ( used <= available ) |
| 418 | { |
| 419 | s.resize( used ); |
| 420 | break; |
| 421 | } |
| 422 | available = used; // Assume this is advice of how much space we need. |
| 423 | } |
| 424 | else |
| 425 | available = available * 2 + 1; |
| 426 | s.resize(available); |
| 427 | } |
| 428 | return s; |
| 429 | } |
| 430 | |
Marshall Clow | 141c2b7 | 2019-06-10 23:20:01 | [diff] [blame] | 431 | template <class S> |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 432 | struct initial_string; |
| 433 | |
Marshall Clow | 141c2b7 | 2019-06-10 23:20:01 | [diff] [blame] | 434 | template <> |
| 435 | struct initial_string<string> |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 436 | { |
| 437 | string |
| 438 | operator()() const |
| 439 | { |
| 440 | string s; |
| 441 | s.resize(s.capacity()); |
| 442 | return s; |
| 443 | } |
| 444 | }; |
| 445 | |
Louis Dionne | f4c1258 | 2021-08-23 19:32:36 | [diff] [blame] | 446 | #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS |
Marshall Clow | 141c2b7 | 2019-06-10 23:20:01 | [diff] [blame] | 447 | template <> |
| 448 | struct initial_string<wstring> |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 449 | { |
| 450 | wstring |
| 451 | operator()() const |
| 452 | { |
| 453 | wstring s(20, wchar_t()); |
| 454 | s.resize(s.capacity()); |
| 455 | return s; |
| 456 | } |
| 457 | }; |
| 458 | |
| 459 | typedef int (*wide_printf)(wchar_t* __restrict, size_t, const wchar_t*__restrict, ...); |
| 460 | |
| 461 | inline |
| 462 | wide_printf |
| 463 | get_swprintf() |
| 464 | { |
Howard Hinnant | 0be8f64 | 2013-08-01 18:17:34 | [diff] [blame] | 465 | #ifndef _LIBCPP_MSVCRT |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 466 | return swprintf; |
| 467 | #else |
Eric Fiselier | 5d50aa3 | 2017-05-10 20:57:45 | [diff] [blame] | 468 | 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] | 469 | #endif |
| 470 | } |
Louis Dionne | f4c1258 | 2021-08-23 19:32:36 | [diff] [blame] | 471 | #endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 472 | |
Marshall Clow | 141c2b7 | 2019-06-10 23:20:01 | [diff] [blame] | 473 | template <typename S, typename V> |
Louis Dionne | a562853 | 2021-06-29 17:52:26 | [diff] [blame] | 474 | S i_to_string(V v) |
Marshall Clow | 141c2b7 | 2019-06-10 23:20:01 | [diff] [blame] | 475 | { |
| 476 | // numeric_limits::digits10 returns value less on 1 than desired for unsigned numbers. |
| 477 | // For example, for 1-byte unsigned value digits10 is 2 (999 can not be represented), |
| 478 | // so we need +1 here. |
| 479 | constexpr size_t bufsize = numeric_limits<V>::digits10 + 2; // +1 for minus, +1 for digits10 |
| 480 | char buf[bufsize]; |
| 481 | const auto res = to_chars(buf, buf + bufsize, v); |
| 482 | _LIBCPP_ASSERT(res.ec == errc(), "bufsize must be large enough to accomodate the value"); |
| 483 | return S(buf, res.ptr); |
| 484 | } |
| 485 | |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 486 | } // unnamed namespace |
| 487 | |
Marshall Clow | 141c2b7 | 2019-06-10 23:20:01 | [diff] [blame] | 488 | string to_string (int val) { return i_to_string< string>(val); } |
| 489 | string to_string (long val) { return i_to_string< string>(val); } |
| 490 | string to_string (long long val) { return i_to_string< string>(val); } |
| 491 | string to_string (unsigned val) { return i_to_string< string>(val); } |
| 492 | string to_string (unsigned long val) { return i_to_string< string>(val); } |
| 493 | string to_string (unsigned long long val) { return i_to_string< string>(val); } |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 494 | |
Louis Dionne | f4c1258 | 2021-08-23 19:32:36 | [diff] [blame] | 495 | #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS |
Marshall Clow | 141c2b7 | 2019-06-10 23:20:01 | [diff] [blame] | 496 | wstring to_wstring(int val) { return i_to_string<wstring>(val); } |
| 497 | wstring to_wstring(long val) { return i_to_string<wstring>(val); } |
| 498 | wstring to_wstring(long long val) { return i_to_string<wstring>(val); } |
| 499 | wstring to_wstring(unsigned val) { return i_to_string<wstring>(val); } |
| 500 | wstring to_wstring(unsigned long val) { return i_to_string<wstring>(val); } |
| 501 | wstring to_wstring(unsigned long long val) { return i_to_string<wstring>(val); } |
Louis Dionne | f4c1258 | 2021-08-23 19:32:36 | [diff] [blame] | 502 | #endif |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 503 | |
Marshall Clow | 141c2b7 | 2019-06-10 23:20:01 | [diff] [blame] | 504 | string to_string (float val) { return as_string(snprintf, initial_string< string>()(), "%f", val); } |
| 505 | string to_string (double val) { return as_string(snprintf, initial_string< string>()(), "%f", val); } |
| 506 | 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] | 507 | |
Louis Dionne | f4c1258 | 2021-08-23 19:32:36 | [diff] [blame] | 508 | #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS |
Marshall Clow | 141c2b7 | 2019-06-10 23:20:01 | [diff] [blame] | 509 | wstring to_wstring(float val) { return as_string(get_swprintf(), initial_string<wstring>()(), L"%f", val); } |
| 510 | wstring to_wstring(double val) { return as_string(get_swprintf(), initial_string<wstring>()(), L"%f", val); } |
| 511 | 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] | 512 | #endif |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 513 | |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 514 | _LIBCPP_END_NAMESPACE_STD |