msvcrt: Fixed floating point numbers printing when flags.Sign=' '.

This commit is contained in:
Piotr Caban 2012-12-12 11:15:12 +01:00 committed by Alexandre Julliard
parent 101abcd8b1
commit d958ecbc69
2 changed files with 43 additions and 17 deletions

View File

@ -78,7 +78,7 @@ static inline int FUNC_NAME(pf_fill)(FUNC_NAME(puts_clbk) pf_puts, void *puts_ct
{ {
int i, r = 0, written; int i, r = 0, written;
if(flags->Sign && !(flags->Format=='d' || flags->Format=='i')) if(flags->Sign && !strchr("diaeEfgG", flags->Format))
flags->Sign = 0; flags->Sign = 0;
if(left && flags->Sign) { if(left && flags->Sign) {
@ -228,18 +228,8 @@ static inline int FUNC_NAME(pf_handle_string)(FUNC_NAME(puts_clbk) pf_puts, void
static inline void FUNC_NAME(pf_rebuild_format_string)(char *p, FUNC_NAME(pf_flags) *flags) static inline void FUNC_NAME(pf_rebuild_format_string)(char *p, FUNC_NAME(pf_flags) *flags)
{ {
*p++ = '%'; *p++ = '%';
if(flags->Sign)
*p++ = flags->Sign;
if(flags->LeftAlign)
*p++ = flags->LeftAlign;
if(flags->Alternate) if(flags->Alternate)
*p++ = flags->Alternate; *p++ = flags->Alternate;
if(flags->PadZero)
*p++ = flags->PadZero;
if(flags->FieldLength) {
sprintf(p, "%d", flags->FieldLength);
p += strlen(p);
}
if(flags->Precision >= 0) { if(flags->Precision >= 0) {
sprintf(p, ".%d", flags->Precision); sprintf(p, ".%d", flags->Precision);
p += strlen(p); p += strlen(p);
@ -541,8 +531,9 @@ int FUNC_NAME(pf_printf)(FUNC_NAME(puts_clbk) pf_puts, void *puts_ctx, const API
HeapFree(GetProcessHeap(), 0, tmp); HeapFree(GetProcessHeap(), 0, tmp);
} else if(flags.Format && strchr("aeEfgG", flags.Format)) { } else if(flags.Format && strchr("aeEfgG", flags.Format)) {
char float_fmt[20], buf_a[32], *tmp = buf_a, *decimal_point; char float_fmt[20], buf_a[32], *tmp = buf_a, *decimal_point;
int max_len = (flags.FieldLength>flags.Precision ? flags.FieldLength : flags.Precision) + 10; int len = flags.Precision + 10;
double val = pf_args(args_ctx, pos, VT_R8, valist).get_double; double val = pf_args(args_ctx, pos, VT_R8, valist).get_double;
int r;
if(flags.Format=='f') { if(flags.Format=='f') {
if(val>-10.0 && val<10.0) if(val>-10.0 && val<10.0)
@ -552,16 +543,20 @@ int FUNC_NAME(pf_printf)(FUNC_NAME(puts_clbk) pf_puts, void *puts_ctx, const API
/* Default precision is 6, additional space for sign, separator and nullbyte is required */ /* Default precision is 6, additional space for sign, separator and nullbyte is required */
i += (flags.Precision==-1 ? 6 : flags.Precision) + 3; i += (flags.Precision==-1 ? 6 : flags.Precision) + 3;
if(i > max_len) if(i > len)
max_len = i; len = i;
} }
if(max_len > sizeof(buf_a)) if(len > sizeof(buf_a))
tmp = HeapAlloc(GetProcessHeap(), 0, max_len); tmp = HeapAlloc(GetProcessHeap(), 0, len);
if(!tmp) if(!tmp)
return -1; return -1;
FUNC_NAME(pf_rebuild_format_string)(float_fmt, &flags); FUNC_NAME(pf_rebuild_format_string)(float_fmt, &flags);
if(val < 0) {
flags.Sign = '-';
val = -val;
}
sprintf(tmp, float_fmt, val); sprintf(tmp, float_fmt, val);
if(toupper(flags.Format)=='E' || toupper(flags.Format)=='G') if(toupper(flags.Format)=='E' || toupper(flags.Format)=='G')
@ -571,9 +566,20 @@ int FUNC_NAME(pf_printf)(FUNC_NAME(puts_clbk) pf_puts, void *puts_ctx, const API
if(decimal_point) if(decimal_point)
*decimal_point = *locinfo->lconv->decimal_point; *decimal_point = *locinfo->lconv->decimal_point;
i = FUNC_NAME(pf_output_str)(pf_puts, puts_ctx, tmp, strlen(tmp), locinfo); len = strlen(tmp);
i = FUNC_NAME(pf_fill)(pf_puts, puts_ctx, len, &flags, TRUE);
if(i < 0)
return i;
r = FUNC_NAME(pf_output_str)(pf_puts, puts_ctx, tmp, len, locinfo);
if(r < 0)
return r;
i += r;
if(tmp != buf_a) if(tmp != buf_a)
HeapFree(GetProcessHeap(), 0, tmp); HeapFree(GetProcessHeap(), 0, tmp);
r = FUNC_NAME(pf_fill)(pf_puts, puts_ctx, len, &flags, FALSE);
if(r < 0)
return r;
i += r;
} else { } else {
if(invoke_invalid_param_handler) { if(invoke_invalid_param_handler) {
MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0); MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);

View File

@ -515,6 +515,26 @@ static void test_sprintf( void )
ok(!strcmp(buffer,"8.6000e+000"), "failed\n"); ok(!strcmp(buffer,"8.6000e+000"), "failed\n");
ok( r==11, "return count wrong\n"); ok( r==11, "return count wrong\n");
format = "% 2.4e";
r = sprintf(buffer, format,8.6);
ok(!strcmp(buffer," 8.6000e+000"), "failed: %s\n", buffer);
ok( r==12, "return count wrong\n");
format = "% 014.4e";
r = sprintf(buffer, format,8.6);
ok(!strcmp(buffer," 008.6000e+000"), "failed: %s\n", buffer);
ok( r==14, "return count wrong\n");
format = "% 2.4e";
r = sprintf(buffer, format,-8.6);
ok(!strcmp(buffer,"-8.6000e+000"), "failed: %s\n", buffer);
ok( r==12, "return count wrong\n");
format = "%+2.4e";
r = sprintf(buffer, format,8.6);
ok(!strcmp(buffer,"+8.6000e+000"), "failed: %s\n", buffer);
ok( r==12, "return count wrong\n");
format = "%2.4g"; format = "%2.4g";
r = sprintf(buffer, format,8.6); r = sprintf(buffer, format,8.6);
ok(!strcmp(buffer,"8.6"), "failed\n"); ok(!strcmp(buffer,"8.6"), "failed\n");