瀏覽代碼

json_emit_raw_str -> json_emit_unquoted_str, json_emit_int -> json_emit_long

Sergey Lyubka 11 年之前
父節點
當前提交
2e135bf267
共有 4 個文件被更改,包括 34 次插入32 次删除
  1. 2 2
      README.md
  2. 21 19
      frozen.c
  3. 2 2
      frozen.h
  4. 9 9
      unit_test.c

+ 2 - 2
README.md

@@ -117,10 +117,10 @@ that points to number `"1"`.
 Return: pointer to the found token, or NULL on failure.
 
 
-    int json_emit_int(char *buf, int buf_len, long int value);
+    int json_emit_long(char *buf, int buf_len, long value);
     int json_emit_double(char *buf, int buf_len, double value);
     int json_emit_quoted_str(char *buf, int buf_len, const char *str);
-    int json_emit_raw_str(char *buf, int buf_len, const char *str);
+    int json_emit_unquoted_str(char *buf, int buf_len, const char *str);
 
 These functions are used to generate JSON string. All of them accept
 a destination buffer and a value to output, and return number of bytes printed.

+ 21 - 19
frozen.c

@@ -358,23 +358,22 @@ const struct json_token *find_json_token(const struct json_token *toks,
   return 0;
 }
 
-int json_emit_int(char *buf, int buf_len, long int value) {
-  return buf_len <= 0 ? 0 : snprintf(buf, buf_len, "%ld", value);
+int json_emit_long(char *buf, int buf_len, long int value) {
+  return snprintf(buf, buf_len > 0 ? buf_len : 0, "%ld", value);
 }
 
 int json_emit_double(char *buf, int buf_len, double value) {
-  return buf_len <= 0 ? 0 : snprintf(buf, buf_len, "%g", value);
+  return snprintf(buf, buf_len > 0 ? buf_len : 0, "%g", value);
 }
 
-int json_emit_quoted_str(char *buf, int buf_len, const char *str) {
-  int i = 0, j = 0, ch;
+int json_emit_quoted_str(char *s, int s_len, const char *str) {
+  const char *begin = s, *end = s + s_len;
+  char ch;
 
-  if (buf_len <= 1) return 0;
-
-#define EMIT(x) do { if (j < buf_len) buf[j++] = x; } while (0)
+#define EMIT(x) do { if (s < end) *s = x; s++; } while (0)
 
   EMIT('"');
-  while ((ch = str[i++]) != '\0' && j < buf_len) {
+  while ((ch = *str++) != '\0') {
     switch (ch) {
       case '"':  EMIT('\\'); EMIT('"'); break;
       case '\\': EMIT('\\'); EMIT('\\'); break;
@@ -387,13 +386,13 @@ int json_emit_quoted_str(char *buf, int buf_len, const char *str) {
     }
   }
   EMIT('"');
-  EMIT(0);
+  if (s < end) *s = '\0';
 
-  return j == 0 ? 0 : j - 1;
+  return s - begin;
 }
 
-int json_emit_raw_str(char *buf, int buf_len, const char *str) {
-  return buf_len <= 0 ? 0 : snprintf(buf, buf_len, "%s", str);
+int json_emit_unquoted_str(char *buf, int buf_len, const char *str) {
+  return snprintf(buf, buf_len > 0 ? buf_len : 0, "%s", str);
 }
 
 int json_emit(char *s, int s_len, const char *fmt, ...) {
@@ -405,10 +404,11 @@ int json_emit(char *s, int s_len, const char *fmt, ...) {
     switch (*fmt) {
       case '[': case ']': case '{': case '}': case ',': case ':':
       case ' ': case '\r': case '\n': case '\t':
-        if (s < end) *s++ = *fmt;
+        if (s < end) *s = *fmt;
+        s++;
         break;
       case 'i':
-        s += json_emit_int(s, end - s, va_arg(ap, long));
+        s += json_emit_long(s, end - s, va_arg(ap, long));
         break;
       case 'f':
         s += json_emit_double(s, end - s, va_arg(ap, double));
@@ -417,16 +417,16 @@ int json_emit(char *s, int s_len, const char *fmt, ...) {
         s += json_emit_quoted_str(s, end - s, va_arg(ap, char *));
         break;
       case 'S':
-        s += json_emit_raw_str(s, end - s, va_arg(ap, char *));
+        s += json_emit_unquoted_str(s, end - s, va_arg(ap, char *));
         break;
       case 'T':
-        s += json_emit_raw_str(s, end - s, "true");
+        s += json_emit_unquoted_str(s, end - s, "true");
         break;
       case 'F':
-        s += json_emit_raw_str(s, end - s, "false");
+        s += json_emit_unquoted_str(s, end - s, "false");
         break;
       case 'N':
-        s += json_emit_raw_str(s, end - s, "null");
+        s += json_emit_unquoted_str(s, end - s, "null");
         break;
       default:
         return 0;
@@ -435,5 +435,7 @@ int json_emit(char *s, int s_len, const char *fmt, ...) {
   }
   va_end(ap);
 
+  if (s < end) *s = '\0';
+
   return end - s;
 }

+ 2 - 2
frozen.h

@@ -53,10 +53,10 @@ struct json_token *parse_json2(const char *json_string, int string_length);
 const struct json_token *find_json_token(const struct json_token *toks,
                                          const char *path);
 
-int json_emit_int(char *buf, int buf_len, long int value);
+int json_emit_long(char *buf, int buf_len, long value);
 int json_emit_double(char *buf, int buf_len, double value);
 int json_emit_quoted_str(char *buf, int buf_len, const char *str);
-int json_emit_raw_str(char *buf, int buf_len, const char *str);
+int json_emit_unquoted_str(char *buf, int buf_len, const char *str);
 int json_emit(char *buf, int buf_len, const char *fmt, ...);
 
 #ifdef __cplusplus

+ 9 - 9
unit_test.c

@@ -184,8 +184,8 @@ static const char *test_emit_overflow(void) {
   char buf[1000];
 
   memset(buf, 0, sizeof(buf));
-  ASSERT(json_emit_raw_str(buf, 0, "hi") == 0);
-  ASSERT(json_emit_quoted_str(buf, 0, "hi") == 0);
+  ASSERT(json_emit_unquoted_str(buf, 0, "hi") == 2);
+  ASSERT(json_emit_quoted_str(buf, 0, "hi") == 4);
   ASSERT(buf[0] == '\0');
 
   return NULL;
@@ -203,15 +203,15 @@ static const char *test_emit(void) {
   char buf[1000], *p = buf;
   const char *s5 = "{\"foo\":[-123,1.23,true]}";
 
-  p += json_emit_raw_str(p, &buf[sizeof(buf)] - p, "{");
+  p += json_emit_unquoted_str(p, &buf[sizeof(buf)] - p, "{");
   p += json_emit_quoted_str(p, &buf[sizeof(buf)] - p, "foo");
-  p += json_emit_raw_str(p, &buf[sizeof(buf)] - p, ":[");
-  p += json_emit_int(p, &buf[sizeof(buf)] - p, -123);
-  p += json_emit_raw_str(p, &buf[sizeof(buf)] - p, ",");
+  p += json_emit_unquoted_str(p, &buf[sizeof(buf)] - p, ":[");
+  p += json_emit_long(p, &buf[sizeof(buf)] - p, -123);
+  p += json_emit_unquoted_str(p, &buf[sizeof(buf)] - p, ",");
   p += json_emit_double(p, &buf[sizeof(buf)] - p, 1.23);
-  p += json_emit_raw_str(p, &buf[sizeof(buf)] - p, ",");
-  p += json_emit_raw_str(p, &buf[sizeof(buf)] - p, "true");
-  p += json_emit_raw_str(p, &buf[sizeof(buf)] - p, "]}");
+  p += json_emit_unquoted_str(p, &buf[sizeof(buf)] - p, ",");
+  p += json_emit_unquoted_str(p, &buf[sizeof(buf)] - p, "true");
+  p += json_emit_unquoted_str(p, &buf[sizeof(buf)] - p, "]}");
   ASSERT(strcmp(buf, s5) == 0);
   ASSERT(p < &buf[sizeof(buf)]);