1
1
mirror of https://github.com/wez/wezterm.git synced 2024-11-27 02:25:28 +03:00

Line::as_str() -> Cow<str>

Previously this would create a new String because it had to, but
with the clustered storage we may be able to simply reference the
existing string as a str reference, so allow for that.
This commit is contained in:
Wez Furlong 2022-07-23 12:10:13 -07:00
parent d5d161b510
commit c8b1b92e08
2 changed files with 15 additions and 8 deletions

View File

@ -447,6 +447,7 @@ impl_downcast!(Pane);
mod test {
use super::*;
use k9::snapshot;
use std::borrow::Cow;
use termwiz::surface::SEQ_ZERO;
struct FakePane {
@ -577,7 +578,7 @@ mod test {
let width = 20;
let physical_lines = physical_lines_from_text(text, width);
fn text_from_lines(lines: &[Line]) -> Vec<String> {
fn text_from_lines(lines: &[Line]) -> Vec<Cow<str>> {
lines.iter().map(|l| l.as_str()).collect::<Vec<_>>()
}
@ -603,7 +604,7 @@ mod test {
lines: physical_lines,
};
fn summarize_logical_lines(lines: &[LogicalLine]) -> Vec<(StableRowIndex, String)> {
fn summarize_logical_lines(lines: &[LogicalLine]) -> Vec<(StableRowIndex, Cow<str>)> {
lines
.iter()
.map(|l| (l.first_row, l.logical.as_str()))

View File

@ -7,6 +7,7 @@ use bitflags::bitflags;
use fixedbitset::FixedBitSet;
#[cfg(feature = "use_serde")]
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::borrow::Cow;
use std::ops::Range;
use std::sync::Arc;
use unicode_segmentation::UnicodeSegmentation;
@ -497,7 +498,7 @@ impl Line {
// use this as an opportunity to rebuild HAS_HYPERLINK, skip matching
// cells with existing non-implicit hyperlinks, and avoid matching
// text with zero-width cells.
let line = self.as_str();
let line = self.as_str().into_owned();
self.bits |= LineBits::SCANNED_IMPLICIT_HYPERLINKS;
self.bits &= !LineBits::HAS_IMPLICIT_HYPERLINKS;
@ -541,12 +542,17 @@ impl Line {
}
/// Recompose line into the corresponding utf8 string.
pub fn as_str(&self) -> String {
let mut s = String::new();
for cell in self.visible_cells() {
s.push_str(cell.str());
pub fn as_str(&self) -> Cow<str> {
match &self.cells {
CellStorage::V(_) => {
let mut s = String::new();
for cell in self.visible_cells() {
s.push_str(cell.str());
}
Cow::Owned(s)
}
CellStorage::C(cl) => Cow::Borrowed(&cl.text),
}
s
}
pub fn split_off(&mut self, idx: usize, seqno: SequenceNo) -> Self {