@@ -94,15 +94,26 @@ extern {
9494
9595}
9696
97+ /// Returns Some(code) if `s` is a line that should be stripped from
98+ /// documentation but used in example code. `code` is the portion of
99+ /// `s` that should be used in tests. (None for lines that should be
100+ /// left as-is.)
101+ fn stripped_filtered_line < ' a > ( s : & ' a str ) -> Option < & ' a str > {
102+ let trimmed = s. trim ( ) ;
103+ if trimmed. starts_with ( "# " ) {
104+ Some ( trimmed. slice_from ( 2 ) )
105+ } else {
106+ None
107+ }
108+ }
109+
97110pub fn render ( w : & mut io:: Writer , s : & str ) {
98111 extern fn block ( ob : * buf , text : * buf , lang : * buf , opaque : * libc:: c_void ) {
99112 unsafe {
100113 let my_opaque: & my_opaque = cast:: transmute ( opaque) ;
101114 vec:: raw:: buf_as_slice ( ( * text) . data , ( * text) . size as uint , |text| {
102115 let text = str:: from_utf8 ( text) ;
103- let mut lines = text. lines ( ) . filter ( |l| {
104- !l. trim ( ) . starts_with ( "# " )
105- } ) ;
116+ let mut lines = text. lines ( ) . filter ( |l| stripped_filtered_line ( * l) . is_none ( ) ) ;
106117 let text = lines. to_owned_vec ( ) . connect ( "\n " ) ;
107118
108119 let buf = buf {
@@ -169,9 +180,7 @@ pub fn find_testable_code(doc: &str, tests: &mut ::test::Collector) {
169180 vec:: raw:: buf_as_slice ( ( * text) . data , ( * text) . size as uint , |text| {
170181 let tests: & mut :: test:: Collector = intrinsics:: transmute ( opaque) ;
171182 let text = str:: from_utf8 ( text) ;
172- let mut lines = text. lines ( ) . map ( |l| {
173- if l. starts_with ( "# " ) { l. slice_from ( 2 ) } else { l}
174- } ) ;
183+ let mut lines = text. lines ( ) . map ( |l| stripped_filtered_line ( l) . unwrap_or ( l) ) ;
175184 let text = lines. to_owned_vec ( ) . connect ( "\n " ) ;
176185 tests. add_test ( text, ignore, shouldfail) ;
177186 } )
0 commit comments