Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ mod snippet;
/// This is important for untrusted input, as it can contain
/// invalid unicode sequences.
pub fn normalize_untrusted_str(s: &str) -> String {
renderer::normalize_whitespace(s)
renderer::normalize_whitespace(s).into_owned()
}

#[doc(inline)]
Expand Down
22 changes: 14 additions & 8 deletions src/renderer/render.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Most of this file is adapted from https://github.com/rust-lang/rust/blob/160905b6253f42967ed4aef4b98002944c7df24c/compiler/rustc_errors/src/emitter.rs

use alloc::borrow::{Cow, ToOwned};
use alloc::borrow::Cow;
use alloc::collections::BTreeMap;
use alloc::string::{String, ToString};
use alloc::{format, vec, vec::Vec};
Expand Down Expand Up @@ -225,9 +225,7 @@ pub(crate) fn render(renderer: &Renderer, groups: Report<'_>) -> String {
.render(&level, &renderer.stylesheet, &mut out_string)
.unwrap();
if g != group_len - 1 {
use core::fmt::Write;

writeln!(out_string).unwrap();
out_string.push('\n');
}
}
out_string
Expand Down Expand Up @@ -401,7 +399,7 @@ fn render_title(
});

let (title_str, style) = if title.allows_styling() {
(title.text().to_owned(), ElementStyle::NoStyle)
(Cow::Borrowed(title.text()), ElementStyle::NoStyle)
} else {
(normalize_whitespace(title.text()), title_element_style)
};
Expand Down Expand Up @@ -2513,17 +2511,25 @@ const OUTPUT_REPLACEMENTS: &[(char, &str)] = &[
('\u{2069}', "�"),
];

pub(crate) fn normalize_whitespace(s: &str) -> String {
pub(crate) fn normalize_whitespace(s: &str) -> Cow<'_, str> {
if !s
.chars()
.any(|user| OUTPUT_REPLACEMENTS.iter().any(|(bad, _)| user == *bad))
{
return Cow::Borrowed(s);
}

// Scan the input string for a character in the ordered table above.
// If it's present, replace it with its alternative string (it can be more than 1 char!).
// Otherwise, retain the input char.
s.chars().fold(String::with_capacity(s.len()), |mut s, c| {
let normalized = s.chars().fold(String::with_capacity(s.len()), |mut s, c| {
match OUTPUT_REPLACEMENTS.binary_search_by_key(&c, |(k, _)| *k) {
Ok(i) => s.push_str(OUTPUT_REPLACEMENTS[i].1),
_ => s.push(c),
}
s
})
});
Cow::Owned(normalized)
}

#[derive(Clone, Copy, Debug, PartialOrd, Ord, PartialEq, Eq)]
Expand Down
17 changes: 13 additions & 4 deletions src/renderer/styled_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ impl StyledBuffer {
stylesheet: &Stylesheet,
str: &mut String,
) -> Result<(), fmt::Error> {
let capacity = self.lines.iter().map(|line| line.len()).sum();
str.reserve(capacity);

for (i, line) in self.lines.iter().enumerate() {
let mut current_style = stylesheet.none;
for StyledChar { ch, style } in line {
Expand All @@ -57,11 +60,11 @@ impl StyledBuffer {
current_style = ch_style;
write!(str, "{current_style}")?;
}
write!(str, "{ch}")?;
str.push(*ch);
}
write!(str, "{current_style:#}")?;
if i != self.lines.len() - 1 {
writeln!(str)?;
str.push('\n');
}
}
Ok(())
Expand All @@ -82,8 +85,14 @@ impl StyledBuffer {
/// If `line` does not exist in our buffer, adds empty lines up to the given
/// and fills the last line with unstyled whitespace.
pub(crate) fn puts(&mut self, line: usize, col: usize, string: &str, style: ElementStyle) {
for (offset, c) in string.chars().enumerate() {
self.putc(line, col + offset, c, style);
self.ensure_lines(line);
let line = &mut self.lines[line];
for (offset, chr) in string.chars().enumerate() {
let col = col + offset;
if col >= line.len() {
line.resize(col + 1, StyledChar::SPACE);
}
line[col] = StyledChar::new(chr, style);
}
}

Expand Down
Loading