8000 fix crashes on prev_boundary · unicode-rs/unicode-segmentation@fb5d7b6 · GitHub
[go: up one dir, main page]

Skip to content

Commit fb5d7b6

Browse files
committed
fix crashes on prev_boundary
Fixes #38, #39
1 parent 611eaa7 commit fb5d7b6

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/grapheme.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,9 @@ impl GraphemeCursor {
624624
if self.offset == 0 {
625625
return Ok(None);
626626
}
627+
if self.offset == chunk_start {
628+
return Err(GraphemeIncomplete::PrevChunk);
629+
}
627630
let mut iter = chunk[..self.offset - chunk_start].chars().rev();
628631
let mut ch = iter.next().unwrap();
629632
loop {
@@ -647,6 +650,7 @@ impl GraphemeCursor {
647650
self.decide(true);
648651
} else {
649652
self.resuming = true;
653+
self.cat_after = Some(gr::grapheme_category(ch));
650654
return Err(GraphemeIncomplete::PrevChunk);
651655
}
652656
}
@@ -659,3 +663,19 @@ impl GraphemeCursor {
659663
}
660664
}
661665
}
666+
667+
#[test]
668+
fn test_grapheme_cursor_prev_boundary() {
669+
let s = "abcd";
670+
let mut c = GraphemeCursor::new(3, s.len(), true);
671+
assert_eq!(c.prev_boundary(&s[2..], 2), Err(GraphemeIncomplete::PrevChunk));
672+
assert_eq!(c.prev_boundary(&s[..2], 0), Ok(Some(2)));
673+
}
674+
675+
#[test]
676+
fn test_grapheme_cursor_prev_boundary_chunk_start() {
677+
let s = "abcd";
678+
let mut c = GraphemeCursor::new(2, s.len(), true);
679+
assert_eq!(c.prev_boundary(&s[2..], 2), Err(GraphemeIncomplete::PrevChunk));
680+
assert_eq!(c.prev_boundary(&s[..2], 0), Ok(Some(1)));
681+
}

0 commit comments

Comments
 (0)
0