@@ -228,7 +228,7 @@ impl<'p, W: fmt::Write> Writer<'p, W> {
228
228
fn write_literal_byte ( & mut self , b : u8 ) -> fmt:: Result {
229
229
let c = b as char ;
230
230
if c <= 0x7F as char && !c. is_control ( ) && !c. is_whitespace ( ) {
231
- self . wtr . write_char ( c)
231
+ self . write_literal_char ( c)
232
232
} else {
233
233
write ! ( self . wtr, "(?-u:\\ x{:02X})" , b)
234
234
}
@@ -237,7 +237,7 @@ impl<'p, W: fmt::Write> Writer<'p, W> {
237
237
fn write_literal_class_byte ( & mut self , b : u8 ) -> fmt:: Result {
238
238
let c = b as char ;
239
239
if c <= 0x7F as char && !c. is_control ( ) && !c. is_whitespace ( ) {
240
- self . wtr . write_char ( c)
240
+ self . write_literal_char ( c)
241
241
} else {
242
242
write ! ( self . wtr, "\\ x{:02X}" , b)
243
243
}
@@ -267,6 +267,10 @@ mod tests {
267
267
let mut printer = Printer :: new ( ) ;
268
268
let mut dst = String :: new ( ) ;
269
269
printer. print ( & hir, & mut dst) . unwrap ( ) ;
270
+
271
+ // Check that the result is actually valid.
272
+ builder. build ( ) . parse ( & dst) . unwrap ( ) ;
273
+
270
274
assert_eq ! ( expected, dst) ;
271
275
}
272
276
@@ -291,6 +295,18 @@ mod tests {
291
295
roundtrip ( r"(?-u)[a]" , r"(?-u:[a])" ) ;
292
296
roundtrip ( r"(?-u)[a-z]" , r"(?-u:[a-z])" ) ;
293
297
roundtrip_bytes ( r"(?-u)[a-\xFF]" , r"(?-u:[a-\xFF])" ) ;
298
+
299
+ // The following test that the printer escapes meta characters
300
+ // in character classes.
301
+ roundtrip ( r"[\[]" , r"[\[]" ) ;
302
+ roundtrip ( r"[Z-_]" , r"[Z-_]" ) ;
303
+ roundtrip ( r"[Z-_--Z]" , r"[\[-_]" ) ;
304
+
305
+ // The following test that the printer escapes meta characters
306
+ // in byte oriented character classes.
307
+ roundtrip_bytes ( r"(?-u)[\[]" , r"(?-u:[\[])" ) ;
308
+ roundtrip_bytes ( r"(?-u)[Z-_]" , r"(?-u:[Z-_])" ) ;
309
+ roundtrip_bytes ( r"(?-u)[Z-_--Z]" , r"(?-u:[\[-_])" ) ;
294
310
}
295
311
296
312
#[ test]
0 commit comments