c11 safe string handling support
[vpp.git] / src / vppinfra / format.c
index 78e52e9..886f03d 100644 (file)
@@ -62,6 +62,7 @@
 #include <vppinfra/error.h>
 #include <vppinfra/string.h>
 #include <vppinfra/os.h>       /* os_puts */
+#include <vppinfra/math.h>
 
 typedef struct
 {
@@ -138,25 +139,25 @@ justify (u8 * s, format_info_t * fi, uword s_len_orig)
       if (n_left > 0)
        {
          vec_insert (s, n_left, i0);
-         memset (s + i0, fi->pad_char, n_left);
+         clib_memset (s + i0, fi->pad_char, n_left);
          l1 = vec_len (s);
        }
       if (n_right > 0)
        {
          vec_resize (s, n_right);
-         memset (s + l1, fi->pad_char, n_right);
+         clib_memset (s + l1, fi->pad_char, n_right);
        }
     }
   return s;
 }
 
-static u8 *
-do_percent (u8 ** _s, u8 * fmt, va_list * va)
+static const u8 *
+do_percent (u8 ** _s, const u8 * fmt, va_list * va)
 {
   u8 *s = *_s;
   uword c;
 
-  u8 *f = fmt;
+  const u8 *f = fmt;
 
   format_info_t fi = {
     .justify = '+',
@@ -385,7 +386,7 @@ done:
 u8 *
 va_format (u8 * s, const char *fmt, va_list * va)
 {
-  u8 *f = (u8 *) fmt, *g;
+  const u8 *f = (u8 *) fmt, *g;
   u8 c;
 
   g = f;
@@ -462,6 +463,17 @@ fformat (FILE * f, char *fmt, ...)
 }
 
 #ifdef CLIB_UNIX
+void
+fformat_append_cr (FILE * ofp, const char *fmt, ...)
+{
+  va_list va;
+
+  va_start (va, fmt);
+  (void) va_fformat (ofp, (char *) fmt, &va);
+  va_end (va);
+  fformat (ofp, "\n");
+}
+
 word
 fdformat (int fd, char *fmt, ...)
 {
@@ -708,8 +720,12 @@ format_float (u8 * s, f64 x, uword n_fraction_digits, uword output_style)
       sign = 1;
     }
 
+  /* Check for not-a-number. */
+  if (isnan (x))
+    return format (s, "%cNaN", sign ? '-' : '+');
+
   /* Check for infinity. */
-  if (x == x / 2)
+  if (isinf (x))
     return format (s, "%cinfinity", sign ? '-' : '+');
 
   x = normalize (x, &expon, &prec);