Unify splitting function, and finish comment sentences

This commit is contained in:
René Kijewski 2025-06-15 22:05:24 +02:00
parent 94e7757078
commit b70de9cd73

View File

@ -617,7 +617,7 @@ impl<'a> fmt::Write for WordCountWriter<'a> {
} }
} }
/// Replaces line breaks in plain text with appropriate HTML /// Replaces line breaks in plain text with appropriate HTML.
/// ///
/// A single newline becomes an HTML line break `<br>` and a new line /// A single newline becomes an HTML line break `<br>` and a new line
/// followed by a blank line becomes a paragraph break `<p>`. /// followed by a blank line becomes a paragraph break `<p>`.
@ -733,9 +733,7 @@ impl<W: fmt::Write + ?Sized> fmt::Write for NewlineCountingFormatter<'_, W> {
if s.is_empty() { if s.is_empty() {
return Ok(()); return Ok(());
} }
for (has_eol, line) in split_lines(s) {
for line in s.split_inclusive('\n') {
let (has_eol, line) = strip_newline_suffix(line);
if !line.is_empty() { if !line.is_empty() {
match replace(&mut self.counter, if has_eol { 1 } else { 0 }) { match replace(&mut self.counter, if has_eol { 1 } else { 0 }) {
..=0 => {} ..=0 => {}
@ -751,7 +749,7 @@ impl<W: fmt::Write + ?Sized> fmt::Write for NewlineCountingFormatter<'_, W> {
} }
} }
/// Converts all newlines in a piece of plain text to HTML line breaks /// Converts all newlines in a piece of plain text to HTML line breaks.
/// ///
/// ``` /// ```
/// # #[cfg(feature = "code-in-doc")] { /// # #[cfg(feature = "code-in-doc")] {
@ -802,8 +800,10 @@ impl<S: FastWritable> FastWritable for Linebreaksbr<S> {
impl<W: fmt::Write + ?Sized> fmt::Write for LinebreaksbrFormatter<'_, W> { impl<W: fmt::Write + ?Sized> fmt::Write for LinebreaksbrFormatter<'_, W> {
fn write_str(&mut self, s: &str) -> fmt::Result { fn write_str(&mut self, s: &str) -> fmt::Result {
for line in s.split_inclusive('\n') { if s.is_empty() {
let (has_eol, line) = strip_newline_suffix(line); return Ok(());
}
for (has_eol, line) in split_lines(s) {
self.0.write_str(line)?; self.0.write_str(line)?;
if has_eol { if has_eol {
self.0.write_str("<br/>")?; self.0.write_str("<br/>")?;
@ -813,15 +813,16 @@ impl<W: fmt::Write + ?Sized> fmt::Write for LinebreaksbrFormatter<'_, W> {
} }
} }
/// Returns whether a newline suffix was stripped and the (maybe stripped) line. /// Splits the input at `/\r?\n/g``; returns whether a newline suffix was stripped and the
fn strip_newline_suffix(line: &str) -> (bool, &str) { /// (maybe stripped) line.
if let Some(line) = line.strip_suffix("\r\n") { fn split_lines(s: &str) -> impl Iterator<Item = (bool, &str)> {
(true, line) s.split_inclusive('\n').map(|line| {
} else if let Some(line) = line.strip_suffix('\n') { if let Some(line) = line.strip_suffix('\n') {
(true, line) (true, line.strip_suffix('\r').unwrap_or(line))
} else { } else {
(false, line) (false, line)
} }
})
} }
#[cfg(all(test, feature = "alloc"))] #[cfg(all(test, feature = "alloc"))]