diff options
author | Bram Moolenaar <Bram@vim.org> | 2016-08-28 16:03:38 +0200 |
---|---|---|
committer | Bram Moolenaar <Bram@vim.org> | 2016-08-28 16:03:38 +0200 |
commit | e999782e369999539a1783a7ebe4eadcc6da28a8 (patch) | |
tree | 408ae82bd87804915887ae839627b1f434a3ef79 /src/message.c | |
parent | 46328f9a1cc8047d1e05095bc9f531038c5a4028 (diff) | |
download | vim-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)
Diffstat (limited to 'src/message.c')
-rw-r--r-- | src/message.c | 79 |
1 files changed, 48 insertions, 31 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) { |