2 * Copyright 2011 Piotr Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #define APICHAR wchar_t
24 #define FUNC_NAME(func) func ## _w
27 #define CONVCHAR wchar_t
28 #define FUNC_NAME(func) func ## _a
31 struct FUNC_NAME(_str_ctx
) {
36 static int FUNC_NAME(puts_clbk_str
)(void *ctx
, int len
, const APICHAR
*str
)
38 struct FUNC_NAME(_str_ctx
) *out
= ctx
;
44 memmove(out
->buf
, str
, out
->len
*sizeof(APICHAR
));
50 memmove(out
->buf
, str
, len
*sizeof(APICHAR
));
56 static inline const APICHAR
* FUNC_NAME(pf_parse_int
)(const APICHAR
*fmt
, int *val
)
60 while (*fmt
>= '0' && *fmt
<= '9') {
68 /* pf_fill: takes care of signs, alignment, zero and field padding */
69 static inline int FUNC_NAME(pf_fill
)(FUNC_NAME(puts_clbk
) pf_puts
, void *puts_ctx
,
70 int len
, pf_flags
*flags
, BOOL left
)
72 int i
, r
= 0, written
;
74 if(flags
->Sign
&& !strchr("diaAeEfFgG", flags
->Format
))
77 if(left
&& flags
->Sign
) {
78 APICHAR ch
= flags
->Sign
;
81 r
= pf_puts(puts_ctx
, 1, &ch
);
85 if((!left
&& flags
->LeftAlign
) || (left
&& !flags
->LeftAlign
)) {
88 if(left
&& flags
->PadZero
)
93 for(i
=0; i
<flags
->FieldLength
-len
&& r
>=0; i
++) {
94 r
= pf_puts(puts_ctx
, 1, &ch
);
100 if(r
>=0 && left
&& flags
->Sign
&& !flags
->PadZero
) {
101 APICHAR ch
= flags
->Sign
;
102 r
= pf_puts(puts_ctx
, 1, &ch
);
106 return r
>=0 ? written
: r
;
109 #ifndef PRINTF_HELPERS
110 #define PRINTF_HELPERS
111 static inline int wcstombs_len(char *mbstr
, const wchar_t *wcstr
,
112 int len
, _locale_t locale
)
114 char buf
[MB_LEN_MAX
];
117 for(i
=0; i
<len
; i
++) {
118 r
= MSVCRT__wctomb_l(mbstr
? mbstr
+mblen
: buf
, wcstr
[i
], locale
);
125 static inline int mbstowcs_len(wchar_t *wcstr
, const char *mbstr
,
126 int len
, _locale_t locale
)
131 for(i
=0; i
<len
; wlen
++) {
132 r
= _mbtowc_l(wcstr
? wcstr
+wlen
: &buf
, mbstr
+i
, len
-i
, locale
);
140 static inline int FUNC_NAME(pf_output_wstr
)(FUNC_NAME(puts_clbk
) pf_puts
, void *puts_ctx
,
141 const wchar_t *str
, int len
, _locale_t locale
)
144 return pf_puts(puts_ctx
, len
, str
);
147 int len_a
= wcstombs_len(NULL
, str
, len
, locale
);
151 out
= HeapAlloc(GetProcessHeap(), 0, len_a
);
155 wcstombs_len(out
, str
, len
, locale
);
156 len
= pf_puts(puts_ctx
, len_a
, out
);
157 HeapFree(GetProcessHeap(), 0, out
);
162 static inline int FUNC_NAME(pf_output_str
)(FUNC_NAME(puts_clbk
) pf_puts
, void *puts_ctx
,
163 const char *str
, int len
, _locale_t locale
)
167 int len_w
= mbstowcs_len(NULL
, str
, len
, locale
);
171 out
= HeapAlloc(GetProcessHeap(), 0, len_w
*sizeof(WCHAR
));
175 mbstowcs_len(out
, str
, len
, locale
);
176 len
= pf_puts(puts_ctx
, len_w
, out
);
177 HeapFree(GetProcessHeap(), 0, out
);
180 return pf_puts(puts_ctx
, len
, str
);
184 static inline int FUNC_NAME(pf_output_format_wstr
)(FUNC_NAME(puts_clbk
) pf_puts
, void *puts_ctx
,
185 const wchar_t *str
, int len
, pf_flags
*flags
, _locale_t locale
)
190 /* Do not search past the length specified by the precision. */
191 if(flags
->Precision
>=0)
192 len
= MSVCRT_wcsnlen(str
, flags
->Precision
);
194 len
= MSVCRT_wcslen(str
);
197 if(flags
->Precision
>=0 && flags
->Precision
<len
)
198 len
= flags
->Precision
;
200 r
= FUNC_NAME(pf_fill
)(pf_puts
, puts_ctx
, len
, flags
, TRUE
);
203 r
= FUNC_NAME(pf_output_wstr
)(pf_puts
, puts_ctx
, str
, len
, locale
);
207 r
= FUNC_NAME(pf_fill
)(pf_puts
, puts_ctx
, len
, flags
, FALSE
);
211 return r
>=0 ? ret
: r
;
214 static inline int FUNC_NAME(pf_output_format_str
)(FUNC_NAME(puts_clbk
) pf_puts
, void *puts_ctx
,
215 const char *str
, int len
, pf_flags
*flags
, _locale_t locale
)
220 /* Do not search past the length specified by the precision. */
221 if(flags
->Precision
>=0)
222 len
= strnlen(str
, flags
->Precision
);
227 if(flags
->Precision
>=0 && flags
->Precision
<len
)
228 len
= flags
->Precision
;
230 r
= FUNC_NAME(pf_fill
)(pf_puts
, puts_ctx
, len
, flags
, TRUE
);
233 r
= FUNC_NAME(pf_output_str
)(pf_puts
, puts_ctx
, str
, len
, locale
);
237 r
= FUNC_NAME(pf_fill
)(pf_puts
, puts_ctx
, len
, flags
, FALSE
);
241 return r
>=0 ? ret
: r
;
244 static inline int FUNC_NAME(pf_handle_string
)(FUNC_NAME(puts_clbk
) pf_puts
, void *puts_ctx
,
245 const void *str
, int len
, pf_flags
*flags
, _locale_t locale
, BOOL legacy_wide
)
247 BOOL api_is_wide
= sizeof(APICHAR
) == sizeof(wchar_t);
248 BOOL complement_is_narrow
= legacy_wide
? api_is_wide
: FALSE
;
252 return FUNC_NAME(pf_output_format_wstr
)(pf_puts
, puts_ctx
, L
"(null)", 6, flags
, locale
);
255 return FUNC_NAME(pf_output_format_str
)(pf_puts
, puts_ctx
, "(null)", 6, flags
, locale
);
258 if((flags
->NaturalString
&& api_is_wide
) || flags
->WideString
|| flags
->IntegerLength
== LEN_LONG
)
259 return FUNC_NAME(pf_output_format_wstr
)(pf_puts
, puts_ctx
, str
, len
, flags
, locale
);
260 if((flags
->NaturalString
&& !api_is_wide
) || flags
->IntegerLength
== LEN_SHORT
)
261 return FUNC_NAME(pf_output_format_str
)(pf_puts
, puts_ctx
, str
, len
, flags
, locale
);
263 if((flags
->Format
=='S' || flags
->Format
=='C') == complement_is_narrow
)
264 return FUNC_NAME(pf_output_format_str
)(pf_puts
, puts_ctx
, str
, len
, flags
, locale
);
266 return FUNC_NAME(pf_output_format_wstr
)(pf_puts
, puts_ctx
, str
, len
, flags
, locale
);
269 static inline int FUNC_NAME(pf_output_special_fp
)(FUNC_NAME(puts_clbk
) pf_puts
, void *puts_ctx
,
270 double v
, pf_flags
*flags
, _locale_t locale
,
271 BOOL legacy_msvcrt_compat
, BOOL three_digit_exp
)
273 APICHAR pfx
[16], sfx
[8], *p
;
274 int len
= 0, r
, frac_len
, pfx_len
, sfx_len
;
276 if(!legacy_msvcrt_compat
) {
280 if(strchr("AEFG", flags
->Format
)) str
= "INF";
283 if(strchr("AEFG", flags
->Format
)) str
= (flags
->Sign
== '-' ? "NAN(IND)" : "NAN");
284 else str
= (flags
->Sign
== '-' ? "nan(ind)" : "nan");
287 flags
->Precision
= -1;
288 flags
->PadZero
= FALSE
;
289 return FUNC_NAME(pf_output_format_str
)(pf_puts
, puts_ctx
, str
, -1, flags
, locale
);
292 /* workaround a bug in native implementation */
293 if(flags
->Format
=='g' || flags
->Format
=='G')
297 if(flags
->PadZero
&& (flags
->Format
=='a' || flags
->Format
=='A')) {
298 if (flags
->Sign
) *p
++ = flags
->Sign
;
300 *p
++ = (flags
->Format
=='a' ? 'x' : 'X');
301 r
= pf_puts(puts_ctx
, p
-pfx
, pfx
);
305 flags
->FieldLength
-= p
-pfx
;
309 if(!flags
->PadZero
&& (flags
->Format
=='a' || flags
->Format
=='A')) {
311 *p
++ = (flags
->Format
=='a' ? 'x' : 'X');
315 *p
++ = *(locale
? locale
->locinfo
: get_locinfo())->lconv
->decimal_point
;
324 }else if(flags
->Sign
== '-') {
339 if(len
) flags
->Sign
= 0;
341 if(flags
->Precision
>=0 && flags
->Precision
<frac_len
)
342 p
[flags
->Precision
- frac_len
- 1]++;
345 if(strchr("aAeE", flags
->Format
)) {
346 if(flags
->Format
== 'a') *p
++ = 'p';
347 else if(flags
->Format
== 'A') *p
++ = 'P';
348 else if(flags
->Format
== 'e') *p
++ = 'e';
354 if(flags
->Format
== 'e' || flags
->Format
== 'E') {
356 if(three_digit_exp
) *p
++ = '0';
361 if(!flags
->Alternate
&& (flags
->Format
== 'g' || flags
->Format
== 'G')) sfx_len
= frac_len
;
362 else sfx_len
= flags
->Precision
;
365 if(strchr("fFeE", flags
->Format
)) sfx_len
= 6;
366 else if(flags
->Format
== 'a' || flags
->Format
== 'A') sfx_len
= 13;
368 sfx_len
+= p
- sfx
- frac_len
;
370 if(sfx_len
> 0) flags
->FieldLength
-= sfx_len
;
371 if(flags
->Precision
>= 0) {
372 if(!flags
->Precision
) flags
->Precision
--;
373 flags
->Precision
+= pfx_len
- frac_len
;
376 r
= FUNC_NAME(pf_output_format_wstr
)(pf_puts
, puts_ctx
, pfx
, -1, flags
, locale
);
378 r
= FUNC_NAME(pf_output_format_str
)(pf_puts
, puts_ctx
, pfx
, -1, flags
, locale
);
383 flags
->FieldLength
= sfx_len
;
384 flags
->PadZero
= TRUE
;
385 flags
->Precision
= -1;
388 r
= FUNC_NAME(pf_output_format_wstr
)(pf_puts
, puts_ctx
, sfx
, -1, flags
, locale
);
390 r
= FUNC_NAME(pf_output_format_str
)(pf_puts
, puts_ctx
, sfx
, -1, flags
, locale
);
398 static inline int FUNC_NAME(pf_output_hex_fp
)(FUNC_NAME(puts_clbk
) pf_puts
, void *puts_ctx
,
399 double v
, pf_flags
*flags
, _locale_t locale
, BOOL standard_rounding
)
401 const APICHAR digits
[2][16] = {
402 { '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f' },
403 { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' }
406 APICHAR pfx
[4+MANT_BITS
/4+1], sfx
[8], *p
;
408 int len
= 0, sfx_len
= 0, r
, exp
;
410 mant
= (*(ULONGLONG
*)&v
) << 1;
411 exp
= (mant
>> MANT_BITS
);
412 exp
-= (1 << (EXP_BITS
- 1)) - 1;
413 mant
= (mant
<< EXP_BITS
) >> (EXP_BITS
+1);
417 if(flags
->Sign
) *p
++ = flags
->Sign
;
419 *p
++ = (flags
->Format
=='a' ? 'x' : 'X');
420 r
= pf_puts(puts_ctx
, p
-pfx
, pfx
);
424 flags
->FieldLength
-= p
-pfx
;
429 *p
++ = (flags
->Format
=='a' ? 'x' : 'X');
431 if(exp
== -(1 << (EXP_BITS
-1))+1) {
438 *p
++ = *(locale
? locale
->locinfo
: get_locinfo())->lconv
->decimal_point
;
439 for(r
=MANT_BITS
/4-1; r
>=0; r
--) {
440 p
[r
] = digits
[flags
->Format
== 'A'][mant
& 15];
443 if(!flags
->Precision
) {
444 if(p
[0] >= '8') p
[-2]++;
445 if(!flags
->Alternate
) p
--;
446 }else if(flags
->Precision
>0 && flags
->Precision
<MANT_BITS
/4) {
447 BOOL round_up
= FALSE
;
449 if(!standard_rounding
) round_up
= (p
[flags
->Precision
] >= '8');
450 else if(p
[flags
->Precision
] > '8') round_up
= TRUE
;
451 else if(p
[flags
->Precision
] == '8') {
452 for(r
= flags
->Precision
+1; r
<MANT_BITS
/4; r
++) {
460 if(p
[flags
->Precision
-1] <= '9') round_up
= (p
[flags
->Precision
-1] - '0') & 1;
461 else if(p
[flags
->Precision
-1] <= 'F') round_up
= (p
[flags
->Precision
-1] - 'A') & 1;
462 else round_up
= (p
[flags
->Precision
-1] - 'a') & 1;
466 for(r
=flags
->Precision
-1; r
>=0 && round_up
; r
--) {
468 if(p
[r
]=='f' || p
[r
]=='F') {
471 }else if(p
[r
] == '9') {
472 p
[r
] = (flags
->Format
== 'a' ? 'a' : 'A');
477 if(round_up
) p
[-2]++;
478 p
+= flags
->Precision
;
481 if(flags
->Precision
> MANT_BITS
/4) sfx_len
+= flags
->Precision
- MANT_BITS
/4;
486 *p
++ = (flags
->Format
== 'a' ? 'p' : 'P');
493 for(r
=3; r
>=0; r
--) {
498 for(exp
=0; exp
<4-r
; exp
++)
504 flags
->FieldLength
-= sfx_len
;
505 flags
->Precision
= -1;
507 r
= FUNC_NAME(pf_output_format_wstr
)(pf_puts
, puts_ctx
, pfx
, -1, flags
, locale
);
509 r
= FUNC_NAME(pf_output_format_str
)(pf_puts
, puts_ctx
, pfx
, -1, flags
, locale
);
514 flags
->FieldLength
= sfx_len
;
515 flags
->PadZero
= TRUE
;
518 r
= FUNC_NAME(pf_output_format_wstr
)(pf_puts
, puts_ctx
, sfx
, -1, flags
, locale
);
520 r
= FUNC_NAME(pf_output_format_str
)(pf_puts
, puts_ctx
, sfx
, -1, flags
, locale
);
528 /* pf_integer_conv: prints x to buf, including alternate formats and
529 additional precision digits, but not field characters or the sign */
530 static inline void FUNC_NAME(pf_integer_conv
)(APICHAR
*buf
, pf_flags
*flags
, LONGLONG x
)
536 if(flags
->Format
== 'o')
538 else if(flags
->Format
=='x' || flags
->Format
=='X')
543 if(flags
->Format
== 'X')
544 digits
= "0123456789ABCDEFX";
546 digits
= "0123456789abcdefx";
548 if(x
<0 && (flags
->Format
=='d' || flags
->Format
=='i')) {
555 flags
->Alternate
= FALSE
;
560 j
= (ULONGLONG
)x
%base
;
561 x
= (ULONGLONG
)x
/base
;
562 buf
[i
++] = digits
[j
];
565 k
= flags
->Precision
-i
;
568 if(flags
->Alternate
) {
570 buf
[i
++] = digits
[16];
572 } else if(base
==8 && buf
[i
-1]!='0')
576 /* Adjust precision so pf_fill won't truncate the number later */
577 flags
->Precision
= i
;
582 APICHAR tmp
= buf
[j
];
589 static inline int FUNC_NAME(pf_output_fp
)(FUNC_NAME(puts_clbk
) pf_puts
, void *puts_ctx
,
590 double v
, pf_flags
*flags
, _locale_t locale
, BOOL three_digit_exp
,
591 BOOL standard_rounding
)
593 int e2
, e10
= 0, round_pos
, round_limb
, radix_pos
, first_limb_len
, i
, len
, r
, ret
;
594 BYTE bnum_data
[FIELD_OFFSET(struct bnum
, data
[BNUM_PREC64
])];
595 struct bnum
*b
= (struct bnum
*)bnum_data
;
596 APICHAR buf
[LIMB_DIGITS
+ 1];
597 BOOL trim_tail
= FALSE
, round_up
= FALSE
;
603 if(flags
->Precision
== -1)
604 flags
->Precision
= 6;
606 v
= MSVCRT_frexp(v
, &e2
);
608 m
= (ULONGLONG
)1 << (MANT_BITS
- 1);
609 m
|= (*(ULONGLONG
*)&v
& (((ULONGLONG
)1 << (MANT_BITS
- 1)) - 1));
612 b
->size
= BNUM_PREC64
;
613 b
->data
[0] = m
% LIMB_MAX
;
614 b
->data
[1] = m
/ LIMB_MAX
;
618 int shift
= e2
> 29 ? 29 : e2
;
619 if(bnum_lshift(b
, shift
)) e10
+= LIMB_DIGITS
;
623 int shift
= -e2
> 9 ? 9 : -e2
;
624 if(bnum_rshift(b
, shift
)) e10
-= LIMB_DIGITS
;
630 b
->size
= BNUM_PREC64
;
635 if(!b
->data
[bnum_idx(b
, b
->e
-1)])
638 first_limb_len
= MSVCRT_floor(MSVCRT_log10(b
->data
[bnum_idx(b
, b
->e
- 1)])) + 1;
639 radix_pos
= first_limb_len
+ LIMB_DIGITS
+ e10
;
641 round_pos
= flags
->Precision
;
642 if(flags
->Format
=='f' || flags
->Format
=='F')
643 round_pos
+= radix_pos
;
644 else if(!flags
->Precision
|| flags
->Format
=='e' || flags
->Format
=='E')
646 if (round_pos
<= first_limb_len
)
647 round_limb
= b
->e
+ (first_limb_len
- round_pos
) / LIMB_DIGITS
- 1;
649 round_limb
= b
->e
- (round_pos
- first_limb_len
- 1) / LIMB_DIGITS
- 2;
651 if (b
->b
<=round_limb
&& round_limb
<b
->e
) {
652 if (round_pos
<= first_limb_len
) {
653 round_pos
= first_limb_len
- round_pos
;
655 round_pos
= LIMB_DIGITS
- (round_pos
- first_limb_len
) % LIMB_DIGITS
;
656 if (round_pos
== LIMB_DIGITS
) round_pos
= 0;
660 l
= b
->data
[bnum_idx(b
, round_limb
)] % p10s
[round_pos
];
661 b
->data
[bnum_idx(b
, round_limb
)] -= l
;
662 if(!standard_rounding
) round_up
= (2*l
>= p10s
[round_pos
]);
663 else if(2*l
> p10s
[round_pos
]) round_up
= TRUE
;
664 else if(2*l
== p10s
[round_pos
]) {
665 for(r
= round_limb
-1; r
>= b
->b
; r
--) {
666 if(b
->data
[bnum_idx(b
, r
)]) {
672 if(!round_up
) round_up
= b
->data
[bnum_idx(b
, round_limb
)] / p10s
[round_pos
] & 1;
674 } else if(round_limb
- 1 >= b
->b
) {
675 if(!standard_rounding
) round_up
= (2*b
->data
[bnum_idx(b
, round_limb
-1)] >= LIMB_MAX
);
676 else if(2*b
->data
[bnum_idx(b
, round_limb
-1)] > LIMB_MAX
) round_up
= TRUE
;
677 else if(2*b
->data
[bnum_idx(b
, round_limb
-1)] == LIMB_MAX
) {
678 for(r
= round_limb
-2; r
>= b
->b
; r
--) {
679 if(b
->data
[bnum_idx(b
, r
)]) {
685 if(!round_up
) round_up
= b
->data
[bnum_idx(b
, round_limb
)] & 1;
691 b
->data
[bnum_idx(b
, b
->b
)] += p10s
[round_pos
];
692 for(i
= b
->b
; i
< b
->e
; i
++) {
693 if(b
->data
[bnum_idx(b
, i
)] < LIMB_MAX
) break;
695 b
->data
[bnum_idx(b
, i
)] -= LIMB_MAX
;
696 if(i
+1 < b
->e
) b
->data
[bnum_idx(b
, i
+1)]++;
697 else b
->data
[bnum_idx(b
, i
+1)] = 1;
700 if(!b
->data
[bnum_idx(b
, b
->e
-1)])
703 i
= MSVCRT_floor(MSVCRT_log10(b
->data
[bnum_idx(b
, b
->e
-1)])) + 1;
704 if(i
!= first_limb_len
) {
708 } else if(i
== b
->e
) {
715 else if(b
->e
<= round_limb
) { /* got 0 or 1 after rounding */
716 if(b
->e
== round_limb
) {
717 if(!standard_rounding
) round_up
= b
->data
[bnum_idx(b
, b
->e
-1)] >= LIMB_MAX
/2;
718 else if(b
->data
[bnum_idx(b
, b
->e
-1)] > LIMB_MAX
/2) round_up
= TRUE
;
719 else if(b
->data
[bnum_idx(b
, b
->e
-1)] == LIMB_MAX
/2) {
720 for(r
= b
->e
-2; r
>= b
->b
; r
--) {
721 if(b
->data
[bnum_idx(b
, r
)]) {
729 b
->data
[bnum_idx(b
, round_limb
)] = round_up
;
736 if(flags
->Format
=='g' || flags
->Format
=='G') {
739 if(radix_pos
>=-3 && radix_pos
<=flags
->Precision
) {
741 if(!flags
->Precision
) flags
->Precision
++;
742 flags
->Precision
-= radix_pos
;
745 if(flags
->Precision
> 0) flags
->Precision
--;
749 if(trim_tail
&& !flags
->Alternate
) {
750 for(i
=round_limb
; flags
->Precision
>0 && i
<b
->e
; i
++) {
752 l
= b
->data
[bnum_idx(b
, i
)];
756 if(i
== round_limb
) {
757 if(flags
->Format
=='f' || flags
->Format
=='F')
758 r
= radix_pos
+ flags
->Precision
;
760 r
= flags
->Precision
+ 1;
761 r
= first_limb_len
+ LIMB_DIGITS
* (b
->e
-1 - b
->b
) - r
;
763 if(r
< 0) r
+= LIMB_DIGITS
;
765 limb_len
= LIMB_DIGITS
- r
;
767 limb_len
= LIMB_DIGITS
;
771 flags
->Precision
-= limb_len
;
779 if(flags
->Precision
<= 0) {
780 flags
->Precision
= 0;
788 len
= flags
->Precision
;
789 if(flags
->Precision
|| flags
->Alternate
) len
++;
790 if(flags
->Format
=='f' || flags
->Format
=='F') {
791 len
+= (radix_pos
> 0 ? radix_pos
: 1);
792 } else if(flags
->Format
=='e' || flags
->Format
=='E') {
794 if(!trim_tail
|| radix_pos
) {
795 len
+= 3; /* strlen("1e+") */
796 if(three_digit_exp
|| radix_pos
<-99 || radix_pos
>99) len
+= 3;
803 r
= FUNC_NAME(pf_fill
)(pf_puts
, puts_ctx
, len
, flags
, TRUE
);
809 if(flags
->Format
=='f' || flags
->Format
=='F') {
812 r
= pf_puts(puts_ctx
, 1, buf
);
817 limb_len
= LIMB_DIGITS
;
818 for(i
=b
->e
-1; radix_pos
>0 && i
>=b
->b
; i
--) {
819 limb_len
= (i
== b
->e
-1 ? first_limb_len
: LIMB_DIGITS
);
820 l
= b
->data
[bnum_idx(b
, i
)];
821 if(limb_len
> radix_pos
) {
822 f
.Precision
= radix_pos
;
823 l
/= p10s
[limb_len
- radix_pos
];
824 limb_len
= limb_len
- radix_pos
;
826 f
.Precision
= limb_len
;
827 limb_len
= LIMB_DIGITS
;
829 radix_pos
-= f
.Precision
;
830 FUNC_NAME(pf_integer_conv
)(buf
, &f
, l
);
832 r
= pf_puts(puts_ctx
, f
.Precision
, buf
);
838 for(; radix_pos
>0; radix_pos
--) {
839 r
= pf_puts(puts_ctx
, 1, buf
);
844 if(flags
->Precision
|| flags
->Alternate
) {
845 buf
[0] = *(locale
? locale
->locinfo
: get_locinfo())->lconv
->decimal_point
;
846 r
= pf_puts(puts_ctx
, 1, buf
);
851 prec
= flags
->Precision
;
853 for(; prec
>0 && radix_pos
+LIMB_DIGITS
-first_limb_len
<0; radix_pos
++, prec
--) {
854 r
= pf_puts(puts_ctx
, 1, buf
);
859 for(; prec
>0 && i
>=b
->b
; i
--) {
860 l
= b
->data
[bnum_idx(b
, i
)];
861 if(limb_len
!= LIMB_DIGITS
)
863 if(limb_len
> prec
) {
865 l
/= p10s
[limb_len
- prec
];
867 f
.Precision
= limb_len
;
868 limb_len
= LIMB_DIGITS
;
871 FUNC_NAME(pf_integer_conv
)(buf
, &f
, l
);
873 r
= pf_puts(puts_ctx
, f
.Precision
, buf
);
879 for(; prec
>0; prec
--) {
880 r
= pf_puts(puts_ctx
, 1, buf
);
885 l
= b
->data
[bnum_idx(b
, b
->e
- 1)];
886 l
/= p10s
[first_limb_len
- 1];
889 r
= pf_puts(puts_ctx
, 1, buf
);
893 if(flags
->Precision
|| flags
->Alternate
) {
894 buf
[0] = *(locale
? locale
->locinfo
: get_locinfo())->lconv
->decimal_point
;
895 r
= pf_puts(puts_ctx
, 1, buf
);
900 prec
= flags
->Precision
;
901 limb_len
= LIMB_DIGITS
;
902 for(i
=b
->e
-1; prec
>0 && i
>=b
->b
; i
--) {
903 l
= b
->data
[bnum_idx(b
, i
)];
905 limb_len
= first_limb_len
- 1;
909 if(limb_len
> prec
) {
911 l
/= p10s
[limb_len
- prec
];
913 f
.Precision
= limb_len
;
914 limb_len
= LIMB_DIGITS
;
917 FUNC_NAME(pf_integer_conv
)(buf
, &f
, l
);
919 r
= pf_puts(puts_ctx
, f
.Precision
, buf
);
925 for(; prec
>0; prec
--) {
926 r
= pf_puts(puts_ctx
, 1, buf
);
931 if(!trim_tail
|| radix_pos
) {
932 buf
[0] = flags
->Format
;
933 buf
[1] = radix_pos
< 0 ? '-' : '+';
934 r
= pf_puts(puts_ctx
, 2, buf
);
938 f
.Precision
= three_digit_exp
? 3 : 2;
939 FUNC_NAME(pf_integer_conv
)(buf
, &f
, radix_pos
);
940 r
= pf_puts(puts_ctx
, f
.Precision
, buf
);
946 r
= FUNC_NAME(pf_fill
)(pf_puts
, puts_ctx
, len
, flags
, FALSE
);
952 int FUNC_NAME(pf_printf
)(FUNC_NAME(puts_clbk
) pf_puts
, void *puts_ctx
, const APICHAR
*fmt
,
953 _locale_t locale
, DWORD options
,
954 args_clbk pf_args
, void *args_ctx
, __ms_va_list
*valist
)
956 const APICHAR
*q
, *p
= fmt
;
958 int written
= 0, pos
, i
;
960 BOOL positional_params
= options
& MSVCRT_PRINTF_POSITIONAL_PARAMS
;
961 BOOL invoke_invalid_param_handler
= options
& MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
;
962 #if _MSVCR_VER >= 140
963 BOOL legacy_wide
= options
& _CRT_INTERNAL_PRINTF_LEGACY_WIDE_SPECIFIERS
;
964 BOOL legacy_msvcrt_compat
= options
& _CRT_INTERNAL_PRINTF_LEGACY_MSVCRT_COMPATIBILITY
;
965 BOOL three_digit_exp
= options
& _CRT_INTERNAL_PRINTF_LEGACY_THREE_DIGIT_EXPONENTS
;
966 BOOL standard_rounding
= options
& _CRT_INTERNAL_PRINTF_STANDARD_ROUNDING
;
968 BOOL legacy_wide
= TRUE
, legacy_msvcrt_compat
= TRUE
;
969 BOOL three_digit_exp
= _get_output_format() != MSVCRT__TWO_DIGIT_EXPONENT
;
970 BOOL standard_rounding
= FALSE
;
973 if (!MSVCRT_CHECK_PMT(fmt
!= NULL
))
977 /* output characters before '%' */
978 for(q
=p
; *q
&& *q
!='%'; q
++);
980 i
= pf_puts(puts_ctx
, q
-p
, p
);
992 /* output a single '%' character */
994 i
= pf_puts(puts_ctx
, 1, p
++);
1002 /* check parameter position */
1003 if(positional_params
&& (q
= FUNC_NAME(pf_parse_int
)(p
, &pos
)) && *q
=='$')
1008 /* parse the flags */
1009 memset(&flags
, 0, sizeof(flags
));
1011 if(*p
=='+' || *p
==' ') {
1012 if(flags
.Sign
!= '+')
1014 } else if(*p
== '-')
1015 flags
.LeftAlign
= TRUE
;
1017 flags
.PadZero
= TRUE
;
1019 flags
.Alternate
= TRUE
;
1026 /* parse the width */
1029 if(positional_params
&& (q
= FUNC_NAME(pf_parse_int
)(p
, &i
)) && *q
=='$')
1034 flags
.FieldLength
= pf_args(args_ctx
, i
, VT_INT
, valist
).get_int
;
1035 if(flags
.FieldLength
< 0) {
1036 flags
.LeftAlign
= TRUE
;
1037 flags
.FieldLength
= -flags
.FieldLength
;
1039 } else while (*p
>= '0' && *p
<= '9') {
1040 flags
.FieldLength
*= 10;
1041 flags
.FieldLength
+= *p
++ - '0';
1044 /* parse the precision */
1045 flags
.Precision
= -1;
1047 flags
.Precision
= 0;
1051 if(positional_params
&& (q
= FUNC_NAME(pf_parse_int
)(p
, &i
)) && *q
=='$')
1056 flags
.Precision
= pf_args(args_ctx
, i
, VT_INT
, valist
).get_int
;
1057 } else while (*p
>= '0' && *p
<= '9') {
1058 flags
.Precision
*= 10;
1059 flags
.Precision
+= *p
++ - '0';
1063 /* parse argument size modifier */
1065 if(*p
=='l' && *(p
+1)=='l') {
1066 flags
.IntegerDouble
= TRUE
;
1068 } else if(*p
=='l') {
1069 flags
.IntegerLength
= LEN_LONG
;
1070 } else if(*p
== 'h') {
1071 flags
.IntegerLength
= LEN_SHORT
;
1072 } else if(*p
== 'I') {
1073 if(*(p
+1)=='6' && *(p
+2)=='4') {
1074 flags
.IntegerDouble
= TRUE
;
1076 } else if(*(p
+1)=='3' && *(p
+2)=='2')
1078 else if(p
[1] && strchr("diouxX", p
[1]))
1079 flags
.IntegerNative
= TRUE
;
1082 } else if(*p
== 'w')
1083 flags
.WideString
= TRUE
;
1084 #if _MSVCR_VER == 0 || _MSVCR_VER >= 140
1085 else if((*p
== 'z' || *p
== 't') && p
[1] && strchr("diouxX", p
[1]))
1086 flags
.IntegerNative
= TRUE
;
1088 flags
.IntegerDouble
= TRUE
;
1090 #if _MSVCR_VER >= 140
1092 flags
.NaturalString
= TRUE
;
1094 else if(*p
!= 'L' && ((*p
!= 'F' && *p
!= 'N') || !legacy_msvcrt_compat
))
1101 if(flags
.Format
== 's' || flags
.Format
== 'S') {
1102 i
= FUNC_NAME(pf_handle_string
)(pf_puts
, puts_ctx
,
1103 pf_args(args_ctx
, pos
, VT_PTR
, valist
).get_ptr
,
1104 -1, &flags
, locale
, legacy_wide
);
1105 } else if(flags
.Format
== 'c' || flags
.Format
== 'C') {
1106 int ch
= pf_args(args_ctx
, pos
, VT_INT
, valist
).get_int
;
1108 i
= FUNC_NAME(pf_handle_string
)(pf_puts
, puts_ctx
, &ch
, 1, &flags
, locale
, legacy_wide
);
1109 if(i
< 0) i
= 0; /* ignore conversion error */
1110 } else if(flags
.Format
== 'p') {
1112 flags
.PadZero
= TRUE
;
1113 i
= flags
.Precision
;
1114 flags
.Precision
= 2*sizeof(void*);
1115 FUNC_NAME(pf_integer_conv
)(buf
, &flags
,
1116 (ULONG_PTR
)pf_args(args_ctx
, pos
, VT_PTR
, valist
).get_ptr
);
1117 flags
.PadZero
= FALSE
;
1118 flags
.Precision
= i
;
1121 i
= FUNC_NAME(pf_output_format_wstr
)(pf_puts
, puts_ctx
, buf
, -1, &flags
, locale
);
1123 i
= FUNC_NAME(pf_output_format_str
)(pf_puts
, puts_ctx
, buf
, -1, &flags
, locale
);
1125 } else if(flags
.Format
== 'n') {
1128 if(!n_format_enabled
) {
1129 MSVCRT_INVALID_PMT("\'n\' format specifier disabled", EINVAL
);
1133 used
= pf_args(args_ctx
, pos
, VT_PTR
, valist
).get_ptr
;
1136 } else if(flags
.Format
&& strchr("diouxX", flags
.Format
)) {
1140 /* 0 padding is added after '0x' if Alternate flag is in use */
1141 if((flags
.Format
=='x' || flags
.Format
=='X') && flags
.PadZero
&& flags
.Alternate
1142 && !flags
.LeftAlign
&& flags
.Precision
<flags
.FieldLength
-2)
1143 flags
.Precision
= flags
.FieldLength
- 2;
1145 max_len
= (flags
.FieldLength
>flags
.Precision
? flags
.FieldLength
: flags
.Precision
) + 10;
1146 if(max_len
> ARRAY_SIZE(buf
))
1147 tmp
= HeapAlloc(GetProcessHeap(), 0, max_len
);
1151 if(flags
.IntegerDouble
|| (flags
.IntegerNative
&& sizeof(void*) == 8))
1152 FUNC_NAME(pf_integer_conv
)(tmp
, &flags
, pf_args(args_ctx
, pos
,
1153 VT_I8
, valist
).get_longlong
);
1154 else if(flags
.Format
=='d' || flags
.Format
=='i')
1155 FUNC_NAME(pf_integer_conv
)(tmp
, &flags
,
1156 flags
.IntegerLength
!= LEN_SHORT
?
1157 pf_args(args_ctx
, pos
, VT_INT
, valist
).get_int
:
1158 (short)pf_args(args_ctx
, pos
, VT_INT
, valist
).get_int
);
1160 FUNC_NAME(pf_integer_conv
)(tmp
, &flags
,
1161 flags
.IntegerLength
!= LEN_SHORT
?
1162 (unsigned)pf_args(args_ctx
, pos
, VT_INT
, valist
).get_int
:
1163 (unsigned short)pf_args(args_ctx
, pos
, VT_INT
, valist
).get_int
);
1166 i
= FUNC_NAME(pf_output_format_wstr
)(pf_puts
, puts_ctx
, tmp
, -1, &flags
, locale
);
1168 i
= FUNC_NAME(pf_output_format_str
)(pf_puts
, puts_ctx
, tmp
, -1, &flags
, locale
);
1171 HeapFree(GetProcessHeap(), 0, tmp
);
1172 } else if(flags
.Format
&& strchr("aAeEfFgG", flags
.Format
)) {
1173 double val
= pf_args(args_ctx
, pos
, VT_R8
, valist
).get_double
;
1180 if(isinf(val
) || isnan(val
))
1181 i
= FUNC_NAME(pf_output_special_fp
)(pf_puts
, puts_ctx
, val
, &flags
,
1182 locale
, legacy_msvcrt_compat
, three_digit_exp
);
1183 else if(flags
.Format
=='a' || flags
.Format
=='A')
1184 i
= FUNC_NAME(pf_output_hex_fp
)(pf_puts
, puts_ctx
, val
, &flags
,
1185 locale
, standard_rounding
);
1187 i
= FUNC_NAME(pf_output_fp
)(pf_puts
, puts_ctx
, val
, &flags
,
1188 locale
, three_digit_exp
, standard_rounding
);
1190 if(invoke_invalid_param_handler
) {
1191 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
1209 enum types_clbk_flags
{
1210 TYPE_CLBK_VA_LIST
= 1,
1211 TYPE_CLBK_POSITIONAL
= 2,
1212 TYPE_CLBK_ERROR_POS
= 4,
1213 TYPE_CLBK_ERROR_TYPE
= 8
1216 /* This functions stores types of arguments. It uses args[0] internally */
1217 static printf_arg
arg_clbk_type(void *ctx
, int pos
, int type
, __ms_va_list
*valist
)
1219 static const printf_arg ret
;
1220 printf_arg
*args
= ctx
;
1223 args
[0].get_int
|= TYPE_CLBK_VA_LIST
;
1226 args
[0].get_int
|= TYPE_CLBK_POSITIONAL
;
1228 if(pos
<1 || pos
>_ARGMAX
)
1229 args
[0].get_int
|= TYPE_CLBK_ERROR_POS
;
1230 else if(args
[pos
].get_int
&& args
[pos
].get_int
!=type
)
1231 args
[0].get_int
|= TYPE_CLBK_ERROR_TYPE
;
1233 args
[pos
].get_int
= type
;
1239 int FUNC_NAME(create_positional_ctx
)(void *args_ctx
, const APICHAR
*format
, __ms_va_list valist
)
1241 struct FUNC_NAME(_str_ctx
) puts_ctx
= {INT_MAX
, NULL
};
1242 printf_arg
*args
= args_ctx
;
1245 i
= FUNC_NAME(pf_printf
)(FUNC_NAME(puts_clbk_str
), &puts_ctx
, format
, NULL
,
1246 MSVCRT_PRINTF_POSITIONAL_PARAMS
, arg_clbk_type
, args_ctx
, NULL
);
1250 if(args
[0].get_int
==0 || args
[0].get_int
==TYPE_CLBK_VA_LIST
)
1252 if(args
[0].get_int
!= TYPE_CLBK_POSITIONAL
)
1255 for(i
=_ARGMAX
; i
>0; i
--)
1259 for(j
=1; j
<=i
; j
++) {
1260 switch(args
[j
].get_int
) {
1262 args
[j
].get_longlong
= va_arg(valist
, LONGLONG
);
1265 args
[j
].get_int
= va_arg(valist
, int);
1268 args
[j
].get_double
= va_arg(valist
, double);
1271 args
[j
].get_ptr
= va_arg(valist
, void*);