2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
15 /*------------------------------------------------------------------
16 * format.c -- see notice below
18 * October 2003, Eliot Dresselhaus
20 * Modifications to this file Copyright (c) 2003 by cisco Systems, Inc.
21 * All rights reserved.
22 *------------------------------------------------------------------
26 Copyright (c) 2001, 2002, 2003, 2006 Eliot Dresselhaus
28 Permission is hereby granted, free of charge, to any person obtaining
29 a copy of this software and associated documentation files (the
30 "Software"), to deal in the Software without restriction, including
31 without limitation the rights to use, copy, modify, merge, publish,
32 distribute, sublicense, and/or sell copies of the Software, and to
33 permit persons to whom the Software is furnished to do so, subject to
34 the following conditions:
36 The above copyright notice and this permission notice shall be
37 included in all copies or substantial portions of the Software.
39 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
40 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
41 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
42 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
43 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
44 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
45 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
48 #include <stdarg.h> /* va_start, etc */
55 #ifdef CLIB_STANDALONE
56 #include <vppinfra/standalone_stdio.h>
59 #include <vppinfra/mem.h>
60 #include <vppinfra/format.h>
61 #include <vppinfra/vec.h>
62 #include <vppinfra/error.h>
63 #include <vppinfra/string.h>
64 #include <vppinfra/os.h> /* os_puts */
65 #include <vppinfra/math.h>
69 /* Output number in this base. */
72 /* Number of show of 64 bit number. */
75 /* Signed or unsigned. */
78 /* Output digits uppercase (not lowercase) %X versus %x. */
80 } format_integer_options_t;
82 static u8 *format_integer (u8 * s, u64 number,
83 format_integer_options_t * options);
84 static u8 *format_float (u8 * s, f64 x, uword n_digits_to_print,
89 /* String justification: + => right, - => left, = => center. */
92 /* Width of string (before and after decimal point for numbers).
93 0 => natural width. */
96 /* Long => 'l', long long 'L', int 0. */
99 /* Pad character. Defaults to space. */
104 justify (u8 * s, format_info_t * fi, uword s_len_orig)
109 l0 = i0 + fi->width[0];
112 /* If width is zero user returned width. */
121 uword n_left = 0, n_right = 0;
134 n_right = n_left = n / 2;
141 vec_insert (s, n_left, i0);
142 memset (s + i0, fi->pad_char, n_left);
147 vec_resize (s, n_right);
148 memset (s + l1, fi->pad_char, n_right);
155 do_percent (u8 ** _s, const u8 * fmt, va_list * va)
171 ASSERT (f[0] == '%');
189 /* Parse width0 . width1. */
191 uword is_first_digit = 1;
193 fi.width[0] = fi.width[1] = 0;
194 for (i = 0; i < 2; i++)
196 if (c == '0' && i == 0 && is_first_digit)
201 fi.width[i] = va_arg (*va, int);
206 while (c >= '0' && c <= '9')
208 fi.width[i] = 10 * fi.width[i] + (c - '0');
218 /* Parse %l* and %L* */
231 if (c == 'l' && *f == 'l')
239 /* Finally we are ready for format letter. */
242 uword s_initial_len = vec_len (s);
243 format_integer_options_t o = {
246 .n_bits = BITS (uword),
247 .uppercase_digits = 0,
256 /* Try to give a helpful error message. */
258 s = format (s, "**** CLIB unknown format `%%%c' ****", c);
263 vec_add1 (s, va_arg (*va, int));
271 o.n_bits = BITS (uword *);
273 o.uppercase_digits = 0;
275 s = format_integer (s, pointer_to_uword (va_arg (*va, void *)), &o);
286 if (c == 'x' || c == 'X')
288 o.is_signed = c == 'd';
289 o.uppercase_digits = c == 'X';
294 number = va_arg (*va, unsigned long long);
295 o.n_bits = BITS (unsigned long long);
299 number = va_arg (*va, long);
300 o.n_bits = BITS (long);
304 number = va_arg (*va, word);
305 o.n_bits = BITS (uword);
309 number = va_arg (*va, int);
310 o.n_bits = BITS (int);
314 s = format_integer (s, number, &o);
321 char *cstring = va_arg (*va, char *);
329 else if (fi.width[1] != 0)
330 len = clib_min (strlen (cstring), fi.width[1]);
332 len = strlen (cstring);
334 /* %S => format string as C identifier (replace _ with space). */
337 for (i = 0; i < len; i++)
338 vec_add1 (s, cstring[i] == '_' ? ' ' : cstring[i]);
341 vec_add (s, cstring, len);
347 u8 *v = va_arg (*va, u8 *);
350 if (fi.width[1] != 0)
351 len = clib_min (vec_len (v), fi.width[1]);
362 /* Floating point. */
363 ASSERT (fi.how_long == 0 || fi.how_long == 'l');
364 s = format_float (s, va_arg (*va, double), fi.width[1], c);
368 /* User defined function. */
370 typedef u8 *(user_func_t) (u8 * s, va_list * args);
371 user_func_t *u = va_arg (*va, user_func_t *);
378 s = justify (s, &fi, s_initial_len);
387 va_format (u8 * s, const char *fmt, va_list * va)
389 const u8 *f = (u8 *) fmt, *g;
403 vec_add (s, g, f - g);
404 f = g = do_percent (&s, f, va);
413 vec_add (s, g, f - g);
419 format (u8 * s, const char *fmt, ...)
423 s = va_format (s, fmt, &va);
429 va_fformat (FILE * f, char *fmt, va_list * va)
434 s = va_format (0, fmt, va);
439 ret = fwrite (s, vec_len (s), 1, f);
442 #endif /* CLIB_UNIX */
445 os_puts (s, vec_len (s), /* is_error */ 0);
453 fformat (FILE * f, char *fmt, ...)
459 ret = va_fformat (f, fmt, &va);
467 fformat_append_cr (FILE * ofp, const char *fmt, ...)
472 (void) va_fformat (ofp, (char *) fmt, &va);
478 fdformat (int fd, char *fmt, ...)
485 s = va_format (0, fmt, &va);
488 ret = write (fd, s, vec_len (s));
494 /* Format integral type. */
496 format_integer (u8 * s, u64 number, format_integer_options_t * options)
500 u8 digit_buffer[128];
501 u8 *d = digit_buffer + sizeof (digit_buffer);
504 if (options->is_signed && (i64) number < 0)
510 if (options->n_bits < BITS (number))
511 number &= ((u64) 1 << options->n_bits) - 1;
513 base = options->base;
520 if (r < 10 + 26 + 26)
524 else if (r < 10 + 26)
527 c = 'A' + (r - 10 - 26);
529 if (options->uppercase_digits
530 && base <= 10 + 26 && c >= 'a' && c <= 'z')
535 else /* will never happen, warning be gone */
546 vec_add (s, d, digit_buffer + sizeof (digit_buffer) - d);
550 /* Floating point formatting. */
551 /* Deconstruct IEEE 64 bit number into sign exponent and fraction. */
552 #define f64_down(f,sign,expon,fraction) \
554 union { u64 u; f64 f; } _f64_down_tmp; \
555 _f64_down_tmp.f = (f); \
556 (sign) = (_f64_down_tmp.u >> 63); \
557 (expon) = ((_f64_down_tmp.u >> 52) & 0x7ff) - 1023; \
558 (fraction) = ((_f64_down_tmp.u << 12) >> 12) | ((u64) 1 << 52); \
561 /* Construct IEEE 64 bit number. */
563 f64_up (uword sign, word expon, u64 fraction)
571 tmp.u = (u64) ((sign) != 0) << 63;
578 tmp.u |= (u64) expon << 52;
580 tmp.u |= fraction & (((u64) 1 << 52) - 1);
585 /* Returns approximate precision of number given its exponent. */
587 f64_precision (int base2_expon)
589 static int n_bits = 0;
593 /* Compute number of significant bits in floating point representation. */
605 return f64_up (0, base2_expon - n_bits, 0);
610 times_power_of_ten (f64 x, int n)
614 static f64 t[8] = { 1e+0, 1e+1, 1e+2, 1e+3, 1e+4, 1e+5, 1e+6, 1e+7, };
624 static f64 t[8] = { 1e-0, 1e-1, 1e-2, 1e-3, 1e-4, 1e-5, 1e-6, 1e-7, };
635 /* Write x = y * 10^expon with 1 < y < 10. */
637 normalize (f64 x, word * expon_return, f64 * prec_return)
639 word expon2, expon10;
640 CLIB_UNUSED (u64 fraction);
641 CLIB_UNUSED (word sign);
644 f64_down (x, sign, expon2, fraction);
648 expon2 * .301029995663981195213738894724493 /* Log (2) / Log (10) */ ;
650 prec = f64_precision (expon2);
651 x = times_power_of_ten (x, -expon10);
652 prec = times_power_of_ten (prec, -expon10);
674 *expon_return = expon10;
681 add_some_zeros (u8 * s, uword n_zeros)
691 /* Format a floating point number with the given number of fractional
692 digits (e.g. 1.2345 with 2 fraction digits yields "1.23") and output style. */
694 format_float (u8 * s, f64 x, uword n_fraction_digits, uword output_style)
697 word sign, expon, n_fraction_done, added_decimal_point;
698 /* Position of decimal point relative to where we are. */
701 /* Default number of digits to print when its not specified. */
702 if (n_fraction_digits == ~0)
703 n_fraction_digits = 7;
706 added_decimal_point = 0;
709 /* Special case: zero. */
723 /* Check for not-a-number. */
725 return format (s, "%cNaN", sign ? '-' : '+');
727 /* Check for infinity. */
729 return format (s, "%cinfinity", sign ? '-' : '+');
731 x = normalize (x, &expon, &prec);
733 /* Not enough digits to print anything: so just print 0 */
734 if ((word) - expon > (word) n_fraction_digits
735 && (output_style == 'f' || (output_style == 'g')))
741 if (output_style == 'f'
742 || (output_style == 'g' && expon > -10 && expon < 10))
746 /* Add decimal point and leading zeros. */
748 n_fraction_done = clib_min (-(expon + 1), n_fraction_digits);
749 s = add_some_zeros (s, n_fraction_done);
750 decimal_point = -n_fraction_done;
751 added_decimal_point = 1;
754 decimal_point = expon + 1;
758 /* Exponential output style. */
767 /* Number is smaller than precision: call it zero. */
779 /* Round last printed digit. */
780 if (decimal_point <= 0
781 && n_fraction_done + 1 == n_fraction_digits && digit < 9)
784 vec_add1 (s, '0' + digit);
786 /* Move rightwards towards/away from decimal point. */
789 n_fraction_done += decimal_point < 0;
790 if (decimal_point <= 0 && n_fraction_done >= n_fraction_digits)
793 if (decimal_point == 0 && x != 0)
796 added_decimal_point = 1;
804 if (decimal_point > 0)
806 s = add_some_zeros (s, decimal_point);
810 if (n_fraction_done < n_fraction_digits)
812 if (!added_decimal_point)
814 s = add_some_zeros (s, n_fraction_digits - n_fraction_done);
817 if (output_style == 'e')
818 s = format (s, "e%wd", expon);
825 * fd.io coding-style-patch-verification: ON
828 * eval: (c-set-style "gnu")