print_number: return boolean instead of pointer

This commit is contained in:
Max Bruckner 2017-03-03 00:26:58 +01:00
parent 748f4bfd4f
commit 1749de02f8
2 changed files with 11 additions and 13 deletions

17
cJSON.c
View File

@ -327,14 +327,14 @@ static void update_offset(printbuffer * const buffer)
} }
/* Removes trailing zeroes from the end of a printed number */ /* Removes trailing zeroes from the end of a printed number */
static unsigned char *trim_trailing_zeroes(printbuffer * const buffer) static cJSON_bool trim_trailing_zeroes(printbuffer * const buffer)
{ {
size_t offset = 0; size_t offset = 0;
unsigned char *content = NULL; unsigned char *content = NULL;
if ((buffer == NULL) || (buffer->buffer == NULL) || (buffer->offset < 1)) if ((buffer == NULL) || (buffer->buffer == NULL) || (buffer->offset < 1))
{ {
return NULL; return false;
} }
offset = buffer->offset - 1; offset = buffer->offset - 1;
@ -354,11 +354,11 @@ static unsigned char *trim_trailing_zeroes(printbuffer * const buffer)
buffer->offset = offset; buffer->offset = offset;
return content + offset; return true;
} }
/* Render the number nicely from the given item into a string. */ /* Render the number nicely from the given item into a string. */
static unsigned char *print_number(const cJSON * const item, printbuffer * const output_buffer, const internal_hooks * const hooks) static cJSON_bool print_number(const cJSON * const item, printbuffer * const output_buffer, const internal_hooks * const hooks)
{ {
unsigned char *output_pointer = NULL; unsigned char *output_pointer = NULL;
double d = item->valuedouble; double d = item->valuedouble;
@ -367,7 +367,7 @@ static unsigned char *print_number(const cJSON * const item, printbuffer * const
if (output_buffer == NULL) if (output_buffer == NULL)
{ {
return NULL; return false;
} }
/* This is a nice tradeoff. */ /* This is a nice tradeoff. */
@ -399,7 +399,7 @@ static unsigned char *print_number(const cJSON * const item, printbuffer * const
/* sprintf failed */ /* sprintf failed */
if (length < 0) if (length < 0)
{ {
return NULL; return false;
} }
output_buffer->offset += (size_t)length; output_buffer->offset += (size_t)length;
@ -409,7 +409,7 @@ static unsigned char *print_number(const cJSON * const item, printbuffer * const
return trim_trailing_zeroes(output_buffer); return trim_trailing_zeroes(output_buffer);
} }
return output_buffer->buffer; return true;
} }
/* parse 4 digit hexadecimal number */ /* parse 4 digit hexadecimal number */
@ -1069,8 +1069,7 @@ static cJSON_bool print_value(const cJSON * const item, const size_t depth, cons
} }
break; break;
case cJSON_Number: case cJSON_Number:
output = print_number(item, output_buffer, hooks); return print_number(item, output_buffer, hooks);
break;
case cJSON_Raw: case cJSON_Raw:
{ {
size_t raw_length = 0; size_t raw_length = 0;

View File

@ -37,7 +37,7 @@ static void assert_print_number(const char *expected, double input)
memset(item, 0, sizeof(item)); memset(item, 0, sizeof(item));
cJSON_SetNumberValue(item, input); cJSON_SetNumberValue(item, input);
TEST_ASSERT_NOT_NULL_MESSAGE(print_number(item, &buffer, &global_hooks), "Failed to print number."); TEST_ASSERT_TRUE_MESSAGE(print_number(item, &buffer, &global_hooks), "Failed to print number.");
TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, buffer.buffer, "Printed number is not as expected."); TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, buffer.buffer, "Printed number is not as expected.");
} }
@ -91,14 +91,13 @@ static void trim_trailing_zeroes_should_trim_trailing_zeroes(void)
{ {
printbuffer buffer; printbuffer buffer;
unsigned char number[100]; unsigned char number[100];
unsigned char *pointer = NULL;
buffer.length = sizeof(number); buffer.length = sizeof(number);
buffer.buffer = number; buffer.buffer = number;
strcpy((char*)number, "10.00"); strcpy((char*)number, "10.00");
buffer.offset = sizeof("10.00") - 1; buffer.offset = sizeof("10.00") - 1;
pointer = trim_trailing_zeroes(&buffer); TEST_ASSERT_TRUE(trim_trailing_zeroes(&buffer));
TEST_ASSERT_EQUAL_UINT8('\0', *pointer); TEST_ASSERT_EQUAL_UINT8('\0', buffer.buffer[buffer.offset]);
TEST_ASSERT_EQUAL_STRING("10", number); TEST_ASSERT_EQUAL_STRING("10", number);
TEST_ASSERT_EQUAL_UINT(sizeof("10") - 1, buffer.offset); TEST_ASSERT_EQUAL_UINT(sizeof("10") - 1, buffer.offset);
} }