diff options
author | Matthias Deiml | 2022-08-04 05:44:43 +0000 |
---|---|---|
committer | GitHub | 2022-08-04 05:44:43 +0000 |
commit | 0ee20611022b5a7bec727d2159ec7c6b36e956b6 (patch) | |
tree | c6140d3657184c19dc4a2a8bef47a643b4c247a8 /helix-core | |
parent | afd292e3b9616ea9098ca3a7d24a730508b84809 (diff) |
Avoid copying fragments (#3136)
* Avoid copying fragments
* Add slice / slices method
* Better documentation for fragment and slice methods
Diffstat (limited to 'helix-core')
-rw-r--r-- | helix-core/src/selection.rs | 20 |
1 files changed, 19 insertions, 1 deletions
diff --git a/helix-core/src/selection.rs b/helix-core/src/selection.rs index 83bab5e3..59bd736e 100644 --- a/helix-core/src/selection.rs +++ b/helix-core/src/selection.rs @@ -222,9 +222,23 @@ impl Range { // groupAt + /// Returns the text inside this range given the text of the whole buffer. + /// + /// The returned `Cow` is a reference if the range of text is inside a single + /// chunk of the rope. Otherwise a copy of the text is returned. Consider + /// using `slice` instead if you do not need a `Cow` or `String` to avoid copying. #[inline] pub fn fragment<'a, 'b: 'a>(&'a self, text: RopeSlice<'b>) -> Cow<'b, str> { - text.slice(self.from()..self.to()).into() + self.slice(text).into() + } + + /// Returns the text inside this range given the text of the whole buffer. + /// + /// The returned value is a reference to the passed slice. This method never + /// copies any contents. + #[inline] + pub fn slice<'a, 'b: 'a>(&'a self, text: RopeSlice<'b>) -> RopeSlice<'b> { + text.slice(self.from()..self.to()) } //-------------------------------- @@ -548,6 +562,10 @@ impl Selection { self.ranges.iter().map(move |range| range.fragment(text)) } + pub fn slices<'a>(&'a self, text: RopeSlice<'a>) -> impl Iterator<Item = RopeSlice> + 'a { + self.ranges.iter().map(move |range| range.slice(text)) + } + #[inline(always)] pub fn iter(&self) -> std::slice::Iter<'_, Range> { self.ranges.iter() |