To: vim_dev@googlegroups.com Subject: Patch 7.4.2280 Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ------------ Patch 7.4.2280 Problem: printf() doesn't handle infinity float values correctly. Solution: Add a table with possible infinity values. (Dominique Pelle) Files: src/message.c, src/testdir/test_expr.vim *** ../vim-7.4.2279/src/message.c 2016-08-27 15:41:28.886869527 +0200 --- src/message.c 2016-08-28 15:55:33.169169987 +0200 *************** *** 3991,3996 **** --- 3991,4018 ---- #endif /* + * Return the representation of infinity for printf() function: + * "-inf", "inf", "+inf", " inf", "-INF", "INF", "+INF" or " INF". + */ + static const char * + infinity_str(int positive, + char fmt_spec, + int force_sign, + int space_for_positive) + { + static const char *table[] = + { + "-inf", "inf", "+inf", " inf", + "-INF", "INF", "+INF", " INF" + }; + int idx = positive * (1 + force_sign + force_sign * space_for_positive); + + if (ASCII_ISUPPER(fmt_spec)) + idx += 4; + return table[idx]; + } + + /* * This code was included to provide a portable vsnprintf() and snprintf(). * Some systems may provide their own, but we always use this one for * consistency. *************** *** 4008,4015 **** * * Limited support for floating point was added: 'f', 'e', 'E', 'g', 'G'. * ! * Length modifiers 'h' (short int) and 'l' (long int) are supported. ! * 'll' (long long int) is not supported. * * The locale is not used, the string is used as a byte string. This is only * relevant for double-byte encodings where the second byte may be '%'. --- 4030,4037 ---- * * Limited support for floating point was added: 'f', 'e', 'E', 'g', 'G'. * ! * Length modifiers 'h' (short int) and 'l' (long int) and 'll' (long long int) ! * are supported. * * The locale is not used, the string is used as a byte string. This is only * relevant for double-byte encodings where the second byte may be '%'. *************** *** 4397,4403 **** uvarnumber_T ullong_arg = 0; # endif ! /* only defined for b convertion */ uvarnumber_T bin_arg = 0; /* pointer argument value -only defined for p --- 4419,4425 ---- uvarnumber_T ullong_arg = 0; # endif ! /* only defined for b conversion */ uvarnumber_T bin_arg = 0; /* pointer argument value -only defined for p *************** *** 4702,4708 **** char format[40]; int l; int remove_trailing_zeroes = FALSE; - char *s; f = # if defined(FEAT_EVAL) --- 4724,4729 ---- *************** *** 4732,4747 **** ) { /* Avoid a buffer overflow */ ! if (f < 0) ! { ! strcpy(tmp, "-inf"); ! str_arg_l = 4; ! } ! else ! { ! strcpy(tmp, "inf"); ! str_arg_l = 3; ! } } else { --- 4753,4762 ---- ) { /* Avoid a buffer overflow */ ! STRCPY(tmp, infinity_str(f > 0.0, fmt_spec, ! force_sign, space_for_positive)); ! str_arg_l = STRLEN(tmp); ! zero_padding = 0; } else { *************** *** 4761,4783 **** } format[l] = fmt_spec; format[l + 1] = NUL; - str_arg_l = sprintf(tmp, format, f); ! /* Be consistent: Change "1.#IND" to "nan" and ! * "1.#INF" to "inf". */ ! s = *tmp == '-' ? tmp + 1 : tmp; ! if (STRNCMP(s, "1.#INF", 6) == 0) ! STRCPY(s, "inf"); ! else if (STRNCMP(s, "1.#IND", 6) == 0) ! STRCPY(s, "nan"); ! ! /* Remove sign before "nan". */ ! if (STRNCMP(tmp, "-nan", 4) == 0) ! STRCPY(tmp, "nan"); ! ! /* Add sign before "inf" if needed. */ ! if (isinf(f) == -1 && STRNCMP(tmp, "inf", 3) == 0) ! STRCPY(tmp, "-inf"); if (remove_trailing_zeroes) { --- 4776,4800 ---- } format[l] = fmt_spec; format[l + 1] = NUL; ! if (isnan(f)) ! { ! /* Not a number: nan or NAN */ ! STRCPY(tmp, ASCII_ISUPPER(fmt_spec) ? "NAN" ! : "nan"); ! str_arg_l = 3; ! zero_padding = 0; ! } ! else if (isinf(f)) ! { ! STRCPY(tmp, infinity_str(f > 0.0, fmt_spec, ! force_sign, space_for_positive)); ! str_arg_l = STRLEN(tmp); ! zero_padding = 0; ! } ! else ! /* Regular float number */ ! str_arg_l = sprintf(tmp, format, f); if (remove_trailing_zeroes) { *** ../vim-7.4.2279/src/testdir/test_expr.vim 2016-08-27 15:26:31.874459807 +0200 --- src/testdir/test_expr.vim 2016-08-28 15:50:23.815857823 +0200 *************** *** 154,166 **** call assert_equal(' 123', printf('% d', 123)) call assert_equal('-123', printf('% d', -123)) call assert_equal('00123', printf('%.*d', 5, 123)) call assert_equal(' 123', printf('% *d', 5, 123)) call assert_equal(' +123', printf('%+ *d', 5, 123)) - call assert_equal('123', printf('%2d', 123)) - call assert_equal(' 123', printf('%5d', 123)) - call assert_equal('00123', printf('%05d', 123)) call assert_equal('123 ', printf('%-5d', 123)) call assert_equal('0x7b', printf('%#x', 123)) call assert_equal('0X7B', printf('%#X', 123)) --- 154,175 ---- call assert_equal(' 123', printf('% d', 123)) call assert_equal('-123', printf('% d', -123)) + call assert_equal('123', printf('%2d', 123)) + call assert_equal(' 123', printf('%6d', 123)) + call assert_equal('000123', printf('%06d', 123)) + call assert_equal('+00123', printf('%+06d', 123)) + call assert_equal(' 00123', printf('% 06d', 123)) + call assert_equal(' +123', printf('%+6d', 123)) + call assert_equal(' 123', printf('% 6d', 123)) + call assert_equal(' -123', printf('% 6d', -123)) + call assert_equal('+123 ', printf('%-+6d', 123)) + call assert_equal(' 123 ', printf('%- 6d', 123)) + call assert_equal('-123 ', printf('%- 6d', -123)) + call assert_equal('00123', printf('%.*d', 5, 123)) call assert_equal(' 123', printf('% *d', 5, 123)) call assert_equal(' +123', printf('%+ *d', 5, 123)) call assert_equal('123 ', printf('%-5d', 123)) call assert_equal('0x7b', printf('%#x', 123)) call assert_equal('0X7B', printf('%#X', 123)) *************** *** 194,213 **** if has('float') call assert_equal('1.230000', printf('%f', 1.23)) call assert_equal('1.230000', printf('%F', 1.23)) ! call assert_equal('1.23', printf('%g', 1.23)) ! call assert_equal('1.23', printf('%G', 1.23)) call assert_equal('1.230000e+00', printf('%e', 1.23)) call assert_equal('1.230000E+00', printf('%E', 1.23)) call assert_equal('1.200000e-02', printf('%e', 0.012)) call assert_equal('-1.200000e-02', printf('%e', -0.012)) ! call assert_equal('1.2', printf('%.1f', 1.23)) call assert_equal('inf', printf('%f', 1.0/0.0)) ! ! call assert_match('^-inf$', printf('%f', -1.0/0.0)) ! ! call assert_match('^nan$', printf('%f', sqrt(-1.0))) ! call assert_match('^nan$', printf('%f', 0.0/0.0)) call assert_fails('echo printf("%f", "a")', 'E807:') endif --- 203,283 ---- if has('float') call assert_equal('1.230000', printf('%f', 1.23)) call assert_equal('1.230000', printf('%F', 1.23)) ! call assert_equal('9999999.9', printf('%g', 9999999.9)) ! call assert_equal('9999999.9', printf('%G', 9999999.9)) ! call assert_equal('1.00000001e7', printf('%.8g', 10000000.1)) ! call assert_equal('1.00000001E7', printf('%.8G', 10000000.1)) call assert_equal('1.230000e+00', printf('%e', 1.23)) call assert_equal('1.230000E+00', printf('%E', 1.23)) call assert_equal('1.200000e-02', printf('%e', 0.012)) call assert_equal('-1.200000e-02', printf('%e', -0.012)) ! call assert_equal('0.33', printf('%.2f', 1.0/3.0)) ! call assert_equal(' 0.33', printf('%6.2f', 1.0/3.0)) ! call assert_equal(' -0.33', printf('%6.2f', -1.0/3.0)) ! call assert_equal('000.33', printf('%06.2f', 1.0/3.0)) ! " FIXME: call assert_equal('-00.33', printf('%06.2f', -1.0/3.0)) ! " FIXME: call assert_equal('-00.33', printf('%+06.2f', -1.0/3.0)) ! " FIXME: call assert_equal('+00.33', printf('%+06.2f', 1.0/3.0)) ! " FIXME: call assert_equal(' 00.33', printf('% 06.2f', 1.0/3.0)) + " Float infinity can be signed. call assert_equal('inf', printf('%f', 1.0/0.0)) ! call assert_equal('-inf', printf('%f', -1.0/0.0)) ! call assert_equal('inf', printf('%g', 1.0/0.0)) ! call assert_equal('-inf', printf('%g', -1.0/0.0)) ! call assert_equal('inf', printf('%e', 1.0/0.0)) ! call assert_equal('-inf', printf('%e', -1.0/0.0)) ! call assert_equal('INF', printf('%E', 1.0/0.0)) ! call assert_equal('-INF', printf('%E', -1.0/0.0)) ! call assert_equal('INF', printf('%E', 1.0/0.0)) ! call assert_equal('-INF', printf('%G', -1.0/0.0)) ! call assert_equal('+inf', printf('%+f', 1.0/0.0)) ! call assert_equal('-inf', printf('%+f', -1.0/0.0)) ! call assert_equal(' inf', printf('% f', 1.0/0.0)) ! call assert_equal(' inf', printf('%6f', 1.0/0.0)) ! call assert_equal(' -inf', printf('%6f', -1.0/0.0)) ! call assert_equal(' inf', printf('%6g', 1.0/0.0)) ! call assert_equal(' -inf', printf('%6g', -1.0/0.0)) ! call assert_equal(' +inf', printf('%+6f', 1.0/0.0)) ! call assert_equal(' inf', printf('% 6f', 1.0/0.0)) ! call assert_equal(' +inf', printf('%+06f', 1.0/0.0)) ! call assert_equal('inf ', printf('%-6f', 1.0/0.0)) ! call assert_equal('-inf ', printf('%-6f', -1.0/0.0)) ! call assert_equal('+inf ', printf('%-+6f', 1.0/0.0)) ! call assert_equal(' inf ', printf('%- 6f', 1.0/0.0)) ! call assert_equal('INF ', printf('%-6G', 1.0/0.0)) ! call assert_equal('-INF ', printf('%-6G', -1.0/0.0)) ! call assert_equal('INF ', printf('%-6E', 1.0/0.0)) ! call assert_equal('-INF ', printf('%-6E', -1.0/0.0)) ! call assert_equal('inf', printf('%s', 1.0/0.0)) ! call assert_equal('-inf', printf('%s', -1.0/0.0)) ! ! " Float zero can be signed. ! call assert_equal('0.000000', printf('%f', 1.0/(1.0/0.0))) ! call assert_equal('-0.000000', printf('%f', 1.0/(-1.0/0.0))) ! call assert_equal('0.0', printf('%s', 1.0/(1.0/0.0))) ! call assert_equal('-0.0', printf('%s', 1.0/(-1.0/0.0))) ! call assert_equal('0.0', printf('%S', 1.0/(1.0/0.0))) ! call assert_equal('-0.0', printf('%S', 1.0/(-1.0/0.0))) ! ! " Float nan (not a number) has no sign. ! call assert_equal('nan', printf('%f', sqrt(-1.0))) ! call assert_equal('nan', printf('%f', 0.0/0.0)) ! call assert_equal('nan', printf('%f', -0.0/0.0)) ! call assert_equal('nan', printf('%g', 0.0/0.0)) ! call assert_equal('nan', printf('%e', 0.0/0.0)) ! call assert_equal('NAN', printf('%G', 0.0/0.0)) ! call assert_equal('NAN', printf('%E', 0.0/0.0)) ! call assert_equal('NAN', printf('%G', -0.0/0.0)) ! call assert_equal('NAN', printf('%E', -0.0/0.0)) ! call assert_equal(' nan', printf('%6f', 0.0/0.0)) ! call assert_equal(' nan', printf('%06f', 0.0/0.0)) ! call assert_equal('nan ', printf('%-6f', 0.0/0.0)) ! call assert_equal('nan ', printf('%- 6f', 0.0/0.0)) ! call assert_equal('nan', printf('%s', 0.0/0.0)) ! call assert_equal('nan', printf('%s', -0.0/0.0)) ! call assert_equal('nan', printf('%S', 0.0/0.0)) ! call assert_equal('nan', printf('%S', -0.0/0.0)) call assert_fails('echo printf("%f", "a")', 'E807:') endif *** ../vim-7.4.2279/src/version.c 2016-08-28 15:39:53.473320888 +0200 --- src/version.c 2016-08-28 15:51:56.811049450 +0200 *************** *** 765,766 **** --- 765,768 ---- { /* Add new patch number below this line */ + /**/ + 2280, /**/ -- I AM THANKFUL... ...for the clothes that fit a little too snug because it means I have more than enough to eat. /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\ /// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\ \\\ an exciting new programming language -- http://www.Zimbu.org /// \\\ help me help AIDS victims -- http://ICCF-Holland.org ///