@@ -94,15 +94,26 @@ extern {
94
94
95
95
}
96
96
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
+
97
110
pub fn render ( w : & mut io:: Writer , s : & str ) {
98
111
extern fn block ( ob : * buf , text : * buf , lang : * buf , opaque : * libc:: c_void ) {
99
112
unsafe {
100
113
let my_opaque: & my_opaque = cast:: transmute ( opaque) ;
101
114
vec:: raw:: buf_as_slice ( ( * text) . data , ( * text) . size as uint , |text| {
102
115
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 ( ) ) ;
106
117
let text = lines. to_owned_vec ( ) . connect ( "\n " ) ;
107
118
108
119
let buf = buf {
@@ -169,9 +180,7 @@ pub fn find_testable_code(doc: &str, tests: &mut ::test::Collector) {
169
180
vec:: raw:: buf_as_slice ( ( * text) . data , ( * text) . size as uint , |text| {
170
181
let tests: & mut :: test:: Collector = intrinsics:: transmute ( opaque) ;
171
182
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) ) ;
175
184
let text = lines. to_owned_vec ( ) . connect ( "\n " ) ;
176
185
tests. add_test ( text, ignore, shouldfail) ;
177
186
} )
0 commit comments