Skip to content

Commit 53b897f

Browse files
committed
change
1 parent dd9382f commit 53b897f

File tree

3 files changed

+67
-4
lines changed

3 files changed

+67
-4
lines changed

leptjson.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "leptjson.h"
1+
#include "leptjson.h"
22
#include <assert.h> /* assert() */
33
#include <stdlib.h> /* NULL */
44

@@ -44,13 +44,25 @@ static int lept_parse_false(lept_context *c, lept_value *v)
4444
return LEPT_PARSE_OK;
4545
}
4646

47+
static int lept_parse_number(lept_context *c, lept_value *v)
48+
{
49+
char *end;
50+
v->n = strtod(c->json, &end);
51+
if (c->json == end)
52+
return LEPT_PARSE_INVALID_VALUE;
53+
c->json = end;
54+
v->type = LEPT_NUMBER;
55+
return LEPT_PARSE_OK;
56+
}
57+
58+
4759
static int lept_parse_value(lept_context* c, lept_value* v) {
4860
switch (*c->json) {
4961
case 'n': return lept_parse_null(c, v);
5062
case 't': return lept_parse_true(c, v);
5163
case 'f': return lept_parse_false(c, v);
5264
case '\0': return LEPT_PARSE_EXPECT_VALUE;
53-
default: return LEPT_PARSE_INVALID_VALUE;
65+
default: return lept_parse_number(c,v);
5466
}
5567
}
5668

@@ -76,3 +88,8 @@ lept_type lept_get_type(const lept_value* v) {
7688
assert(v != NULL);
7789
return v->type;
7890
}
91+
92+
double lept_get_number(const lept_value *v) {
93+
assert(v != NULL && v->type == LEPT_NUMBER);
94+
return v->n;
95+
}

leptjson.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#ifndef LEPTJSON_H__
1+
#ifndef LEPTJSON_H__
22
#define LEPTJSON_H__
33

44
typedef enum {
@@ -12,6 +12,7 @@ typedef enum {
1212
} lept_type;
1313

1414
typedef struct {
15+
double n;
1516
lept_type type;
1617
}lept_value;
1718

@@ -24,6 +25,8 @@ enum {
2425

2526
int lept_parse(lept_value* v, const char* json);
2627

28+
double lept_get_number(const lept_value *v);
29+
2730
lept_type lept_get_type(const lept_value* v);
2831

2932
#endif /* LEPTJSON_H__ */

test.c

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include <stdio.h>
1+
#include <stdio.h>
22
#include <stdlib.h>
33
#include <string.h>
44
#include "leptjson.h"
@@ -18,8 +18,24 @@ static int test_pass = 0;
1818
}\
1919
} while(0)
2020

21+
#define TEST_NUMBER(expect, json) \
22+
do{\
23+
lept_value v;\
24+
EXPECT_EQ_INT(LEPT_PARSE_OK, lept_parse(&v, json));\
25+
EXPECT_EQ_INT(LEPT_NUMBER, lept_get_type(&v));\
26+
EXPECT_EQ_DOUBLE(expect, lept_get_number(&v));\
27+
}while(0)
28+
2129
#define EXPECT_EQ_INT(expect, actual) EXPECT_EQ_BASE((expect) == (actual), expect, actual, "%d")
2230

31+
#define TEST_ERROR(error, json) \
32+
do{\
33+
lept_value v;\
34+
v.type = LEPT_FALSE;\
35+
EXPECT_EQ_INT(error, lept_parse(&v, json)); \
36+
EXPECT_EQ_INT(LEPT_NULL, lept_get_type(&v)); \
37+
}while(0)
38+
2339
static void test_parse_null() {
2440
lept_value v;
2541
v.type = LEPT_FALSE;
@@ -71,6 +87,33 @@ static void test_parse_false() {
7187
EXPECT_EQ_INT(LEPT_FALSE, lept_get_type(&v));
7288
}
7389

90+
static void test_parse_expect_value() {
91+
TEST_ERROR(LEPT_PARSE_EXPECT_VALUE, "");
92+
TEST_ERROR(LEPT_PARSE_EXPECT_VALUE, " ");
93+
}
94+
95+
static void test_parse_number() {
96+
TEST_NUMBER(0.0, "0");
97+
TEST_NUMBER(0.0, "-0");
98+
TEST_NUMBER(0.0, "-0.0");
99+
TEST_NUMBER(1.0, "1");
100+
TEST_NUMBER(-1.0, "-1");
101+
TEST_NUMBER(1.5, "1.5");
102+
TEST_NUMBER(-1.5, "-1.5");
103+
TEST_NUMBER(3.1416, "3.1416");
104+
TEST_NUMBER(1E10, "1E10");
105+
TEST_NUMBER(1e10, "1e10");
106+
TEST_NUMBER(1E+10, "1E+10");
107+
TEST_NUMBER(1E-10, "1E-10");
108+
TEST_NUMBER(-1E10, "-1E10");
109+
TEST_NUMBER(-1e10, "-1e10");
110+
TEST_NUMBER(-1E+10, "-1E+10");
111+
TEST_NUMBER(-1E-10, "-1E-10");
112+
TEST_NUMBER(1.234E+10, "1.234E+10");
113+
TEST_NUMBER(1.234E-10, "1.234E-10");
114+
TEST_NUMBER(0.0, "1e-10000"); /* must underflow */
115+
}
116+
74117
static void test_parse() {
75118
test_parse_null();
76119
test_parse_true();

0 commit comments

Comments
 (0)