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