Quellcode durchsuchen

Integrate https://github.com/cesanta/frozen/pull/38 - fix "%g" and "%e" in json_printf and json_printf_array

CL: Integrate https://github.com/cesanta/frozen/pull/38 - fix "%g" and "%e" in json_printf and json_printf_array

PUBLISHED_FROM=926cca4ea91daf3404d60d184b684534a0325352
Sergey Lyubka vor 7 Jahren
Ursprung
Commit
690b21931a
2 geänderte Dateien mit 22 neuen und 3 gelöschten Zeilen
  1. 2 2
      frozen.c
  2. 20 1
      unit_test.c

+ 2 - 2
frozen.c

@@ -641,7 +641,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.
          */
 
-        const char *end_of_format_specifier = "sdfFgGlhuIcx.*-0123456789";
+        const char *end_of_format_specifier = "sdfFeEgGlhuIcx.*-0123456789";
         int n = strspn(fmt + 1, end_of_format_specifier);
         char *pbuf = buf;
         int need_len, size = sizeof(buf);
@@ -769,7 +769,7 @@ int json_printf_array(struct json_out *out, va_list *ap) {
     memcpy(&val, arr + i * elem_size,
            elem_size > sizeof(val) ? sizeof(val) : elem_size);
     if (i > 0) len += json_printf(out, ", ");
-    if (strchr(fmt, 'f') != NULL) {
+    if (strpbrk(fmt, "efg") != NULL) {
       len += json_printf(out, fmt, val.d);
     } else {
       len += json_printf(out, fmt, val.i);

+ 20 - 1
unit_test.c

@@ -341,10 +341,29 @@ static const char *test_json_printf(void) {
     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);
   }
 
+  {
+    struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
+    double arr[] = {9.32156, 3.1415926};
+#if !defined(_MSC_VER) || _MSC_VER >= 1700
+    const char *result = "[9.32e+00, 3.14e+00]";  // Modern compilers
+#else
+    const char *result = "[9.32e+000, 3.14e+000]";  // Old VC98 compiler
+#endif
+    json_printf(&out, "%M", json_printf_array, arr, sizeof(arr), sizeof(arr[0]),
+                "%.2e");
+    ASSERT(strcmp(buf, result) == 0);
+  }
+  {
+    struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
+    double arr[] = {9.32156, 3.1415926};
+    json_printf(&out, "%M", json_printf_array, arr, sizeof(arr), sizeof(arr[0]),
+                "%.4g");
+    ASSERT(strcmp(buf, "[9.322, 3.142]") == 0);
+  }
+
   return NULL;
 }