37 int v = 0, n_s, base, fmt,
flags;
39 char *sp, *ssp, *endsp, *ep, *endep;
40 int dot, width, precis, n, n_output = 0;
55 char decimal = 0, thousand = 0;
57#define SFputc(f,c) do { \
58 if (putc((c), (f)) == EOF) { \
62#define SFnputc(f,c,n) do { \
63 for (int i_ = 0; i_ < (n); ++i_) { \
64 if (putc((c), (f)) == EOF) { \
70#define SFwrite(f,s,n) do { \
71 if ((n) > 0 && fwrite((s), (size_t)(n), 1, (f)) < 1) { \
87 const char *form = argv.
ft->
form;
94 while (*form && *form !=
'%')
103 size = width = precis = base = n_s = argp = -1;
106 endsp = sp = buf + (
sizeof(buf) - 1);
111 switch ((fmt = *form++)) {
135 n_str = (form - 1) - t_str;
141 n = ft->
extf(&argv, ft);
145 if ((t_str = argv.
s) && (n_str = (
int)ft->
size) < 0)
146 n_str = (ssize_t)strlen(t_str);
183 }
else if (
dot == 2) {
185 if (*form ==
'c' || *form ==
's')
188 (form[1] ==
'c' || form[1] ==
's')) {
201 }
else if (*form !=
'*')
210 if (ft->
extf(&argv, ft) < 0)
226 for (v = fmt -
'0';
gv_isdigit(*form); ++form)
227 v = v * 10 + (*form -
'0');
230 if ((width = v) < 0) {
245 size = size * 10 + (n -
'0');
246 }
else if (*form ==
'*') {
250 FMTSET(ft, form,
'I',
sizeof(
int), 0, 0, 0, 0,
NULL, 0);
251 if (ft->
extf(&argv, ft) < 0)
260 flags &= ~SFFMT_TYPES;
269 flags &= ~SFFMT_TYPES;
313 argp =
FP_SET(argp, argn);
314 FMTSET(ft, form, fmt, size,
flags, width, precis, base, t_str, n_str);
315 v = ft->
extf(&argv, ft);
320 FMTGET(ft, form, fmt, size,
flags, width, precis, base);
335 if (!(ls = argv.
sp) || !ls[0])
344 if ((v = size) >= 0) {
345 if (precis >= 0 && v > precis)
347 }
else if (precis < 0)
350 for (v = 0; v < precis; ++v)
354 if ((n = width - v) > 0) {
375 if (!(sp = argv.
s) || !sp[0])
410 if (
sizeof(
void*) >
sizeof(int)) {
411 lv = (
long long)(intptr_t)argv.
vp;
414 v = (int)(intptr_t)argv.
vp;
424 ssp =
"0123456789ABCDEF";
442 if ((base & (n_s = base - 1)) == 0) {
444 n = base < 4 ? 1 : 2;
446 n = base < 16 ? 3 : 4;
448 n = base < 64 ? 5 : 6;
450 n_s = base == 10 ? -1 : 0;
453 if ((
sizeof(
long) >
sizeof(int) ||
sizeof(
void*) >
sizeof(int))
454 &&
FMTCMP(size,
long long,
long long)) {
457 }
else if ((
sizeof(
long) >
sizeof(
int) ||
sizeof(
void*) >
sizeof(
int))
458 &&
FMTCMP(size,
long,
long long)) {
460 lv = (
long long)argv.
l;
462 lv = (
long long)(
ulong)argv.
l;
464 if (lv == 0 && precis == 0)
466 if (lv < 0 && fmt ==
'd') {
468 if ((
unsigned long long)lv ==
HIGHBITL) {
469 lv = (
long long)(
HIGHBITL / (
unsigned long long)base);
471 (
unsigned long long)lv * (
unsigned long long)base];
477 sfucvt(lv, sp, nv, ssp,
long long,
unsigned long long);
478 }
else if (n_s > 0) {
480 *--sp = ssp[lv & n_s];
481 }
while ((lv = (
unsigned long long)lv >> n));
484 *--sp = ssp[(
unsigned long long)lv % (
unsigned long long)base];
485 }
while ((lv = (
unsigned long long)lv / (
unsigned long long)base));
488 if (
sizeof(
short) <
sizeof(int) &&
FMTCMP(size,
short,
long long)) {
491 v = (int) ((
short) argv.
h);
496 v = (int) ((
short) argv.
i);
501 }
else if (size ==
sizeof(
char)) {
504 v = (int) ((
char) argv.
c);
506 v = (int) ((
uchar) argv.
c);
509 v = (int) ((
char) argv.
i);
511 v = (int) ((
uchar) argv.
i);
517 if (v == 0 && precis == 0)
519 if (v < 0 && fmt ==
'd') {
522 v = (int)(
HIGHBITI / (
unsigned)base);
528 sfucvt(v, sp, n, ssp,
int,
unsigned);
529 }
else if (n_s > 0) {
531 *--sp = ssp[v & n_s];
532 }
while ((v = (
unsigned)v >> n));
535 *--sp = ssp[(unsigned)v % (
unsigned)base];
536 }
while ((v = (
unsigned)v / (
unsigned)base));
541 && (n = endsp - sp) > 3) {
544 for (ep = buf +
SLACK, endep = ep + n;;) {
558 if (precis > 0 && (precis -= (endsp - sp)) < (sp - buf) - 64)
568 if (fmt ==
'x' || fmt ==
'X')
573 n = base < 10 ? 2 : 3;
575 n = width - (n + (endsp - sp));
579 if (fmt ==
'x' || fmt ==
'X') {
582 }
else if (
dot >= 2) {
585 *--sp = (char) (
'0' + base);
587 *--sp =
_Sfdec[(base <<= 1) + 1];
604 dval = (double) argv.
f;
606 if (fmt ==
'e' || fmt ==
'E') {
607 n = (precis = precis < 0 ?
FPRECIS : precis) + 1;
613 }
else if (fmt ==
'f' || fmt ==
'F') {
614 precis = precis < 0 ?
FPRECIS : precis;
623 precis = precis < 0 ?
FPRECIS : precis == 0 ? 1 : precis;
634 if ((n =
sfslen()) > precis)
636 while ((n -= 1) >= 1 && ep[n] ==
'0');
641 if (decpt < -3 || decpt > precis) {
652 sp = endsp = buf + 1;
653 *endsp++ = *ep ? *ep++ :
'0';
660 while ((*endsp++ = *ep++) && ep <= endep);
661 precis -= (endsp -= 1) - ssp;
664 ep = endep = buf + (
sizeof(buf) - 1);
666 if ((n = decpt - 1) < 0)
671 *--ep = (char) (
'0' + (v - n * 10));
675 *--ep = (char) (
'0' + n);
680 *--ep = (decpt > 0 || dval == 0.) ?
'+' :
'-';
688 endsp = (sp = ep) +
sfslen();
695 endsp = sp = buf + 1;
698 if ((n = decpt % 3) == 0)
700 while (ep < endep && (*endsp++ = *ep++)) {
701 if (--n == 0 && (ep <= endep - 3)) {
707 while (ep < endep && (*endsp++ = *ep++));
715 if ((n = -decpt) > 0) {
716 ssp = endsp +
imin(n, precis);
724 while ((*endsp++ = *ep++) && ep <= endep);
725 precis -= (endsp -= 1) - ssp;
734 if (
flags == 0 && width <= 0)
742 n = (endsp - sp) + (endep - ep) + (precis <= 0 ? 0 : precis) +
744 if ((v = width - n) <= 0)
751 flags &= ~SFFMT_PREFIX;
772 if ((n = endsp - sp) > 0)
776 if ((n = precis) > 0)
780 if ((n = endep - (sp = ep)) > 0)
#define FMTGET(ft, frm, fv, sz, flgs, wid, pr, bs)
#define FMTSET(ft, frm, fv, sz, flgs, wid, pr, bs, ts, ns)