@@ -69,50 +69,59 @@ pub fn strip_doc_comment_decoration(comment: &str) -> ~str {
69
69
return lines. slice ( i, j) . to_owned ( ) ;
70
70
}
71
71
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| {
80
78
for line. iter( ) . enumerate( ) . advance |( j, c) | {
81
- if j >= i {
79
+ if j > i || !"* \t " . contains_char( c) {
80
+ can_trim = false ;
82
81
break ;
83
82
}
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
+ }
86
90
break ;
87
91
}
88
92
}
93
+ if i > line. len ( ) {
94
+ can_trim = false ;
95
+ }
96
+ if !can_trim {
97
+ break ;
98
+ }
89
99
}
90
100
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 ( )
97
104
}
98
- } ;
105
+ } else {
106
+ lines
107
+ }
99
108
}
100
109
101
110
if comment. starts_with ( "//" ) {
102
111
// 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 ( ) ;
105
114
}
106
115
107
116
if comment. starts_with ( "/*" ) {
108
117
let lines = comment. slice ( 3 u, comment. len ( ) - 2 u)
109
118
. any_line_iter ( )
110
119
. transform ( |s| s. to_owned ( ) )
111
120
. collect :: < ~[ ~str ] > ( ) ;
121
+
112
122
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
+
116
125
return lines. connect ( "\n " ) ;
117
126
}
118
127
@@ -370,3 +379,36 @@ pub fn gather_comments_and_literals(span_diagnostic:
370
379
371
380
( comments, literals)
372
381
}
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