Skip to content

Commit 64bbf00

Browse files
vskynwf
authored andcommitted
Adding '+' flag to string.format
1 parent 4d78479 commit 64bbf00

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

app/libc/stdio.c

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ llatob(u_quad_t *vp, char *p, int base)
361361
* converts value to ascii, result in dst
362362
*/
363363
char *
364-
btoa(char *dst, u_int value, int base)
364+
btoa(char *dst, u_int value, int base, int plussgn)
365365
{
366366
char buf[34], digit;
367367
int i, j, rem, neg;
@@ -394,6 +394,8 @@ btoa(char *dst, u_int value, int base)
394394
buf[i] = 0;
395395
if (neg)
396396
strcat (buf, "-");
397+
else if (plussgn)
398+
strcat (buf, "+");
397399

398400
/* reverse the string */
399401
for (i = 0, j = strlen (buf) - 1; j >= 0; i++, j--)
@@ -407,7 +409,7 @@ btoa(char *dst, u_int value, int base)
407409
* converts value to ascii, result in dst
408410
*/
409411
char *
410-
llbtoa(char *dst, u_quad_t value, int base)
412+
llbtoa(char *dst, u_quad_t value, int base, int plussgn)
411413
{
412414
char buf[66], digit;
413415
int i, j, rem, neg;
@@ -440,6 +442,8 @@ llbtoa(char *dst, u_quad_t value, int base)
440442
buf[i] = 0;
441443
if (neg)
442444
strcat (buf, "-");
445+
else if (plussgn)
446+
strcat (buf, "+");
443447

444448
/* reverse the string */
445449
for (i = 0, j = strlen (buf) - 1; j >= 0; i++, j--)
@@ -526,7 +530,7 @@ vsprintf (char *d, const char *s, va_list ap)
526530
const char *t;
527531
char *p, *dst, tmp[40];
528532
unsigned int n;
529-
int fmt, trunc, haddot, width, base, longlong;
533+
int fmt, trunc, haddot, width, base, longlong, plussgn;
530534
#ifdef FLOATINGPT
531535
double dbl;
532536

@@ -540,12 +544,14 @@ vsprintf (char *d, const char *s, va_list ap)
540544
if (*s == '%') {
541545
s++;
542546
fmt = FMT_RJUST;
543-
width = trunc = haddot = longlong = 0;
547+
width = trunc = haddot = longlong = plussgn = 0;
544548
for (; *s; s++) {
545549
if (strchr("bcdefgilopPrRsuxX%", *s))
546550
break;
547551
else if (*s == '-')
548552
fmt = FMT_LJUST;
553+
else if (*s == '+')
554+
plussgn = 1;
549555
else if (*s == '0')
550556
fmt = FMT_RJUST0;
551557
else if (*s == '~')
@@ -630,22 +636,22 @@ vsprintf (char *d, const char *s, va_list ap)
630636
base = 2;
631637
if (longlong)
632638
llbtoa(d, va_arg (ap, quad_t),
633-
base);
639+
base, plussgn);
634640
else
635-
btoa(d, va_arg (ap, int), base);
641+
btoa(d, va_arg (ap, int), base, plussgn);
636642

637643
if (*s == 'X')
638644
strtoupper(d);
639645
}
640646
#ifdef FLOATINGPT
641647
else if (strchr ("eEfgG", *s)) {
642-
//static void dtoa (char *, double, int, int, int);
643-
void dtoa (char *dbuf, rtype arg, int fmtch, int width, int prec);
648+
//static void dtoa (char *, double, int, int, int, int);
649+
void dtoa (char *dbuf, rtype arg, int fmtch, int width, int prec, int plussgn);
644650
dbl = va_arg(ap, double);
645651
if (!haddot) {
646652
trunc = 6;
647653
}
648-
dtoa(d, dbl, *s, width, trunc);
654+
dtoa(d, dbl, *s, width, trunc, plussgn);
649655
trunc = 0;
650656
}
651657
#endif
@@ -718,7 +724,7 @@ extern double modf(double, double *);
718724
#define _isNan(arg) ((arg) != (arg))
719725

720726
static int cvt (rtype arg, int prec, char *signp, int fmtch,
721-
char *startp, char *endp);
727+
char *startp, char *endp, int plussgn);
722728
static char *c_round (double fract, int *exp, char *start, char *end,
723729
char ch, char *signp);
724730
static char *exponent(char *p, int exp, int fmtch);
@@ -750,7 +756,7 @@ static int _finite(rtype d)
750756
}
751757

752758

753-
void dtoa (char *dbuf, rtype arg, int fmtch, int width, int prec)
759+
void dtoa (char *dbuf, rtype arg, int fmtch, int width, int prec, int plussgn)
754760
{
755761
char buf[MAX_FCONVERSION+1], *cp;
756762
char sign;
@@ -780,7 +786,7 @@ void dtoa (char *dbuf, rtype arg, int fmtch, int width, int prec)
780786
* no significant digits will be shown.
781787
*/
782788
*cp = '\0';
783-
size = cvt (arg, prec, &sign, fmtch, cp, buf + sizeof(buf));
789+
size = cvt (arg, prec, &sign, fmtch, cp, buf + sizeof(buf), plussgn);
784790
if (*cp == '\0')
785791
cp++;
786792

@@ -793,7 +799,7 @@ void dtoa (char *dbuf, rtype arg, int fmtch, int width, int prec)
793799

794800

795801
static int
796-
cvt(rtype number, int prec, char *signp, int fmtch, char *startp, char *endp)
802+
cvt(rtype number, int prec, char *signp, int fmtch, char *startp, char *endp, int plussgn)
797803
{
798804
register char *p, *t;
799805
register double fract;
@@ -804,6 +810,8 @@ cvt(rtype number, int prec, char *signp, int fmtch, char *startp, char *endp)
804810
if (number < 0) {
805811
number = -number;
806812
*signp = '-';
813+
} else if (plussgn) {
814+
*signp = '+';
807815
} else
808816
*signp = 0;
809817

@@ -1001,14 +1009,14 @@ c_round(double fract, int *exp, char *start, char *end, char ch, char *signp)
10011009
++*exp;
10021010
}
10031011
else { /* f; add extra digit */
1004-
*--end = '1';
1005-
--start;
1012+
*--end = '1';
1013+
--start;
10061014
}
10071015
break;
10081016
}
10091017
}
10101018
/* ``"%.3f", (double)-0.0004'' gives you a negative 0. */
1011-
else if (*signp == '-')
1019+
else if (*signp == '-' || *signp == '+')
10121020
for (;; --end) {
10131021
if (*end == '.')
10141022
--end;

0 commit comments

Comments
 (0)