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