Commit 8ce36643 authored by dearblue's avatar dearblue

Supports width specifier with `mrb_float_to_str()`

Based on src/stdio/vfprintf.c in git://git.musl-libc.org/musl
parent ccd84851
...@@ -92,7 +92,7 @@ typedef char compiler_defines_long_double_incorrectly[9-(int)sizeof(long double) ...@@ -92,7 +92,7 @@ typedef char compiler_defines_long_double_incorrectly[9-(int)sizeof(long double)
#endif #endif
static int static int
fmt_fp(struct fmt_args *f, long double y, ptrdiff_t p, uint8_t fl, int t) fmt_fp(struct fmt_args *f, long double y, ptrdiff_t w, ptrdiff_t p, uint8_t fl, int t)
{ {
uint32_t big[(LDBL_MANT_DIG+28)/29 + 1 // mantissa expansion uint32_t big[(LDBL_MANT_DIG+28)/29 + 1 // mantissa expansion
+ (LDBL_MAX_EXP+LDBL_MANT_DIG+28+8)/9]; // exponent expansion + (LDBL_MAX_EXP+LDBL_MANT_DIG+28+8)/9]; // exponent expansion
...@@ -117,11 +117,11 @@ fmt_fp(struct fmt_args *f, long double y, ptrdiff_t p, uint8_t fl, int t) ...@@ -117,11 +117,11 @@ fmt_fp(struct fmt_args *f, long double y, ptrdiff_t p, uint8_t fl, int t)
if (!isfinite(y)) { if (!isfinite(y)) {
const char *ss = (t&32)?"inf":"INF"; const char *ss = (t&32)?"inf":"INF";
if (y!=y) ss=(t&32)?"nan":"NAN"; if (y!=y) ss=(t&32)?"nan":"NAN";
pad(f, ' ', 0, 3+pl, fl&~ZERO_PAD); pad(f, ' ', w, 3+pl, fl&~ZERO_PAD);
out(f, prefix, pl); out(f, prefix, pl);
out(f, ss, 3); out(f, ss, 3);
pad(f, ' ', 0, 3+pl, fl^LEFT_ADJ); pad(f, ' ', w, 3+pl, fl^LEFT_ADJ);
return 3+(int)pl; return MAX(w, 3+(int)pl);
} }
y = frexp((double)y, &e2) * 2; y = frexp((double)y, &e2) * 2;
...@@ -169,14 +169,14 @@ fmt_fp(struct fmt_args *f, long double y, ptrdiff_t p, uint8_t fl, int t) ...@@ -169,14 +169,14 @@ fmt_fp(struct fmt_args *f, long double y, ptrdiff_t p, uint8_t fl, int t)
else else
l = (s-buf) + (ebuf-estr); l = (s-buf) + (ebuf-estr);
pad(f, ' ', 0, pl+l, fl); pad(f, ' ', w, pl+l, fl);
out(f, prefix, pl); out(f, prefix, pl);
pad(f, '0', 0, pl+l, fl^ZERO_PAD); pad(f, '0', w, pl+l, fl^ZERO_PAD);
out(f, buf, s-buf); out(f, buf, s-buf);
pad(f, '0', l-(ebuf-estr)-(s-buf), 0, 0); pad(f, '0', l-(ebuf-estr)-(s-buf), 0, 0);
out(f, estr, ebuf-estr); out(f, estr, ebuf-estr);
pad(f, ' ', 0, pl+l, fl^LEFT_ADJ); pad(f, ' ', w, pl+l, fl^LEFT_ADJ);
return (int)pl+(int)l; return MAX(w, (int)pl+(int)l);
} }
if (p<0) p=6; if (p<0) p=6;
...@@ -288,9 +288,9 @@ fmt_fp(struct fmt_args *f, long double y, ptrdiff_t p, uint8_t fl, int t) ...@@ -288,9 +288,9 @@ fmt_fp(struct fmt_args *f, long double y, ptrdiff_t p, uint8_t fl, int t)
l += ebuf-estr; l += ebuf-estr;
} }
pad(f, ' ', 0, pl+l, fl); pad(f, ' ', w, pl+l, fl);
out(f, prefix, pl); out(f, prefix, pl);
pad(f, '0', 0, pl+l, fl^ZERO_PAD); pad(f, '0', w, pl+l, fl^ZERO_PAD);
if ((t|32)=='f') { if ((t|32)=='f') {
if (a>r) a=r; if (a>r) a=r;
...@@ -325,21 +325,25 @@ fmt_fp(struct fmt_args *f, long double y, ptrdiff_t p, uint8_t fl, int t) ...@@ -325,21 +325,25 @@ fmt_fp(struct fmt_args *f, long double y, ptrdiff_t p, uint8_t fl, int t)
out(f, estr, ebuf-estr); out(f, estr, ebuf-estr);
} }
pad(f, ' ', 0, pl+l, fl^LEFT_ADJ); pad(f, ' ', w, pl+l, fl^LEFT_ADJ);
return (int)pl+(int)l; return MAX(w, (int)pl+(int)l);
} }
static int static int
fmt_core(struct fmt_args *f, const char *fmt, mrb_float flo) fmt_core(struct fmt_args *f, const char *fmt, mrb_float flo)
{ {
ptrdiff_t p; ptrdiff_t w, p;
if (*fmt != '%') { if (*fmt != '%') {
return -1; return -1;
} }
++fmt; ++fmt;
for (w = 0; ISDIGIT(*fmt); ++fmt) {
w = 10 * w + (*fmt - '0');
}
if (*fmt == '.') { if (*fmt == '.') {
++fmt; ++fmt;
for (p = 0; ISDIGIT(*fmt); ++fmt) { for (p = 0; ISDIGIT(*fmt); ++fmt) {
...@@ -353,7 +357,7 @@ fmt_core(struct fmt_args *f, const char *fmt, mrb_float flo) ...@@ -353,7 +357,7 @@ fmt_core(struct fmt_args *f, const char *fmt, mrb_float flo)
switch (*fmt) { switch (*fmt) {
case 'e': case 'f': case 'g': case 'a': case 'e': case 'f': case 'g': case 'a':
case 'E': case 'F': case 'G': case 'A': case 'E': case 'F': case 'G': case 'A':
return fmt_fp(f, flo, p, 0, *fmt); return fmt_fp(f, flo, w, p, 0, *fmt);
default: default:
return -1; return -1;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment