Explorar o código

Added vV format specs for strings with length

Sergey Lyubka %!s(int64=11) %!d(string=hai) anos
pai
achega
d00145a685
Modificáronse 3 ficheiros con 18 adicións e 10 borrados
  1. 5 4
      README.md
  2. 10 2
      frozen.c
  3. 3 4
      unit_test.c

+ 5 - 4
README.md

@@ -140,10 +140,12 @@ Characters allowed in `format` string:
 are appended to the output buffer as-is  
 `i`: argument must be an `long` value, outputs number  
 `f`: argument must be a `double` value, outputs number  
-`s`: arguments must be a `char *` value, followed by `size_t` value,
+`v`: arguments must be a `char *` value, followed by `size_t` value,
      outputs quoted string  
-`S`: arguments must be a `char *` value, followed by `size_t` value,
+`V`: arguments must be a `char *` value, followed by `size_t` value,
      outputs unquoted string  
+`s`: arguments must be a `\0`-terminated `char *` value, outputs quoted string  
+`S`: arguments must be a `\0`-terminated `char *` value, outputs unquoted string  
 `N`: outputs `null`  
 `T`: outputs `true`  
 `F`: outputs `false`  
@@ -176,8 +178,7 @@ are appended to the output buffer as-is
 ## Example: generating JSON string `{ "foo": [-123, true, false, null] }`
 
     char buf[1000];
-    json_emit(buf, sizeof(buf), "{ s: [i, T, F, N] }",
-              "foo", (size_t) 3, (long) -123);
+    json_emit(buf, sizeof(buf), "{ s: [i, T, F, N] }", "foo", (long) -123);
 
 # License
 

+ 10 - 2
frozen.c

@@ -418,16 +418,24 @@ int json_emit(char *s, int s_len, const char *fmt, ...) {
       case 'f':
         s += json_emit_double(s, end - s, va_arg(ap, double));
         break;
-      case 's':
+      case 'v':
         str = va_arg(ap, char *);
         len = va_arg(ap, size_t);
         s += json_emit_quoted_str(s, end - s, str, len);
         break;
-      case 'S':
+      case 'V':
         str = va_arg(ap, char *);
         len = va_arg(ap, size_t);
         s += json_emit_unquoted_str(s, end - s, str, len);
         break;
+      case 's':
+        str = va_arg(ap, char *);
+        s += json_emit_quoted_str(s, end - s, str, strlen(str));
+        break;
+      case 'S':
+        str = va_arg(ap, char *);
+        s += json_emit_unquoted_str(s, end - s, str, strlen(str));
+        break;
       case 'T':
         s += json_emit_unquoted_str(s, end - s, "true", 4);
         break;

+ 3 - 4
unit_test.c

@@ -215,13 +215,12 @@ static const char *test_emit(void) {
   ASSERT(strcmp(buf, s5) == 0);
   ASSERT(p < &buf[sizeof(buf)]);
 
-  ASSERT(json_emit(buf, sizeof(buf), "{s:[i,f,S]}",
-         "foo", (long) -123, 1.23, "true") > 0);
+  ASSERT(json_emit(buf, sizeof(buf), "{v:[i,f,V]}",
+         "foo", 3, (long) -123, 1.23, "true", 4) > 0);
   ASSERT(json_emit(buf, 4, "{S:i}", "a", 12345) < 0);
   ASSERT(json_emit(buf, sizeof(buf), "{S:d}", "a", 12345) == 0);
 
-  ASSERT(json_emit(buf, sizeof(buf), "{s:[i,T, F,N]}", "foo",
-         (long) 3, (long) -7) > 0);
+  ASSERT(json_emit(buf, sizeof(buf), "{s:[i,T, F,N]}", "foo", (long) -7) > 0);
   printf("[%s]\n", buf);
   ASSERT(strcmp(buf, "{\"foo\":[-7,true, false,null]}") == 0);