@@ -59,11 +59,19 @@ pub fn strip_doc_comment_decoration(comment: &str) -> ~str {
5959 fn vertical_trim ( lines : ~[ ~str ] ) -> ~[ ~str ] {
6060 let mut i = 0 u;
6161 let mut j = lines. len ( ) ;
62+ // first line of all-stars should be omitted
63+ if lines. len ( ) > 0 && lines[ 0 ] . iter ( ) . all ( |c| c == '*' ) {
64+ i += 1 ;
65+ }
6266 while i < j && lines[ i] . trim ( ) . is_empty ( ) {
63- i += 1 u;
67+ i += 1 ;
68+ }
69+ // like the first, a last line of all stars should be omitted
70+ if j > i && lines[ j - 1 ] . iter ( ) . skip ( 1 ) . all ( |c| c == '*' ) {
71+ j -= 1 ;
6472 }
65- while j > i && lines[ j - 1 u ] . trim ( ) . is_empty ( ) {
66- j -= 1 u ;
73+ while j > i && lines[ j - 1 ] . trim ( ) . is_empty ( ) {
74+ j -= 1 ;
6775 }
6876 return lines. slice ( i, j) . to_owned ( ) ;
6977 }
@@ -106,8 +114,12 @@ pub fn strip_doc_comment_decoration(comment: &str) -> ~str {
106114 }
107115 }
108116
109- if comment. starts_with ( "//" ) {
110- return comment. slice ( 3 u, comment. len ( ) ) . to_owned ( ) ;
117+ // one-line comments lose their prefix
118+ static ONLINERS : & ' static [ & ' static str ] = & [ "///!" , "///" , "//!" , "//" ] ;
119+ for prefix in ONLINERS . iter ( ) {
120+ if comment. starts_with ( * prefix) {
121+ return comment. slice_from ( prefix. len ( ) ) . to_owned ( ) ;
122+ }
111123 }
112124
113125 if comment. starts_with ( "/*" ) {
@@ -384,29 +396,42 @@ mod test {
384396
385397 #[ test] fn test_block_doc_comment_1 ( ) {
386398 let comment = "/**\n * Test \n ** Test\n * Test\n */" ;
387- let correct_stripped = " Test \n * Test\n Test" ;
388399 let stripped = strip_doc_comment_decoration ( comment) ;
389- assert_eq ! ( stripped. slice ( 0 , stripped . len ( ) ) , correct_stripped ) ;
400+ assert_eq!( stripped, ~" Test \n * Test \n Test " );
390401 }
391402
392403 #[test] fn test_block_doc_comment_2() {
393404 let comment = " /**\n * Test\n * Test\n*/ ";
394- let correct_stripped = " Test\n Test" ;
395405 let stripped = strip_doc_comment_decoration(comment);
396- assert_eq ! ( stripped. slice ( 0 , stripped . len ( ) ) , correct_stripped ) ;
406+ assert_eq!(stripped, ~" Test \n Test " );
397407 }
398408
399409 #[test] fn test_block_doc_comment_3() {
400410 let comment = " /**\n let a: *int;\n *a = 5;\n*/ ";
401- let correct_stripped = " let a: *int;\n *a = 5;" ;
402411 let stripped = strip_doc_comment_decoration(comment);
403- assert_eq ! ( stripped. slice ( 0 , stripped . len ( ) ) , correct_stripped ) ;
412+ assert_eq!(stripped, ~" let a : * int ; \n * a = 5 ; " );
404413 }
405414
406- #[ test] fn test_line_doc_comment ( ) {
407- let comment = "/// Test" ;
408- let correct_stripped = " Test" ;
415+ #[test] fn test_block_doc_comment_4() {
416+ let comment = " /*******************\n test\n *********************/ ";
409417 let stripped = strip_doc_comment_decoration(comment);
410- assert_eq ! ( stripped. slice( 0 , stripped. len( ) ) , correct_stripped) ;
418+ assert_eq!(stripped, ~" test");
419+ }
420+
421+ #[test] fn test_line_doc_comment() {
422+ let stripped = strip_doc_comment_decoration(" /// test");
423+ assert_eq!( stripped, ~" test");
424+ let stripped = strip_doc_comment_decoration(" ///! test");
425+ assert_eq!( stripped, ~" test");
426+ let stripped = strip_doc_comment_decoration(" // test");
427+ assert_eq!( stripped, ~" test");
428+ let stripped = strip_doc_comment_decoration(" // test");
429+ assert_eq!( stripped, ~" test");
430+ let stripped = strip_doc_comment_decoration(" ///test");
431+ assert_eq!( stripped, ~"test");
432+ let stripped = strip_doc_comment_decoration(" ///!test");
433+ assert_eq!( stripped, ~"test");
434+ let stripped = strip_doc_comment_decoration(" //test");
435+ assert_eq!( stripped, ~"test" ) ;
411436 }
412437}
0 commit comments