@@ -578,9 +578,12 @@ static char *print_number(char *buf,
578
578
#ifdef RT_PRINTF_LONGLONG
579
579
char tmp [32 ];
580
580
#else
581
- char tmp [16 ];
581
+ // char tmp[16];
582
582
#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 ;
584
587
585
588
if (type & LEFT )
586
589
type &= ~ZEROPAD ;
@@ -590,6 +593,7 @@ static char *print_number(char *buf,
590
593
num = - num ;
591
594
type |= NEGATIVE ;
592
595
}
596
+ saved_num = num ;
593
597
594
598
#ifdef RT_PRINTF_SPECIAL
595
599
if (type & SPECIAL )
@@ -601,25 +605,18 @@ static char *print_number(char *buf,
601
605
}
602
606
#endif
603
607
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 );
616
613
617
614
#ifdef RT_PRINTF_PRECISION
618
- if (i > precision )
619
- precision = i ;
615
+ if (number_width > precision )
616
+ precision = number_width ;
620
617
size -= precision ;
621
618
#else
622
- size -= i ;
619
+ size -= number_width ;
623
620
#endif
624
621
625
622
if (!(type & (ZEROPAD | LEFT )))
@@ -629,46 +626,46 @@ static char *print_number(char *buf,
629
626
630
627
while (size -- > 0 )
631
628
{
632
- if (buf <= end )
633
- * buf = ' ' ;
634
- ++ buf ;
629
+ if (i <= buflen )
630
+ buf [ i ] = ' ' ;
631
+ i ++ ;
635
632
}
636
633
}
637
634
638
635
if (type & SIGN )
639
636
{
640
- if (buf <= end )
637
+ if (i <= buflen )
641
638
{
642
639
if (type & NEGATIVE )
643
- * buf = '-' ;
640
+ buf [ i ] = '-' ;
644
641
else if (type & PLUS )
645
- * buf = '+' ;
642
+ buf [ i ] = '+' ;
646
643
else if (type & SPACE )
647
- * buf = ' ' ;
648
- -- size ;
644
+ buf [i ] = ' ' ;
649
645
}
650
- ++ buf ;
646
+ -- size ;
647
+ ++ i ;
651
648
}
652
649
653
650
#ifdef RT_PRINTF_SPECIAL
654
651
if (type & SPECIAL )
655
652
{
656
653
if (base == 8 )
657
654
{
658
- if (buf <= end )
659
- * buf = '0' ;
660
- ++ buf ;
655
+ if (i <= buflen )
656
+ buf [ i ] = '0' ;
657
+ ++ i ;
661
658
}
662
659
else if (base == 16 )
663
660
{
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 )
668
665
{
669
- * buf = type & LARGE ? 'X' : 'x' ;
666
+ buf [ i ] = type & LARGE ? 'X' : 'x' ;
670
667
}
671
- ++ buf ;
668
+ ++ i ;
672
669
}
673
670
}
674
671
#endif
@@ -678,38 +675,63 @@ static char *print_number(char *buf,
678
675
{
679
676
while (size -- > 0 )
680
677
{
681
- if (buf <= end )
682
- * buf = (type & ZEROPAD ) ? '0' : ' ' ;
683
-
684
- ++ buf ;
678
+ if (i <= buflen )
679
+ buf [i ] = (type & ZEROPAD ) ? '0' : ' ' ;
680
+ ++ i ;
685
681
}
686
682
}
687
683
688
684
#ifdef RT_PRINTF_PRECISION
689
685
while (i < precision -- )
690
686
{
691
- if (buf <= end )
692
- * buf = '0' ;
693
- ++ buf ;
687
+ if (i <= buflen )
688
+ buf [ i ] = '0' ;
689
+ ++ i ;
694
690
}
695
691
#endif
696
692
697
- /* put number in the temporary buffer */
698
- while ( i -- > 0 )
693
+ /* put number */
694
+ if ( num == 0 )
699
695
{
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
+ }
703
725
}
704
726
705
727
while (size -- > 0 )
706
728
{
707
- if (buf <= end )
708
- * buf = ' ' ;
709
- ++ buf ;
729
+ if (i <= buflen )
730
+ buf [ i ] = ' ' ;
731
+ ++ i ;
710
732
}
711
733
712
- return buf ;
734
+ return buf + i ;
713
735
}
714
736
715
737
rt_int32_t rt_vsnprintf (char * buf ,
0 commit comments