Changes in uspace/lib/posix/src/stdlib/strtold.c [1b20da0:a35b458] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/posix/src/stdlib/strtold.c
r1b20da0 ra35b458 141 141 return mant; 142 142 } 143 143 144 144 if (abs(exp) >> (MAX_POW5 + 1) != 0) { 145 145 /* Too large exponent. */ … … 147 147 return exp < 0 ? LDBL_MIN : HUGE_VALL; 148 148 } 149 149 150 150 if (exp < 0) { 151 151 exp = abs(exp); … … 175 175 } 176 176 } 177 177 178 178 return mant; 179 179 } … … 191 191 return mant; 192 192 } 193 193 194 194 if (exp > LDBL_MAX_EXP || exp < LDBL_MIN_EXP) { 195 195 errno = ERANGE; 196 196 return exp < 0 ? LDBL_MIN : HUGE_VALL; 197 197 } 198 198 199 199 if (exp < 0) { 200 200 exp = abs(exp); … … 220 220 } 221 221 } 222 222 223 223 return mant; 224 224 } … … 242 242 assert(sptr != NULL); 243 243 assert (*sptr != NULL); 244 244 245 245 const int DEC_BASE = 10; 246 246 const char DECIMAL_POINT = '.'; 247 247 const char EXPONENT_MARK = 'e'; 248 248 249 249 const char *str = *sptr; 250 250 long double significand = 0; 251 251 long exponent = 0; 252 252 253 253 /* number of digits parsed so far */ 254 254 int parsed_digits = 0; 255 255 bool after_decimal = false; 256 256 257 257 while (isdigit(*str) || (!after_decimal && *str == DECIMAL_POINT)) { 258 258 if (*str == DECIMAL_POINT) { … … 261 261 continue; 262 262 } 263 263 264 264 if (parsed_digits == 0 && *str == '0') { 265 265 /* Nothing, just skip leading zeros. */ … … 270 270 exponent++; 271 271 } 272 272 273 273 if (after_decimal) { 274 274 /* Decrement exponent if we are parsing the fractional part. */ 275 275 exponent--; 276 276 } 277 277 278 278 str++; 279 279 } 280 280 281 281 /* exponent */ 282 282 if (tolower(*str) == EXPONENT_MARK) { 283 283 str++; 284 284 285 285 /* Returns MIN/MAX value on error, which is ok. */ 286 286 long exp = strtol(str, (char **) &str, DEC_BASE); 287 287 288 288 if (exponent > 0 && exp > LONG_MAX - exponent) { 289 289 exponent = LONG_MAX; … … 294 294 } 295 295 } 296 296 297 297 *sptr = str; 298 298 299 299 /* Return multiplied by a power of ten. */ 300 300 return mul_pow2(mul_pow5(significand, exponent), exponent); … … 330 330 { 331 331 assert(sptr != NULL && *sptr != NULL); 332 332 333 333 const int DEC_BASE = 10; 334 334 const int HEX_BASE = 16; 335 335 const char DECIMAL_POINT = '.'; 336 336 const char EXPONENT_MARK = 'p'; 337 337 338 338 const char *str = *sptr; 339 339 long double significand = 0; 340 340 long exponent = 0; 341 341 342 342 /* number of bits parsed so far */ 343 343 int parsed_bits = 0; 344 344 bool after_decimal = false; 345 345 346 346 while (isxdigit(*str) || (!after_decimal && *str == DECIMAL_POINT)) { 347 347 if (*str == DECIMAL_POINT) { … … 350 350 continue; 351 351 } 352 352 353 353 if (parsed_bits == 0 && *str == '0') { 354 354 /* Nothing, just skip leading zeros. */ … … 359 359 exponent += 4; 360 360 } 361 361 362 362 if (after_decimal) { 363 363 exponent -= 4; 364 364 } 365 365 366 366 str++; 367 367 } 368 368 369 369 /* exponent */ 370 370 if (tolower(*str) == EXPONENT_MARK) { 371 371 str++; 372 372 373 373 /* Returns MIN/MAX value on error, which is ok. */ 374 374 long exp = strtol(str, (char **) &str, DEC_BASE); 375 375 376 376 if (exponent > 0 && exp > LONG_MAX - exponent) { 377 377 exponent = LONG_MAX; … … 382 382 } 383 383 } 384 384 385 385 *sptr = str; 386 386 387 387 /* Return multiplied by a power of two. */ 388 388 return mul_pow2(significand, exponent); … … 407 407 { 408 408 assert(nptr != NULL); 409 409 410 410 const int RADIX = '.'; 411 411 412 412 /* minus sign */ 413 413 bool negative = false; 414 414 /* current position in the string */ 415 415 int i = 0; 416 416 417 417 /* skip whitespace */ 418 418 while (isspace(nptr[i])) { 419 419 i++; 420 420 } 421 421 422 422 /* parse sign */ 423 423 switch (nptr[i]) { … … 428 428 i++; 429 429 } 430 430 431 431 /* check for NaN */ 432 432 if (strncasecmp(&nptr[i], "nan", 3) == 0) { 433 433 // FIXME: return NaN 434 434 // TODO: handle the parenthesised case 435 435 436 436 if (endptr != NULL) { 437 437 *endptr = (char *) nptr; … … 440 440 return 0; 441 441 } 442 442 443 443 /* check for Infinity */ 444 444 if (strncasecmp(&nptr[i], "inf", 3) == 0) { … … 447 447 i += 5; 448 448 } 449 449 450 450 if (endptr != NULL) { 451 451 *endptr = (char *) &nptr[i]; … … 459 459 (nptr[i + 2] == RADIX && isxdigit(nptr[i + 3])))) { 460 460 i += 2; 461 461 462 462 const char *ptr = &nptr[i]; 463 463 /* this call sets errno if appropriate. */ … … 468 468 return negative ? -result : result; 469 469 } 470 470 471 471 /* check for a decimal number */ 472 472 if (isdigit(nptr[i]) || (nptr[i] == RADIX && isdigit(nptr[i + 1]))) { … … 479 479 return negative ? -result : result; 480 480 } 481 481 482 482 /* nothing to parse */ 483 483 if (endptr != NULL) {
Note:
See TracChangeset
for help on using the changeset viewer.