MCPcopy Create free account
hub / github.com/HelenOS/helenos / print_number

Function print_number

kernel/generic/src/printf/printf_core.c:379–530  ·  view source on GitHub ↗

Print a number in a given base. * * Print significant digits of a number in given base. * * @param num Number to print. * @param width Width modifier. * @param precision Precision modifier. * @param base Base to print the number in (must be between 2 and 16). * @param flags Flags that modify the way the number is printed. * * @return Number of characters printed. * *

Source from the content-addressed store, hash-verified

377 *
378 */
379static int print_number(uint64_t num, int width, int precision, int base,
380 uint32_t flags, printf_spec_t *ps)
381{
382 const char *digits;
383 if (flags & __PRINTF_FLAG_BIGCHARS)
384 digits = digits_big;
385 else
386 digits = digits_small;
387
388 char data[PRINT_NUMBER_BUFFER_SIZE];
389 char *ptr = &data[PRINT_NUMBER_BUFFER_SIZE - 1];
390
391 /* Size of number with all prefixes and signs */
392 int size = 0;
393
394 /* Put zero at end of string */
395 *ptr-- = 0;
396
397 if (num == 0) {
398 *ptr-- = '0';
399 size++;
400 } else {
401 do {
402 *ptr-- = digits[num % base];
403 size++;
404 } while (num /= base);
405 }
406
407 /* Size of plain number */
408 int number_size = size;
409
410 /*
411 * Collect the sum of all prefixes/signs/etc. to calculate padding and
412 * leading zeroes.
413 */
414 if (flags & __PRINTF_FLAG_PREFIX) {
415 switch (base) {
416 case 2:
417 /* Binary formating is not standard, but usefull */
418 size += 2;
419 break;
420 case 8:
421 size++;
422 break;
423 case 16:
424 size += 2;
425 break;
426 }
427 }
428
429 char sgn = 0;
430 if (flags & __PRINTF_FLAG_SIGNED) {
431 if (flags & __PRINTF_FLAG_NEGATIVE) {
432 sgn = '-';
433 size++;
434 } else if (flags & __PRINTF_FLAG_SHOWPLUS) {
435 sgn = '+';
436 size++;

Callers 1

printf_coreFunction · 0.70

Calls 2

printf_putcharFunction · 0.70
printf_putstrFunction · 0.70

Tested by

no test coverage detected