File tree Expand file tree Collapse file tree 2 files changed +68
-61
lines changed
include/tao/json/internal Expand file tree Collapse file tree 2 files changed +68
-61
lines changed Original file line number Diff line number Diff line change @@ -24,71 +24,59 @@ namespace tao
2424 const char * l = p;
2525 const char * const e = s.data () + s.size ();
2626 while ( p != e ) {
27- switch ( const unsigned char c = *p ) {
28- case ' \b ' :
29- if ( l != p ) {
30- o.write ( l, p - l );
31- }
32- l = ++p;
33- o << " \\ b" ;
34- break ;
35- case ' \f ' :
36- if ( l != p ) {
37- o.write ( l, p - l );
38- }
39- l = ++p;
40- o << " \\ f" ;
41- break ;
42- case ' \n ' :
43- if ( l != p ) {
44- o.write ( l, p - l );
45- }
46- l = ++p;
47- o << " \\ n" ;
48- break ;
49- case ' \r ' :
50- if ( l != p ) {
51- o.write ( l, p - l );
52- }
53- l = ++p;
54- o << " \\ r" ;
55- break ;
56- case ' \t ' :
57- if ( l != p ) {
58- o.write ( l, p - l );
59- }
60- l = ++p;
61- o << " \\ t" ;
62- break ;
63- case ' \\ ' :
64- if ( l != p ) {
65- o.write ( l, p - l );
66- }
67- l = ++p;
68- o << " \\\\ " ;
69- break ;
70- case ' \" ' :
71- if ( l != p ) {
72- o.write ( l, p - l );
73- }
74- l = ++p;
75- o << " \\\" " ;
76- break ;
77- default :
78- if ( ( c < 32 ) || ( c == 127 ) ) {
79- if ( l != p ) {
80- o.write ( l, p - l );
81- }
82- l = ++p;
27+ const unsigned char c = *p;
28+ if ( c == ' \\ ' ) {
29+ if ( l != p ) {
30+ o.write ( l, p - l );
31+ }
32+ l = ++p;
33+ o << " \\\\ " ;
34+ }
35+ else if ( c == ' "' ) {
36+ if ( l != p ) {
37+ o.write ( l, p - l );
38+ }
39+ l = ++p;
40+ o << " \\\" " ;
41+ }
42+ else if ( c < 32 ) {
43+ if ( l != p ) {
44+ o.write ( l, p - l );
45+ }
46+ l = ++p;
47+ switch ( c ) {
48+ case ' \b ' :
49+ o << " \\ b" ;
50+ break ;
51+ case ' \f ' :
52+ o << " \\ f" ;
53+ break ;
54+ case ' \n ' :
55+ o << " \\ n" ;
56+ break ;
57+ case ' \r ' :
58+ o << " \\ r" ;
59+ break ;
60+ case ' \t ' :
61+ o << " \\ t" ;
62+ break ;
63+ default :
8364 o << " \\ u00" << h[ ( c & 0xf0 ) >> 4 ] << h[ c & 0x0f ];
84- }
85- else {
86- ++p;
87- }
65+ }
66+ }
67+ else if ( c == 127 ) {
68+ if ( l != p ) {
69+ o.write ( l, p - l );
70+ }
71+ l = ++p;
72+ o << " \\ u007f" ;
73+ }
74+ else {
75+ ++p;
8876 }
8977 }
9078 if ( l != p ) {
91- o.write ( l, p - l );
79+ o.write ( l, p - l );
9280 }
9381 o << ' "' ;
9482 }
Original file line number Diff line number Diff line change @@ -9,11 +9,30 @@ namespace tao
99{
1010 namespace json
1111 {
12+ void roundtrip ( const std::string & value )
13+ {
14+ TEST_ASSERT ( to_string ( from_string ( value ) ) == value );
15+ }
16+
1217 void unit_test ()
1318 {
19+ roundtrip ( " \" \\ u0000 \" " );
20+ roundtrip ( " \" \\ b \" " );
21+ roundtrip ( " \" \\ f \" " );
22+ roundtrip ( " \" \\ n \" " );
23+ roundtrip ( " \" \\ r \" " );
24+ roundtrip ( " \" \\ t \" " );
25+ roundtrip ( " \" \\\\ \" " );
26+ roundtrip ( " \" \\\" \" " );
27+ roundtrip ( " \" \\ u007f \" " );
28+ roundtrip ( " \" \\ b.\\ f \" " );
29+ roundtrip ( " \" \\ b\\ f\\ n\\ r\\ t\\\\\\\" \" " );
30+ roundtrip ( " \" \\ bx\\ fxz\\ nzx\\ ryx\\ txy\\\\ yz\\\" \" " );
31+
1432 const std::string s1 = " \" \\ b\\ f\\ n\\ r\\ t\\\\\\\" \" " ;
1533 TEST_ASSERT ( from_string ( s1 ).get_string () == " \b\f\n\r\t\\\" " );
1634 TEST_ASSERT ( to_string ( from_string ( s1 ) ) == s1 );
35+
1736 const std::string s2 = " \" \\ u007f \" " ;
1837 TEST_ASSERT ( from_string ( s2 ).get_string () == " \x7f " );
1938 TEST_ASSERT ( to_string ( from_string ( s2 ) ) == s2 );
You can’t perform that action at this time.
0 commit comments