mirror of
https://github.com/DaveGamble/cJSON.git
synced 2023-08-10 21:13:26 +03:00
reformatting: parse_number
This commit is contained in:
parent
46cfa68a2b
commit
88cbe57ba4
66
cJSON.c
66
cJSON.c
@ -132,22 +132,70 @@ void cJSON_Delete(cJSON *c)
|
|||||||
/* Parse the input text to generate a number, and populate the result into item. */
|
/* Parse the input text to generate a number, and populate the result into item. */
|
||||||
static const char *parse_number(cJSON *item, const char *num)
|
static const char *parse_number(cJSON *item, const char *num)
|
||||||
{
|
{
|
||||||
double n=0,sign=1,scale=0;int subscale=0,signsubscale=1;
|
double n = 0;
|
||||||
|
double sign = 1;
|
||||||
|
double scale = 0;
|
||||||
|
int subscale = 0;
|
||||||
|
int signsubscale = 1;
|
||||||
|
|
||||||
if (*num=='-') sign=-1,num++; /* Has sign? */
|
/* Has sign? */
|
||||||
if (*num=='0') num++; /* is zero */
|
if (*num == '-')
|
||||||
if (*num>='1' && *num<='9') do n=(n*10.0)+(*num++ -'0'); while (*num>='0' && *num<='9'); /* Number? */
|
{
|
||||||
if (*num=='.' && num[1]>='0' && num[1]<='9') {num++; do n=(n*10.0)+(*num++ -'0'),scale--; while (*num>='0' && *num<='9');} /* Fractional part? */
|
sign = -1;
|
||||||
if (*num=='e' || *num=='E') /* Exponent? */
|
num++;
|
||||||
{ num++;if (*num=='+') num++; else if (*num=='-') signsubscale=-1,num++; /* With sign? */
|
}
|
||||||
while (*num>='0' && *num<='9') subscale=(subscale*10)+(*num++ - '0'); /* Number? */
|
/* is zero */
|
||||||
|
if (*num == '0')
|
||||||
|
{
|
||||||
|
num++;
|
||||||
|
}
|
||||||
|
/* Number? */
|
||||||
|
if ((*num >= '1') && (*num <= '9'))
|
||||||
|
{
|
||||||
|
do
|
||||||
|
{
|
||||||
|
n = (n * 10.0) + (*num++ - '0');
|
||||||
|
}
|
||||||
|
while ((*num >= '0') && (*num<='9'));
|
||||||
|
}
|
||||||
|
/* Fractional part? */
|
||||||
|
if ((*num == '.') && (num[1] >= '0') && (num[1] <= '9'))
|
||||||
|
{
|
||||||
|
num++;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
n = (n *10.0) + (*num++ - '0');
|
||||||
|
scale--;
|
||||||
|
} while ((*num >= '0') && (*num <= '9'));
|
||||||
|
}
|
||||||
|
/* Exponent? */
|
||||||
|
if ((*num == 'e') || (*num == 'E'))
|
||||||
|
{
|
||||||
|
num++;
|
||||||
|
/* With sign? */
|
||||||
|
if (*num == '+')
|
||||||
|
{
|
||||||
|
num++;
|
||||||
|
}
|
||||||
|
else if (*num == '-')
|
||||||
|
{
|
||||||
|
signsubscale = -1;
|
||||||
|
num++;
|
||||||
|
}
|
||||||
|
/* Number? */
|
||||||
|
while ((*num>='0') && (*num<='9'))
|
||||||
|
{
|
||||||
|
subscale = (subscale * 10) + (*num++ - '0');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
n=sign*n*pow(10.0,(scale+subscale*signsubscale)); /* number = +/- number.fraction * 10^+/- exponent */
|
/* number = +/- number.fraction * 10^+/- exponent */
|
||||||
|
n = sign * n * pow(10.0, (scale + subscale * signsubscale));
|
||||||
|
|
||||||
item->valuedouble = n;
|
item->valuedouble = n;
|
||||||
item->valueint = (int)n;
|
item->valueint = (int)n;
|
||||||
item->type = cJSON_Number;
|
item->type = cJSON_Number;
|
||||||
|
|
||||||
return num;
|
return num;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user