From c7bf7126ea36375c5428fd84b1a7e64573d10d17 Mon Sep 17 00:00:00 2001 From: Sean Date: Fri, 16 Sep 2016 12:18:57 +0800 Subject: [PATCH 1/2] finish tutorial 1 --- tutorial01/leptjson.c | 34 +++++++++++++++++++++++++++++++++- tutorial01/test.c | 16 ++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/tutorial01/leptjson.c b/tutorial01/leptjson.c index a68718b8..27f59b5a 100644 --- a/tutorial01/leptjson.c +++ b/tutorial01/leptjson.c @@ -21,12 +21,44 @@ static int lept_parse_null(lept_context* c, lept_value* v) { return LEPT_PARSE_INVALID_VALUE; c->json += 3; v->type = LEPT_NULL; - return 0; + lept_parse_whitespace(c); + if(*c->json != '\0'){ + return LEPT_PARSE_ROOT_NOT_SINGULAR; + } + return 0; +} + +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_NULL; + lept_parse_whitespace(c); + if(*c->json != '\0'){ + return LEPT_PARSE_ROOT_NOT_SINGULAR; + } + return 0; +} + +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_NULL; + lept_parse_whitespace(c); + if(*c->json != '\0'){ + return LEPT_PARSE_ROOT_NOT_SINGULAR; + } + return 0; } 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; } diff --git a/tutorial01/test.c b/tutorial01/test.c index e7672181..df49a3a6 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_NULL, 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_NULL, lept_get_type(&v)); +} + static void test_parse_expect_value() { lept_value v; @@ -59,6 +73,8 @@ 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(); From f66a6b762ec41696d955dfb0024c82911c3b7742 Mon Sep 17 00:00:00 2001 From: Sean Date: Sat, 17 Sep 2016 21:55:01 +0800 Subject: [PATCH 2/2] Revert "finish tutorial 1" This reverts commit c7bf7126ea36375c5428fd84b1a7e64573d10d17. --- tutorial01/leptjson.c | 34 +--------------------------------- tutorial01/test.c | 16 ---------------- 2 files changed, 1 insertion(+), 49 deletions(-) diff --git a/tutorial01/leptjson.c b/tutorial01/leptjson.c index 27f59b5a..a68718b8 100644 --- a/tutorial01/leptjson.c +++ b/tutorial01/leptjson.c @@ -21,44 +21,12 @@ static int lept_parse_null(lept_context* c, lept_value* v) { return LEPT_PARSE_INVALID_VALUE; c->json += 3; v->type = LEPT_NULL; - lept_parse_whitespace(c); - if(*c->json != '\0'){ - return LEPT_PARSE_ROOT_NOT_SINGULAR; - } - return 0; -} - -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_NULL; - lept_parse_whitespace(c); - if(*c->json != '\0'){ - return LEPT_PARSE_ROOT_NOT_SINGULAR; - } - return 0; -} - -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_NULL; - lept_parse_whitespace(c); - if(*c->json != '\0'){ - return LEPT_PARSE_ROOT_NOT_SINGULAR; - } - return 0; + return 0; } 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; } diff --git a/tutorial01/test.c b/tutorial01/test.c index df49a3a6..e7672181 100644 --- a/tutorial01/test.c +++ b/tutorial01/test.c @@ -27,20 +27,6 @@ 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_NULL, 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_NULL, lept_get_type(&v)); -} - static void test_parse_expect_value() { lept_value v; @@ -73,8 +59,6 @@ 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();