Skip to content

Commit da1b896

Browse files
committed
solution for t1
1 parent 80c4da2 commit da1b896

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

tutorial01/leptjson.c

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,54 @@ static int lept_parse_null(lept_context* c, lept_value* v) {
2424
return LEPT_PARSE_OK;
2525
}
2626

27+
/* true = "true" */
28+
static int lept_parse_true(lept_context* c, lept_value* v) {
29+
EXPECT(c, 't');
30+
if (c->json[0] != 'r' || c->json[1] != 'u' || c->json[2] != 'e')
31+
return LEPT_PARSE_INVALID_VALUE;
32+
c->json += 3;
33+
v->type = LEPT_TRUE;
34+
return LEPT_PARSE_OK;
35+
}
36+
37+
/* false = "false" */
38+
static int lept_parse_false(lept_context* c, lept_value* v) {
39+
EXPECT(c, 'f');
40+
if (c->json[0] != 'a' || c->json[1] != 'l' || c->json[2] != 's' || c->json[3] != 'e')
41+
return LEPT_PARSE_INVALID_VALUE;
42+
c->json += 4;
43+
v->type = LEPT_FALSE;
44+
return LEPT_PARSE_OK;
45+
}
46+
2747
static int lept_parse_value(lept_context* c, lept_value* v) {
28-
switch (*c->json) {
29-
case 'n': return lept_parse_null(c, v);
48+
int ret_code = -1;
49+
50+
switch (*c ->json) {
51+
case 'n':
52+
ret_code = lept_parse_null(c, v);
53+
break;
54+
case 't':
55+
ret_code = lept_parse_true(c, v);
56+
break;
57+
case 'f':
58+
ret_code = lept_parse_false(c, v);
59+
break;
3060
case '\0': return LEPT_PARSE_EXPECT_VALUE;
3161
default: return LEPT_PARSE_INVALID_VALUE;
3262
}
63+
64+
if (c->json[0] == ' ') {
65+
lept_parse_whitespace(c);
66+
67+
if (c->json[0] != '\0')
68+
return LEPT_PARSE_ROOT_NOT_SINGULAR;
69+
else {
70+
return ret_code;
71+
}
72+
}
73+
74+
return ret_code;
3375
}
3476

3577
int lept_parse(lept_value* v, const char* json) {

tutorial01/test.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,20 @@ static void test_parse_null() {
2727
EXPECT_EQ_INT(LEPT_NULL, lept_get_type(&v));
2828
}
2929

30+
static void test_parse_true() {
31+
lept_value v;
32+
v.type = LEPT_FALSE;
33+
EXPECT_EQ_INT(LEPT_PARSE_OK, lept_parse(&v, "true"));
34+
EXPECT_EQ_INT(LEPT_TRUE, lept_get_type(&v));
35+
}
36+
37+
static void test_parse_false() {
38+
lept_value v;
39+
v.type = LEPT_FALSE;
40+
EXPECT_EQ_INT(LEPT_PARSE_OK, lept_parse(&v, "false"));
41+
EXPECT_EQ_INT(LEPT_FALSE, lept_get_type(&v));
42+
}
43+
3044
static void test_parse_expect_value() {
3145
lept_value v;
3246

@@ -59,6 +73,8 @@ static void test_parse_root_not_singular() {
5973

6074
static void test_parse() {
6175
test_parse_null();
76+
test_parse_true();
77+
test_parse_false();
6278
test_parse_expect_value();
6379
test_parse_invalid_value();
6480
test_parse_root_not_singular();

0 commit comments

Comments
 (0)