string.cpp (232924) | string.cpp (246468) |
---|---|
1//===------------------------- string.cpp ---------------------------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is dual licensed under the MIT and the University of Illinois Open 6// Source Licenses. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// --- 17 unchanged lines hidden (view full) --- 26 string 27 operator+<char, char_traits<char>, allocator<char> >(char const*, string const&); 28 29int 30stoi(const string& str, size_t* idx, int base) 31{ 32 char* ptr; 33 const char* const p = str.c_str(); | 1//===------------------------- string.cpp ---------------------------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is dual licensed under the MIT and the University of Illinois Open 6// Source Licenses. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// --- 17 unchanged lines hidden (view full) --- 26 string 27 operator+<char, char_traits<char>, allocator<char> >(char const*, string const&); 28 29int 30stoi(const string& str, size_t* idx, int base) 31{ 32 char* ptr; 33 const char* const p = str.c_str(); |
34 typename remove_reference<decltype(errno)>::type errno_save = errno; 35 errno = 0; |
|
34 long r = strtol(p, &ptr, base); | 36 long r = strtol(p, &ptr, base); |
35 if (r < numeric_limits<int>::min() || numeric_limits<int>::max() < r) 36 ptr = const_cast<char*>(p); 37 if (ptr == p) 38 { | 37 swap(errno, errno_save); |
39#ifndef _LIBCPP_NO_EXCEPTIONS | 38#ifndef _LIBCPP_NO_EXCEPTIONS |
40 if (r == 0) 41 throw invalid_argument("stoi: no conversion"); | 39 if (errno_save == ERANGE || r < numeric_limits<int>::min() || 40 numeric_limits<int>::max() < r) |
42 throw out_of_range("stoi: out of range"); | 41 throw out_of_range("stoi: out of range"); |
42 if (ptr == p) 43 throw invalid_argument("stoi: no conversion"); |
|
43#endif // _LIBCPP_NO_EXCEPTIONS | 44#endif // _LIBCPP_NO_EXCEPTIONS |
44 } | |
45 if (idx) 46 *idx = static_cast<size_t>(ptr - p); 47 return static_cast<int>(r); 48} 49 50int 51stoi(const wstring& str, size_t* idx, int base) 52{ 53 wchar_t* ptr; 54 const wchar_t* const p = str.c_str(); | 45 if (idx) 46 *idx = static_cast<size_t>(ptr - p); 47 return static_cast<int>(r); 48} 49 50int 51stoi(const wstring& str, size_t* idx, int base) 52{ 53 wchar_t* ptr; 54 const wchar_t* const p = str.c_str(); |
55 typename remove_reference<decltype(errno)>::type errno_save = errno; 56 errno = 0; |
|
55 long r = wcstol(p, &ptr, base); | 57 long r = wcstol(p, &ptr, base); |
56 if (r < numeric_limits<int>::min() || numeric_limits<int>::max() < r) 57 ptr = const_cast<wchar_t*>(p); 58 if (ptr == p) 59 { | 58 swap(errno, errno_save); |
60#ifndef _LIBCPP_NO_EXCEPTIONS | 59#ifndef _LIBCPP_NO_EXCEPTIONS |
61 if (r == 0) 62 throw invalid_argument("stoi: no conversion"); | 60 if (errno_save == ERANGE || r < numeric_limits<int>::min() || 61 numeric_limits<int>::max() < r) |
63 throw out_of_range("stoi: out of range"); | 62 throw out_of_range("stoi: out of range"); |
63 if (ptr == p) 64 throw invalid_argument("stoi: no conversion"); |
|
64#endif // _LIBCPP_NO_EXCEPTIONS | 65#endif // _LIBCPP_NO_EXCEPTIONS |
65 } | |
66 if (idx) 67 *idx = static_cast<size_t>(ptr - p); 68 return static_cast<int>(r); 69} 70 71long 72stol(const string& str, size_t* idx, int base) 73{ 74 char* ptr; 75 const char* const p = str.c_str(); | 66 if (idx) 67 *idx = static_cast<size_t>(ptr - p); 68 return static_cast<int>(r); 69} 70 71long 72stol(const string& str, size_t* idx, int base) 73{ 74 char* ptr; 75 const char* const p = str.c_str(); |
76 typename remove_reference<decltype(errno)>::type errno_save = errno; 77 errno = 0; |
|
76 long r = strtol(p, &ptr, base); | 78 long r = strtol(p, &ptr, base); |
77 if (ptr == p) 78 { | 79 swap(errno, errno_save); |
79#ifndef _LIBCPP_NO_EXCEPTIONS | 80#ifndef _LIBCPP_NO_EXCEPTIONS |
80 if (r == 0) 81 throw invalid_argument("stol: no conversion"); | 81 if (errno_save == ERANGE) |
82 throw out_of_range("stol: out of range"); | 82 throw out_of_range("stol: out of range"); |
83 if (ptr == p) 84 throw invalid_argument("stol: no conversion"); |
|
83#endif // _LIBCPP_NO_EXCEPTIONS | 85#endif // _LIBCPP_NO_EXCEPTIONS |
84 } | |
85 if (idx) 86 *idx = static_cast<size_t>(ptr - p); 87 return r; 88} 89 90long 91stol(const wstring& str, size_t* idx, int base) 92{ 93 wchar_t* ptr; 94 const wchar_t* const p = str.c_str(); | 86 if (idx) 87 *idx = static_cast<size_t>(ptr - p); 88 return r; 89} 90 91long 92stol(const wstring& str, size_t* idx, int base) 93{ 94 wchar_t* ptr; 95 const wchar_t* const p = str.c_str(); |
96 typename remove_reference<decltype(errno)>::type errno_save = errno; 97 errno = 0; |
|
95 long r = wcstol(p, &ptr, base); | 98 long r = wcstol(p, &ptr, base); |
96 if (ptr == p) 97 { | 99 swap(errno, errno_save); |
98#ifndef _LIBCPP_NO_EXCEPTIONS | 100#ifndef _LIBCPP_NO_EXCEPTIONS |
99 if (r == 0) 100 throw invalid_argument("stol: no conversion"); | 101 if (errno_save == ERANGE) |
101 throw out_of_range("stol: out of range"); | 102 throw out_of_range("stol: out of range"); |
103 if (ptr == p) 104 throw invalid_argument("stol: no conversion"); |
|
102#endif // _LIBCPP_NO_EXCEPTIONS | 105#endif // _LIBCPP_NO_EXCEPTIONS |
103 } | |
104 if (idx) 105 *idx = static_cast<size_t>(ptr - p); 106 return r; 107} 108 109unsigned long 110stoul(const string& str, size_t* idx, int base) 111{ 112 char* ptr; 113 const char* const p = str.c_str(); | 106 if (idx) 107 *idx = static_cast<size_t>(ptr - p); 108 return r; 109} 110 111unsigned long 112stoul(const string& str, size_t* idx, int base) 113{ 114 char* ptr; 115 const char* const p = str.c_str(); |
116 typename remove_reference<decltype(errno)>::type errno_save = errno; 117 errno = 0; |
|
114 unsigned long r = strtoul(p, &ptr, base); | 118 unsigned long r = strtoul(p, &ptr, base); |
115 if (ptr == p) 116 { | 119 swap(errno, errno_save); |
117#ifndef _LIBCPP_NO_EXCEPTIONS | 120#ifndef _LIBCPP_NO_EXCEPTIONS |
118 if (r == 0) 119 throw invalid_argument("stoul: no conversion"); | 121 if (errno_save == ERANGE) |
120 throw out_of_range("stoul: out of range"); | 122 throw out_of_range("stoul: out of range"); |
123 if (ptr == p) 124 throw invalid_argument("stoul: no conversion"); |
|
121#endif // _LIBCPP_NO_EXCEPTIONS | 125#endif // _LIBCPP_NO_EXCEPTIONS |
122 } | |
123 if (idx) 124 *idx = static_cast<size_t>(ptr - p); 125 return r; 126} 127 128unsigned long 129stoul(const wstring& str, size_t* idx, int base) 130{ 131 wchar_t* ptr; 132 const wchar_t* const p = str.c_str(); | 126 if (idx) 127 *idx = static_cast<size_t>(ptr - p); 128 return r; 129} 130 131unsigned long 132stoul(const wstring& str, size_t* idx, int base) 133{ 134 wchar_t* ptr; 135 const wchar_t* const p = str.c_str(); |
136 typename remove_reference<decltype(errno)>::type errno_save = errno; 137 errno = 0; |
|
133 unsigned long r = wcstoul(p, &ptr, base); | 138 unsigned long r = wcstoul(p, &ptr, base); |
134 if (ptr == p) 135 { | 139 swap(errno, errno_save); |
136#ifndef _LIBCPP_NO_EXCEPTIONS | 140#ifndef _LIBCPP_NO_EXCEPTIONS |
137 if (r == 0) 138 throw invalid_argument("stoul: no conversion"); | 141 if (errno_save == ERANGE) |
139 throw out_of_range("stoul: out of range"); | 142 throw out_of_range("stoul: out of range"); |
143 if (ptr == p) 144 throw invalid_argument("stoul: no conversion"); |
|
140#endif // _LIBCPP_NO_EXCEPTIONS | 145#endif // _LIBCPP_NO_EXCEPTIONS |
141 } | |
142 if (idx) 143 *idx = static_cast<size_t>(ptr - p); 144 return r; 145} 146 147long long 148stoll(const string& str, size_t* idx, int base) 149{ 150 char* ptr; 151 const char* const p = str.c_str(); | 146 if (idx) 147 *idx = static_cast<size_t>(ptr - p); 148 return r; 149} 150 151long long 152stoll(const string& str, size_t* idx, int base) 153{ 154 char* ptr; 155 const char* const p = str.c_str(); |
156 typename remove_reference<decltype(errno)>::type errno_save = errno; 157 errno = 0; |
|
152 long long r = strtoll(p, &ptr, base); | 158 long long r = strtoll(p, &ptr, base); |
153 if (ptr == p) 154 { | 159 swap(errno, errno_save); |
155#ifndef _LIBCPP_NO_EXCEPTIONS | 160#ifndef _LIBCPP_NO_EXCEPTIONS |
156 if (r == 0) 157 throw invalid_argument("stoll: no conversion"); | 161 if (errno_save == ERANGE) |
158 throw out_of_range("stoll: out of range"); | 162 throw out_of_range("stoll: out of range"); |
163 if (ptr == p) 164 throw invalid_argument("stoll: no conversion"); |
|
159#endif // _LIBCPP_NO_EXCEPTIONS | 165#endif // _LIBCPP_NO_EXCEPTIONS |
160 } | |
161 if (idx) 162 *idx = static_cast<size_t>(ptr - p); 163 return r; 164} 165 166long long 167stoll(const wstring& str, size_t* idx, int base) 168{ 169 wchar_t* ptr; 170 const wchar_t* const p = str.c_str(); | 166 if (idx) 167 *idx = static_cast<size_t>(ptr - p); 168 return r; 169} 170 171long long 172stoll(const wstring& str, size_t* idx, int base) 173{ 174 wchar_t* ptr; 175 const wchar_t* const p = str.c_str(); |
176 typename remove_reference<decltype(errno)>::type errno_save = errno; 177 errno = 0; |
|
171 long long r = wcstoll(p, &ptr, base); | 178 long long r = wcstoll(p, &ptr, base); |
172 if (ptr == p) 173 { | 179 swap(errno, errno_save); |
174#ifndef _LIBCPP_NO_EXCEPTIONS | 180#ifndef _LIBCPP_NO_EXCEPTIONS |
175 if (r == 0) 176 throw invalid_argument("stoll: no conversion"); | 181 if (errno_save == ERANGE) |
177 throw out_of_range("stoll: out of range"); | 182 throw out_of_range("stoll: out of range"); |
183 if (ptr == p) 184 throw invalid_argument("stoll: no conversion"); |
|
178#endif // _LIBCPP_NO_EXCEPTIONS | 185#endif // _LIBCPP_NO_EXCEPTIONS |
179 } | |
180 if (idx) 181 *idx = static_cast<size_t>(ptr - p); 182 return r; 183} 184 185unsigned long long 186stoull(const string& str, size_t* idx, int base) 187{ 188 char* ptr; 189 const char* const p = str.c_str(); | 186 if (idx) 187 *idx = static_cast<size_t>(ptr - p); 188 return r; 189} 190 191unsigned long long 192stoull(const string& str, size_t* idx, int base) 193{ 194 char* ptr; 195 const char* const p = str.c_str(); |
196 typename remove_reference<decltype(errno)>::type errno_save = errno; 197 errno = 0; |
|
190 unsigned long long r = strtoull(p, &ptr, base); | 198 unsigned long long r = strtoull(p, &ptr, base); |
191 if (ptr == p) 192 { | 199 swap(errno, errno_save); |
193#ifndef _LIBCPP_NO_EXCEPTIONS | 200#ifndef _LIBCPP_NO_EXCEPTIONS |
194 if (r == 0) 195 throw invalid_argument("stoull: no conversion"); | 201 if (errno_save == ERANGE) |
196 throw out_of_range("stoull: out of range"); | 202 throw out_of_range("stoull: out of range"); |
203 if (ptr == p) 204 throw invalid_argument("stoull: no conversion"); |
|
197#endif // _LIBCPP_NO_EXCEPTIONS | 205#endif // _LIBCPP_NO_EXCEPTIONS |
198 } | |
199 if (idx) 200 *idx = static_cast<size_t>(ptr - p); 201 return r; 202} 203 204unsigned long long 205stoull(const wstring& str, size_t* idx, int base) 206{ 207 wchar_t* ptr; 208 const wchar_t* const p = str.c_str(); | 206 if (idx) 207 *idx = static_cast<size_t>(ptr - p); 208 return r; 209} 210 211unsigned long long 212stoull(const wstring& str, size_t* idx, int base) 213{ 214 wchar_t* ptr; 215 const wchar_t* const p = str.c_str(); |
216 typename remove_reference<decltype(errno)>::type errno_save = errno; 217 errno = 0; |
|
209 unsigned long long r = wcstoull(p, &ptr, base); | 218 unsigned long long r = wcstoull(p, &ptr, base); |
210 if (ptr == p) 211 { | 219 swap(errno, errno_save); |
212#ifndef _LIBCPP_NO_EXCEPTIONS | 220#ifndef _LIBCPP_NO_EXCEPTIONS |
213 if (r == 0) 214 throw invalid_argument("stoull: no conversion"); | 221 if (errno_save == ERANGE) |
215 throw out_of_range("stoull: out of range"); | 222 throw out_of_range("stoull: out of range"); |
223 if (ptr == p) 224 throw invalid_argument("stoull: no conversion"); |
|
216#endif // _LIBCPP_NO_EXCEPTIONS | 225#endif // _LIBCPP_NO_EXCEPTIONS |
217 } | |
218 if (idx) 219 *idx = static_cast<size_t>(ptr - p); 220 return r; 221} 222 223float 224stof(const string& str, size_t* idx) 225{ 226 char* ptr; 227 const char* const p = str.c_str(); | 226 if (idx) 227 *idx = static_cast<size_t>(ptr - p); 228 return r; 229} 230 231float 232stof(const string& str, size_t* idx) 233{ 234 char* ptr; 235 const char* const p = str.c_str(); |
228 int errno_save = errno; | 236 typename remove_reference<decltype(errno)>::type errno_save = errno; |
229 errno = 0; | 237 errno = 0; |
230 double r = strtod(p, &ptr); | 238 float r = strtof(p, &ptr); |
231 swap(errno, errno_save); 232#ifndef _LIBCPP_NO_EXCEPTIONS 233 if (errno_save == ERANGE) 234 throw out_of_range("stof: out of range"); 235 if (ptr == p) 236 throw invalid_argument("stof: no conversion"); 237#endif // _LIBCPP_NO_EXCEPTIONS 238 if (idx) 239 *idx = static_cast<size_t>(ptr - p); | 239 swap(errno, errno_save); 240#ifndef _LIBCPP_NO_EXCEPTIONS 241 if (errno_save == ERANGE) 242 throw out_of_range("stof: out of range"); 243 if (ptr == p) 244 throw invalid_argument("stof: no conversion"); 245#endif // _LIBCPP_NO_EXCEPTIONS 246 if (idx) 247 *idx = static_cast<size_t>(ptr - p); |
240 return static_cast<float>(r); | 248 return r; |
241} 242 243float 244stof(const wstring& str, size_t* idx) 245{ 246 wchar_t* ptr; 247 const wchar_t* const p = str.c_str(); | 249} 250 251float 252stof(const wstring& str, size_t* idx) 253{ 254 wchar_t* ptr; 255 const wchar_t* const p = str.c_str(); |
248 int errno_save = errno; | 256 typename remove_reference<decltype(errno)>::type errno_save = errno; |
249 errno = 0; | 257 errno = 0; |
250 double r = wcstod(p, &ptr); | 258 float r = wcstof(p, &ptr); |
251 swap(errno, errno_save); 252#ifndef _LIBCPP_NO_EXCEPTIONS 253 if (errno_save == ERANGE) 254 throw out_of_range("stof: out of range"); 255 if (ptr == p) 256 throw invalid_argument("stof: no conversion"); 257#endif // _LIBCPP_NO_EXCEPTIONS 258 if (idx) 259 *idx = static_cast<size_t>(ptr - p); | 259 swap(errno, errno_save); 260#ifndef _LIBCPP_NO_EXCEPTIONS 261 if (errno_save == ERANGE) 262 throw out_of_range("stof: out of range"); 263 if (ptr == p) 264 throw invalid_argument("stof: no conversion"); 265#endif // _LIBCPP_NO_EXCEPTIONS 266 if (idx) 267 *idx = static_cast<size_t>(ptr - p); |
260 return static_cast<float>(r); | 268 return r; |
261} 262 263double 264stod(const string& str, size_t* idx) 265{ 266 char* ptr; 267 const char* const p = str.c_str(); | 269} 270 271double 272stod(const string& str, size_t* idx) 273{ 274 char* ptr; 275 const char* const p = str.c_str(); |
268 int errno_save = errno; | 276 typename remove_reference<decltype(errno)>::type errno_save = errno; |
269 errno = 0; 270 double r = strtod(p, &ptr); 271 swap(errno, errno_save); 272#ifndef _LIBCPP_NO_EXCEPTIONS 273 if (errno_save == ERANGE) 274 throw out_of_range("stod: out of range"); 275 if (ptr == p) 276 throw invalid_argument("stod: no conversion"); 277#endif // _LIBCPP_NO_EXCEPTIONS 278 if (idx) 279 *idx = static_cast<size_t>(ptr - p); 280 return r; 281} 282 283double 284stod(const wstring& str, size_t* idx) 285{ 286 wchar_t* ptr; 287 const wchar_t* const p = str.c_str(); | 277 errno = 0; 278 double r = strtod(p, &ptr); 279 swap(errno, errno_save); 280#ifndef _LIBCPP_NO_EXCEPTIONS 281 if (errno_save == ERANGE) 282 throw out_of_range("stod: out of range"); 283 if (ptr == p) 284 throw invalid_argument("stod: no conversion"); 285#endif // _LIBCPP_NO_EXCEPTIONS 286 if (idx) 287 *idx = static_cast<size_t>(ptr - p); 288 return r; 289} 290 291double 292stod(const wstring& str, size_t* idx) 293{ 294 wchar_t* ptr; 295 const wchar_t* const p = str.c_str(); |
288 int errno_save = errno; | 296 typename remove_reference<decltype(errno)>::type errno_save = errno; |
289 errno = 0; 290 double r = wcstod(p, &ptr); 291 swap(errno, errno_save); 292#ifndef _LIBCPP_NO_EXCEPTIONS 293 if (errno_save == ERANGE) 294 throw out_of_range("stod: out of range"); 295 if (ptr == p) 296 throw invalid_argument("stod: no conversion"); 297#endif // _LIBCPP_NO_EXCEPTIONS 298 if (idx) 299 *idx = static_cast<size_t>(ptr - p); 300 return r; 301} 302 303long double 304stold(const string& str, size_t* idx) 305{ 306 char* ptr; 307 const char* const p = str.c_str(); | 297 errno = 0; 298 double r = wcstod(p, &ptr); 299 swap(errno, errno_save); 300#ifndef _LIBCPP_NO_EXCEPTIONS 301 if (errno_save == ERANGE) 302 throw out_of_range("stod: out of range"); 303 if (ptr == p) 304 throw invalid_argument("stod: no conversion"); 305#endif // _LIBCPP_NO_EXCEPTIONS 306 if (idx) 307 *idx = static_cast<size_t>(ptr - p); 308 return r; 309} 310 311long double 312stold(const string& str, size_t* idx) 313{ 314 char* ptr; 315 const char* const p = str.c_str(); |
308 int errno_save = errno; | 316 typename remove_reference<decltype(errno)>::type errno_save = errno; |
309 errno = 0; 310 long double r = strtold(p, &ptr); 311 swap(errno, errno_save); 312#ifndef _LIBCPP_NO_EXCEPTIONS 313 if (errno_save == ERANGE) 314 throw out_of_range("stold: out of range"); 315 if (ptr == p) 316 throw invalid_argument("stold: no conversion"); 317#endif // _LIBCPP_NO_EXCEPTIONS 318 if (idx) 319 *idx = static_cast<size_t>(ptr - p); 320 return r; 321} 322 323long double 324stold(const wstring& str, size_t* idx) 325{ 326 wchar_t* ptr; 327 const wchar_t* const p = str.c_str(); | 317 errno = 0; 318 long double r = strtold(p, &ptr); 319 swap(errno, errno_save); 320#ifndef _LIBCPP_NO_EXCEPTIONS 321 if (errno_save == ERANGE) 322 throw out_of_range("stold: out of range"); 323 if (ptr == p) 324 throw invalid_argument("stold: no conversion"); 325#endif // _LIBCPP_NO_EXCEPTIONS 326 if (idx) 327 *idx = static_cast<size_t>(ptr - p); 328 return r; 329} 330 331long double 332stold(const wstring& str, size_t* idx) 333{ 334 wchar_t* ptr; 335 const wchar_t* const p = str.c_str(); |
328 int errno_save = errno; | 336 typename remove_reference<decltype(errno)>::type errno_save = errno; |
329 errno = 0; 330 long double r = wcstold(p, &ptr); 331 swap(errno, errno_save); 332#ifndef _LIBCPP_NO_EXCEPTIONS 333 if (errno_save == ERANGE) 334 throw out_of_range("stold: out of range"); 335 if (ptr == p) 336 throw invalid_argument("stold: no conversion"); --- 343 unchanged lines hidden --- | 337 errno = 0; 338 long double r = wcstold(p, &ptr); 339 swap(errno, errno_save); 340#ifndef _LIBCPP_NO_EXCEPTIONS 341 if (errno_save == ERANGE) 342 throw out_of_range("stold: out of range"); 343 if (ptr == p) 344 throw invalid_argument("stold: no conversion"); --- 343 unchanged lines hidden --- |