@@ -23,19 +23,21 @@ use rustc_hir::HirId;
2323use rustc_middle:: ty:: TyCtxt ;
2424use rustc_span:: edition:: Edition ;
2525use rustc_span:: Span ;
26+
2627use std:: borrow:: Cow ;
2728use std:: cell:: RefCell ;
2829use std:: collections:: VecDeque ;
2930use std:: default:: Default ;
3031use std:: fmt:: Write ;
31- use std:: ops:: Range ;
32+ use std:: ops:: { ControlFlow , Range } ;
3233use std:: str;
3334
3435use crate :: clean:: RenderedLink ;
3536use crate :: doctest;
3637use crate :: html:: escape:: Escape ;
3738use crate :: html:: format:: Buffer ;
3839use crate :: html:: highlight;
40+ use crate :: html:: length_limit:: HtmlWithLimit ;
3941use crate :: html:: toc:: TocBuilder ;
4042
4143use pulldown_cmark:: {
@@ -1081,15 +1083,6 @@ fn markdown_summary_with_limit(
10811083 return ( String :: new ( ) , false ) ;
10821084 }
10831085
1084- let mut s = String :: with_capacity ( md. len ( ) * 3 / 2 ) ;
1085- let mut text_length = 0 ;
1086- let mut stopped_early = false ;
1087-
1088- fn push ( s : & mut String , text_length : & mut usize , text : & str ) {
1089- write ! ( s, "{}" , Escape ( text) ) . unwrap ( ) ;
1090- * text_length += text. len ( ) ;
1091- }
1092-
10931086 let mut replacer = |broken_link : BrokenLink < ' _ > | {
10941087 if let Some ( link) =
10951088 link_names. iter ( ) . find ( |link| & * link. original_text == broken_link. reference )
@@ -1101,56 +1094,49 @@ fn markdown_summary_with_limit(
11011094 } ;
11021095
11031096 let p = Parser :: new_with_broken_link_callback ( md, opts ( ) , Some ( & mut replacer) ) ;
1104- let p = LinkReplacer :: new ( p, link_names) ;
1097+ let mut p = LinkReplacer :: new ( p, link_names) ;
11051098
1106- ' outer: for event in p {
1099+ // FIXME: capacity
1100+ let mut buf = HtmlWithLimit :: new ( length_limit) ;
1101+ let mut stopped_early = false ;
1102+ p. try_for_each ( |event| {
11071103 match & event {
11081104 Event :: Text ( text) => {
1109- for word in text. split_inclusive ( char:: is_whitespace) {
1110- if text_length + word. len ( ) >= length_limit {
1111- stopped_early = true ;
1112- break ' outer;
1113- }
1114-
1115- push ( & mut s, & mut text_length, word) ;
1105+ let r =
1106+ text. split_inclusive ( char:: is_whitespace) . try_for_each ( |word| buf. push ( word) ) ;
1107+ if r. is_break ( ) {
1108+ stopped_early = true ;
11161109 }
1110+ return r;
11171111 }
11181112 Event :: Code ( code) => {
1119- if text_length + code. len ( ) >= length_limit {
1113+ buf. open_tag ( "code" ) ;
1114+ let r = buf. push ( code) ;
1115+ if r. is_break ( ) {
11201116 stopped_early = true ;
1121- break ;
1117+ } else {
1118+ buf. close_tag ( ) ;
11221119 }
1123-
1124- s. push_str ( "<code>" ) ;
1125- push ( & mut s, & mut text_length, code) ;
1126- s. push_str ( "</code>" ) ;
1120+ return r;
11271121 }
11281122 Event :: Start ( tag) => match tag {
1129- Tag :: Emphasis => s . push_str ( "<em> ") ,
1130- Tag :: Strong => s . push_str ( "< strong> ") ,
1131- Tag :: CodeBlock ( ..) => break ,
1123+ Tag :: Emphasis => buf . open_tag ( "em ") ,
1124+ Tag :: Strong => buf . open_tag ( " strong") ,
1125+ Tag :: CodeBlock ( ..) => return ControlFlow :: BREAK ,
11321126 _ => { }
11331127 } ,
11341128 Event :: End ( tag) => match tag {
1135- Tag :: Emphasis => s. push_str ( "</em>" ) ,
1136- Tag :: Strong => s. push_str ( "</strong>" ) ,
1137- Tag :: Paragraph => break ,
1138- Tag :: Heading ( ..) => break ,
1129+ Tag :: Emphasis | Tag :: Strong => buf. close_tag ( ) ,
1130+ Tag :: Paragraph | Tag :: Heading ( ..) => return ControlFlow :: BREAK ,
11391131 _ => { }
11401132 } ,
1141- Event :: HardBreak | Event :: SoftBreak => {
1142- if text_length + 1 >= length_limit {
1143- stopped_early = true ;
1144- break ;
1145- }
1146-
1147- push ( & mut s, & mut text_length, " " ) ;
1148- }
1133+ Event :: HardBreak | Event :: SoftBreak => buf. push ( " " ) ?,
11491134 _ => { }
1150- }
1151- }
1135+ } ;
1136+ ControlFlow :: CONTINUE
1137+ } ) ;
11521138
1153- ( s , stopped_early)
1139+ ( buf . finish ( ) , stopped_early)
11541140}
11551141
11561142/// Renders a shortened first paragraph of the given Markdown as a subset of Markdown,
0 commit comments