Skip to content

Commit 85960a2

Browse files
committed
my_answer for tutorial01
1 parent 14966ed commit 85960a2

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

tutorial01/leptjson.c

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,31 @@ static int lept_parse_null(lept_context* c, lept_value* v) {
2424
return LEPT_PARSE_OK;
2525
}
2626

27+
static int lept_parse_true(lept_context* c, lept_value* v) {
28+
EXPECT(c, 't');
29+
if (c->json[0] != 'r' || c->json[1] != 'u' || c->json[2] != 'e')
30+
return LEPT_PARSE_INVALID_VALUE;
31+
c->json += 3;
32+
v->type = LEPT_TRUE;
33+
return LEPT_PARSE_OK;
34+
}
35+
36+
static int lept_parse_false(lept_context* c, lept_value* v) {
37+
EXPECT(c, 'f');
38+
if (c->json[0] != 'a' || c->json[1] != 'l' || c->json[2] != 's' || c->json[3] != 'e')
39+
return LEPT_PARSE_INVALID_VALUE;
40+
c->json += 4;
41+
v->type = LEPT_FALSE;
42+
return LEPT_PARSE_OK;
43+
}
44+
45+
46+
2747
static int lept_parse_value(lept_context* c, lept_value* v) {
2848
switch (*c->json) {
2949
case 'n': return lept_parse_null(c, v);
50+
case 't': return lept_parse_true(c, v);
51+
case 'f': return lept_parse_false(c, v);
3052
case '\0': return LEPT_PARSE_EXPECT_VALUE;
3153
default: return LEPT_PARSE_INVALID_VALUE;
3254
}
@@ -38,7 +60,13 @@ int lept_parse(lept_value* v, const char* json) {
3860
c.json = json;
3961
v->type = LEPT_NULL;
4062
lept_parse_whitespace(&c);
41-
return lept_parse_value(&c, v);
63+
int parse_value_state = lept_parse_value(&c, v);
64+
if (parse_value_state == LEPT_PARSE_OK)
65+
{
66+
lept_parse_whitespace(&c);
67+
if (*c.json != '\0') return LEPT_PARSE_ROOT_NOT_SINGULAR;
68+
}
69+
return parse_value_state;
4270
}
4371

4472
lept_type lept_get_type(const lept_value* v) {

tutorial01/test.c

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,28 @@ static int test_pass = 0;
2121
#define EXPECT_EQ_INT(expect, actual) EXPECT_EQ_BASE((expect) == (actual), expect, actual, "%d")
2222

2323
static void test_parse_null() {
24+
lept_value v;
25+
v.type = LEPT_FALSE;
26+
EXPECT_EQ_INT(LEPT_PARSE_OK, lept_parse(&v, "null"));
27+
EXPECT_EQ_INT(LEPT_NULL, lept_get_type(&v));
28+
}
29+
30+
static void test_parse_true() {
2431
lept_value v;
2532
v.type = LEPT_FALSE;
26-
EXPECT_EQ_INT(LEPT_PARSE_OK, lept_parse(&v, "null"));
27-
EXPECT_EQ_INT(LEPT_NULL, lept_get_type(&v));
33+
EXPECT_EQ_INT(LEPT_PARSE_OK, lept_parse(&v, "true"));
34+
EXPECT_EQ_INT(LEPT_TRUE, lept_get_type(&v));
2835
}
2936

37+
static void test_parse_false() {
38+
lept_value v;
39+
v.type = LEPT_TRUE;
40+
EXPECT_EQ_INT(LEPT_PARSE_OK, lept_parse(&v, "false"));
41+
EXPECT_EQ_INT(LEPT_FALSE, lept_get_type(&v));
42+
}
43+
44+
45+
3046
static void test_parse_expect_value() {
3147
lept_value v;
3248

@@ -59,6 +75,8 @@ static void test_parse_root_not_singular() {
5975

6076
static void test_parse() {
6177
test_parse_null();
78+
test_parse_true();
79+
test_parse_false();
6280
test_parse_expect_value();
6381
test_parse_invalid_value();
6482
test_parse_root_not_singular();

0 commit comments

Comments
 (0)