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