Avoid allocations when sanitizing TUI user text (#38214)

## Why

Sanitizing user messages and pasted text always allocated a new string, even
when the input contained no terminal control characters.

## What changed

- Return a `Cow<str>` from `sanitize_user_text` so clean borrowed input remains
  borrowed.
- Reuse owned input buffers when sanitization leaves the text intact or only
  removes content at the edges.
- Update transcript rendering, history cells, and paste handling to consume the
  borrowed-or-owned result without unnecessary cloning.

## Testing

Add sanitizer coverage for clean text, control sequences, Unicode control
characters, incomplete escape sequences, buffer reuse, and multi-fragment
output.

GitOrigin-RevId: 9e39cbc0d83c5791941b37a7eb4bb918c1a05f68
This commit is contained in:
Tamir Duberstein
2026-08-12 16:33:32 +00:00
committed by copyberry
parent 95aada11c4
commit 91d6f48992
4 changed files with 135 additions and 24 deletions
+2 -4
View File
@@ -224,7 +224,7 @@ fn render_markdown_transcript(cells: &[Arc<dyn HistoryCell>]) -> Result<String,
let lines = if let Some(user) = cell.as_any().downcast_ref::<UserHistoryCell>() {
let (message, _) =
crate::ide_context::extract_prompt_request_with_offset(&user.message);
let message = crate::history_cell::sanitize_user_text(message);
let message = crate::history_cell::sanitize_user_text(message.into());
let mut lines = raw_lines_from_source(&message);
let image_count = user.local_image_paths.len() + user.remote_image_urls.len();
let image_labels = (0..image_count)
@@ -273,9 +273,7 @@ fn render_markdown_transcript(cells: &[Arc<dyn HistoryCell>]) -> Result<String,
markdown.push_str(" ");
}
for span in line.spans {
markdown.push_str(&crate::history_cell::sanitize_user_text(
span.content.as_ref(),
));
markdown.push_str(&crate::history_cell::sanitize_user_text(span.content));
}
markdown.push('\n');
}