Skip to content

Commit 27f815f

Browse files
committed
kservice try RT-Thread#1
1 parent 181335c commit 27f815f

File tree

1 file changed

+73
-51
lines changed

1 file changed

+73
-51
lines changed

Diff for: src/kservice.c

+73-51
Original file line numberDiff line numberDiff line change
@@ -578,9 +578,12 @@ static char *print_number(char *buf,
578578
#ifdef RT_PRINTF_LONGLONG
579579
char tmp[32];
580580
#else
581-
char tmp[16];
581+
//char tmp[16];
582582
#endif
583-
register int i;
583+
register int i = 0;
584+
register int buflen = (int)(end - buf);
585+
int number_width;
586+
long saved_num;
584587

585588
if (type & LEFT)
586589
type &= ~ZEROPAD;
@@ -590,6 +593,7 @@ static char *print_number(char *buf,
590593
num = -num;
591594
type |= NEGATIVE;
592595
}
596+
saved_num = num;
593597

594598
#ifdef RT_PRINTF_SPECIAL
595599
if (type & SPECIAL)
@@ -601,25 +605,18 @@ static char *print_number(char *buf,
601605
}
602606
#endif
603607

604-
i = 0;
605-
if (num == 0)
606-
tmp[i++]='0';
607-
else
608-
{
609-
if (type & LARGE)
610-
while (num != 0)
611-
tmp[i++] = "0123456789ABCDEF"[divide(&num, base)];
612-
else
613-
while (num != 0)
614-
tmp[i++] = "0123456789abcdef"[divide(&num, base)];
615-
}
608+
number_width = 0;
609+
do {
610+
number_width++;
611+
divide(&saved_num, base);
612+
} while (saved_num);
616613

617614
#ifdef RT_PRINTF_PRECISION
618-
if (i > precision)
619-
precision = i;
615+
if (number_width > precision)
616+
precision = number_width;
620617
size -= precision;
621618
#else
622-
size -= i;
619+
size -= number_width;
623620
#endif
624621

625622
if (!(type&(ZEROPAD | LEFT)))
@@ -629,46 +626,46 @@ static char *print_number(char *buf,
629626

630627
while (size-->0)
631628
{
632-
if (buf <= end)
633-
*buf = ' ';
634-
++ buf;
629+
if (i <= buflen)
630+
buf[i] = ' ';
631+
i++;
635632
}
636633
}
637634

638635
if (type & SIGN)
639636
{
640-
if (buf <= end)
637+
if (i <= buflen)
641638
{
642639
if (type & NEGATIVE)
643-
*buf = '-';
640+
buf[i] = '-';
644641
else if (type & PLUS)
645-
*buf = '+';
642+
buf[i] = '+';
646643
else if (type & SPACE)
647-
*buf = ' ';
648-
-- size;
644+
buf[i] = ' ';
649645
}
650-
++ buf;
646+
-- size;
647+
++ i;
651648
}
652649

653650
#ifdef RT_PRINTF_SPECIAL
654651
if (type & SPECIAL)
655652
{
656653
if (base==8)
657654
{
658-
if (buf <= end)
659-
*buf = '0';
660-
++ buf;
655+
if (i <= buflen)
656+
buf[i] = '0';
657+
++ i;
661658
}
662659
else if (base == 16)
663660
{
664-
if (buf <= end)
665-
*buf = '0';
666-
++ buf;
667-
if (buf <= end)
661+
if (i <= buflen)
662+
buf[i] = '0';
663+
++ i;
664+
if (i <= buflen)
668665
{
669-
*buf = type & LARGE? 'X' : 'x';
666+
buf[i] = type & LARGE? 'X' : 'x';
670667
}
671-
++ buf;
668+
++ i;
672669
}
673670
}
674671
#endif
@@ -678,38 +675,63 @@ static char *print_number(char *buf,
678675
{
679676
while (size-- > 0)
680677
{
681-
if (buf <= end)
682-
*buf = (type & ZEROPAD) ? '0' : ' ';
683-
684-
++ buf;
678+
if (i <= buflen)
679+
buf[i] = (type & ZEROPAD) ? '0' : ' ';
680+
++ i;
685681
}
686682
}
687683

688684
#ifdef RT_PRINTF_PRECISION
689685
while (i < precision--)
690686
{
691-
if (buf <= end)
692-
*buf = '0';
693-
++ buf;
687+
if (i <= buflen)
688+
buf[i] = '0';
689+
++ i;
694690
}
695691
#endif
696692

697-
/* put number in the temporary buffer */
698-
while (i-- > 0)
693+
/* put number */
694+
if (num == 0)
699695
{
700-
if (buf <= end)
701-
*buf = tmp[i];
702-
++ buf;
696+
if (i <= buflen)
697+
buf[i] ='0';
698+
++ i;
699+
}
700+
else
701+
{
702+
number_width--;
703+
if (type & LARGE)
704+
while (num != 0)
705+
{
706+
char c;
707+
708+
c = "0123456789ABCDEF"[divide(&num, base)];
709+
if (i + number_width <= buflen)
710+
buf[i + number_width] = c;
711+
number_width -= 2;
712+
i++;
713+
}
714+
else
715+
while (num != 0)
716+
{
717+
char c;
718+
719+
c = "0123456789abcdef"[divide(&num, base)];
720+
if (i + number_width <= buflen)
721+
buf[i + number_width] = c;
722+
number_width -= 2;
723+
i++;
724+
}
703725
}
704726

705727
while (size-- > 0)
706728
{
707-
if (buf <= end)
708-
*buf = ' ';
709-
++ buf;
729+
if (i <= buflen)
730+
buf[i] = ' ';
731+
++ i;
710732
}
711733

712-
return buf;
734+
return buf+i;
713735
}
714736

715737
rt_int32_t rt_vsnprintf(char *buf,

0 commit comments

Comments
 (0)