summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBram Moolenaar <Bram@vim.org>2016-08-28 16:03:38 +0200
committerBram Moolenaar <Bram@vim.org>2016-08-28 16:03:38 +0200
commite999782e369999539a1783a7ebe4eadcc6da28a8 (patch)
tree408ae82bd87804915887ae839627b1f434a3ef79
parent46328f9a1cc8047d1e05095bc9f531038c5a4028 (diff)
downloadvim-git-e999782e369999539a1783a7ebe4eadcc6da28a8.tar.gz
patch 7.4.2280v7.4.2280
Problem: printf() doesn't handle infinity float values correctly. Solution: Add a table with possible infinity values. (Dominique Pelle)
-rw-r--r--src/message.c79
-rw-r--r--src/testdir/test_expr.vim94
-rw-r--r--src/version.c2
3 files changed, 132 insertions, 43 deletions
diff --git a/src/message.c b/src/message.c
index 037d45605..12cb2c41a 100644
--- a/src/message.c
+++ b/src/message.c
@@ -3991,6 +3991,28 @@ tv_float(typval_T *tvs, int *idxp)
#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,8 +4030,8 @@ tv_float(typval_T *tvs, int *idxp)
*
* 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.
+ * 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,7 +4419,7 @@ vim_vsnprintf(
uvarnumber_T ullong_arg = 0;
# endif
- /* only defined for b convertion */
+ /* only defined for b conversion */
uvarnumber_T bin_arg = 0;
/* pointer argument value -only defined for p
@@ -4702,7 +4724,6 @@ vim_vsnprintf(
char format[40];
int l;
int remove_trailing_zeroes = FALSE;
- char *s;
f =
# if defined(FEAT_EVAL)
@@ -4732,16 +4753,10 @@ vim_vsnprintf(
)
{
/* Avoid a buffer overflow */
- if (f < 0)
- {
- strcpy(tmp, "-inf");
- str_arg_l = 4;
- }
- else
- {
- strcpy(tmp, "inf");
- str_arg_l = 3;
- }
+ STRCPY(tmp, infinity_str(f > 0.0, fmt_spec,
+ force_sign, space_for_positive));
+ str_arg_l = STRLEN(tmp);
+ zero_padding = 0;
}
else
{
@@ -4761,23 +4776,25 @@ vim_vsnprintf(
}
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 (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)
{
diff --git a/src/testdir/test_expr.vim b/src/testdir/test_expr.vim
index 6f1de59c9..11ca288dd 100644
--- a/src/testdir/test_expr.vim
+++ b/src/testdir/test_expr.vim
@@ -154,13 +154,22 @@ function Test_printf_misc()
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('%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))
@@ -194,20 +203,81 @@ function Test_printf_float()
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('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('1.2', printf('%.1f', 1.23))
-
+ 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_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_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
diff --git a/src/version.c b/src/version.c
index 007a56c59..8bca41753 100644
--- a/src/version.c
+++ b/src/version.c
@@ -764,6 +764,8 @@ static char *(features[]) =
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
+ 2280,
+/**/
2279,
/**/
2278,