mirror of
https://github.com/DaveGamble/cJSON.git
synced 2023-08-10 21:13:26 +03:00
parse_hex4: deduplicate into a for loop
This commit is contained in:
parent
0747669972
commit
d7b5545748
64
cJSON.c
64
cJSON.c
@ -362,8 +362,11 @@ static unsigned char *print_number(const cJSON *item, printbuffer *p)
|
||||
static unsigned parse_hex4(const unsigned char *str)
|
||||
{
|
||||
unsigned int h = 0;
|
||||
size_t i = 0;
|
||||
|
||||
/* first digit */
|
||||
for (i = 0; i < 4; i++)
|
||||
{
|
||||
/* parse digit */
|
||||
if ((*str >= '0') && (*str <= '9'))
|
||||
{
|
||||
h += (unsigned int) (*str) - '0';
|
||||
@ -381,65 +384,12 @@ static unsigned parse_hex4(const unsigned char *str)
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/* second digit */
|
||||
if (i < 3)
|
||||
{
|
||||
/* shift left to make place for the next nibble */
|
||||
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;
|
||||
}
|
||||
|
||||
/* 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;
|
||||
|
Loading…
Reference in New Issue
Block a user