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
= _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
= wcsnlen(str
, flags
->Precision
);
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;
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
= floor(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
= floor(log10(b
->data
[bnum_idx(b
, b
->e
-1)])) + 1;
704 if(i
!= first_limb_len
) {
709 if (round_pos
== LIMB_DIGITS
)
715 } else if(i
== b
->e
) {
721 if (round_pos
== LIMB_DIGITS
)
729 else if(b
->e
<= round_limb
) { /* got 0 or 1 after rounding */
730 if(b
->e
== round_limb
) {
731 if(!standard_rounding
) round_up
= b
->data
[bnum_idx(b
, b
->e
-1)] >= LIMB_MAX
/2;
732 else if(b
->data
[bnum_idx(b
, b
->e
-1)] > LIMB_MAX
/2) round_up
= TRUE
;
733 else if(b
->data
[bnum_idx(b
, b
->e
-1)] == LIMB_MAX
/2) {
734 for(r
= b
->e
-2; r
>= b
->b
; r
--) {
735 if(b
->data
[bnum_idx(b
, r
)]) {
743 b
->data
[bnum_idx(b
, round_limb
)] = round_up
;
750 if(flags
->Format
=='g' || flags
->Format
=='G') {
753 if(radix_pos
>=-3 && radix_pos
<=flags
->Precision
) {
755 if(!flags
->Precision
) flags
->Precision
++;
756 flags
->Precision
-= radix_pos
;
759 if(flags
->Precision
> 0) flags
->Precision
--;
763 if(trim_tail
&& !flags
->Alternate
) {
764 for(i
=round_limb
; flags
->Precision
>0 && i
<b
->e
; i
++) {
766 l
= b
->data
[bnum_idx(b
, i
)];
770 if(i
== round_limb
) {
771 if(flags
->Format
=='f' || flags
->Format
=='F')
772 r
= radix_pos
+ flags
->Precision
;
774 r
= flags
->Precision
+ 1;
775 r
= first_limb_len
+ LIMB_DIGITS
* (b
->e
-1 - b
->b
) - r
;
777 if(r
< 0) r
+= LIMB_DIGITS
;
779 limb_len
= LIMB_DIGITS
- r
;
781 limb_len
= LIMB_DIGITS
;
785 flags
->Precision
-= limb_len
;
793 if(flags
->Precision
<= 0) {
794 flags
->Precision
= 0;
802 len
= flags
->Precision
;
803 if(flags
->Precision
|| flags
->Alternate
) len
++;
804 if(flags
->Format
=='f' || flags
->Format
=='F') {
805 len
+= (radix_pos
> 0 ? radix_pos
: 1);
806 } else if(flags
->Format
=='e' || flags
->Format
=='E') {
808 if(!trim_tail
|| radix_pos
) {
809 len
+= 3; /* strlen("1e+") */
810 if(three_digit_exp
|| radix_pos
<-99 || radix_pos
>99) len
+= 3;
817 r
= FUNC_NAME(pf_fill
)(pf_puts
, puts_ctx
, len
, flags
, TRUE
);
823 if(flags
->Format
=='f' || flags
->Format
=='F') {
826 r
= pf_puts(puts_ctx
, 1, buf
);
831 limb_len
= LIMB_DIGITS
;
832 for(i
=b
->e
-1; radix_pos
>0 && i
>=b
->b
; i
--) {
833 limb_len
= (i
== b
->e
-1 ? first_limb_len
: LIMB_DIGITS
);
834 l
= b
->data
[bnum_idx(b
, i
)];
835 if(limb_len
> radix_pos
) {
836 f
.Precision
= radix_pos
;
837 l
/= p10s
[limb_len
- radix_pos
];
838 limb_len
= limb_len
- radix_pos
;
840 f
.Precision
= limb_len
;
841 limb_len
= LIMB_DIGITS
;
843 radix_pos
-= f
.Precision
;
844 FUNC_NAME(pf_integer_conv
)(buf
, &f
, l
);
846 r
= pf_puts(puts_ctx
, f
.Precision
, buf
);
852 for(; radix_pos
>0; radix_pos
--) {
853 r
= pf_puts(puts_ctx
, 1, buf
);
858 if(flags
->Precision
|| flags
->Alternate
) {
859 buf
[0] = *(locale
? locale
->locinfo
: get_locinfo())->lconv
->decimal_point
;
860 r
= pf_puts(puts_ctx
, 1, buf
);
865 prec
= flags
->Precision
;
867 for(; prec
>0 && radix_pos
+LIMB_DIGITS
-first_limb_len
<0; radix_pos
++, prec
--) {
868 r
= pf_puts(puts_ctx
, 1, buf
);
873 for(; prec
>0 && i
>=b
->b
; i
--) {
874 l
= b
->data
[bnum_idx(b
, i
)];
875 if(limb_len
!= LIMB_DIGITS
)
877 if(limb_len
> prec
) {
879 l
/= p10s
[limb_len
- prec
];
881 f
.Precision
= limb_len
;
882 limb_len
= LIMB_DIGITS
;
885 FUNC_NAME(pf_integer_conv
)(buf
, &f
, l
);
887 r
= pf_puts(puts_ctx
, f
.Precision
, buf
);
893 for(; prec
>0; prec
--) {
894 r
= pf_puts(puts_ctx
, 1, buf
);
899 l
= b
->data
[bnum_idx(b
, b
->e
- 1)];
900 l
/= p10s
[first_limb_len
- 1];
903 r
= pf_puts(puts_ctx
, 1, buf
);
907 if(flags
->Precision
|| flags
->Alternate
) {
908 buf
[0] = *(locale
? locale
->locinfo
: get_locinfo())->lconv
->decimal_point
;
909 r
= pf_puts(puts_ctx
, 1, buf
);
914 prec
= flags
->Precision
;
915 limb_len
= LIMB_DIGITS
;
916 for(i
=b
->e
-1; prec
>0 && i
>=b
->b
; i
--) {
917 l
= b
->data
[bnum_idx(b
, i
)];
919 limb_len
= first_limb_len
- 1;
923 if(limb_len
> prec
) {
925 l
/= p10s
[limb_len
- prec
];
927 f
.Precision
= limb_len
;
928 limb_len
= LIMB_DIGITS
;
931 FUNC_NAME(pf_integer_conv
)(buf
, &f
, l
);
933 r
= pf_puts(puts_ctx
, f
.Precision
, buf
);
939 for(; prec
>0; prec
--) {
940 r
= pf_puts(puts_ctx
, 1, buf
);
945 if(!trim_tail
|| radix_pos
) {
946 buf
[0] = flags
->Format
;
947 buf
[1] = radix_pos
< 0 ? '-' : '+';
948 r
= pf_puts(puts_ctx
, 2, buf
);
952 f
.Precision
= three_digit_exp
? 3 : 2;
953 FUNC_NAME(pf_integer_conv
)(buf
, &f
, radix_pos
);
954 r
= pf_puts(puts_ctx
, f
.Precision
, buf
);
960 r
= FUNC_NAME(pf_fill
)(pf_puts
, puts_ctx
, len
, flags
, FALSE
);
966 int FUNC_NAME(pf_printf
)(FUNC_NAME(puts_clbk
) pf_puts
, void *puts_ctx
, const APICHAR
*fmt
,
967 _locale_t locale
, DWORD options
,
968 args_clbk pf_args
, void *args_ctx
, __ms_va_list
*valist
)
970 const APICHAR
*q
, *p
= fmt
;
972 int written
= 0, pos
, i
;
974 BOOL positional_params
= options
& MSVCRT_PRINTF_POSITIONAL_PARAMS
;
975 BOOL invoke_invalid_param_handler
= options
& MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
;
976 #if _MSVCR_VER >= 140
977 BOOL legacy_wide
= options
& _CRT_INTERNAL_PRINTF_LEGACY_WIDE_SPECIFIERS
;
978 BOOL legacy_msvcrt_compat
= options
& _CRT_INTERNAL_PRINTF_LEGACY_MSVCRT_COMPATIBILITY
;
979 BOOL three_digit_exp
= options
& _CRT_INTERNAL_PRINTF_LEGACY_THREE_DIGIT_EXPONENTS
;
980 BOOL standard_rounding
= options
& _CRT_INTERNAL_PRINTF_STANDARD_ROUNDING
;
982 BOOL legacy_wide
= TRUE
, legacy_msvcrt_compat
= TRUE
;
983 BOOL three_digit_exp
= _get_output_format() != _TWO_DIGIT_EXPONENT
;
984 BOOL standard_rounding
= FALSE
;
987 if (!MSVCRT_CHECK_PMT(fmt
!= NULL
))
991 /* output characters before '%' */
992 for(q
=p
; *q
&& *q
!='%'; q
++);
994 i
= pf_puts(puts_ctx
, q
-p
, p
);
1003 /* *p == '%' here */
1006 /* output a single '%' character */
1008 i
= pf_puts(puts_ctx
, 1, p
++);
1016 /* check parameter position */
1017 if(positional_params
&& (q
= FUNC_NAME(pf_parse_int
)(p
, &pos
)) && *q
=='$')
1022 /* parse the flags */
1023 memset(&flags
, 0, sizeof(flags
));
1025 if(*p
=='+' || *p
==' ') {
1026 if(flags
.Sign
!= '+')
1028 } else if(*p
== '-')
1029 flags
.LeftAlign
= TRUE
;
1031 flags
.PadZero
= TRUE
;
1033 flags
.Alternate
= TRUE
;
1040 /* parse the width */
1043 if(positional_params
&& (q
= FUNC_NAME(pf_parse_int
)(p
, &i
)) && *q
=='$')
1048 flags
.FieldLength
= pf_args(args_ctx
, i
, VT_INT
, valist
).get_int
;
1049 if(flags
.FieldLength
< 0) {
1050 flags
.LeftAlign
= TRUE
;
1051 flags
.FieldLength
= -flags
.FieldLength
;
1053 } else while (*p
>= '0' && *p
<= '9') {
1054 flags
.FieldLength
*= 10;
1055 flags
.FieldLength
+= *p
++ - '0';
1058 /* parse the precision */
1059 flags
.Precision
= -1;
1061 flags
.Precision
= 0;
1065 if(positional_params
&& (q
= FUNC_NAME(pf_parse_int
)(p
, &i
)) && *q
=='$')
1070 flags
.Precision
= pf_args(args_ctx
, i
, VT_INT
, valist
).get_int
;
1071 } else while (*p
>= '0' && *p
<= '9') {
1072 flags
.Precision
*= 10;
1073 flags
.Precision
+= *p
++ - '0';
1077 /* parse argument size modifier */
1079 if(*p
=='l' && *(p
+1)=='l') {
1080 flags
.IntegerDouble
= TRUE
;
1082 } else if(*p
=='l') {
1083 flags
.IntegerLength
= LEN_LONG
;
1084 } else if(*p
== 'h') {
1085 flags
.IntegerLength
= LEN_SHORT
;
1086 } else if(*p
== 'I') {
1087 if(*(p
+1)=='6' && *(p
+2)=='4') {
1088 flags
.IntegerDouble
= TRUE
;
1090 } else if(*(p
+1)=='3' && *(p
+2)=='2')
1092 else if(p
[1] && strchr("diouxX", p
[1]))
1093 flags
.IntegerNative
= TRUE
;
1096 } else if(*p
== 'w')
1097 flags
.WideString
= TRUE
;
1098 #if _MSVCR_VER == 0 || _MSVCR_VER >= 140
1099 else if((*p
== 'z' || *p
== 't') && p
[1] && strchr("diouxX", p
[1]))
1100 flags
.IntegerNative
= TRUE
;
1102 flags
.IntegerDouble
= TRUE
;
1104 #if _MSVCR_VER >= 140
1106 flags
.NaturalString
= TRUE
;
1108 else if(*p
!= 'L' && ((*p
!= 'F' && *p
!= 'N') || !legacy_msvcrt_compat
))
1115 if(flags
.Format
== 's' || flags
.Format
== 'S') {
1116 i
= FUNC_NAME(pf_handle_string
)(pf_puts
, puts_ctx
,
1117 pf_args(args_ctx
, pos
, VT_PTR
, valist
).get_ptr
,
1118 -1, &flags
, locale
, legacy_wide
);
1119 } else if(flags
.Format
== 'c' || flags
.Format
== 'C') {
1120 int ch
= pf_args(args_ctx
, pos
, VT_INT
, valist
).get_int
;
1122 i
= FUNC_NAME(pf_handle_string
)(pf_puts
, puts_ctx
, &ch
, 1, &flags
, locale
, legacy_wide
);
1123 if(i
< 0) i
= 0; /* ignore conversion error */
1124 } else if(flags
.Format
== 'p') {
1126 flags
.PadZero
= TRUE
;
1127 i
= flags
.Precision
;
1128 flags
.Precision
= 2*sizeof(void*);
1129 FUNC_NAME(pf_integer_conv
)(buf
, &flags
,
1130 (ULONG_PTR
)pf_args(args_ctx
, pos
, VT_PTR
, valist
).get_ptr
);
1131 flags
.PadZero
= FALSE
;
1132 flags
.Precision
= i
;
1135 i
= FUNC_NAME(pf_output_format_wstr
)(pf_puts
, puts_ctx
, buf
, -1, &flags
, locale
);
1137 i
= FUNC_NAME(pf_output_format_str
)(pf_puts
, puts_ctx
, buf
, -1, &flags
, locale
);
1139 } else if(flags
.Format
== 'n') {
1142 if(!n_format_enabled
) {
1143 MSVCRT_INVALID_PMT("\'n\' format specifier disabled", EINVAL
);
1147 used
= pf_args(args_ctx
, pos
, VT_PTR
, valist
).get_ptr
;
1150 } else if(flags
.Format
&& strchr("diouxX", flags
.Format
)) {
1154 /* 0 padding is added after '0x' if Alternate flag is in use */
1155 if((flags
.Format
=='x' || flags
.Format
=='X') && flags
.PadZero
&& flags
.Alternate
1156 && !flags
.LeftAlign
&& flags
.Precision
<flags
.FieldLength
-2)
1157 flags
.Precision
= flags
.FieldLength
- 2;
1159 max_len
= (flags
.FieldLength
>flags
.Precision
? flags
.FieldLength
: flags
.Precision
) + 10;
1160 if(max_len
> ARRAY_SIZE(buf
))
1161 tmp
= HeapAlloc(GetProcessHeap(), 0, max_len
);
1165 if(flags
.IntegerDouble
|| (flags
.IntegerNative
&& sizeof(void*) == 8))
1166 FUNC_NAME(pf_integer_conv
)(tmp
, &flags
, pf_args(args_ctx
, pos
,
1167 VT_I8
, valist
).get_longlong
);
1168 else if(flags
.Format
=='d' || flags
.Format
=='i')
1169 FUNC_NAME(pf_integer_conv
)(tmp
, &flags
,
1170 flags
.IntegerLength
!= LEN_SHORT
?
1171 pf_args(args_ctx
, pos
, VT_INT
, valist
).get_int
:
1172 (short)pf_args(args_ctx
, pos
, VT_INT
, valist
).get_int
);
1174 FUNC_NAME(pf_integer_conv
)(tmp
, &flags
,
1175 flags
.IntegerLength
!= LEN_SHORT
?
1176 (unsigned)pf_args(args_ctx
, pos
, VT_INT
, valist
).get_int
:
1177 (unsigned short)pf_args(args_ctx
, pos
, VT_INT
, valist
).get_int
);
1180 i
= FUNC_NAME(pf_output_format_wstr
)(pf_puts
, puts_ctx
, tmp
, -1, &flags
, locale
);
1182 i
= FUNC_NAME(pf_output_format_str
)(pf_puts
, puts_ctx
, tmp
, -1, &flags
, locale
);
1185 HeapFree(GetProcessHeap(), 0, tmp
);
1186 } else if(flags
.Format
&& strchr("aAeEfFgG", flags
.Format
)) {
1187 double val
= pf_args(args_ctx
, pos
, VT_R8
, valist
).get_double
;
1194 if(isinf(val
) || isnan(val
))
1195 i
= FUNC_NAME(pf_output_special_fp
)(pf_puts
, puts_ctx
, val
, &flags
,
1196 locale
, legacy_msvcrt_compat
, three_digit_exp
);
1197 else if(flags
.Format
=='a' || flags
.Format
=='A')
1198 i
= FUNC_NAME(pf_output_hex_fp
)(pf_puts
, puts_ctx
, val
, &flags
,
1199 locale
, standard_rounding
);
1201 i
= FUNC_NAME(pf_output_fp
)(pf_puts
, puts_ctx
, val
, &flags
,
1202 locale
, three_digit_exp
, standard_rounding
);
1204 if(invoke_invalid_param_handler
) {
1205 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
1223 enum types_clbk_flags
{
1224 TYPE_CLBK_VA_LIST
= 1,
1225 TYPE_CLBK_POSITIONAL
= 2,
1226 TYPE_CLBK_ERROR_POS
= 4,
1227 TYPE_CLBK_ERROR_TYPE
= 8
1230 /* This functions stores types of arguments. It uses args[0] internally */
1231 static printf_arg
arg_clbk_type(void *ctx
, int pos
, int type
, __ms_va_list
*valist
)
1233 static const printf_arg ret
;
1234 printf_arg
*args
= ctx
;
1237 args
[0].get_int
|= TYPE_CLBK_VA_LIST
;
1240 args
[0].get_int
|= TYPE_CLBK_POSITIONAL
;
1242 if(pos
<1 || pos
>_ARGMAX
)
1243 args
[0].get_int
|= TYPE_CLBK_ERROR_POS
;
1244 else if(args
[pos
].get_int
&& args
[pos
].get_int
!=type
)
1245 args
[0].get_int
|= TYPE_CLBK_ERROR_TYPE
;
1247 args
[pos
].get_int
= type
;
1253 int FUNC_NAME(create_positional_ctx
)(void *args_ctx
, const APICHAR
*format
, __ms_va_list valist
)
1255 struct FUNC_NAME(_str_ctx
) puts_ctx
= {INT_MAX
, NULL
};
1256 printf_arg
*args
= args_ctx
;
1259 i
= FUNC_NAME(pf_printf
)(FUNC_NAME(puts_clbk_str
), &puts_ctx
, format
, NULL
,
1260 MSVCRT_PRINTF_POSITIONAL_PARAMS
, arg_clbk_type
, args_ctx
, NULL
);
1264 if(args
[0].get_int
==0 || args
[0].get_int
==TYPE_CLBK_VA_LIST
)
1266 if(args
[0].get_int
!= TYPE_CLBK_POSITIONAL
)
1269 for(i
=_ARGMAX
; i
>0; i
--)
1273 for(j
=1; j
<=i
; j
++) {
1274 switch(args
[j
].get_int
) {
1276 args
[j
].get_longlong
= va_arg(valist
, LONGLONG
);
1279 args
[j
].get_int
= va_arg(valist
, int);
1282 args
[j
].get_double
= va_arg(valist
, double);
1285 args
[j
].get_ptr
= va_arg(valist
, void*);