瀏覽代碼

print_string: return boolean instead of pointer

Max Bruckner 8 年之前
父節點
當前提交
5ea4fad263
共有 2 個文件被更改,包括 11 次插入11 次删除
  1. 10 10
      cJSON.c
  2. 1 1
      tests/print_string.c

+ 10 - 10
cJSON.c

@@ -694,7 +694,7 @@ fail:
 }
 
 /* Render the cstring provided to an escaped version that can be printed. */
-static unsigned char *print_string_ptr(const unsigned char * const input, printbuffer * const output_buffer, const internal_hooks * const hooks)
+static cJSON_bool print_string_ptr(const unsigned char * const input, printbuffer * const output_buffer, const internal_hooks * const hooks)
 {
     const unsigned char *input_pointer = NULL;
     unsigned char *output = NULL;
@@ -705,7 +705,7 @@ static unsigned char *print_string_ptr(const unsigned char * const input, printb
 
     if (output_buffer == NULL)
     {
-        return NULL;
+        return false;
     }
 
     /* empty string */
@@ -714,11 +714,11 @@ static unsigned char *print_string_ptr(const unsigned char * const input, printb
         output = ensure(output_buffer, sizeof("\"\""), hooks);
         if (output == NULL)
         {
-            return NULL;
+            return false;
         }
         strcpy((char*)output, "\"\"");
 
-        return output;
+        return true;
     }
 
     /* set "flag" to 1 if something needs to be escaped */
@@ -740,7 +740,7 @@ static unsigned char *print_string_ptr(const unsigned char * const input, printb
     output = ensure(output_buffer, output_length + sizeof("\"\""), hooks);
     if (output == NULL)
     {
-        return NULL;
+        return false;
     }
 
     /* no characters have to be escaped */
@@ -751,7 +751,7 @@ static unsigned char *print_string_ptr(const unsigned char * const input, printb
         output[output_length + 1] = '\"';
         output[output_length + 2] = '\0';
 
-        return output;
+        return true;
     }
 
     output[0] = '\"';
@@ -802,11 +802,11 @@ static unsigned char *print_string_ptr(const unsigned char * const input, printb
     output[output_length + 1] = '\"';
     output[output_length + 2] = '\0';
 
-    return output;
+    return true;
 }
 
 /* Invoke print_string_ptr (which is useful) on an item. */
-static unsigned char *print_string(const cJSON * const item, printbuffer * const p, const internal_hooks * const hooks)
+static cJSON_bool print_string(const cJSON * const item, printbuffer * const p, const internal_hooks * const hooks)
 {
     return print_string_ptr((unsigned char*)item->valuestring, p, hooks);
 }
@@ -1092,7 +1092,7 @@ static cJSON_bool print_value(const cJSON * const item, const size_t depth, cons
             break;
         }
         case cJSON_String:
-            output = print_string(item, output_buffer, hooks);
+            return print_string(item, output_buffer, hooks);
             break;
         case cJSON_Array:
             return print_array(item, depth, format, output_buffer, hooks);
@@ -1384,7 +1384,7 @@ static cJSON_bool print_object(const cJSON * const item, const size_t depth, con
         }
 
         /* print key */
-        if (print_string_ptr((unsigned char*)current_item->string, output_buffer, hooks) == NULL)
+        if (!print_string_ptr((unsigned char*)current_item->string, output_buffer, hooks))
         {
             return false;
         }

+ 1 - 1
tests/print_string.c

@@ -33,7 +33,7 @@ static void assert_print_string(const char *expected, const char *input)
     buffer.offset = 0;
     buffer.noalloc = true;
 
-    TEST_ASSERT_NOT_NULL_MESSAGE(print_string_ptr((const unsigned char*)input, &buffer, &global_hooks), "Failed to print string.");
+    TEST_ASSERT_TRUE_MESSAGE(print_string_ptr((const unsigned char*)input, &buffer, &global_hooks), "Failed to print string.");
     TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, printed, "The printed string isn't as expected.");
 }