Skip to content

Commit fe704b1

Browse files
committed
updata code
1 parent a704d35 commit fe704b1

File tree

2 files changed

+40
-13
lines changed

2 files changed

+40
-13
lines changed

tutorial03/leptjson.c

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,28 @@ static int lept_parse_string(lept_context* c, lept_value* v) {
102102
case '\0':
103103
c->top = head;
104104
return LEPT_PARSE_MISS_QUOTATION_MARK;
105+
case '\\':
106+
{
107+
switch (*p++) {
108+
case '\"': PUTC(c, '\"'); break;
109+
case '\\': PUTC(c, '\\'); break;
110+
case '/': PUTC(c, '/'); break;
111+
case 'b': PUTC(c, '\b'); break;
112+
case 'f': PUTC(c, '\f'); break;
113+
case 'n': PUTC(c, '\n'); break;
114+
case 'r': PUTC(c, '\r'); break;
115+
case 't': PUTC(c, '\t'); break;
116+
default:
117+
c->top = head;
118+
return LEPT_PARSE_INVALID_STRING_ESCAPE;
119+
}
120+
break;
121+
}
105122
default:
123+
if ((unsigned char)ch < 0x20) {
124+
c->top = head;
125+
return LEPT_PARSE_INVALID_STRING_CHAR;
126+
}
106127
PUTC(c, ch);
107128
}
108129
}
@@ -153,12 +174,13 @@ lept_type lept_get_type(const lept_value* v) {
153174
}
154175

155176
int lept_get_boolean(const lept_value* v) {
156-
/* \TODO */
157-
return 0;
177+
assert(v != NULL && (v->type == LEPT_TRUE || v->type == LEPT_FALSE));
178+
return v->type == LEPT_TRUE;
158179
}
159180

160181
void lept_set_boolean(lept_value* v, int b) {
161-
/* \TODO */
182+
lept_free(v);
183+
v->type = (b ? LEPT_TRUE : LEPT_FALSE);
162184
}
163185

164186
double lept_get_number(const lept_value* v) {
@@ -167,7 +189,9 @@ double lept_get_number(const lept_value* v) {
167189
}
168190

169191
void lept_set_number(lept_value* v, double n) {
170-
/* \TODO */
192+
lept_free(v);
193+
v->type = LEPT_NUMBER;
194+
v->u.n = n;
171195
}
172196

173197
const char* lept_get_string(const lept_value* v) {

tutorial03/test.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,8 @@ static void test_parse_number() {
107107
static void test_parse_string() {
108108
TEST_STRING("", "\"\"");
109109
TEST_STRING("Hello", "\"Hello\"");
110-
#if 0
111110
TEST_STRING("Hello\nWorld", "\"Hello\\nWorld\"");
112111
TEST_STRING("\" \\ / \b \f \n \r \t", "\"\\\" \\\\ \\/ \\b \\f \\n \\r \\t\"");
113-
#endif
114112
}
115113

116114
#define TEST_ERROR(error, json)\
@@ -163,19 +161,15 @@ static void test_parse_missing_quotation_mark() {
163161
}
164162

165163
static void test_parse_invalid_string_escape() {
166-
#if 0
167164
TEST_ERROR(LEPT_PARSE_INVALID_STRING_ESCAPE, "\"\\v\"");
168165
TEST_ERROR(LEPT_PARSE_INVALID_STRING_ESCAPE, "\"\\'\"");
169166
TEST_ERROR(LEPT_PARSE_INVALID_STRING_ESCAPE, "\"\\0\"");
170167
TEST_ERROR(LEPT_PARSE_INVALID_STRING_ESCAPE, "\"\\x12\"");
171-
#endif
172168
}
173169

174170
static void test_parse_invalid_string_char() {
175-
#if 0
176171
TEST_ERROR(LEPT_PARSE_INVALID_STRING_CHAR, "\"\x01\"");
177172
TEST_ERROR(LEPT_PARSE_INVALID_STRING_CHAR, "\"\x1F\"");
178-
#endif
179173
}
180174

181175
static void test_access_null() {
@@ -188,12 +182,21 @@ static void test_access_null() {
188182
}
189183

190184
static void test_access_boolean() {
191-
/* \TODO */
192-
/* Use EXPECT_TRUE() and EXPECT_FALSE() */
185+
lept_value v;
186+
lept_init(&v);
187+
lept_set_boolean(&v, 0);
188+
EXPECT_FALSE(lept_get_boolean(&v));
189+
lept_set_boolean(&v, 1);
190+
EXPECT_TRUE(lept_get_boolean(&v));
191+
lept_free(&v);
193192
}
194193

195194
static void test_access_number() {
196-
/* \TODO */
195+
lept_value v;
196+
lept_init(&v);
197+
lept_set_number(&v, 10.1);
198+
EXPECT_EQ_DOUBLE(10.1, lept_get_number(&v));
199+
lept_free(&v);
197200
}
198201

199202
static void test_access_string() {

0 commit comments

Comments
 (0)