@@ -69,50 +69,59 @@ pub fn strip_doc_comment_decoration(comment: &str) -> ~str {
6969 return lines. slice ( i, j) . to_owned ( ) ;
7070 }
7171
72- // drop leftmost columns that contain only values in chars
73- fn block_trim ( lines : ~[ ~str ] , chars : ~str , max : Option < uint > ) -> ~[ ~str ] {
74-
75- let mut i = max. get_or_default ( uint:: max_value) ;
76- for lines. each |line| {
77- if line. trim ( ) . is_empty ( ) {
78- loop ;
79- }
72+ /// remove a "[ \t]*\*" block from each line, if possible
73+ fn horizontal_trim ( lines : ~[ ~str ] ) -> ~[ ~str ] {
74+ let mut i = uint:: max_value;
75+ let mut can_trim = true ;
76+ let mut first = true ;
77+ for lines. iter( ) . advance |line| {
8078 for line. iter( ) . enumerate( ) . advance |( j, c) | {
81- if j >= i {
79+ if j > i || !"* \t " . contains_char( c) {
80+ can_trim = false ;
8281 break ;
8382 }
84- if !chars. contains_char ( c) {
85- i = j;
83+ if c == '*' {
84+ if first {
85+ i = j;
86+ first = false ;
87+ } else if i != j {
88+ can_trim = false ;
89+ }
8690 break ;
8791 }
8892 }
93+ if i > line. len ( ) {
94+ can_trim = false ;
95+ }
96+ if !can_trim {
97+ break ;
98+ }
8999 }
90100
91- return do lines. map |line| {
92- let chars = line. iter ( ) . collect :: < ~[ char ] > ( ) ;
93- if i > chars. len ( ) {
94- ~""
95- } else {
96- str:: from_chars ( chars. slice ( i, chars. len ( ) ) )
101+ if can_trim {
102+ do lines. map |line| {
103+ line. slice ( i + 1 , line. len ( ) ) . to_owned ( )
97104 }
98- } ;
105+ } else {
106+ lines
107+ }
99108 }
100109
101110 if comment. starts_with ( "//" ) {
102111 // FIXME #5475:
103- // return comment.slice(3u, comment.len()).trim(). to_owned();
104- let r = comment. slice ( 3 u, comment. len ( ) ) ; return r. trim ( ) . to_owned ( ) ;
112+ // return comment.slice(3u, comment.len()).to_owned();
113+ let r = comment. slice ( 3 u, comment. len ( ) ) ; return r. to_owned ( ) ;
105114 }
106115
107116 if comment. starts_with ( "/*" ) {
108117 let lines = comment. slice ( 3 u, comment. len ( ) - 2 u)
109118 . any_line_iter ( )
110119 . transform ( |s| s. to_owned ( ) )
111120 . collect :: < ~[ ~str ] > ( ) ;
121+
112122 let lines = vertical_trim ( lines) ;
113- let lines = block_trim ( lines, ~"\t ", None);
114- let lines = block_trim(lines, ~" * ", Some(1u));
115- let lines = block_trim(lines, ~"\t " , None ) ;
123+ let lines = horizontal_trim ( lines) ;
124+
116125 return lines. connect ( "\n " ) ;
117126 }
118127
@@ -370,3 +379,36 @@ pub fn gather_comments_and_literals(span_diagnostic:
370379
371380 ( comments, literals)
372381}
382+
383+ #[ cfg( test) ]
384+ mod test {
385+ use super :: * ;
386+
387+ #[ test] fn test_block_doc_comment_1 ( ) {
388+ let comment = "/**\n * Test \n ** Test\n * Test\n */" ;
389+ let correct_stripped = " Test \n * Test\n Test" ;
390+ let stripped = strip_doc_comment_decoration ( comment) ;
391+ assert_eq ! ( stripped. slice( 0 , stripped. len( ) ) , correct_stripped) ;
392+ }
393+
394+ #[ test] fn test_block_doc_comment_2 ( ) {
395+ let comment = "/**\n * Test\n * Test\n */" ;
396+ let correct_stripped = " Test\n Test" ;
397+ let stripped = strip_doc_comment_decoration ( comment) ;
398+ assert_eq ! ( stripped. slice( 0 , stripped. len( ) ) , correct_stripped) ;
399+ }
400+
401+ #[ test] fn test_block_doc_comment_3 ( ) {
402+ let comment = "/**\n let a: *int;\n *a = 5;\n */" ;
403+ let correct_stripped = " let a: *int;\n *a = 5;" ;
404+ let stripped = strip_doc_comment_decoration ( comment) ;
405+ assert_eq ! ( stripped. slice( 0 , stripped. len( ) ) , correct_stripped) ;
406+ }
407+
408+ #[ test] fn test_line_doc_comment ( ) {
409+ let comment = "/// Test" ;
410+ let correct_stripped = " Test" ;
411+ let stripped = strip_doc_comment_decoration ( comment) ;
412+ assert_eq ! ( stripped. slice( 0 , stripped. len( ) ) , correct_stripped) ;
413+ }
414+ }
0 commit comments