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 | 6325184 | 2022-05-24 15:31:31 | [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> |
Louis Dionne | 6325184 | 2022-05-24 15:31:31 | [diff] [blame^] | 65 | inline void throw_helper(const string& msg) { |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 66 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Louis Dionne | 6325184 | 2022-05-24 15:31:31 | [diff] [blame^] | 67 | throw T(msg); |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 68 | #else |
Ed Schouten | c19393c | 2015-03-10 07:57:43 | [diff] [blame] | 69 | fprintf(stderr, "%s\n", msg.c_str()); |
Marshall Clow | d437fa5 | 2016-08-25 15:09:01 | [diff] [blame] | 70 | _VSTD::abort(); |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 71 | #endif |
| 72 | } |
| 73 | |
Louis Dionne | 6325184 | 2022-05-24 15:31:31 | [diff] [blame^] | 74 | inline void throw_from_string_out_of_range(const string& func) { |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 75 | throw_helper<out_of_range>(func + ": out of range"); |
| 76 | } |
| 77 | |
Louis Dionne | 6325184 | 2022-05-24 15:31:31 | [diff] [blame^] | 78 | inline void throw_from_string_invalid_arg(const string& func) { |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 79 | throw_helper<invalid_argument>(func + ": no conversion"); |
| 80 | } |
| 81 | |
| 82 | // as_integer |
| 83 | |
| 84 | template<typename V, typename S, typename F> |
Louis Dionne | 6325184 | 2022-05-24 15:31:31 | [diff] [blame^] | 85 | inline V as_integer_helper(const string& func, const S& str, size_t* idx, int base, F f) { |
Eric Fiselier | d6bd7bf | 2014-11-14 22:23:57 | [diff] [blame] | 86 | typename S::value_type* ptr = nullptr; |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 87 | const typename S::value_type* const p = str.c_str(); |
| 88 | typename remove_reference<decltype(errno)>::type errno_save = errno; |
| 89 | errno = 0; |
| 90 | V r = f(p, &ptr, base); |
| 91 | swap(errno, errno_save); |
| 92 | if (errno_save == ERANGE) |
| 93 | throw_from_string_out_of_range(func); |
| 94 | if (ptr == p) |
| 95 | throw_from_string_invalid_arg(func); |
| 96 | if (idx) |
| 97 | *idx = static_cast<size_t>(ptr - p); |
| 98 | return r; |
| 99 | } |
| 100 | |
| 101 | template<typename V, typename S> |
Louis Dionne | 6325184 | 2022-05-24 15:31:31 | [diff] [blame^] | 102 | 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] | 103 | |
| 104 | // string |
| 105 | template<> |
Louis Dionne | 6325184 | 2022-05-24 15:31:31 | [diff] [blame^] | 106 | inline int as_integer(const string& func, const string& s, size_t* idx, int base) { |
Joerg Sonnenberger | 8092c95 | 2013-09-17 08:46:53 | [diff] [blame] | 107 | // Use long as no Standard string to integer exists. |
Louis Dionne | 6325184 | 2022-05-24 15:31:31 | [diff] [blame^] | 108 | long r = as_integer_helper<long>(func, s, idx, base, strtol); |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 109 | if (r < numeric_limits<int>::min() || numeric_limits<int>::max() < r) |
| 110 | throw_from_string_out_of_range(func); |
| 111 | return static_cast<int>(r); |
| 112 | } |
| 113 | |
| 114 | template<> |
Louis Dionne | 6325184 | 2022-05-24 15:31:31 | [diff] [blame^] | 115 | inline long as_integer(const string& func, const string& s, size_t* idx, int base) { |
| 116 | return as_integer_helper<long>(func, s, idx, base, strtol); |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | template<> |
Louis Dionne | 6325184 | 2022-05-24 15:31:31 | [diff] [blame^] | 120 | inline unsigned long as_integer(const string& func, const string& s, size_t* idx, int base) { |
| 121 | return as_integer_helper<unsigned long>(func, s, idx, base, strtoul); |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | template<> |
Louis Dionne | 6325184 | 2022-05-24 15:31:31 | [diff] [blame^] | 125 | inline long long as_integer(const string& func, const string& s, size_t* idx, int base) { |
| 126 | return as_integer_helper<long long>(func, s, idx, base, strtoll); |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | template<> |
Louis Dionne | 6325184 | 2022-05-24 15:31:31 | [diff] [blame^] | 130 | inline unsigned long long as_integer(const string& func, const string& s, size_t* idx, int base) { |
| 131 | return as_integer_helper<unsigned long long>(func, s, idx, base, strtoull); |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 132 | } |
| 133 | |
Louis Dionne | f4c1258 | 2021-08-23 19:32:36 | [diff] [blame] | 134 | #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 135 | // wstring |
| 136 | template<> |
Louis Dionne | 6325184 | 2022-05-24 15:31:31 | [diff] [blame^] | 137 | inline int as_integer(const string& func, const wstring& s, size_t* idx, int base) { |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 138 | // Use long as no Stantard string to integer exists. |
Louis Dionne | 6325184 | 2022-05-24 15:31:31 | [diff] [blame^] | 139 | long r = as_integer_helper<long>(func, s, idx, base, wcstol); |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 140 | if (r < numeric_limits<int>::min() || numeric_limits<int>::max() < r) |
| 141 | throw_from_string_out_of_range(func); |
| 142 | return static_cast<int>(r); |
| 143 | } |
| 144 | |
| 145 | template<> |
Louis Dionne | 6325184 | 2022-05-24 15:31:31 | [diff] [blame^] | 146 | inline long as_integer(const string& func, const wstring& s, size_t* idx, int base) { |
| 147 | return as_integer_helper<long>(func, s, idx, base, wcstol); |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | template<> |
| 151 | inline |
| 152 | unsigned long |
Louis Dionne | 6325184 | 2022-05-24 15:31:31 | [diff] [blame^] | 153 | as_integer(const string& func, const wstring& s, size_t* idx, int base) |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 154 | { |
Louis Dionne | 6325184 | 2022-05-24 15:31:31 | [diff] [blame^] | 155 | return as_integer_helper<unsigned long>(func, s, idx, base, wcstoul); |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | template<> |
Louis Dionne | 6325184 | 2022-05-24 15:31:31 | [diff] [blame^] | 159 | inline long long as_integer(const string& func, const wstring& s, size_t* idx, int base) { |
| 160 | return as_integer_helper<long long>(func, s, idx, base, wcstoll); |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | template<> |
Louis Dionne | 6325184 | 2022-05-24 15:31:31 | [diff] [blame^] | 164 | inline unsigned long long as_integer(const string& func, const wstring& s, size_t* idx, int base) { |
| 165 | return as_integer_helper<unsigned long long>(func, s, idx, base, wcstoull); |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 166 | } |
Louis Dionne | f4c1258 | 2021-08-23 19:32:36 | [diff] [blame] | 167 | #endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 168 | |
| 169 | // as_float |
| 170 | |
Marshall Clow | 141c2b7 | 2019-06-10 23:20:01 | [diff] [blame] | 171 | template<typename V, typename S, typename F> |
Louis Dionne | 6325184 | 2022-05-24 15:31:31 | [diff] [blame^] | 172 | inline V as_float_helper(const string& func, const S& str, size_t* idx, F f) { |
Eric Fiselier | d6bd7bf | 2014-11-14 22:23:57 | [diff] [blame] | 173 | typename S::value_type* ptr = nullptr; |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 174 | const typename S::value_type* const p = str.c_str(); |
| 175 | typename remove_reference<decltype(errno)>::type errno_save = errno; |
| 176 | errno = 0; |
| 177 | V r = f(p, &ptr); |
| 178 | swap(errno, errno_save); |
| 179 | if (errno_save == ERANGE) |
| 180 | throw_from_string_out_of_range(func); |
| 181 | if (ptr == p) |
| 182 | throw_from_string_invalid_arg(func); |
| 183 | if (idx) |
| 184 | *idx = static_cast<size_t>(ptr - p); |
| 185 | return r; |
| 186 | } |
| 187 | |
| 188 | template<typename V, typename S> |
Louis Dionne | 6325184 | 2022-05-24 15:31:31 | [diff] [blame^] | 189 | 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] | 190 | |
| 191 | template<> |
Louis Dionne | 6325184 | 2022-05-24 15:31:31 | [diff] [blame^] | 192 | inline float as_float(const string& func, const string& s, size_t* idx) { |
| 193 | return as_float_helper<float>(func, s, idx, strtof); |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | template<> |
Louis Dionne | 6325184 | 2022-05-24 15:31:31 | [diff] [blame^] | 197 | inline double as_float(const string& func, const string& s, size_t* idx) { |
| 198 | return as_float_helper<double>(func, s, idx, strtod); |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | template<> |
Louis Dionne | 6325184 | 2022-05-24 15:31:31 | [diff] [blame^] | 202 | inline long double as_float(const string& func, const string& s, size_t* idx) { |
| 203 | return as_float_helper<long double>(func, s, idx, strtold); |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 204 | } |
| 205 | |
Louis Dionne | f4c1258 | 2021-08-23 19:32:36 | [diff] [blame] | 206 | #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 207 | template<> |
Louis Dionne | 6325184 | 2022-05-24 15:31:31 | [diff] [blame^] | 208 | inline float as_float(const string& func, const wstring& s, size_t* idx) { |
| 209 | return as_float_helper<float>(func, s, idx, wcstof); |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | template<> |
Louis Dionne | 6325184 | 2022-05-24 15:31:31 | [diff] [blame^] | 213 | inline double as_float(const string& func, const wstring& s, size_t* idx) { |
| 214 | return as_float_helper<double>(func, s, idx, wcstod); |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | template<> |
Louis Dionne | 6325184 | 2022-05-24 15:31:31 | [diff] [blame^] | 218 | inline long double as_float(const string& func, const wstring& s, size_t* idx) { |
| 219 | return as_float_helper<long double>(func, s, idx, wcstold); |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 220 | } |
Louis Dionne | f4c1258 | 2021-08-23 19:32:36 | [diff] [blame] | 221 | #endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 222 | |
| 223 | } // unnamed namespace |
| 224 | |
Louis Dionne | 6325184 | 2022-05-24 15:31:31 | [diff] [blame^] | 225 | int stoi(const string& str, size_t* idx, int base) { |
| 226 | return as_integer<int>("stoi", str, idx, base); |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 227 | } |
| 228 | |
Louis Dionne | 6325184 | 2022-05-24 15:31:31 | [diff] [blame^] | 229 | long stol(const string& str, size_t* idx, int base) { |
| 230 | return as_integer<long>("stol", str, idx, base); |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 231 | } |
| 232 | |
Louis Dionne | 6325184 | 2022-05-24 15:31:31 | [diff] [blame^] | 233 | unsigned long stoul(const string& str, size_t* idx, int base) { |
| 234 | return as_integer<unsigned long>("stoul", str, idx, base); |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 235 | } |
| 236 | |
Louis Dionne | 6325184 | 2022-05-24 15:31:31 | [diff] [blame^] | 237 | long long stoll(const string& str, size_t* idx, int base) { |
| 238 | return as_integer<long long>("stoll", str, idx, base); |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 239 | } |
| 240 | |
Louis Dionne | 6325184 | 2022-05-24 15:31:31 | [diff] [blame^] | 241 | unsigned long long stoull(const string& str, size_t* idx, int base) { |
| 242 | return as_integer<unsigned long long>("stoull", str, idx, base); |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 243 | } |
| 244 | |
Louis Dionne | 6325184 | 2022-05-24 15:31:31 | [diff] [blame^] | 245 | float stof(const string& str, size_t* idx) { |
| 246 | return as_float<float>("stof", str, idx); |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 247 | } |
| 248 | |
Louis Dionne | 6325184 | 2022-05-24 15:31:31 | [diff] [blame^] | 249 | double stod(const string& str, size_t* idx) { |
| 250 | return as_float<double>("stod", str, idx); |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 251 | } |
| 252 | |
Louis Dionne | 6325184 | 2022-05-24 15:31:31 | [diff] [blame^] | 253 | long double stold(const string& str, size_t* idx) { |
| 254 | return as_float<long double>("stold", str, idx); |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 255 | } |
| 256 | |
Louis Dionne | f4c1258 | 2021-08-23 19:32:36 | [diff] [blame] | 257 | #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS |
Louis Dionne | 6325184 | 2022-05-24 15:31:31 | [diff] [blame^] | 258 | int stoi(const wstring& str, size_t* idx, int base) { |
| 259 | return as_integer<int>("stoi", str, idx, base); |
Louis Dionne | dc8a9a0 | 2022-05-24 15:22:43 | [diff] [blame] | 260 | } |
| 261 | |
Louis Dionne | 6325184 | 2022-05-24 15:31:31 | [diff] [blame^] | 262 | long stol(const wstring& str, size_t* idx, int base) { |
| 263 | return as_integer<long>("stol", str, idx, base); |
Louis Dionne | dc8a9a0 | 2022-05-24 15:22:43 | [diff] [blame] | 264 | } |
| 265 | |
Louis Dionne | 6325184 | 2022-05-24 15:31:31 | [diff] [blame^] | 266 | unsigned long stoul(const wstring& str, size_t* idx, int base) { |
| 267 | return as_integer<unsigned long>("stoul", str, idx, base); |
Louis Dionne | dc8a9a0 | 2022-05-24 15:22:43 | [diff] [blame] | 268 | } |
| 269 | |
Louis Dionne | 6325184 | 2022-05-24 15:31:31 | [diff] [blame^] | 270 | long long stoll(const wstring& str, size_t* idx, int base) { |
| 271 | return as_integer<long long>("stoll", str, idx, base); |
Louis Dionne | dc8a9a0 | 2022-05-24 15:22:43 | [diff] [blame] | 272 | } |
| 273 | |
Louis Dionne | 6325184 | 2022-05-24 15:31:31 | [diff] [blame^] | 274 | unsigned long long stoull(const wstring& str, size_t* idx, int base) { |
| 275 | return as_integer<unsigned long long>("stoull", str, idx, base); |
Louis Dionne | dc8a9a0 | 2022-05-24 15:22:43 | [diff] [blame] | 276 | } |
| 277 | |
Louis Dionne | 6325184 | 2022-05-24 15:31:31 | [diff] [blame^] | 278 | float stof(const wstring& str, size_t* idx) { |
| 279 | return as_float<float>("stof", str, idx); |
Louis Dionne | dc8a9a0 | 2022-05-24 15:22:43 | [diff] [blame] | 280 | } |
| 281 | |
Louis Dionne | 6325184 | 2022-05-24 15:31:31 | [diff] [blame^] | 282 | double stod(const wstring& str, size_t* idx) { |
| 283 | return as_float<double>("stod", str, idx); |
Louis Dionne | dc8a9a0 | 2022-05-24 15:22:43 | [diff] [blame] | 284 | } |
| 285 | |
Louis Dionne | 6325184 | 2022-05-24 15:31:31 | [diff] [blame^] | 286 | long double stold(const wstring& str, size_t* idx) { |
| 287 | return as_float<long double>("stold", str, idx); |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 288 | } |
Louis Dionne | dc8a9a0 | 2022-05-24 15:22:43 | [diff] [blame] | 289 | #endif // !_LIBCPP_HAS_NO_WIDE_CHARACTERS |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 290 | |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 291 | // to_string |
| 292 | |
| 293 | namespace |
| 294 | { |
| 295 | |
| 296 | // as_string |
| 297 | |
| 298 | template<typename S, typename P, typename V > |
Louis Dionne | 6325184 | 2022-05-24 15:31:31 | [diff] [blame^] | 299 | inline S as_string(P sprintf_like, S s, const typename S::value_type* fmt, V a) { |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 300 | typedef typename S::size_type size_type; |
| 301 | size_type available = s.size(); |
Louis Dionne | 6325184 | 2022-05-24 15:31:31 | [diff] [blame^] | 302 | while (true) { |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 303 | int status = sprintf_like(&s[0], available + 1, fmt, a); |
Louis Dionne | 6325184 | 2022-05-24 15:31:31 | [diff] [blame^] | 304 | if (status >= 0) { |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 305 | size_type used = static_cast<size_type>(status); |
Louis Dionne | 6325184 | 2022-05-24 15:31:31 | [diff] [blame^] | 306 | if (used <= available) { |
| 307 | s.resize(used); |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 308 | break; |
| 309 | } |
| 310 | available = used; // Assume this is advice of how much space we need. |
| 311 | } |
| 312 | else |
| 313 | available = available * 2 + 1; |
| 314 | s.resize(available); |
| 315 | } |
| 316 | return s; |
| 317 | } |
| 318 | |
Marshall Clow | 141c2b7 | 2019-06-10 23:20:01 | [diff] [blame] | 319 | template <class S> |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 320 | struct initial_string; |
| 321 | |
Marshall Clow | 141c2b7 | 2019-06-10 23:20:01 | [diff] [blame] | 322 | template <> |
Louis Dionne | 6325184 | 2022-05-24 15:31:31 | [diff] [blame^] | 323 | struct initial_string<string> { |
| 324 | string operator()() const { |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 325 | string s; |
| 326 | s.resize(s.capacity()); |
| 327 | return s; |
| 328 | } |
| 329 | }; |
| 330 | |
Louis Dionne | f4c1258 | 2021-08-23 19:32:36 | [diff] [blame] | 331 | #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS |
Marshall Clow | 141c2b7 | 2019-06-10 23:20:01 | [diff] [blame] | 332 | template <> |
Louis Dionne | 6325184 | 2022-05-24 15:31:31 | [diff] [blame^] | 333 | struct initial_string<wstring> { |
| 334 | wstring operator()() const { |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 335 | wstring s(20, wchar_t()); |
| 336 | s.resize(s.capacity()); |
| 337 | return s; |
| 338 | } |
| 339 | }; |
| 340 | |
| 341 | typedef int (*wide_printf)(wchar_t* __restrict, size_t, const wchar_t*__restrict, ...); |
| 342 | |
Louis Dionne | 6325184 | 2022-05-24 15:31:31 | [diff] [blame^] | 343 | inline wide_printf get_swprintf() { |
Howard Hinnant | 0be8f64 | 2013-08-01 18:17:34 | [diff] [blame] | 344 | #ifndef _LIBCPP_MSVCRT |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 345 | return swprintf; |
| 346 | #else |
Eric Fiselier | 5d50aa3 | 2017-05-10 20:57:45 | [diff] [blame] | 347 | 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] | 348 | #endif |
| 349 | } |
Louis Dionne | f4c1258 | 2021-08-23 19:32:36 | [diff] [blame] | 350 | #endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 351 | |
Marshall Clow | 141c2b7 | 2019-06-10 23:20:01 | [diff] [blame] | 352 | template <typename S, typename V> |
Louis Dionne | 6325184 | 2022-05-24 15:31:31 | [diff] [blame^] | 353 | S i_to_string(V v) { |
Marshall Clow | 141c2b7 | 2019-06-10 23:20:01 | [diff] [blame] | 354 | // numeric_limits::digits10 returns value less on 1 than desired for unsigned numbers. |
| 355 | // For example, for 1-byte unsigned value digits10 is 2 (999 can not be represented), |
| 356 | // so we need +1 here. |
| 357 | constexpr size_t bufsize = numeric_limits<V>::digits10 + 2; // +1 for minus, +1 for digits10 |
| 358 | char buf[bufsize]; |
| 359 | const auto res = to_chars(buf, buf + bufsize, v); |
| 360 | _LIBCPP_ASSERT(res.ec == errc(), "bufsize must be large enough to accomodate the value"); |
| 361 | return S(buf, res.ptr); |
| 362 | } |
| 363 | |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 | [diff] [blame] | 364 | } // unnamed namespace |
| 365 | |
Marshall Clow | 141c2b7 | 2019-06-10 23:20:01 | [diff] [blame] | 366 | string to_string (int val) { return i_to_string< string>(val); } |
| 367 | string to_string (long val) { return i_to_string< string>(val); } |
| 368 | string to_string (long long val) { return i_to_string< string>(val); } |
| 369 | string to_string (unsigned val) { return i_to_string< string>(val); } |
| 370 | string to_string (unsigned long val) { return i_to_string< string>(val); } |
| 371 | string to_string (unsigned long long val) { return i_to_string< string>(val); } |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 372 | |
Louis Dionne | f4c1258 | 2021-08-23 19:32:36 | [diff] [blame] | 373 | #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS |
Marshall Clow | 141c2b7 | 2019-06-10 23:20:01 | [diff] [blame] | 374 | wstring to_wstring(int val) { return i_to_string<wstring>(val); } |
| 375 | wstring to_wstring(long val) { return i_to_string<wstring>(val); } |
| 376 | wstring to_wstring(long long val) { return i_to_string<wstring>(val); } |
| 377 | wstring to_wstring(unsigned val) { return i_to_string<wstring>(val); } |
| 378 | wstring to_wstring(unsigned long val) { return i_to_string<wstring>(val); } |
| 379 | wstring to_wstring(unsigned long long val) { return i_to_string<wstring>(val); } |
Louis Dionne | f4c1258 | 2021-08-23 19:32:36 | [diff] [blame] | 380 | #endif |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 381 | |
Marshall Clow | 141c2b7 | 2019-06-10 23:20:01 | [diff] [blame] | 382 | string to_string (float val) { return as_string(snprintf, initial_string< string>()(), "%f", val); } |
| 383 | string to_string (double val) { return as_string(snprintf, initial_string< string>()(), "%f", val); } |
| 384 | 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] | 385 | |
Louis Dionne | f4c1258 | 2021-08-23 19:32:36 | [diff] [blame] | 386 | #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS |
Marshall Clow | 141c2b7 | 2019-06-10 23:20:01 | [diff] [blame] | 387 | wstring to_wstring(float val) { return as_string(get_swprintf(), initial_string<wstring>()(), L"%f", val); } |
| 388 | wstring to_wstring(double val) { return as_string(get_swprintf(), initial_string<wstring>()(), L"%f", val); } |
| 389 | 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] | 390 | #endif |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 391 | |
Howard Hinnant | cbbf633 | 2010-06-02 18:20:39 | [diff] [blame] | 392 | _LIBCPP_END_NAMESPACE_STD |