From 1c2cbbb511fe5f277293e08d91f31ab4644abc08 Mon Sep 17 00:00:00 2001 From: wonderjeo <455708864@qq.com> Date: Tue, 20 Sep 2016 10:32:12 +0800 Subject: [PATCH] Homework --- tutorial01/leptjson.c | 29 ++++++++++++++++++++++++++++- tutorial01/test.c | 18 +++++++++++++++++- tutorial02/leptjson.c | 41 ++++++++++++++--------------------------- 3 files changed, 59 insertions(+), 29 deletions(-) diff --git a/tutorial01/leptjson.c b/tutorial01/leptjson.c index 5299fe1d..e80e23ba 100644 --- a/tutorial01/leptjson.c +++ b/tutorial01/leptjson.c @@ -24,9 +24,29 @@ static int lept_parse_null(lept_context* c, lept_value* v) { return LEPT_PARSE_OK; } +static int lept_parse_true(lept_context* c, lept_value* v) { + EXPECT(c, 't'); + if (c->json[0] != 'r' || c->json[1] != 'u' || c->json[2] != 'e') + return LEPT_PARSE_INVALID_VALUE; + c->json += 3; + v->type = LEPT_TRUE; + return LEPT_PARSE_OK; +} + +static int lept_parse_false(lept_context* c, lept_value* v) { + EXPECT(c, 'f'); + if (c->json[0] != 'a' || c->json[1] != 'l' || c->json[2] != 's' || c->json[3] != 'e') + return LEPT_PARSE_INVALID_VALUE; + c->json += 4; + v->type = LEPT_FALSE; + return LEPT_PARSE_OK; +} + static int lept_parse_value(lept_context* c, lept_value* v) { switch (*c->json) { case 'n': return lept_parse_null(c, v); + case 't': return lept_parse_true(c, v); + case 'f': return lept_parse_false(c, v); case '\0': return LEPT_PARSE_EXPECT_VALUE; default: return LEPT_PARSE_INVALID_VALUE; } @@ -38,7 +58,14 @@ int lept_parse(lept_value* v, const char* json) { c.json = json; v->type = LEPT_NULL; lept_parse_whitespace(&c); - return lept_parse_value(&c, v); + int ret = lept_parse_value(&c, v); + if(!ret){ + lept_parse_whitespace(&c); + if(*(c.json) != '\0') + return LEPT_PARSE_ROOT_NOT_SINGULAR; + } + else + return ret; } lept_type lept_get_type(const lept_value* v) { diff --git a/tutorial01/test.c b/tutorial01/test.c index e7672181..c6d5b56c 100644 --- a/tutorial01/test.c +++ b/tutorial01/test.c @@ -27,6 +27,20 @@ static void test_parse_null() { EXPECT_EQ_INT(LEPT_NULL, lept_get_type(&v)); } +static void test_parse_true() { + lept_value v; + v.type = LEPT_FALSE; + EXPECT_EQ_INT(LEPT_PARSE_OK, lept_parse(&v, "true")); + EXPECT_EQ_INT(LEPT_TRUE, lept_get_type(&v)); +} + +static void test_parse_false() { + lept_value v; + v.type = LEPT_FALSE; + EXPECT_EQ_INT(LEPT_PARSE_OK, lept_parse(&v, "false")); + EXPECT_EQ_INT(LEPT_FALSE, lept_get_type(&v)); +} + static void test_parse_expect_value() { lept_value v; @@ -59,9 +73,11 @@ static void test_parse_root_not_singular() { static void test_parse() { test_parse_null(); + test_parse_true(); + test_parse_false(); test_parse_expect_value(); test_parse_invalid_value(); - test_parse_root_not_singular(); + test_parse_root_not_singular(); } int main() { diff --git a/tutorial02/leptjson.c b/tutorial02/leptjson.c index 7693e43b..ac97e22a 100644 --- a/tutorial02/leptjson.c +++ b/tutorial02/leptjson.c @@ -15,30 +15,17 @@ static void lept_parse_whitespace(lept_context* c) { c->json = p; } -static int lept_parse_true(lept_context* c, lept_value* v) { - EXPECT(c, 't'); - if (c->json[0] != 'r' || c->json[1] != 'u' || c->json[2] != 'e') - return LEPT_PARSE_INVALID_VALUE; - c->json += 3; - v->type = LEPT_TRUE; - return LEPT_PARSE_OK; -} - -static int lept_parse_false(lept_context* c, lept_value* v) { - EXPECT(c, 'f'); - if (c->json[0] != 'a' || c->json[1] != 'l' || c->json[2] != 's' || c->json[3] != 'e') - return LEPT_PARSE_INVALID_VALUE; - c->json += 4; - v->type = LEPT_FALSE; - return LEPT_PARSE_OK; -} - -static int lept_parse_null(lept_context* c, lept_value* v) { - EXPECT(c, 'n'); - if (c->json[0] != 'u' || c->json[1] != 'l' || c->json[2] != 'l') - return LEPT_PARSE_INVALID_VALUE; - c->json += 3; - v->type = LEPT_NULL; +static int lept_parse_literal(lept_context* c, lept_value* v, char* s, lept_type type) { + const char *p = c->json; + EXPECT(c, s[0]); + while(*s!='\0'){ + if(*p!=*s) + return LEPT_PARSE_INVALID_VALUE; + p++; + s++; + } + c->json = p; + v->type = type; return LEPT_PARSE_OK; } @@ -55,9 +42,9 @@ static int lept_parse_number(lept_context* c, lept_value* v) { static int lept_parse_value(lept_context* c, lept_value* v) { switch (*c->json) { - case 't': return lept_parse_true(c, v); - case 'f': return lept_parse_false(c, v); - case 'n': return lept_parse_null(c, v); + case 't': return lept_parse_literal(c, v, "true", LEPT_TRUE); + case 'f': return lept_parse_literal(c, v, "false", LEPT_FALSE); + case 'n': return lept_parse_literal(c, v, "null", LEPT_NULL); default: return lept_parse_number(c, v); case '\0': return LEPT_PARSE_EXPECT_VALUE; }