@@ -3,7 +3,6 @@ use std::num::FpCategory;
33use JsonValue ;
44
55extern crate itoa;
6- extern crate dtoa;
76
87const QU : u8 = b'"' ;
98const BS : u8 = b'\\' ;
@@ -59,13 +58,18 @@ pub trait Generator {
5958
6059 #[ inline( never) ]
6160 fn write_string_complex ( & mut self , string : & str , mut start : usize ) {
61+ self . write ( string[ .. start] . as_bytes ( ) ) ;
62+
6263 for ( index, ch) in string. bytes ( ) . enumerate ( ) . skip ( start) {
6364 let escape = ESCAPED [ ch as usize ] ;
6465 if escape > 0 {
6566 self . write ( string[ start .. index] . as_bytes ( ) ) ;
6667 self . write ( & [ b'\\' , escape] ) ;
6768 start = index + 1 ;
6869 }
70+ if escape == b'u' {
71+ write ! ( self . get_writer( ) , "{:04x}" , ch) . unwrap ( ) ;
72+ }
6973 }
7074 self . write ( string[ start ..] . as_bytes ( ) ) ;
7175
@@ -93,13 +97,12 @@ pub trait Generator {
9397 if num. fract ( ) == 0.0 && num. abs ( ) < 1e19 {
9498 itoa:: write ( self . get_writer ( ) , num as i64 ) . unwrap ( ) ;
9599 } else {
96- dtoa:: write ( self . get_writer ( ) , num) . unwrap ( ) ;
97- // let abs = num.abs();
98- // if abs < 1e-15 || abs > 1e19 {
99- // write!(self.get_writer(), "{:e}", num).unwrap();
100- // } else {
101- // write!(self.get_writer(), "{}", num).unwrap();
102- // }
100+ let abs = num. abs ( ) ;
101+ if abs < 1e-15 || abs > 1e19 {
102+ write ! ( self . get_writer( ) , "{:e}" , num) . unwrap ( ) ;
103+ } else {
104+ write ! ( self . get_writer( ) , "{}" , num) . unwrap ( ) ;
105+ }
103106 }
104107 } ,
105108 FpCategory :: Zero => {
@@ -177,7 +180,9 @@ impl DumpGenerator {
177180 }
178181
179182 pub fn consume ( self ) -> String {
180- String :: from_utf8 ( self . code ) . unwrap ( )
183+ // Original strings were unicode, numbers are all ASCII,
184+ // therefore this is safe.
185+ unsafe { String :: from_utf8_unchecked ( self . code ) }
181186 }
182187}
183188
@@ -221,7 +226,7 @@ impl PrettyGenerator {
221226 }
222227
223228 pub fn consume ( self ) -> String {
224- String :: from_utf8 ( self . code ) . unwrap ( )
229+ unsafe { String :: from_utf8_unchecked ( self . code ) }
225230 }
226231}
227232
0 commit comments