msvcrt: Fixed printf behaviour on hexadecimal, zero-padded numbers.

This commit is contained in:
Piotr Caban 2012-03-12 19:49:11 +01:00 committed by Alexandre Julliard
parent c9bd71f32f
commit 79c19aef29
2 changed files with 23 additions and 1 deletions

View File

@ -508,8 +508,14 @@ int FUNC_NAME(pf_printf)(FUNC_NAME(puts_clbk) pf_puts, void *puts_ctx, const API
i = 0; i = 0;
} else if(flags.Format && strchr("diouxX", flags.Format)) { } else if(flags.Format && strchr("diouxX", flags.Format)) {
APICHAR *tmp = buf; APICHAR *tmp = buf;
int max_len = (flags.FieldLength>flags.Precision ? flags.FieldLength : flags.Precision) + 10; int max_len;
/* 0 padding is added after '0x' in Alternate flag is in use */
if((flags.Format=='x' || flags.Format=='X') && flags.PadZero && flags.Alternate
&& !flags.LeftAlign && flags.Precision<flags.FieldLength-2)
flags.Precision = flags.FieldLength - 2;
max_len = (flags.FieldLength>flags.Precision ? flags.FieldLength : flags.Precision) + 10;
if(max_len > sizeof(buf)/sizeof(APICHAR)) if(max_len > sizeof(buf)/sizeof(APICHAR))
tmp = HeapAlloc(GetProcessHeap(), 0, max_len); tmp = HeapAlloc(GetProcessHeap(), 0, max_len);
if(!tmp) if(!tmp)

View File

@ -323,6 +323,22 @@ static void test_sprintf( void )
ok(!strcmp(buffer,"1 "),"Character zero-padded and/or not left-adjusted \"%s\"\n",buffer); ok(!strcmp(buffer,"1 "),"Character zero-padded and/or not left-adjusted \"%s\"\n",buffer);
ok( r==4, "return count wrong\n"); ok( r==4, "return count wrong\n");
format = "%#012x";
r = sprintf(buffer,format,1);
ok(!strcmp(buffer,"0x0000000001"),"Hexadecimal zero-padded \"%s\"\n",buffer);
format = "%#04.8x";
r = sprintf(buffer,format,1);
ok(!strcmp(buffer,"0x00000001"), "Hexadecimal zero-padded precision \"%s\"\n",buffer);
format = "%#-08.2x";
r = sprintf(buffer,format,1);
ok(!strcmp(buffer,"0x01 "), "Hexadecimal zero-padded not left-adjusted \"%s\"\n",buffer);
format = "%#08o";
r = sprintf(buffer,format,1);
ok(!strcmp(buffer,"00000001"), "Octal zero-padded \"%s\"\n",buffer);
if (sizeof(void *) == 8) if (sizeof(void *) == 8)
{ {
format = "%p"; format = "%p";