Exemplo n.º 1
0
static int lept_parse_number(lept_context* c, lept_value* v) {
    const char* p = c->json;
    if (*p == '-') p++;
    if (*p == '0') p++;
    else {
        if (!ISDIGIT1TO9(*p)) return LEPT_PARSE_INVALID_VALUE;
        for (p++; ISDIGIT(*p); p++);
    }
    if (*p == '.') {
        p++;
        if (!ISDIGIT(*p)) return LEPT_PARSE_INVALID_VALUE;
        for (p++; ISDIGIT(*p); p++);
    }
    if (*p == 'e' || *p == 'E') {
        p++;
        if (*p == '+' || *p == '-') p++;
        if (!ISDIGIT(*p)) return LEPT_PARSE_INVALID_VALUE;
        for (p++; ISDIGIT(*p); p++);
    }
    errno = 0;
    v->u.n = strtod(c->json, NULL);
    if (errno == ERANGE && (v->u.n == HUGE_VAL || v->u.n == -HUGE_VAL))
        return LEPT_PARSE_NUMBER_TOO_BIG;
    v->type = LEPT_NUMBER;
    c->json = p;
    return LEPT_PARSE_OK;
}
Exemplo n.º 2
0
/* 
 *parse number
 *这里的双精度double 由几部分组成 负号,整数,小数,指数. 除了整数都不是必须的,
*/
static int rajson_parse_number(rajson_context* c, rajson_value* v)
{
	int ret;
	const char* tmpjson = c->json;
	if (*tmpjson == '-')  tmpjson++; 
	//整数部分
	if (*tmpjson == '0') tmpjson++;
	else{
		if (!ISDIGIT1TO9(*tmpjson)) return RAJSON_PARSE_INVALID_VALUE;
		for (tmpjson++; ISDIGIT(*tmpjson); tmpjson++);
	}
	//小数点后
	if (*tmpjson == '.')
	{
		tmpjson++;
		if (!ISDIGIT(*tmpjson)) return RAJSON_PARSE_INVALID_VALUE;
		for (tmpjson++; ISDIGIT(*tmpjson); tmpjson++);
	}
	if (*tmpjson == 'e' || *tmpjson == 'E')
	{
		tmpjson++;
		if (*tmpjson == '+' || *tmpjson == '-')
			tmpjson++;
		if (!ISDIGIT(*tmpjson)) return RAJSON_PARSE_INVALID_VALUE;
		for (tmpjson++; ISDIGIT(*tmpjson); tmpjson++);
	}
	errno = 0;
	v->n = strtod(c->json, NULL);
	if (errno == ERANGE && (v->n == HUGE_VAL || v->n == -HUGE_VAL))
		return RAJSON_PARSE_NUMBER_TOO_BIG;
	v->type = RAJSON_NUMBER;
	c->json = tmpjson;
	return RAJSON_PARSE_OK;
}