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 */
static unsigned char *trim_trailing_zeroes(printbuffer * const buffer)
static cJSON_bool trim_trailing_zeroes(printbuffer * const buffer)
{
size_t offset = 0;
unsigned char *content = NULL;
if ((buffer == NULL) || (buffer->buffer == NULL) || (buffer->offset < 1))
{
return NULL;
return false;
}
offset = buffer->offset - 1;
@ -354,11 +354,11 @@ static unsigned char *trim_trailing_zeroes(printbuffer * const buffer)
buffer->offset = offset;
return content + offset;
return true;
}
/* 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;
double d = item->valuedouble;
@ -367,7 +367,7 @@ static unsigned char *print_number(const cJSON * const item, printbuffer * const
if (output_buffer == NULL)
{
return NULL;
return false;
}
/* This is a nice tradeoff. */
@ -399,7 +399,7 @@ static unsigned char *print_number(const cJSON * const item, printbuffer * const
/* sprintf failed */
if (length < 0)
{
return NULL;
return false;
}
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 output_buffer->buffer;
return true;
}
/* parse 4 digit hexadecimal number */
@ -1069,8 +1069,7 @@ static cJSON_bool print_value(const cJSON * const item, const size_t depth, cons
}
break;
case cJSON_Number:
output = print_number(item, output_buffer, hooks);
break;
return print_number(item, output_buffer, hooks);
case cJSON_Raw:
{
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));
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.");
}
@ -91,14 +91,13 @@ static void trim_trailing_zeroes_should_trim_trailing_zeroes(void)
{
printbuffer buffer;
unsigned char number[100];
unsigned char *pointer = NULL;
buffer.length = sizeof(number);
buffer.buffer = number;
strcpy((char*)number, "10.00");
buffer.offset = sizeof("10.00") - 1;
pointer = trim_trailing_zeroes(&buffer);
TEST_ASSERT_EQUAL_UINT8('\0', *pointer);
TEST_ASSERT_TRUE(trim_trailing_zeroes(&buffer));
TEST_ASSERT_EQUAL_UINT8('\0', buffer.buffer[buffer.offset]);
TEST_ASSERT_EQUAL_STRING("10", number);
TEST_ASSERT_EQUAL_UINT(sizeof("10") - 1, buffer.offset);
}