From 057b95bcbd9263915f575db1055a3110a3e099c3 Mon Sep 17 00:00:00 2001 From: Riccardo Mazzarini Date: Mon, 2 Mar 2026 17:57:58 +0100 Subject: [PATCH 1/2] Add failing test --- tests/slicing.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/slicing.rs b/tests/slicing.rs index c1a0505..fc258d2 100644 --- a/tests/slicing.rs +++ b/tests/slicing.rs @@ -220,3 +220,13 @@ fn rope_from_slice() { } } } + +/// Regression test for https://github.com/noib3/crop/issues/36 (different +/// input but exercises the same code paths). +#[test] +fn byte_slice_of_line() { + let rope = Rope::from("foobar"); + let ooba = rope.byte_slice(1..5); + assert_eq!(ooba.lines().next().unwrap().byte_slice(..), "ooba"); + assert_eq!(ooba.lines().next_back().unwrap().byte_slice(..), "ooba"); +} From 1e811fd9829e0e67cb5e89327e0b924dd8c90401 Mon Sep 17 00:00:00 2001 From: Riccardo Mazzarini Date: Mon, 2 Mar 2026 19:33:53 +0100 Subject: [PATCH 2/2] Fix --- src/tree/units.rs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/tree/units.rs b/src/tree/units.rs index 69fe594..77ab6e5 100644 --- a/src/tree/units.rs +++ b/src/tree/units.rs @@ -324,9 +324,7 @@ impl<'a, const N: usize, L: Leaf, M: UnitMetric> UnitsForward<'a, N, L, M> { match self.first_slice.take() { Some(slice) => { - self.yielded_in_leaf = - leaf.base_len() - slice.base_len(); - + self.yielded_in_leaf = self.base_start - offset; self.start_slice = slice; }, @@ -971,9 +969,10 @@ impl<'a, const N: usize, L: Leaf, M: DoubleEndedUnitMetric> match self.last_slice.take() { Some(slice) => { + let range_end = + self.base_start + self.base_remaining; self.yielded_in_leaf = - leaf.base_len() - slice.base_len(); - + offset + leaf.base_len() - range_end; self.end_slice = slice; }, @@ -1173,10 +1172,14 @@ impl<'a, const N: usize, L: Leaf, M: DoubleEndedUnitMetric> // First, check if the current leaf node is the root. If it is we're // done. if self.base_remaining == advance { + let offset = self.leaf_node.base_len() + - self.yielded_in_leaf + - end_slice.base_len(); + return ( TreeSlice { root: self.leaf_node, - offset: L::BaseMetric::zero(), + offset, summary: end_slice.summarize(), start_slice: end_slice, end_slice,