data-out: Optimize and fix some bad assumptions.
[pspp-builds.git] / src / data / data-out.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2006, 2009, 2011 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include "data-out.h"
20
21 #include <ctype.h>
22 #include <float.h>
23 #include <math.h>
24 #include <stdint.h>
25 #include <stdlib.h>
26 #include <time.h>
27 #include <unistr.h>
28
29 #include <data/calendar.h>
30 #include <data/format.h>
31 #include <data/settings.h>
32 #include <data/value.h>
33
34 #include <libpspp/assertion.h>
35 #include <libpspp/cast.h>
36 #include <libpspp/float-format.h>
37 #include <libpspp/integer-format.h>
38 #include <libpspp/message.h>
39 #include <libpspp/misc.h>
40 #include <libpspp/str.h>
41 #include <libpspp/pool.h>
42 #include <libpspp/i18n.h>
43
44 #include "minmax.h"
45
46 #include "gettext.h"
47 #define _(msgid) gettext (msgid)
48 \f
49 /* A representation of a number that can be quickly rounded to
50    any desired number of decimal places (up to a specified
51    maximum). */
52 struct rounder
53   {
54     char string[64];    /* Magnitude of number with excess precision. */
55     int integer_digits; /* Number of digits before decimal point. */
56     int leading_nines;  /* Number of `9's or `.'s at start of string. */
57     int leading_zeros;  /* Number of `0's or `.'s at start of string. */
58     bool negative;      /* Is the number negative? */
59   };
60
61 static void rounder_init (struct rounder *, double number, int max_decimals);
62 static int rounder_width (const struct rounder *, int decimals,
63                           int *integer_digits, bool *negative);
64 static void rounder_format (const struct rounder *, int decimals,
65                             char *output);
66 \f
67 typedef void data_out_converter_func (const union value *,
68                                       const struct fmt_spec *,
69                                       char *);
70 #define FMT(NAME, METHOD, IMIN, OMIN, IO, CATEGORY) \
71         static data_out_converter_func output_##METHOD;
72 #include "format.def"
73
74 static bool output_decimal (const struct rounder *, const struct fmt_spec *,
75                             bool require_affixes, char *);
76 static bool output_scientific (double, const struct fmt_spec *,
77                                bool require_affixes, char *);
78
79 static double power10 (int) PURE_FUNCTION;
80 static double power256 (int) PURE_FUNCTION;
81
82 static void output_infinite (double, const struct fmt_spec *, char *);
83 static void output_missing (const struct fmt_spec *, char *);
84 static void output_overflow (const struct fmt_spec *, char *);
85 static bool output_bcd_integer (double, int digits, char *);
86 static void output_binary_integer (uint64_t, int bytes, enum integer_format,
87                                    char *);
88 static void output_hex (const void *, size_t bytes, char *);
89 \f
90
91 static data_out_converter_func *const converters[FMT_NUMBER_OF_FORMATS] =
92     {
93 #define FMT(NAME, METHOD, IMIN, OMIN, IO, CATEGORY) output_##METHOD,
94 #include "format.def"
95     };
96
97 /* Converts the INPUT value, encoded in INPUT_ENCODING, according to format
98    specification FORMAT, appending the output to OUTPUT in OUTPUT_ENCODING.
99    However, binary formats (FMT_P, FMT_PK, FMT_IB, FMT_PIB, FMT_RB) yield the
100    binary results, which may not be properly encoded for OUTPUT_ENCODING.
101
102    VALUE must be the correct width for FORMAT, that is, its width must be
103    fmt_var_width(FORMAT).
104
105    INPUT_ENCODING can normally be obtained by calling dict_get_encoding() on
106    the dictionary with which INPUT is associated.  ENCODING is only important
107    when FORMAT's type is FMT_A. */
108 void
109 data_out_recode (const union value *input, const char *input_encoding,
110                  const struct fmt_spec *format,
111                  struct string *output, const char *output_encoding)
112 {
113   assert (fmt_check_output (format));
114   if (format->type == FMT_A)
115     {
116       char *in = CHAR_CAST (char *, value_str (input, format->w));
117       char *out = recode_string (output_encoding, input_encoding,
118                                  in, format->w);
119       ds_put_cstr (output, out);
120       free (out);
121     }
122   else if (fmt_get_category (format->type) == FMT_CAT_BINARY)
123     converters[format->type] (input, format,
124                               ds_put_uninit (output, format->w));
125   else
126     {
127       char *utf8_encoded = data_out (input, input_encoding, format);
128       char *output_encoded = recode_string (output_encoding, UTF8,
129                                             utf8_encoded, -1);
130       ds_put_cstr (output, output_encoded);
131       free (output_encoded);
132       free (utf8_encoded);
133     }
134 }
135
136 static char *
137 binary_to_utf8 (const char *in, struct pool *pool)
138 {
139   uint8_t *out = pool_alloc_unaligned (pool, strlen (in) * 2 + 1);
140   uint8_t *p = out;
141
142   while (*in != '\0')
143     {
144       uint8_t byte = *in++;
145       int mblen = u8_uctomb (p, byte, 2);
146       assert (mblen > 0);
147       p += mblen;
148     }
149   *p = '\0';
150
151   return CHAR_CAST (char *, out);
152 }
153
154 /* Converts the INPUT value into a UTF-8 encoded string, according to format
155    specification FORMAT.
156
157    VALUE must be the correct width for FORMAT, that is, its width must be
158    fmt_var_width(FORMAT).
159
160    ENCODING must be the encoding of INPUT.  Normally this can be obtained by
161    calling dict_get_encoding() on the dictionary with which INPUT is
162    associated.  ENCODING is only important when FORMAT's type is FMT_A.
163
164    The return value is dynamically allocated, and must be freed by the caller.
165    If POOL is non-null, then the return value is allocated on that pool.  */
166 char *
167 data_out_pool (const union value *input, const char *encoding,
168                const struct fmt_spec *format, struct pool *pool)
169 {
170   assert (fmt_check_output (format));
171   if (format->type == FMT_A)
172     {
173       char *in = CHAR_CAST (char *, value_str (input, format->w));
174       return recode_string_pool (UTF8, encoding, in, format->w, pool);
175     }
176   else if (fmt_get_category (format->type) == FMT_CAT_BINARY)
177     {
178       char tmp[16];
179
180       assert (format->w + 1 <= sizeof tmp);
181       converters[format->type] (input, format, tmp);
182       return binary_to_utf8 (tmp, pool);
183     }
184   else
185     {
186       const struct fmt_number_style *style = settings_get_style (format->type);
187       size_t size = format->w + style->extra_bytes + 1;
188       char *output;
189
190       output = pool_alloc_unaligned (pool, size);
191       converters[format->type] (input, format, output);
192       return output;
193     }
194 }
195
196 char *
197 data_out (const union value *input, const char *encoding, const struct fmt_spec *format)
198 {
199   return data_out_pool (input, encoding, format, NULL);
200 }
201
202 \f
203 /* Main conversion functions. */
204
205 /* Outputs F, COMMA, DOT, DOLLAR, PCT, E, CCA, CCB, CCC, CCD, and
206    CCE formats. */
207 static void
208 output_number (const union value *input, const struct fmt_spec *format,
209                char *output)
210 {
211   double number = input->f;
212
213   if (number == SYSMIS)
214     output_missing (format, output);
215   else if (!isfinite (number))
216     output_infinite (number, format, output);
217   else
218     {
219       if (format->type != FMT_E && fabs (number) < 1.5 * power10 (format->w))
220         {
221           struct rounder r;
222           rounder_init (&r, number, format->d);
223
224           if (output_decimal (&r, format, true, output)
225               || output_scientific (number, format, true, output)
226               || output_decimal (&r, format, false, output))
227             return;
228         }
229
230       if (!output_scientific (number, format, false, output))
231         output_overflow (format, output);
232     }
233 }
234
235 /* Outputs N format. */
236 static void
237 output_N (const union value *input, const struct fmt_spec *format,
238           char *output)
239 {
240   double number = input->f * power10 (format->d);
241   if (input->f == SYSMIS || number < 0)
242     output_missing (format, output);
243   else
244     {
245       char buf[128];
246       number = fabs (round (number));
247       if (number < power10 (format->w)
248           && sprintf (buf, "%0*.0f", format->w, number) == format->w)
249         memcpy (output, buf, format->w);
250       else
251         output_overflow (format, output);
252     }
253
254   output[format->w] = '\0';
255 }
256
257 /* Outputs Z format. */
258 static void
259 output_Z (const union value *input, const struct fmt_spec *format,
260           char *output)
261 {
262   double number = input->f * power10 (format->d);
263   char buf[128];
264   if (input->f == SYSMIS)
265     output_missing (format, output);
266   else if (fabs (number) < power10 (format->w)
267            && sprintf (buf, "%0*.0f", format->w,
268                        fabs (round (number))) == format->w)
269     {
270       if (number < 0 && strspn (buf, "0") < format->w)
271         {
272           char *p = &buf[format->w - 1];
273           *p = "}JKLMNOPQR"[*p - '0'];
274         }
275       memcpy (output, buf, format->w);
276       output[format->w] = '\0';
277     }
278   else
279     output_overflow (format, output);
280 }
281
282 /* Outputs P format. */
283 static void
284 output_P (const union value *input, const struct fmt_spec *format,
285           char *output)
286 {
287   if (output_bcd_integer (fabs (input->f * power10 (format->d)),
288                           format->w * 2 - 1, output)
289       && input->f < 0.0)
290     output[format->w - 1] |= 0xd;
291   else
292     output[format->w - 1] |= 0xf;
293 }
294
295 /* Outputs PK format. */
296 static void
297 output_PK (const union value *input, const struct fmt_spec *format,
298            char *output)
299 {
300   output_bcd_integer (input->f * power10 (format->d), format->w * 2, output);
301 }
302
303 /* Outputs IB format. */
304 static void
305 output_IB (const union value *input, const struct fmt_spec *format,
306            char *output)
307 {
308   double number = round (input->f * power10 (format->d));
309   if (input->f == SYSMIS
310       || number >= power256 (format->w) / 2 - 1
311       || number < -power256 (format->w) / 2)
312     memset (output, 0, format->w);
313   else
314     {
315       uint64_t integer = fabs (number);
316       if (number < 0)
317         integer = -integer;
318       output_binary_integer (integer, format->w,
319                              settings_get_output_integer_format (),
320                              output);
321     }
322
323   output[format->w] = '\0';
324 }
325
326 /* Outputs PIB format. */
327 static void
328 output_PIB (const union value *input, const struct fmt_spec *format,
329             char *output)
330 {
331   double number = round (input->f * power10 (format->d));
332   if (input->f == SYSMIS
333       || number < 0 || number >= power256 (format->w))
334     memset (output, 0, format->w);
335   else
336     output_binary_integer (number, format->w,
337                            settings_get_output_integer_format (), output);
338
339   output[format->w] = '\0';
340 }
341
342 /* Outputs PIBHEX format. */
343 static void
344 output_PIBHEX (const union value *input, const struct fmt_spec *format,
345                char *output)
346 {
347   double number = round (input->f);
348   if (input->f == SYSMIS)
349     output_missing (format, output);
350   else if (input->f < 0 || number >= power256 (format->w / 2))
351     output_overflow (format, output);
352   else
353     {
354       char tmp[8];
355       output_binary_integer (number, format->w / 2, INTEGER_MSB_FIRST, tmp);
356       output_hex (tmp, format->w / 2, output);
357     }
358
359 }
360
361 /* Outputs RB format. */
362 static void
363 output_RB (const union value *input, const struct fmt_spec *format,
364            char *output)
365 {
366   double d = input->f;
367   memcpy (output, &d, format->w);
368
369   output[format->w] = '\0';
370 }
371
372 /* Outputs RBHEX format. */
373 static void
374 output_RBHEX (const union value *input, const struct fmt_spec *format,
375               char *output)
376 {
377   double d = input->f;
378
379   output_hex (&d, format->w / 2, output);
380 }
381
382 /* Outputs DATE, ADATE, EDATE, JDATE, SDATE, QYR, MOYR, WKYR,
383    DATETIME, TIME, and DTIME formats. */
384 static void
385 output_date (const union value *input, const struct fmt_spec *format,
386              char *output)
387 {
388   double number = input->f;
389   int year, month, day, yday;
390
391   const char *template = fmt_date_template (format->type);
392   size_t template_width = strlen (template);
393   int excess_width = format->w - template_width;
394
395   char tmp[64];
396   char *p = tmp;
397
398   assert (format->w >= template_width);
399   if (number == SYSMIS)
400     goto missing;
401
402   if (fmt_get_category (format->type) == FMT_CAT_DATE)
403     {
404       if (number <= 0)
405         goto missing;
406       calendar_offset_to_gregorian (number / 60. / 60. / 24.,
407                                     &year, &month, &day, &yday);
408       number = fmod (number, 60. * 60. * 24.);
409     }
410   else
411     year = month = day = yday = 0;
412
413   while (*template != '\0')
414     {
415       int ch = *template;
416       int count = 1;
417       while (template[count] == ch)
418         count++;
419       template += count;
420
421       switch (ch)
422         {
423         case 'd':
424           if (count < 3)
425             p += sprintf (p, "%02d", day);
426           else
427             p += sprintf (p, "%03d", yday);
428           break;
429         case 'm':
430           if (count < 3)
431             p += sprintf (p, "%02d", month);
432           else
433             {
434               static const char *const months[12] =
435                 {
436                   "JAN", "FEB", "MAR", "APR", "MAY", "JUN",
437                   "JUL", "AUG", "SEP", "OCT", "NOV", "DEC",
438                 };
439               p = stpcpy (p, months[month - 1]);
440             }
441           break;
442         case 'y':
443           if (count >= 4 || excess_width >= 2)
444             {
445               if (year <= 9999)
446                 p += sprintf (p, "%04d", year);
447               else if (format->type == FMT_DATETIME)
448                 p = stpcpy (p, "****");
449               else
450                 goto overflow;
451             }
452           else
453             {
454               int epoch =  settings_get_epoch ();
455               int offset = year - epoch;
456               if (offset < 0 || offset > 99)
457                 goto overflow;
458               p += sprintf (p, "%02d", abs (year) % 100);
459             }
460           break;
461         case 'q':
462           p += sprintf (p, "%d", (month - 1) / 3 + 1);
463           break;
464         case 'w':
465           p += sprintf (p, "%2d", (yday - 1) / 7 + 1);
466           break;
467         case 'D':
468           if (number < 0)
469             *p++ = '-';
470           number = fabs (number);
471           p += sprintf (p, "%*.0f", count, floor (number / 60. / 60. / 24.));
472           number = fmod (number, 60. * 60. * 24.);
473           break;
474         case 'H':
475           if (number < 0)
476             *p++ = '-';
477           number = fabs (number);
478           p += sprintf (p, "%0*.0f", count, floor (number / 60. / 60.));
479           number = fmod (number, 60. * 60.);
480           break;
481         case 'M':
482           p += sprintf (p, "%02d", (int) floor (number / 60.));
483           number = fmod (number, 60.);
484           excess_width = format->w - (p - tmp);
485           if (excess_width < 0)
486             goto overflow;
487           if (excess_width == 3 || excess_width == 4
488               || (excess_width >= 5 && format->d == 0))
489             p += sprintf (p, ":%02d", (int) number);
490           else if (excess_width >= 5)
491             {
492               int d = MIN (format->d, excess_width - 4);
493               int w = d + 3;
494               sprintf (p, ":%0*.*f", w, d, number);
495               if (settings_get_decimal_char (FMT_F) != '.')
496                 {
497                   char *cp = strchr (p, '.');
498                   if (cp != NULL)
499                     *cp = settings_get_decimal_char (FMT_F);
500                 }
501               p += strlen (p);
502             }
503           break;
504         case 'X':
505           *p++ = ' ';
506           break;
507         default:
508           assert (count == 1);
509           *p++ = ch;
510           break;
511         }
512     }
513
514   buf_copy_lpad (output, format->w, tmp, p - tmp, ' ');
515   output[format->w] = '\0';
516   return;
517
518  overflow:
519   output_overflow (format, output);
520   return;
521
522  missing:
523   output_missing (format, output);
524   return;
525 }
526
527 /* Outputs WKDAY format. */
528 static void
529 output_WKDAY (const union value *input, const struct fmt_spec *format,
530               char *output)
531 {
532   static const char *const weekdays[7] =
533     {
534       "SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY",
535       "THURSDAY", "FRIDAY", "SATURDAY",
536     };
537
538   if (input->f >= 1 && input->f < 8)
539     {
540       buf_copy_str_rpad (output, format->w,
541                          weekdays[(int) input->f - 1], ' ');
542       output[format->w] = '\0';
543     }
544   else
545     {
546       if (input->f != SYSMIS)
547         msg (ME, _("Weekday number %f is not between 1 and 7."), input->f);
548       output_missing (format, output);
549     }
550
551 }
552
553 /* Outputs MONTH format. */
554 static void
555 output_MONTH (const union value *input, const struct fmt_spec *format,
556               char *output)
557 {
558   static const char *const months[12] =
559     {
560       "JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE",
561       "JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER",
562     };
563
564   if (input->f >= 1 && input->f < 13)
565     {
566       buf_copy_str_rpad (output, format->w, months[(int) input->f - 1], ' ');
567       output[format->w] = '\0';
568     }
569   else
570     {
571       if (input->f != SYSMIS)
572         msg (ME, _("Month number %f is not between 1 and 12."), input->f);
573       output_missing (format, output);
574     }
575
576 }
577
578 /* Outputs A format. */
579 static void
580 output_A (const union value *input UNUSED,
581           const struct fmt_spec *format UNUSED, char *output UNUSED)
582 {
583   NOT_REACHED ();
584 }
585
586 /* Outputs AHEX format. */
587 static void
588 output_AHEX (const union value *input, const struct fmt_spec *format,
589              char *output)
590 {
591   output_hex (value_str (input, format->w), format->w / 2, output);
592 }
593 \f
594 /* Decimal and scientific formatting. */
595
596 /* If REQUEST plus the current *WIDTH fits within MAX_WIDTH,
597    increments *WIDTH by REQUEST and return true.
598    Otherwise returns false without changing *WIDTH. */
599 static bool
600 allocate_space (int request, int max_width, int *width)
601 {
602   assert (*width <= max_width);
603   if (request + *width <= max_width)
604     {
605       *width += request;
606       return true;
607     }
608   else
609     return false;
610 }
611
612 /* Tries to compose the number represented by R, in the style of
613    FORMAT, into OUTPUT.  Returns true if successful, false on
614    failure, which occurs if FORMAT's width is too narrow.  If
615    REQUIRE_AFFIXES is true, then the prefix and suffix specified
616    by FORMAT's style must be included; otherwise, they may be
617    omitted to make the number fit. */
618 static bool
619 output_decimal (const struct rounder *r, const struct fmt_spec *format,
620                 bool require_affixes, char *output)
621 {
622   const struct fmt_number_style *style =
623     settings_get_style (format->type);
624
625   int decimals;
626
627   for (decimals = format->d; decimals >= 0; decimals--)
628     {
629       /* Formatted version of magnitude of NUMBER. */
630       char magnitude[64];
631
632       /* Number of digits in MAGNITUDE's integer and fractional parts. */
633       int integer_digits;
634
635       /* Amount of space within the field width already claimed.
636          Initially this is the width of MAGNITUDE, then it is reduced
637          in stages as space is allocated to prefixes and suffixes and
638          grouping characters. */
639       int width;
640
641       /* Include various decorations? */
642       bool add_neg_prefix;
643       bool add_affixes;
644       bool add_grouping;
645
646       /* Position in output. */
647       char *p;
648
649       /* Make sure there's room for the number's magnitude, plus
650          the negative suffix, plus (if negative) the negative
651          prefix. */
652       width = rounder_width (r, decimals, &integer_digits, &add_neg_prefix);
653       width += style->neg_suffix.width;
654       if (add_neg_prefix)
655         width += style->neg_prefix.width;
656       if (width > format->w)
657         continue;
658
659       /* If there's room for the prefix and suffix, allocate
660          space.  If the affixes are required, but there's no
661          space, give up. */
662       add_affixes = allocate_space (fmt_affix_width (style),
663                                     format->w, &width);
664       if (!add_affixes && require_affixes)
665         continue;
666
667       /* Check whether we should include grouping characters.
668          We need room for a complete set or we don't insert any at all.
669          We don't include grouping characters if decimal places were
670          requested but they were all dropped. */
671       add_grouping = (style->grouping != 0
672                       && integer_digits > 3
673                       && (format->d == 0 || decimals > 0)
674                       && allocate_space ((integer_digits - 1) / 3,
675                                          format->w, &width));
676
677       /* Format the number's magnitude. */
678       rounder_format (r, decimals, magnitude);
679
680       /* Assemble number. */
681       p = output;
682       if (format->w > width)
683         p = mempset (p, ' ', format->w - width);
684       if (add_neg_prefix)
685         p = stpcpy (p, style->neg_prefix.s);
686       if (add_affixes)
687         p = stpcpy (p, style->prefix.s);
688       if (!add_grouping)
689         p = mempcpy (p, magnitude, integer_digits);
690       else
691         {
692           int i;
693           for (i = 0; i < integer_digits; i++)
694             {
695               if (i > 0 && (integer_digits - i) % 3 == 0)
696                 *p++ = style->grouping;
697               *p++ = magnitude[i];
698             }
699         }
700       if (decimals > 0)
701         {
702           *p++ = style->decimal;
703           p = mempcpy (p, &magnitude[integer_digits + 1], decimals);
704         }
705       if (add_affixes)
706         p = stpcpy (p, style->suffix.s);
707       if (add_neg_prefix)
708         p = stpcpy (p, style->neg_suffix.s);
709       else
710         p = mempset (p, ' ', style->neg_suffix.width);
711
712       assert (p >= output + format->w);
713       assert (p <= output + format->w + style->extra_bytes);
714       *p = '\0';
715
716       return true;
717     }
718   return false;
719 }
720
721 /* Formats NUMBER into OUTPUT in scientific notation according to
722    the style of the format specified in FORMAT. */
723 static bool
724 output_scientific (double number, const struct fmt_spec *format,
725                    bool require_affixes, char *output)
726 {
727   const struct fmt_number_style *style =
728     settings_get_style (format->type);
729   int width;
730   int fraction_width;
731   bool add_affixes;
732   char *p;
733
734   /* Allocate minimum required space. */
735   width = 6 + style->neg_suffix.width;
736   if (number < 0)
737     width += style->neg_prefix.width;
738   if (width > format->w)
739     return false;
740
741   /* Check for room for prefix and suffix. */
742   add_affixes = allocate_space (fmt_affix_width (style), format->w, &width);
743   if (require_affixes && !add_affixes)
744     return false;
745
746   /* Figure out number of characters we can use for the fraction,
747      if any.  (If that turns out to be 1, then we'll output a
748      decimal point without any digits following; that's what the
749      # flag does in the call to sprintf, below.) */
750   fraction_width = MIN (MIN (format->d + 1, format->w - width), 16);
751   if (format->type != FMT_E && fraction_width == 1)
752     fraction_width = 0;
753   width += fraction_width;
754
755   /* Format (except suffix). */
756   p = output;
757   if (width < format->w)
758     p = mempset (p, ' ', format->w - width);
759   if (number < 0)
760     p = stpcpy (p, style->neg_prefix.s);
761   if (add_affixes)
762     p = stpcpy (p, style->prefix.s);
763   if (fraction_width > 0)
764     sprintf (p, "%#.*E", fraction_width - 1, fabs (number));
765   else
766     sprintf (p, "%.0E", fabs (number));
767
768   /* The C locale always uses a period `.' as a decimal point.
769      Translate to comma if necessary. */
770   if (style->decimal != '.')
771     {
772       char *cp = strchr (p, '.');
773       if (cp != NULL)
774         *cp = style->decimal;
775     }
776
777   /* Make exponent have exactly three digits, plus sign. */
778   {
779     char *cp = strchr (p, 'E') + 1;
780     long int exponent = strtol (cp, NULL, 10);
781     if (abs (exponent) > 999)
782       return false;
783     sprintf (cp, "%+04ld", exponent);
784   }
785
786   /* Add suffixes. */
787   p = strchr (p, '\0');
788   if (add_affixes)
789     p = stpcpy (p, style->suffix.s);
790   if (number < 0)
791     p = stpcpy (p, style->neg_suffix.s);
792   else
793     p = mempset (p, ' ', style->neg_suffix.width);
794
795   assert (p >= output + format->w);
796   assert (p <= output + format->w + style->extra_bytes);
797   *p = '\0';
798
799   return true;
800 }
801 \f
802 /* Returns true if the magnitude represented by R should be
803    rounded up when chopped off at DECIMALS decimal places, false
804    if it should be rounded down. */
805 static bool
806 should_round_up (const struct rounder *r, int decimals)
807 {
808   int digit = r->string[r->integer_digits + decimals + 1];
809   assert (digit >= '0' && digit <= '9');
810   return digit >= '5';
811 }
812
813 /* Initializes R for formatting the magnitude of NUMBER to no
814    more than MAX_DECIMAL decimal places. */
815 static void
816 rounder_init (struct rounder *r, double number, int max_decimals)
817 {
818   assert (fabs (number) < 1e41);
819   assert (max_decimals >= 0 && max_decimals <= 16);
820   if (max_decimals == 0)
821     {
822       /* Fast path.  No rounding needed.
823
824          We append ".00" to the integer representation because
825          round_up assumes that fractional digits are present.  */
826       sprintf (r->string, "%.0f.00", fabs (round (number)));
827     }
828   else
829     {
830       /* Slow path.
831
832          This is more difficult than it really should be because
833          we have to make sure that numbers that are exactly
834          halfway between two representations are always rounded
835          away from zero.  This is not what sprintf normally does
836          (usually it rounds to even), so we have to fake it as
837          best we can, by formatting with extra precision and then
838          doing the rounding ourselves.
839
840          We take up to two rounds to format numbers.  In the
841          first round, we obtain 2 digits of precision beyond
842          those requested by the user.  If those digits are
843          exactly "50", then in a second round we format with as
844          many digits as are significant in a "double".
845
846          It might be better to directly implement our own
847          floating-point formatting routine instead of relying on
848          the system's sprintf implementation.  But the classic
849          Steele and White paper on printing floating-point
850          numbers does not hint how to do what we want, and it's
851          not obvious how to change their algorithms to do so.  It
852          would also be a lot of work. */
853       sprintf (r->string, "%.*f", max_decimals + 2, fabs (number));
854       if (!strcmp (r->string + strlen (r->string) - 2, "50"))
855         {
856           int binary_exponent, decimal_exponent, format_decimals;
857           frexp (number, &binary_exponent);
858           decimal_exponent = binary_exponent * 3 / 10;
859           format_decimals = (DBL_DIG + 1) - decimal_exponent;
860           if (format_decimals > max_decimals + 2)
861             sprintf (r->string, "%.*f", format_decimals, fabs (number));
862         }
863     }
864
865   if (r->string[0] == '0')
866     memmove (r->string, &r->string[1], strlen (r->string));
867
868   r->leading_zeros = strspn (r->string, "0.");
869   r->leading_nines = strspn (r->string, "9.");
870   r->integer_digits = strchr (r->string, '.') - r->string;
871   r->negative = number < 0;
872 }
873
874 /* Returns the number of characters required to format the
875    magnitude represented by R to DECIMALS decimal places.
876    The return value includes integer digits and a decimal point
877    and fractional digits, if any, but it does not include any
878    negative prefix or suffix or other affixes.
879
880    *INTEGER_DIGITS is set to the number of digits before the
881    decimal point in the output, between 0 and 40.
882
883    If R represents a negative number and its rounded
884    representation would include at least one nonzero digit,
885    *NEGATIVE is set to true; otherwise, it is set to false. */
886 static int
887 rounder_width (const struct rounder *r, int decimals,
888                int *integer_digits, bool *negative)
889 {
890   /* Calculate base measures. */
891   int width = r->integer_digits;
892   if (decimals > 0)
893     width += decimals + 1;
894   *integer_digits = r->integer_digits;
895   *negative = r->negative;
896
897   /* Rounding can cause adjustments. */
898   if (should_round_up (r, decimals))
899     {
900       /* Rounding up leading 9s adds a new digit (a 1). */
901       if (r->leading_nines >= width)
902         {
903           width++;
904           ++*integer_digits;
905         }
906     }
907   else
908     {
909       /* Rounding down. */
910       if (r->leading_zeros >= width)
911         {
912           /* All digits that remain after rounding are zeros.
913              Therefore we drop the negative sign. */
914           *negative = false;
915           if (r->integer_digits == 0 && decimals == 0)
916             {
917               /* No digits at all are left.  We need to display
918                  at least a single digit (a zero). */
919               assert (width == 0);
920               width++;
921               *integer_digits = 1;
922             }
923         }
924     }
925   return width;
926 }
927
928 /* Formats the magnitude represented by R into OUTPUT, rounding
929    to DECIMALS decimal places.  Exactly as many characters as
930    indicated by rounder_width are written.  No terminating null
931    is appended. */
932 static void
933 rounder_format (const struct rounder *r, int decimals, char *output)
934 {
935   int base_width = r->integer_digits + (decimals > 0 ? decimals + 1 : 0);
936   if (should_round_up (r, decimals))
937     {
938       if (r->leading_nines < base_width)
939         {
940           /* Rounding up.  This is the common case where rounding
941              up doesn't add an extra digit. */
942           char *p;
943           memcpy (output, r->string, base_width);
944           for (p = output + base_width - 1; ; p--)
945             {
946               assert (p >= output);
947               if (*p == '9')
948                 *p = '0';
949               else if (*p >= '0' && *p <= '8')
950                 {
951                   (*p)++;
952                   break;
953                 }
954               else
955                 assert (*p == '.');
956             }
957         }
958       else
959         {
960           /* Rounding up leading 9s causes the result to be a 1
961              followed by a number of 0s, plus a decimal point. */
962           char *p = output;
963           *p++ = '1';
964           p = mempset (p, '0', r->integer_digits);
965           if (decimals > 0)
966             {
967               *p++ = '.';
968               p = mempset (p, '0', decimals);
969             }
970           assert (p == output + base_width + 1);
971         }
972     }
973   else
974     {
975       /* Rounding down. */
976       if (r->integer_digits != 0 || decimals != 0)
977         {
978           /* Common case: just copy the digits. */
979           memcpy (output, r->string, base_width);
980         }
981       else
982         {
983           /* No digits remain.  The output is just a zero. */
984           output[0] = '0';
985         }
986     }
987 }
988 \f
989 /* Helper functions. */
990
991 /* Returns 10**X. */
992 static double PURE_FUNCTION
993 power10 (int x)
994 {
995   static const double p[] =
996     {
997       1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
998       1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
999       1e20, 1e21, 1e22, 1e23, 1e24, 1e25, 1e26, 1e27, 1e28, 1e29,
1000       1e30, 1e31, 1e32, 1e33, 1e34, 1e35, 1e36, 1e37, 1e38, 1e39,
1001       1e40,
1002     };
1003   return x >= 0 && x < sizeof p / sizeof *p ? p[x] : pow (10.0, x);
1004 }
1005
1006 /* Returns 256**X. */
1007 static double PURE_FUNCTION
1008 power256 (int x)
1009 {
1010   static const double p[] =
1011     {
1012       1.0,
1013       256.0,
1014       65536.0,
1015       16777216.0,
1016       4294967296.0,
1017       1099511627776.0,
1018       281474976710656.0,
1019       72057594037927936.0,
1020       18446744073709551616.0
1021     };
1022   return x >= 0 && x < sizeof p / sizeof *p ? p[x] : pow (256.0, x);
1023 }
1024
1025 /* Formats non-finite NUMBER into OUTPUT according to the width
1026    given in FORMAT. */
1027 static void
1028 output_infinite (double number, const struct fmt_spec *format, char *output)
1029 {
1030   assert (!isfinite (number));
1031
1032   if (format->w >= 3)
1033     {
1034       const char *s;
1035
1036       if (isnan (number))
1037         s = "NaN";
1038       else if (isinf (number))
1039         s = number > 0 ? "+Infinity" : "-Infinity";
1040       else
1041         s = "Unknown";
1042
1043       buf_copy_str_lpad (output, format->w, s, ' ');
1044     }
1045   else
1046     output_overflow (format, output);
1047
1048   output[format->w] = '\0';
1049 }
1050
1051 /* Formats OUTPUT as a missing value for the given FORMAT. */
1052 static void
1053 output_missing (const struct fmt_spec *format, char *output)
1054 {
1055   memset (output, ' ', format->w);
1056
1057   if (format->type != FMT_N)
1058     {
1059       int dot_ofs = (format->type == FMT_PCT ? 2
1060                      : format->type == FMT_E ? 5
1061                      : 1);
1062       output[MAX (0, format->w - format->d - dot_ofs)] = '.';
1063     }
1064   else
1065     output[format->w - 1] = '.';
1066
1067   output[format->w] = '\0';
1068 }
1069
1070 /* Formats OUTPUT for overflow given FORMAT. */
1071 static void
1072 output_overflow (const struct fmt_spec *format, char *output)
1073 {
1074   memset (output, '*', format->w);
1075   output[format->w] = '\0';
1076 }
1077
1078 /* Converts the integer part of NUMBER to a packed BCD number
1079    with the given number of DIGITS in OUTPUT.  If DIGITS is odd,
1080    the least significant nibble of the final byte in OUTPUT is
1081    set to 0.  Returns true if successful, false if NUMBER is not
1082    representable.  On failure, OUTPUT is cleared to all zero
1083    bytes. */
1084 static bool
1085 output_bcd_integer (double number, int digits, char *output)
1086 {
1087   char decimal[64];
1088
1089   assert (digits < sizeof decimal);
1090
1091   output[DIV_RND_UP (digits, 2)] = '\0';
1092   if (number != SYSMIS
1093       && number >= 0.
1094       && number < power10 (digits)
1095       && sprintf (decimal, "%0*.0f", digits, round (number)) == digits)
1096     {
1097       const char *src = decimal;
1098       int i;
1099
1100       for (i = 0; i < digits / 2; i++)
1101         {
1102           int d0 = *src++ - '0';
1103           int d1 = *src++ - '0';
1104           *output++ = (d0 << 4) + d1;
1105         }
1106       if (digits % 2)
1107         *output = (*src - '0') << 4;
1108
1109       return true;
1110     }
1111   else
1112     {
1113       memset (output, 0, DIV_RND_UP (digits, 2));
1114       return false;
1115     }
1116 }
1117
1118 /* Writes VALUE to OUTPUT as a BYTES-byte binary integer of the
1119    given INTEGER_FORMAT. */
1120 static void
1121 output_binary_integer (uint64_t value, int bytes,
1122                        enum integer_format integer_format, char *output)
1123 {
1124   integer_put (value, integer_format, output, bytes);
1125 }
1126
1127 /* Converts the BYTES bytes in DATA to twice as many hexadecimal
1128    digits in OUTPUT. */
1129 static void
1130 output_hex (const void *data_, size_t bytes, char *output)
1131 {
1132   const uint8_t *data = data_;
1133   size_t i;
1134
1135   for (i = 0; i < bytes; i++)
1136     {
1137       static const char hex_digits[] = "0123456789ABCDEF";
1138       *output++ = hex_digits[data[i] >> 4];
1139       *output++ = hex_digits[data[i] & 15];
1140     }
1141   *output = '\0';
1142 }