Browse Source

Support %x in json_printf

PUBLISHED_FROM=27e71828015e381f94169caf9f9f01722c343f87
Sergey Lyubka 8 years ago
parent
commit
974ea7913d
2 changed files with 12 additions and 1 deletions
  1. 2 1
      frozen.c
  2. 10 0
      unit_test.c

+ 2 - 1
frozen.c

@@ -630,7 +630,7 @@ int json_vprintf(struct json_out *out, const char *fmt, va_list xap) {
          * TODO(dfrank): reimplement %s and %.*s in order to avoid that.
          * TODO(dfrank): reimplement %s and %.*s in order to avoid that.
          */
          */
 
 
-        const char *end_of_format_specifier = "sdfFgGlhuIc.*-0123456789";
+        const char *end_of_format_specifier = "sdfFgGlhuIcx.*-0123456789";
         size_t n = strspn(fmt + 1, end_of_format_specifier);
         size_t n = strspn(fmt + 1, end_of_format_specifier);
         char *pbuf = buf;
         char *pbuf = buf;
         size_t need_len;
         size_t need_len;
@@ -706,6 +706,7 @@ int json_vprintf(struct json_out *out, const char *fmt, va_list xap) {
       }
       }
       len += out->printer(out, quote, 1);
       len += out->printer(out, quote, 1);
     } else {
     } else {
+      len += out->printer(out, fmt, 1);
       fmt++;
       fmt++;
     }
     }
   }
   }

+ 10 - 0
unit_test.c

@@ -330,6 +330,16 @@ static const char *test_json_printf(void) {
     ASSERT(strcmp(buf, "S") == 0);
     ASSERT(strcmp(buf, "S") == 0);
   }
   }
 
 
+  {
+    struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
+    const char *result = "<\"array\">0f";
+    memset(buf, 0, sizeof(buf));
+    ASSERT(json_printf(&out, "<array>%02x", 15) > 0);
+    printf("[%s]\n", buf);
+    ASSERT(strcmp(buf, result) == 0);
+  }
+
+
   return NULL;
   return NULL;
 }
 }