parse_hex4: deduplicate into a for loop

This commit is contained in:
Max Bruckner 2017-02-06 23:01:18 +01:00
parent 0747669972
commit d7b5545748

100
cJSON.c
View File

@ -362,84 +362,34 @@ static unsigned char *print_number(const cJSON *item, printbuffer *p)
static unsigned parse_hex4(const unsigned char *str) static unsigned parse_hex4(const unsigned char *str)
{ {
unsigned int h = 0; unsigned int h = 0;
size_t i = 0;
/* first digit */ for (i = 0; i < 4; i++)
if ((*str >= '0') && (*str <= '9'))
{ {
h += (unsigned int) (*str) - '0'; /* parse digit */
} if ((*str >= '0') && (*str <= '9'))
else if ((*str >= 'A') && (*str <= 'F')) {
{ h += (unsigned int) (*str) - '0';
h += (unsigned int) 10 + (*str) - 'A'; }
} else if ((*str >= 'A') && (*str <= 'F'))
else if ((*str >= 'a') && (*str <= 'f')) {
{ h += (unsigned int) 10 + (*str) - 'A';
h += (unsigned int) 10 + (*str) - 'a'; }
} else if ((*str >= 'a') && (*str <= 'f'))
else /* invalid */ {
{ h += (unsigned int) 10 + (*str) - 'a';
return 0; }
} else /* invalid */
{
return 0;
}
if (i < 3)
/* second digit */ {
h = h << 4; /* shift left to make place for the next nibble */
str++; h = h << 4;
if ((*str >= '0') && (*str <= '9')) str++;
{ }
h += (unsigned int) (*str) - '0';
}
else if ((*str >= 'A') && (*str <= 'F'))
{
h += (unsigned int) 10 + (*str) - 'A';
}
else if ((*str >= 'a') && (*str <= 'f'))
{
h += (unsigned int) 10 + (*str) - 'a';
}
else /* invalid */
{
return 0;
}
/* third digit */
h = h << 4;
str++;
if ((*str >= '0') && (*str <= '9'))
{
h += (unsigned int) (*str) - '0';
}
else if ((*str >= 'A') && (*str <= 'F'))
{
h += (unsigned int) 10 + (*str) - 'A';
}
else if ((*str >= 'a') && (*str <= 'f'))
{
h += (unsigned int) 10 + (*str) - 'a';
}
else /* invalid */
{
return 0;
}
/* fourth digit */
h = h << 4;
str++;
if ((*str >= '0') && (*str <= '9'))
{
h += (unsigned int) (*str) - '0';
}
else if ((*str >= 'A') && (*str <= 'F'))
{
h += (unsigned int) 10 + (*str) - 'A';
}
else if ((*str >= 'a') && (*str <= 'f'))
{
h += (unsigned int) 10 + (*str) - 'a';
}
else /* invalid */
{
return 0;
} }
return h; return h;