@@ -504,18 +504,20 @@ impl<'a> Parser<'a> {
504
504
// having to be read from source to a buffer and then from a buffer to
505
505
// our target string. Nothing to be done about this, really.
506
506
fn read_complex_string < ' b > ( & mut self , start : usize ) -> Result < & ' b str > {
507
- self . buffer . clear ( ) ;
507
+ let len = self . buffer . len ( ) ;
508
+ //self.buffer.clear();
508
509
let mut ch = b'\\' ;
509
510
510
511
// TODO: Use fastwrite here as well
511
- self . buffer . extend_from_slice ( self . source [ start .. self . index - 1 ] . as_bytes ( ) ) ;
512
+ self . buffer . extend_from_slice ( & self . source . as_bytes ( ) [ start .. self . index - 1 ] ) ;
512
513
513
514
loop {
514
515
if ALLOWED [ ch as usize ] {
515
516
self . buffer . push ( ch) ;
516
517
ch = expect_byte ! ( self ) ;
517
518
continue ;
518
519
}
520
+
519
521
match ch {
520
522
b'"' => break ,
521
523
b'\\' => {
@@ -528,7 +530,7 @@ impl<'a> Parser<'a> {
528
530
} ,
529
531
b'"' |
530
532
b'\\' |
531
- b'/' => escaped,
533
+ b'/' => escaped,
532
534
b'b' => 0x8 ,
533
535
b'f' => 0xC ,
534
536
b't' => b'\t' ,
@@ -554,7 +556,7 @@ impl<'a> Parser<'a> {
554
556
// issues here, we construct a new slice from raw parts, which
555
557
// then has lifetime bound to the outer function scope instead
556
558
// of the parser itself.
557
- slice:: from_raw_parts ( self . buffer . as_ptr ( ) , self . buffer . len ( ) )
559
+ slice:: from_raw_parts ( self . buffer [ len.. ] . as_ptr ( ) , self . buffer . len ( ) - len )
558
560
)
559
561
} )
560
562
}
@@ -772,6 +774,7 @@ impl<'a> Parser<'a> {
772
774
}
773
775
}
774
776
777
+ #[ derive( Debug ) ]
775
778
struct StackBlock < ' a > ( JsonValue , & ' a str ) ;
776
779
777
780
// All that hard work, and in the end it's just a single function in the API.
@@ -784,12 +787,45 @@ pub fn parse(source: &str) -> Result<JsonValue> {
784
787
#[ cfg( test) ]
785
788
mod tests {
786
789
use super :: * ;
790
+ use :: stringify;
791
+ use :: stringify_pretty;
787
792
use :: value:: JsonValue ;
788
793
789
794
#[ macro_use]
790
795
use crate :: object;
791
796
use crate :: array;
792
797
798
+ use std:: fs:: File ;
799
+ use std:: io:: prelude:: * ;
800
+
801
+ #[ test]
802
+ fn it_should_parse_escaped_forward_slashes ( ) {
803
+ let mut file = File :: open ( "tests/test_json" ) . unwrap ( ) ;
804
+ let mut contents = String :: new ( ) ;
805
+ file. read_to_string ( & mut contents) . unwrap ( ) ;
806
+
807
+ let actual = parse ( & contents) . unwrap ( ) ;
808
+ let serialized = stringify ( actual. clone ( ) ) ;
809
+
810
+ let serialized_pretty = stringify_pretty ( actual. clone ( ) , 2 ) ;
811
+ println ! ( "serialized: {}" , serialized_pretty) ;
812
+
813
+ assert_eq ! ( serialized, contents) ;
814
+ }
815
+
816
+ #[ test]
817
+ fn it_should_parse_escaped_forward_slashes_2 ( ) {
818
+ let contents = String :: from ( "{\" ab\" :\" c\\ \" d\\ \" e\" ,\" f\" :1}" ) ;
819
+
820
+ let actual = parse ( & contents) . unwrap ( ) ;
821
+ let serialized = stringify ( actual. clone ( ) ) ;
822
+
823
+ let serialized_pretty = stringify_pretty ( actual. clone ( ) , 2 ) ;
824
+ println ! ( "serialized: {}" , serialized_pretty) ;
825
+
826
+ assert_eq ! ( serialized, contents) ;
827
+ }
828
+
793
829
#[ test]
794
830
fn it_should_parse_basic_json_values ( ) {
795
831
let s = "{\" a\" :1,\" b\" :true,\" c\" :false,\" d\" :null,\" e\" :2}" ;
0 commit comments