21#define XWVSNPF_F_ZEROPAD 0x01
22#define XWVSNPF_F_SIGN 0x02
23#define XWVSNPF_F_PLUS 0x04
24#define XWVSNPF_F_SPACE 0x08
25#define XWVSNPF_F_LEFT 0x10
26#define XWVSNPF_F_SMALL 0x20
27#define XWVSNPF_F_SPECIAL 0x40
72 while (isdigit((
int)(**s))) {
73 i = i * 10 + **s -
'0';
82 unsigned int d3, d2, d1, d0;
88 d0 = 6 * (d3 + d2 + d1) + (q & 0xf);
89 q = (d0 * 0xcd) >> 11;
91 *buf++ = (char)d0 +
'0';
93 d1 = q + 9 * d3 + 5 * d2 + d1;
95 q = (d1 * 0xcd) >> 11;
97 *buf++ = (char)d1 +
'0';
100 if ((d2 != 0) || (d3 != 0)) {
103 *buf++ = (char)d2 +
'0';
107 q = (d3 * 0xcd) >> 11;
109 *buf++ = (char)d3 +
'0';
111 *buf++ = (char)q +
'0';
122 unsigned int d3, d2, d1, d0;
128 d0 = 6 * (d3 + d2 + d1) + (q & 0xf);
129 q = (d0 * 0xcd) >> 11;
131 *buf++ = (char)d0 +
'0';
133 d1 = q + 9 * d3 + 5 * d2 + d1;
134 q = (d1 * 0xcd) >> 11;
136 *buf++ = (char)d1 +
'0';
141 *buf++ = (char)d2 +
'0';
144 q = (d3 * 0xcd) >> 11;
147 *buf++ = (char)d3 +
'0';
148 *buf++ = (char)q +
'0';
176 bool is_zero = num == 0LL;
184 if ((
signed long long)num < 0) {
186 num = (
unsigned long long)(-(
signed long long)num);
197 if (16 == spec.
base || 2 == spec.
base) {
199 }
else if (!is_zero) {
206 if (num < spec.
base) {
214 }
else if (spec.
base != 10) {
215 int mask = spec.
base - 1;
218 if (spec.
base == 16) {
220 }
else if (spec.
base == 2) {
255 if (spec.
base == 16 || spec.
base == 2 || !is_zero) {
261 if (spec.
base == 16) {
263 *buf = (
'X' | locase);
266 }
else if (spec.
base == 2) {
268 *buf = (
'B' | locase);
317 if (size <= spaces) {
318 memset(buf,
' ', (
xwsz_t)size);
322 if (len > size - spaces) {
325 memmove(buf + spaces, buf, (
xwsz_t)len);
327 memset(buf,
' ', (
xwsz_t)spaces);
401 default_width = (int)(2 *
sizeof(
void *) +
423 while (num > 0 && i < 29) {
424 tmp[i++] = (char)(
'0' + (num % 10));
429 while (i < digits && i < 29) {
433 for (j = i - 1; j >= 0; j--) {
452 unsigned long long int_part = 0;
453 unsigned long long frac_part = 0;
460 if (buf < end) *buf++ =
'n';
461 if (buf < end) *buf++ =
'a';
462 if (buf < end) *buf++ =
'n';
468 if (buf < end) *buf++ =
'-';
470 if (buf < end) *buf++ =
'i';
471 if (buf < end) *buf++ =
'n';
472 if (buf < end) *buf++ =
'f';
493 if (abs_num == 0.0) {
495 }
else if (abs_num >= 1.0) {
496 while (abs_num >= 10.0) {
501 while (abs_num < 1.0) {
508 int_part = (
unsigned long long)abs_num;
509 double frac = abs_num - (double)int_part;
511 for (i = 0; i < precision; i++) {
514 frac_part = (
unsigned long long)(frac * mult + 0.5);
516 if (frac_part >= (
unsigned long long)mult) {
517 frac_part -= (
unsigned long long)mult;
542 len = (int)(p - tmp);
544 int pad_len = (total_width > len + need_sign) ? (total_width - len - need_sign) : 0;
548 if (pad_char ==
'0' && sign) {
549 if (buf < end) *buf++ = sign;
553 for (i = 0; i < pad_len; i++) {
554 if (buf < end) *buf++ = pad_char;
559 if (buf < end) *buf++ = sign;
562 for (i = 0; i < len; i++) {
563 if (buf < end) *buf++ = tmp[i];
567 for (i = 0; i < pad_len; i++) {
568 if (buf < end) *buf++ =
' ';
585 unsigned long long int_part = 0;
586 unsigned long long frac_part = 0;
593 if (buf < end) *buf++ =
'n';
594 if (buf < end) *buf++ =
'a';
595 if (buf < end) *buf++ =
'n';
601 if (buf < end) *buf++ =
'-';
603 if (buf < end) *buf++ =
'i';
604 if (buf < end) *buf++ =
'n';
605 if (buf < end) *buf++ =
'f';
626 if (abs_num == 0.0L) {
628 }
else if (abs_num >= 1.0L) {
629 while (abs_num >= 10.0L) {
634 while (abs_num < 1.0L) {
641 int_part = (
unsigned long long)abs_num;
642 long double frac = abs_num - (
long double)int_part;
643 long double mult = 1.0L;
644 for (i = 0; i < precision; i++) {
647 frac_part = (
unsigned long long)(frac * mult + 0.5L);
649 if (frac_part >= (
unsigned long long)mult) {
650 frac_part -= (
unsigned long long)mult;
675 len = (int)(p - tmp);
677 int pad_len = (total_width > len + need_sign) ? (total_width - len - need_sign) : 0;
681 if (pad_char ==
'0' && sign) {
682 if (buf < end) *buf++ = sign;
686 for (i = 0; i < pad_len; i++) {
687 if (buf < end) *buf++ = pad_char;
692 if (buf < end) *buf++ = sign;
695 for (i = 0; i < len; i++) {
696 if (buf < end) *buf++ = tmp[i];
700 for (i = 0; i < pad_len; i++) {
701 if (buf < end) *buf++ =
' ';
712 const char * start = fmt;
735 for (; *fmt ; fmt++) {
742 if ((fmt != start) || !(*fmt)) {
779 if (isdigit((
int)(*fmt))) {
781 }
else if (
'*' == *fmt) {
793 if (isdigit((
int)(*fmt))) {
798 }
else if (*fmt ==
'*') {
809 if ((
'h' == *fmt) || (
'l' == *fmt) || (
'L' == *fmt) ||
810 (
'z' == *fmt) || (
'Z' == *fmt) || (
't' == *fmt)) {
962 size = (
xwsz_t)(end - buf);
966 const char * oldfmt = fmt;
973 if (copy > end - str) {
976 memcpy(str, oldfmt, (
xwsz_t)copy);
1001 c = (char)va_arg(args,
int);
1022 va_arg(args,
void *), spec);
1023 while (isalnum((
int)(*fmt))) {
1053 switch (spec.
type) {
1055 num = (
xwu64_t)va_arg(args,
long long);
1058 num = (
xwu64_t)va_arg(args,
unsigned long);
1061 num = (
xwu64_t)va_arg(args,
long);
1071 num = (
xwu64_t)va_arg(args, ptrdiff_t);
1074 num = (
xwu64_t)va_arg(args,
int);
1077 num = (
xwu64_t)va_arg(args,
int);
1080 num = (
xwu64_t)va_arg(args,
int);
1083 num = (
xwu64_t)va_arg(args,
int);
1086 num = (
xwu64_t)va_arg(args,
int);
1089 num = (
xwu64_t)va_arg(args,
unsigned int);
1112 va_start(args, fmt);
1113 i =
xwvsnpf(buf, size, fmt, args);
1119int xwvspf(
char * buf,
const char * fmt, va_list args)
1124int xwspf(
char * buf,
const char * fmt, ...)
1129 va_start(args, fmt);
1130 i =
xwvspf(buf, fmt, args);
xwu32_t xwdiv64(xwu64_t *n, xwu32_t divisor)
64位除法运算
int xwspf(char *buf, const char *fmt,...)
格式化字符串(同sprintf)
int xwsnpf(char *buf, xwsz_t size, const char *fmt,...)
格式化字符串(同snprintf)
int xwvsnpf(char *buf, xwsz_t size, const char *fmt, va_list args)
格式化字符串(同vsnprintf)
int xwvspf(char *buf, const char *fmt, va_list args)
格式化字符串(同vsprintf)
static char * xwvsnpf_format_widen_string(char *buf, xwssz_t n, char *end, struct xwvsnpf_format_spec spec)
static int xwvsnpf_format_decode(const char *fmt, struct xwvsnpf_format_spec *spec)
static char * xwvsnpf_put_float_decimal(char *buf, char *end, unsigned long long num, int digits)
static char * xwvsnpf_format_string(char *buf, char *end, const char *s, struct xwvsnpf_format_spec spec)
static char * xwvsnpf_format_pointer(const char *fmt, char *buf, char *end, void *ptr, struct xwvsnpf_format_spec spec)
static char * xwvsnpf_format_string_nocheck(char *buf, char *end, const char *s, struct xwvsnpf_format_spec spec)
@ XWVSNPF_FT_PERCENT_CHAR
@ XWVSNPF_FT_LONG_DOUBLE_SCI
static char * xwvsnpf_put_dec_trunc(char *buf, unsigned int q)
#define XWVSNPF_F_SPECIAL
static void xwvsnpf_format_move_right(char *buf, char *end, xwssz_t len, xwssz_t spaces)
static const char xwvsnpf_nullstr[]
static char * xwvsnpf_format_long_double(char *buf, char *end, long double num, struct xwvsnpf_format_spec spec)
static char * xwvsnpf_put_dec_full(char *buf, unsigned int q)
#define XWVSNPF_F_ZEROPAD
static const char xwvsnpf_digits[]
static char * xwvsnpf_format_number(char *buf, char *end, xwu64_t num, struct xwvsnpf_format_spec spec)
static char * xwvsnpf_format_float(char *buf, char *end, double num, struct xwvsnpf_format_spec spec)
static int xwvsnpf_skip_atoi(const char **s)
static char * xwvsnpf_put_dec(char *buf, unsigned long long num)