| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 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 |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include <__assert> |
| 10 | #include <cerrno> |
| 11 | #include <charconv> |
| 12 | #include <cstdlib> |
| 13 | #include <limits> |
| 14 | #include <stdexcept> |
| 15 | #include <string> |
| 16 | |
| 17 | #if _LIBCPP_HAS_WIDE_CHARACTERS |
| 18 | # include <cwchar> |
| 19 | #endif |
| 20 | |
| 21 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 22 | |
| 23 | #if _LIBCPP_AVAILABILITY_MINIMUM_HEADER_VERSION < 14 |
| 24 | |
| 25 | template <bool> |
| 26 | struct __basic_string_common; |
| 27 | |
| 28 | // The struct isn't declared anymore in the headers. It's only here for ABI compatibility. |
| 29 | template <> |
| 30 | struct __basic_string_common<true> { |
| 31 | [[noreturn]] _LIBCPP_EXPORTED_FROM_ABI void __throw_length_error() const; |
| 32 | [[noreturn]] _LIBCPP_EXPORTED_FROM_ABI void __throw_out_of_range() const; |
| 33 | }; |
| 34 | |
| 35 | void __basic_string_common<true>::__throw_length_error() const { std::__throw_length_error(msg: "basic_string" ); } |
| 36 | void __basic_string_common<true>::__throw_out_of_range() const { std::__throw_out_of_range(msg: "basic_string" ); } |
| 37 | |
| 38 | #endif // _LIBCPP_AVAILABILITY_MINIMUM_HEADER_VERSION < 14 |
| 39 | |
| 40 | // Define legacy ABI functions |
| 41 | // --------------------------- |
| 42 | |
| 43 | #if _LIBCPP_AVAILABILITY_MINIMUM_HEADER_VERSION < 21 |
| 44 | |
| 45 | // This initializes the string with [__s, __s + __sz), but capacity() == __reserve. Assumes that __reserve >= __sz. |
| 46 | template <class _CharT, class _Traits, class _Allocator> |
| 47 | void basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_type __sz, size_type __reserve) { |
| 48 | pointer __p = __init_internal_buffer(size: __reserve); |
| 49 | __annotate_delete(); |
| 50 | __set_size(s: __sz); |
| 51 | traits_type::copy(std::__to_address(__p), __s, __sz); |
| 52 | traits_type::assign(__p[__sz], value_type()); |
| 53 | __annotate_new(current_size: __sz); |
| 54 | } |
| 55 | |
| 56 | template _LIBCPP_EXPORTED_FROM_ABI void basic_string<char>::__init(const value_type*, size_type, size_type); |
| 57 | # if _LIBCPP_HAS_WIDE_CHARACTERS |
| 58 | template _LIBCPP_EXPORTED_FROM_ABI void basic_string<wchar_t>::__init(const value_type*, size_type, size_type); |
| 59 | # endif |
| 60 | |
| 61 | #endif // _LIBCPP_AVAILABILITY_MINIMUM_HEADER_VERSION < 21 |
| 62 | |
| 63 | #define _LIBCPP_EXTERN_TEMPLATE_DEFINE(...) template _LIBCPP_EXPORTED_FROM_ABI __VA_ARGS__; |
| 64 | #ifdef _LIBCPP_ABI_STRING_OPTIMIZED_EXTERNAL_INSTANTIATION |
| 65 | _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE_DEFINE, char) |
| 66 | # if _LIBCPP_HAS_WIDE_CHARACTERS |
| 67 | _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE_DEFINE, wchar_t) |
| 68 | # endif |
| 69 | #else |
| 70 | _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE_DEFINE, char) |
| 71 | # if _LIBCPP_HAS_WIDE_CHARACTERS |
| 72 | _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE_DEFINE, wchar_t) |
| 73 | # endif |
| 74 | #endif |
| 75 | #undef _LIBCPP_EXTERN_TEMPLATE_DEFINE |
| 76 | |
| 77 | template string operator+ <char, char_traits<char>, allocator<char>>(char const*, string const&); |
| 78 | |
| 79 | namespace { |
| 80 | |
| 81 | inline void throw_from_string_out_of_range(const string& func) { |
| 82 | std::__throw_out_of_range(msg: (func + ": out of range" ).c_str()); |
| 83 | } |
| 84 | |
| 85 | inline void throw_from_string_invalid_arg(const string& func) { |
| 86 | std::__throw_invalid_argument(msg: (func + ": no conversion" ).c_str()); |
| 87 | } |
| 88 | |
| 89 | // as_integer |
| 90 | |
| 91 | template <typename V, typename S, typename F> |
| 92 | inline V as_integer_helper(const string& func, const S& str, size_t* idx, int base, F f) { |
| 93 | typename S::value_type* ptr = nullptr; |
| 94 | const typename S::value_type* const p = str.c_str(); |
| 95 | __libcpp_remove_reference_t<decltype(errno)> errno_save = errno; |
| 96 | errno = 0; |
| 97 | V r = f(p, &ptr, base); |
| 98 | swap(errno, y&: errno_save); |
| 99 | if (errno_save == ERANGE) |
| 100 | throw_from_string_out_of_range(func); |
| 101 | if (ptr == p) |
| 102 | throw_from_string_invalid_arg(func); |
| 103 | if (idx) |
| 104 | *idx = static_cast<size_t>(ptr - p); |
| 105 | return r; |
| 106 | } |
| 107 | |
| 108 | template <typename V, typename S> |
| 109 | inline V as_integer(const string& func, const S& s, size_t* idx, int base); |
| 110 | |
| 111 | // string |
| 112 | template <> |
| 113 | inline int as_integer(const string& func, const string& s, size_t* idx, int base) { |
| 114 | // Use long as no Standard string to integer exists. |
| 115 | long r = as_integer_helper<long>(func, str: s, idx, base, f: strtol); |
| 116 | if (r < numeric_limits<int>::min() || numeric_limits<int>::max() < r) |
| 117 | throw_from_string_out_of_range(func); |
| 118 | return static_cast<int>(r); |
| 119 | } |
| 120 | |
| 121 | template <> |
| 122 | inline long as_integer(const string& func, const string& s, size_t* idx, int base) { |
| 123 | return as_integer_helper<long>(func, str: s, idx, base, f: strtol); |
| 124 | } |
| 125 | |
| 126 | template <> |
| 127 | inline unsigned long as_integer(const string& func, const string& s, size_t* idx, int base) { |
| 128 | return as_integer_helper<unsigned long>(func, str: s, idx, base, f: strtoul); |
| 129 | } |
| 130 | |
| 131 | template <> |
| 132 | inline long long as_integer(const string& func, const string& s, size_t* idx, int base) { |
| 133 | return as_integer_helper<long long>(func, str: s, idx, base, f: strtoll); |
| 134 | } |
| 135 | |
| 136 | template <> |
| 137 | inline unsigned long long as_integer(const string& func, const string& s, size_t* idx, int base) { |
| 138 | return as_integer_helper<unsigned long long>(func, str: s, idx, base, f: strtoull); |
| 139 | } |
| 140 | |
| 141 | #if _LIBCPP_HAS_WIDE_CHARACTERS |
| 142 | // wstring |
| 143 | template <> |
| 144 | inline int as_integer(const string& func, const wstring& s, size_t* idx, int base) { |
| 145 | // Use long as no Stantard string to integer exists. |
| 146 | long r = as_integer_helper<long>(func, str: s, idx, base, f: wcstol); |
| 147 | if (r < numeric_limits<int>::min() || numeric_limits<int>::max() < r) |
| 148 | throw_from_string_out_of_range(func); |
| 149 | return static_cast<int>(r); |
| 150 | } |
| 151 | |
| 152 | template <> |
| 153 | inline long as_integer(const string& func, const wstring& s, size_t* idx, int base) { |
| 154 | return as_integer_helper<long>(func, str: s, idx, base, f: wcstol); |
| 155 | } |
| 156 | |
| 157 | template <> |
| 158 | inline unsigned long as_integer(const string& func, const wstring& s, size_t* idx, int base) { |
| 159 | return as_integer_helper<unsigned long>(func, str: s, idx, base, f: wcstoul); |
| 160 | } |
| 161 | |
| 162 | template <> |
| 163 | inline long long as_integer(const string& func, const wstring& s, size_t* idx, int base) { |
| 164 | return as_integer_helper<long long>(func, str: s, idx, base, f: wcstoll); |
| 165 | } |
| 166 | |
| 167 | template <> |
| 168 | inline unsigned long long as_integer(const string& func, const wstring& s, size_t* idx, int base) { |
| 169 | return as_integer_helper<unsigned long long>(func, str: s, idx, base, f: wcstoull); |
| 170 | } |
| 171 | #endif // _LIBCPP_HAS_WIDE_CHARACTERS |
| 172 | |
| 173 | // as_float |
| 174 | |
| 175 | template <typename V, typename S, typename F> |
| 176 | inline V as_float_helper(const string& func, const S& str, size_t* idx, F f) { |
| 177 | typename S::value_type* ptr = nullptr; |
| 178 | const typename S::value_type* const p = str.c_str(); |
| 179 | __libcpp_remove_reference_t<decltype(errno)> errno_save = errno; |
| 180 | errno = 0; |
| 181 | V r = f(p, &ptr); |
| 182 | swap(errno, y&: errno_save); |
| 183 | if (errno_save == ERANGE) |
| 184 | throw_from_string_out_of_range(func); |
| 185 | if (ptr == p) |
| 186 | throw_from_string_invalid_arg(func); |
| 187 | if (idx) |
| 188 | *idx = static_cast<size_t>(ptr - p); |
| 189 | return r; |
| 190 | } |
| 191 | |
| 192 | template <typename V, typename S> |
| 193 | inline V as_float(const string& func, const S& s, size_t* idx = nullptr); |
| 194 | |
| 195 | template <> |
| 196 | inline float as_float(const string& func, const string& s, size_t* idx) { |
| 197 | return as_float_helper<float>(func, str: s, idx, f: strtof); |
| 198 | } |
| 199 | |
| 200 | template <> |
| 201 | inline double as_float(const string& func, const string& s, size_t* idx) { |
| 202 | return as_float_helper<double>(func, str: s, idx, f: strtod); |
| 203 | } |
| 204 | |
| 205 | template <> |
| 206 | inline long double as_float(const string& func, const string& s, size_t* idx) { |
| 207 | return as_float_helper<long double>(func, str: s, idx, f: strtold); |
| 208 | } |
| 209 | |
| 210 | #if _LIBCPP_HAS_WIDE_CHARACTERS |
| 211 | template <> |
| 212 | inline float as_float(const string& func, const wstring& s, size_t* idx) { |
| 213 | return as_float_helper<float>(func, str: s, idx, f: wcstof); |
| 214 | } |
| 215 | |
| 216 | template <> |
| 217 | inline double as_float(const string& func, const wstring& s, size_t* idx) { |
| 218 | return as_float_helper<double>(func, str: s, idx, f: wcstod); |
| 219 | } |
| 220 | |
| 221 | template <> |
| 222 | inline long double as_float(const string& func, const wstring& s, size_t* idx) { |
| 223 | return as_float_helper<long double>(func, str: s, idx, f: wcstold); |
| 224 | } |
| 225 | #endif // _LIBCPP_HAS_WIDE_CHARACTERS |
| 226 | |
| 227 | } // unnamed namespace |
| 228 | |
| 229 | int stoi(const string& str, size_t* idx, int base) { return as_integer<int>(func: "stoi" , s: str, idx, base); } |
| 230 | |
| 231 | long stol(const string& str, size_t* idx, int base) { return as_integer<long>(func: "stol" , s: str, idx, base); } |
| 232 | |
| 233 | unsigned long stoul(const string& str, size_t* idx, int base) { |
| 234 | return as_integer<unsigned long>(func: "stoul" , s: str, idx, base); |
| 235 | } |
| 236 | |
| 237 | long long stoll(const string& str, size_t* idx, int base) { return as_integer<long long>(func: "stoll" , s: str, idx, base); } |
| 238 | |
| 239 | unsigned long long stoull(const string& str, size_t* idx, int base) { |
| 240 | return as_integer<unsigned long long>(func: "stoull" , s: str, idx, base); |
| 241 | } |
| 242 | |
| 243 | float stof(const string& str, size_t* idx) { return as_float<float>(func: "stof" , s: str, idx); } |
| 244 | |
| 245 | double stod(const string& str, size_t* idx) { return as_float<double>(func: "stod" , s: str, idx); } |
| 246 | |
| 247 | long double stold(const string& str, size_t* idx) { return as_float<long double>(func: "stold" , s: str, idx); } |
| 248 | |
| 249 | #if _LIBCPP_HAS_WIDE_CHARACTERS |
| 250 | int stoi(const wstring& str, size_t* idx, int base) { return as_integer<int>(func: "stoi" , s: str, idx, base); } |
| 251 | |
| 252 | long stol(const wstring& str, size_t* idx, int base) { return as_integer<long>(func: "stol" , s: str, idx, base); } |
| 253 | |
| 254 | unsigned long stoul(const wstring& str, size_t* idx, int base) { |
| 255 | return as_integer<unsigned long>(func: "stoul" , s: str, idx, base); |
| 256 | } |
| 257 | |
| 258 | long long stoll(const wstring& str, size_t* idx, int base) { return as_integer<long long>(func: "stoll" , s: str, idx, base); } |
| 259 | |
| 260 | unsigned long long stoull(const wstring& str, size_t* idx, int base) { |
| 261 | return as_integer<unsigned long long>(func: "stoull" , s: str, idx, base); |
| 262 | } |
| 263 | |
| 264 | float stof(const wstring& str, size_t* idx) { return as_float<float>(func: "stof" , s: str, idx); } |
| 265 | |
| 266 | double stod(const wstring& str, size_t* idx) { return as_float<double>(func: "stod" , s: str, idx); } |
| 267 | |
| 268 | long double stold(const wstring& str, size_t* idx) { return as_float<long double>(func: "stold" , s: str, idx); } |
| 269 | #endif // _LIBCPP_HAS_WIDE_CHARACTERS |
| 270 | |
| 271 | // to_string |
| 272 | |
| 273 | namespace { |
| 274 | |
| 275 | // as_string |
| 276 | |
| 277 | template <typename S, typename P, typename V > |
| 278 | inline S as_string(P sprintf_like, S s, const typename S::value_type* fmt, V a) { |
| 279 | typedef typename S::size_type size_type; |
| 280 | size_type available = s.size(); |
| 281 | while (true) { |
| 282 | int status = sprintf_like(&s[0], available + 1, fmt, a); |
| 283 | if (status >= 0) { |
| 284 | size_type used = static_cast<size_type>(status); |
| 285 | if (used <= available) { |
| 286 | s.resize(used); |
| 287 | break; |
| 288 | } |
| 289 | available = used; // Assume this is advice of how much space we need. |
| 290 | } else |
| 291 | available = available * 2 + 1; |
| 292 | s.resize(available); |
| 293 | } |
| 294 | return s; |
| 295 | } |
| 296 | |
| 297 | template <class S> |
| 298 | struct initial_string; |
| 299 | |
| 300 | template <> |
| 301 | struct initial_string<string> { |
| 302 | string operator()() const { |
| 303 | string s; |
| 304 | s.resize(n: s.capacity()); |
| 305 | return s; |
| 306 | } |
| 307 | }; |
| 308 | |
| 309 | #if _LIBCPP_HAS_WIDE_CHARACTERS |
| 310 | template <> |
| 311 | struct initial_string<wstring> { |
| 312 | wstring operator()() const { |
| 313 | wstring s(20, wchar_t()); |
| 314 | s.resize(n: s.capacity()); |
| 315 | return s; |
| 316 | } |
| 317 | }; |
| 318 | |
| 319 | typedef int (*wide_printf)(wchar_t* __restrict, size_t, const wchar_t* __restrict, ...); |
| 320 | |
| 321 | inline wide_printf get_swprintf() { |
| 322 | # ifndef _LIBCPP_MSVCRT |
| 323 | return swprintf; |
| 324 | # else |
| 325 | return static_cast<int(__cdecl*)(wchar_t* __restrict, size_t, const wchar_t* __restrict, ...)>(_snwprintf); |
| 326 | # endif |
| 327 | } |
| 328 | #endif // _LIBCPP_HAS_WIDE_CHARACTERS |
| 329 | |
| 330 | template <typename S, typename V> |
| 331 | S i_to_string(V v) { |
| 332 | // numeric_limits::digits10 returns value less on 1 than desired for unsigned numbers. |
| 333 | // For example, for 1-byte unsigned value digits10 is 2 (999 can not be represented), |
| 334 | // so we need +1 here. |
| 335 | constexpr size_t bufsize = numeric_limits<V>::digits10 + 2; // +1 for minus, +1 for digits10 |
| 336 | char buf[bufsize]; |
| 337 | const auto res = to_chars(buf, buf + bufsize, v); |
| 338 | _LIBCPP_ASSERT_INTERNAL(res.ec == errc(), "bufsize must be large enough to accomodate the value" ); |
| 339 | return S(buf, res.ptr); |
| 340 | } |
| 341 | |
| 342 | } // unnamed namespace |
| 343 | |
| 344 | string to_string(int val) { return i_to_string< string>(v: val); } |
| 345 | string to_string(long val) { return i_to_string< string>(v: val); } |
| 346 | string to_string(long long val) { return i_to_string< string>(v: val); } |
| 347 | string to_string(unsigned val) { return i_to_string< string>(v: val); } |
| 348 | string to_string(unsigned long val) { return i_to_string< string>(v: val); } |
| 349 | string to_string(unsigned long long val) { return i_to_string< string>(v: val); } |
| 350 | |
| 351 | #if _LIBCPP_HAS_WIDE_CHARACTERS |
| 352 | wstring to_wstring(int val) { return i_to_string<wstring>(v: val); } |
| 353 | wstring to_wstring(long val) { return i_to_string<wstring>(v: val); } |
| 354 | wstring to_wstring(long long val) { return i_to_string<wstring>(v: val); } |
| 355 | wstring to_wstring(unsigned val) { return i_to_string<wstring>(v: val); } |
| 356 | wstring to_wstring(unsigned long val) { return i_to_string<wstring>(v: val); } |
| 357 | wstring to_wstring(unsigned long long val) { return i_to_string<wstring>(v: val); } |
| 358 | #endif |
| 359 | |
| 360 | string to_string(float val) { return as_string(sprintf_like: snprintf, s: initial_string< string>()(), fmt: "%f" , a: val); } |
| 361 | string to_string(double val) { return as_string(sprintf_like: snprintf, s: initial_string< string>()(), fmt: "%f" , a: val); } |
| 362 | string to_string(long double val) { return as_string(sprintf_like: snprintf, s: initial_string< string>()(), fmt: "%Lf" , a: val); } |
| 363 | |
| 364 | #if _LIBCPP_HAS_WIDE_CHARACTERS |
| 365 | wstring to_wstring(float val) { return as_string(sprintf_like: get_swprintf(), s: initial_string<wstring>()(), fmt: L"%f" , a: val); } |
| 366 | wstring to_wstring(double val) { return as_string(sprintf_like: get_swprintf(), s: initial_string<wstring>()(), fmt: L"%f" , a: val); } |
| 367 | wstring to_wstring(long double val) { return as_string(sprintf_like: get_swprintf(), s: initial_string<wstring>()(), fmt: L"%Lf" , a: val); } |
| 368 | #endif |
| 369 | |
| 370 | _LIBCPP_END_NAMESPACE_STD |
| 371 | |