Skip to content

Commit af51af2

Browse files
evanrelfnoib3
andauthored
Fix is_grapheme_boundary panic on empty rope (#24)
`unwrap()` at `src/rope/utils.rs:172`: https://github.com/nomad/crop/blob/ae8ac1669e1024df42b18ec3e26c3f8d933cebc2/src/rope/utils.rs#L172 If the rope is empty, the `chunks` iterator won't yield any items. We can avoid hitting this case, and do less work, if we recognize that an empty rope has one valid grapheme boundary, at byte offset 0. --------- Co-authored-by: Riccardo Mazzarini <[email protected]>
1 parent ae8ac16 commit af51af2

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

src/rope/utils.rs

+4
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,10 @@ pub(super) fn is_grapheme_boundary(
158158

159159
debug_assert!(byte_offset <= byte_len);
160160

161+
if byte_len == 0 {
162+
return true;
163+
}
164+
161165
let mut cursor = GraphemeCursor::new(0, byte_len, true);
162166
cursor.set_cursor(byte_offset);
163167

tests/graphemes.rs

+6
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,9 @@ fn graphemes_is_boundary_out_of_bounds() {
138138
let r = Rope::from("🇷🇸🇮🇴");
139139
assert!(r.is_grapheme_boundary(17));
140140
}
141+
142+
#[cfg(feature = "graphemes")]
143+
#[test]
144+
fn graphemes_is_boundary_empty_rope() {
145+
assert!(Rope::new().is_grapheme_boundary(0));
146+
}

0 commit comments

Comments
 (0)