Changeset 8565a42 in mainline for uspace/lib/c/generic/io/printf_core.c
- Timestamp:
- 2018-03-02T20:34:50Z (7 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- a1a81f69, d5e5fd1
- Parents:
- 3061bc1 (diff), 34e1206 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - git-author:
- Jiří Zárevúcky <zarevucky.jiri@…> (2018-03-02 20:34:50)
- git-committer:
- GitHub <noreply@…> (2018-03-02 20:34:50)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/c/generic/io/printf_core.c
r3061bc1 r8565a42 211 211 if (str == NULL) 212 212 return printf_putnchars(nullstr, str_size(nullstr), ps); 213 213 214 214 return ps->str_write((void *) str, str_size(str), ps->data); 215 215 } … … 227 227 if (!ascii_check(ch)) 228 228 return ps->str_write((void *) &invalch, 1, ps->data); 229 229 230 230 return ps->str_write(&ch, 1, ps->data); 231 231 } … … 243 243 if (!chr_check(ch)) 244 244 return ps->str_write((void *) &invalch, 1, ps->data); 245 245 246 246 return ps->wstr_write(&ch, sizeof(wchar_t), ps->data); 247 247 } … … 269 269 } 270 270 } 271 271 272 272 if (printf_putchar(ch, ps) > 0) 273 273 counter++; 274 274 275 275 while (--width > 0) { 276 276 /* … … 281 281 counter++; 282 282 } 283 283 284 284 return (int) (counter); 285 285 } … … 307 307 } 308 308 } 309 309 310 310 if (printf_putwchar(ch, ps) > 0) 311 311 counter++; 312 312 313 313 while (--width > 0) { 314 314 /* … … 319 319 counter++; 320 320 } 321 321 322 322 return (int) (counter); 323 323 } … … 337 337 if (str == NULL) 338 338 return printf_putstr(nullstr, ps); 339 339 340 340 size_t strw = str_length(str); 341 341 … … 343 343 if ((precision == 0) || (precision > strw)) 344 344 precision = strw; 345 345 346 346 /* Left padding */ 347 347 size_t counter = 0; … … 353 353 } 354 354 } 355 355 356 356 /* Part of @a str fitting into the alloted space. */ 357 357 int retval; … … 386 386 if (str == NULL) 387 387 return printf_putstr(nullstr, ps); 388 388 389 389 size_t strw = wstr_length(str); 390 390 … … 392 392 if ((precision == 0) || (precision > strw)) 393 393 precision = strw; 394 394 395 395 /* Left padding */ 396 396 size_t counter = 0; … … 402 402 } 403 403 } 404 404 405 405 /* Part of @a wstr fitting into the alloted space. */ 406 406 int retval; … … 408 408 if ((retval = printf_wputnchars(str, size, ps)) < 0) 409 409 return -counter; 410 410 411 411 counter += retval; 412 412 413 413 /* Right padding */ 414 414 while (width-- > 0) { … … 440 440 precision = 0; 441 441 } 442 442 443 443 const char *digits; 444 444 if (flags & __PRINTF_FLAG_BIGCHARS) … … 446 446 else 447 447 digits = digits_small; 448 448 449 449 char data[PRINT_NUMBER_BUFFER_SIZE]; 450 450 char *ptr = &data[PRINT_NUMBER_BUFFER_SIZE - 1]; 451 451 452 452 /* Size of number with all prefixes and signs */ 453 453 int size = 0; 454 454 455 455 /* Put zero at end of string */ 456 456 *ptr-- = 0; 457 457 458 458 if (num == 0) { 459 459 *ptr-- = '0'; … … 465 465 } while (num /= base); 466 466 } 467 467 468 468 /* Size of plain number */ 469 469 int number_size = size; 470 470 471 471 /* 472 472 * Collect the sum of all prefixes/signs/etc. to calculate padding and … … 487 487 } 488 488 } 489 489 490 490 char sgn = 0; 491 491 if (flags & __PRINTF_FLAG_SIGNED) { … … 501 501 } 502 502 } 503 503 504 504 if (flags & __PRINTF_FLAG_LEFTALIGNED) 505 505 flags &= ~__PRINTF_FLAG_ZEROPADDED; 506 506 507 507 /* 508 508 * If the number is left-aligned or precision is specified then … … 513 513 precision = width - size + number_size; 514 514 } 515 515 516 516 /* Print leading spaces */ 517 517 if (number_size > precision) { … … 519 519 precision = number_size; 520 520 } 521 521 522 522 width -= precision + size - number_size; 523 523 size_t counter = 0; 524 524 525 525 if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { 526 526 while (width-- > 0) { … … 529 529 } 530 530 } 531 531 532 532 /* Print sign */ 533 533 if (sgn) { … … 535 535 counter++; 536 536 } 537 537 538 538 /* Print prefix */ 539 539 if (flags & __PRINTF_FLAG_PREFIX) { … … 568 568 } 569 569 } 570 570 571 571 /* Print leading zeroes */ 572 572 precision -= number_size; … … 575 575 counter++; 576 576 } 577 577 578 578 /* Print the number itself */ 579 579 int retval; 580 580 if ((retval = printf_putstr(++ptr, ps)) > 0) 581 581 counter += retval; 582 582 583 583 /* Print trailing spaces */ 584 584 585 585 while (width-- > 0) { 586 586 if (printf_putchar(' ', ps) == 1) 587 587 counter++; 588 588 } 589 589 590 590 return ((int) counter); 591 591 } … … 601 601 const int str_len = 3; 602 602 const char *str; 603 603 604 604 if (flags & __PRINTF_FLAG_BIGCHARS) { 605 605 str = val.is_infinity ? "INF" : "NAN"; … … 624 624 if ((ret = ps->str_write(&sign, 1, ps->data)) < 0) 625 625 return -1; 626 626 627 627 counter += ret; 628 628 } … … 630 630 if ((ret = ps->str_write(str, str_len, ps->data)) < 0) 631 631 return -1; 632 632 633 633 counter += ret; 634 634 … … 751 751 if ((ret = ps->str_write(&sign, 1, ps->data)) < 0) 752 752 return -1; 753 753 754 754 counter += ret; 755 755 } … … 783 783 784 784 counter += ret; 785 785 786 786 /* Print the decimal point and the fractional part. */ 787 787 if (has_decimal_pt) { … … 790 790 if ((ret = ps->str_write(&ch, 1, ps->data)) < 0) 791 791 return -1; 792 792 793 793 counter += ret; 794 794 … … 885 885 /* Let the implementation figure out the proper precision. */ 886 886 val_str.len = double_to_short_str(val, buf, buf_size, &val_str.dec_exp); 887 887 888 888 /* Precision needed for the last significant digit. */ 889 889 precision = max(0, -val_str.dec_exp); … … 903 903 if ((ret = ps->str_write(&exp_ch, 1, ps->data)) < 0) 904 904 return -1; 905 905 906 906 counter += ret; 907 907 … … 915 915 /* Print the exponent. */ 916 916 exp_val = abs(exp_val); 917 917 918 918 char exp_str[4] = { 0 }; 919 919 … … 921 921 exp_str[1] = '0' + (exp_val % 100) / 10 ; 922 922 exp_str[2] = '0' + (exp_val % 10); 923 923 924 924 int exp_len = (exp_str[0] == '0') ? 2 : 3; 925 925 const char *exp_str_start = &exp_str[3] - exp_len; … … 981 981 if ((ret = ps->str_write(&sign, 1, ps->data)) < 0) 982 982 return -1; 983 983 984 984 counter += ret; 985 985 } … … 1004 1004 if ((ret = ps->str_write(&ch, 1, ps->data)) < 0) 1005 1005 return -1; 1006 1006 1007 1007 counter += ret; 1008 1008 … … 1104 1104 /* Let the implementation figure out the proper precision. */ 1105 1105 val_str.len = double_to_short_str(val, buf, buf_size, &val_str.dec_exp); 1106 1106 1107 1107 /* Use all produced digits. */ 1108 1108 precision = val_str.len - 1; … … 1223 1223 precision = (precision < 0) ? 6 : precision; 1224 1224 return print_double_fixed(g, precision, width, flags, ps); 1225 1225 1226 1226 case 'E': 1227 1227 flags |= __PRINTF_FLAG_BIGCHARS; … … 1230 1230 precision = (precision < 0) ? 6 : precision; 1231 1231 return print_double_scientific(g, precision, width, flags, ps); 1232 1232 1233 1233 case 'G': 1234 1234 flags |= __PRINTF_FLAG_BIGCHARS; … … 1236 1236 case 'g': 1237 1237 return print_double_generic(g, precision, width, flags, ps); 1238 1238 1239 1239 default: 1240 1240 assert(false); … … 1337 1337 size_t nxt = 0; /* Index of the next character from fmt */ 1338 1338 size_t j = 0; /* Index to the first not printed nonformating character */ 1339 1339 1340 1340 size_t counter = 0; /* Number of characters printed */ 1341 1341 int retval; /* Return values from nested functions */ 1342 1342 1343 1343 while (true) { 1344 1344 i = nxt; 1345 1345 wchar_t uc = str_decode(fmt, &nxt, STR_NO_LIMIT); 1346 1346 1347 1347 if (uc == 0) 1348 1348 break; 1349 1349 1350 1350 /* Control character */ 1351 1351 if (uc == '%') { … … 1359 1359 counter += retval; 1360 1360 } 1361 1361 1362 1362 j = i; 1363 1363 1364 1364 /* Parse modifiers */ 1365 1365 uint32_t flags = 0; 1366 1366 bool end = false; 1367 1367 1368 1368 do { 1369 1369 i = nxt; … … 1390 1390 }; 1391 1391 } while (!end); 1392 1392 1393 1393 /* Width & '*' operator */ 1394 1394 int width = 0; … … 1397 1397 width *= 10; 1398 1398 width += uc - '0'; 1399 1399 1400 1400 i = nxt; 1401 1401 uc = str_decode(fmt, &nxt, STR_NO_LIMIT); … … 1416 1416 } 1417 1417 } 1418 1418 1419 1419 /* Precision and '*' operator */ 1420 1420 int precision = -1; … … 1427 1427 precision *= 10; 1428 1428 precision += uc - '0'; 1429 1429 1430 1430 i = nxt; 1431 1431 uc = str_decode(fmt, &nxt, STR_NO_LIMIT); … … 1446 1446 } 1447 1447 } 1448 1448 1449 1449 qualifier_t qualifier; 1450 1450 1451 1451 switch (uc) { 1452 1452 case 't': … … 1495 1495 qualifier = PrintfQualifierInt; 1496 1496 } 1497 1497 1498 1498 unsigned int base = 10; 1499 1499 1500 1500 switch (uc) { 1501 1501 /* … … 1504 1504 case 's': 1505 1505 precision = max(0, precision); 1506 1506 1507 1507 if (qualifier == PrintfQualifierLong) 1508 1508 retval = print_wstr(va_arg(ap, wchar_t *), width, precision, flags, ps); 1509 1509 else 1510 1510 retval = print_str(va_arg(ap, char *), width, precision, flags, ps); 1511 1511 1512 1512 if (retval < 0) { 1513 1513 counter = -counter; 1514 1514 goto out; 1515 1515 } 1516 1516 1517 1517 counter += retval; 1518 1518 j = nxt; … … 1523 1523 else 1524 1524 retval = print_char(va_arg(ap, unsigned int), width, flags, ps); 1525 1525 1526 1526 if (retval < 0) { 1527 1527 counter = -counter; 1528 1528 goto out; 1529 1529 }; 1530 1530 1531 1531 counter += retval; 1532 1532 j = nxt; 1533 1533 goto next_char; 1534 1534 1535 1535 /* 1536 1536 * Floating point values … … 1544 1544 retval = print_double(va_arg(ap, double), uc, precision, 1545 1545 width, flags, ps); 1546 1546 1547 1547 if (retval < 0) { 1548 1548 counter = -counter; 1549 1549 goto out; 1550 1550 } 1551 1551 1552 1552 counter += retval; 1553 1553 j = nxt; 1554 1554 goto next_char; 1555 1555 1556 1556 /* 1557 1557 * Integer values … … 1585 1585 base = 16; 1586 1586 break; 1587 1587 1588 1588 /* Percentile itself */ 1589 1589 case '%': 1590 1590 j = i; 1591 1591 goto next_char; 1592 1592 1593 1593 /* 1594 1594 * Bad formatting. … … 1601 1601 goto next_char; 1602 1602 } 1603 1603 1604 1604 /* Print integers */ 1605 1605 size_t size; 1606 1606 uint64_t number; 1607 1607 1608 1608 switch (qualifier) { 1609 1609 case PrintfQualifierByte: … … 1645 1645 goto out; 1646 1646 } 1647 1647 1648 1648 if ((retval = print_number(number, width, precision, 1649 1649 base, flags, ps)) < 0) { … … 1651 1651 goto out; 1652 1652 } 1653 1653 1654 1654 counter += retval; 1655 1655 j = nxt; … … 1658 1658 ; 1659 1659 } 1660 1660 1661 1661 if (i > j) { 1662 1662 if ((retval = printf_putnchars(&fmt[j], i - j, ps)) < 0) { … … 1667 1667 counter += retval; 1668 1668 } 1669 1669 1670 1670 out: 1671 1671 return ((int) counter);
Note:
See TracChangeset
for help on using the changeset viewer.