Skip to content

Commit

Permalink
Handle the \f escape sequence (the two characters: backslash followed…
Browse files Browse the repository at this point in the history
… by an f, not a literal formfeed) and extend the test_parse test to check all valid escape sequences.
  • Loading branch information
hawicz committed Jul 29, 2012
1 parent 8fcfeb6 commit 92f31bd
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
2 changes: 2 additions & 0 deletions json_tokener.c
Original file line number Diff line number Diff line change
Expand Up @@ -423,10 +423,12 @@ struct json_object* json_tokener_parse_ex(struct json_tokener *tok,
case 'n':
case 'r':
case 't':
case 'f':
if(c == 'b') printbuf_memappend_fast(tok->pb, "\b", 1);
else if(c == 'n') printbuf_memappend_fast(tok->pb, "\n", 1);
else if(c == 'r') printbuf_memappend_fast(tok->pb, "\r", 1);
else if(c == 't') printbuf_memappend_fast(tok->pb, "\t", 1);
else if(c == 'f') printbuf_memappend_fast(tok->pb, "\f", 1);
state = saved_state;
break;
case 'u':
Expand Down
11 changes: 11 additions & 0 deletions tests/test_parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,15 @@ struct incremental_step {
/* Strings have a well defined end point, so we can stop at the quote */
{ "\"blue\"", -1, -1, json_tokener_success, 0 },

/* Check each of the escape sequences defined by the spec */
{ "\"\\\"\"", -1, -1, json_tokener_success, 0 },
{ "\"\\\\\"", -1, -1, json_tokener_success, 0 },
{ "\"\\b\"", -1, -1, json_tokener_success, 0 },
{ "\"\\f\"", -1, -1, json_tokener_success, 0 },
{ "\"\\n\"", -1, -1, json_tokener_success, 0 },
{ "\"\\r\"", -1, -1, json_tokener_success, 0 },
{ "\"\\t\"", -1, -1, json_tokener_success, 0 },

{ "[1,2,3]", -1, -1, json_tokener_success, 0 },

/* This behaviour doesn't entirely follow the json spec, but until we have
Expand All @@ -190,6 +199,8 @@ static void test_incremental_parse()
num_error = 0;

printf("Starting incremental tests.\n");
printf("Note: quotes and backslashes seen in the output here are literal values passed\n");
printf(" to the parse functions. e.g. this is 4 characters: \"\\f\"\n");

string_to_parse = "{ \"foo"; /* } */
printf("json_tokener_parse(%s) ... ", string_to_parse);
Expand Down
11 changes: 10 additions & 1 deletion tests/test_parse.expected
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ new_obj.to_string()={ "abc": 12, "foo": "bar", "bool0": false, "bool1": true, "a
json_tokener_parse_versbose() OK
==================================
Starting incremental tests.
Note: quotes and backslashes seen in the output here are literal values passed
to the parse functions. e.g. this is 4 characters: "\f"
json_tokener_parse({ "foo) ... got error as expected
json_tokener_parse_ex(tok, { "foo": 123 }, 14) ... OK: got object of type [object]: { "foo": 123 }
json_tokener_parse_ex(tok, { "foo": 456 }, 14) ... OK: got object of type [object]: { "foo": 456 }
Expand All @@ -39,8 +41,15 @@ json_tokener_parse_ex(tok, "Y" , 3) ... OK: got object of type [string
json_tokener_parse_ex(tok, 1 , 1) ... OK: got correct error: continue
json_tokener_parse_ex(tok, 2 , 2) ... OK: got object of type [int]: 12
json_tokener_parse_ex(tok, "blue" , 6) ... OK: got object of type [string]: "blue"
json_tokener_parse_ex(tok, "\"" , 4) ... OK: got object of type [string]: "\""
json_tokener_parse_ex(tok, "\\" , 4) ... OK: got object of type [string]: "\\"
json_tokener_parse_ex(tok, "\b" , 4) ... OK: got object of type [string]: "\b"
json_tokener_parse_ex(tok, "\f" , 4) ... OK: got object of type [string]: "\u000c"
json_tokener_parse_ex(tok, "\n" , 4) ... OK: got object of type [string]: "\n"
json_tokener_parse_ex(tok, "\r" , 4) ... OK: got object of type [string]: "\r"
json_tokener_parse_ex(tok, "\t" , 4) ... OK: got object of type [string]: "\t"
json_tokener_parse_ex(tok, [1,2,3] , 7) ... OK: got object of type [array]: [ 1, 2, 3 ]
json_tokener_parse_ex(tok, [1,2,3,] , 8) ... OK: got object of type [array]: [ 1, 2, 3 ]
json_tokener_parse_ex(tok, [1,2,,3,] , 9) ... OK: got correct error: unexpected character
End Incremental Tests OK=20 ERROR=0
End Incremental Tests OK=27 ERROR=0
==================================

0 comments on commit 92f31bd

Please sign in to comment.