1
0
mirror of https://github.com/DaveGamble/cJSON synced 2024-12-24 07:32:05 +00:00

print_string: return boolean instead of pointer

This commit is contained in:
Max Bruckner 2017-03-03 00:32:53 +01:00
parent 1749de02f8
commit 5ea4fad263
2 changed files with 11 additions and 11 deletions

20
cJSON.c
View File

@ -694,7 +694,7 @@ fail:
} }
/* Render the cstring provided to an escaped version that can be printed. */ /* 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; const unsigned char *input_pointer = NULL;
unsigned char *output = 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) if (output_buffer == NULL)
{ {
return NULL; return false;
} }
/* empty string */ /* empty string */
@ -714,11 +714,11 @@ static unsigned char *print_string_ptr(const unsigned char * const input, printb
output = ensure(output_buffer, sizeof("\"\""), hooks); output = ensure(output_buffer, sizeof("\"\""), hooks);
if (output == NULL) if (output == NULL)
{ {
return NULL; return false;
} }
strcpy((char*)output, "\"\""); strcpy((char*)output, "\"\"");
return output; return true;
} }
/* set "flag" to 1 if something needs to be escaped */ /* 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); output = ensure(output_buffer, output_length + sizeof("\"\""), hooks);
if (output == NULL) if (output == NULL)
{ {
return NULL; return false;
} }
/* no characters have to be escaped */ /* 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 + 1] = '\"';
output[output_length + 2] = '\0'; output[output_length + 2] = '\0';
return output; return true;
} }
output[0] = '\"'; 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 + 1] = '\"';
output[output_length + 2] = '\0'; output[output_length + 2] = '\0';
return output; return true;
} }
/* Invoke print_string_ptr (which is useful) on an item. */ /* 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); 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; break;
} }
case cJSON_String: case cJSON_String:
output = print_string(item, output_buffer, hooks); return print_string(item, output_buffer, hooks);
break; break;
case cJSON_Array: case cJSON_Array:
return print_array(item, depth, format, output_buffer, hooks); 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 */ /* 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; return false;
} }

View File

@ -33,7 +33,7 @@ static void assert_print_string(const char *expected, const char *input)
buffer.offset = 0; buffer.offset = 0;
buffer.noalloc = true; 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."); TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, printed, "The printed string isn't as expected.");
} }