8000 Merge pull request #40 from jmuk/prev_boundary · unicode-rs/unicode-segmentation@6471399 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6471399

Browse files
authored
Merge pull request #40 from jmuk/prev_boundary
fix crashes on prev_boundary
2 parents c3db7a7 + 1f2f591 commit 6471399

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
@@ -629,6 +629,9 @@ impl GraphemeCursor {
629629
if self.offset == 0 {
630630
return Ok(None);
631631
}
632+
if self.offset == chunk_start {
633+
return Err(GraphemeIncomplete::PrevChunk);
634+
}
632635
let mut iter = chunk[..self.offset - chunk_start].chars().rev();
633636
let mut ch = iter.next().unwrap();
634637
loop {
@@ -652,6 +655,7 @@ impl GraphemeCursor {
652655
self.decide(true);
653656
} else {
654657
self.resuming = true;
658+
self.cat_after = Some(gr::grapheme_category(ch));
655659
return Err(GraphemeIncomplete::PrevChunk);
656660
}
657661
}
@@ -682,3 +686,19 @@ fn test_grapheme_cursor_chunk_start_require_precontext() {
682686
c.provide_context(&s[..1], 0);
683687
assert_eq!(c.is_boundary(&s[1..], 1), Ok(false));
684688
}
689+
690+
#[test]
691+
fn test_grapheme_cursor_prev_boundary() {
692+
let s = "abcd";
693+
let mut c = GraphemeCursor::new(3, s.len(), true);
694+
assert_eq!(c.prev_boundary(&s[2..], 2), Err(GraphemeIncomplete::PrevChunk));
695+
assert_eq!(c.prev_boundary(&s[..2], 0), Ok(Some(2)));
696+
}
697+
698+
#[test]
699+
fn test_grapheme_cursor_prev_boundary_chunk_start() {
700+
let s = "abcd";
701+
let mut c = GraphemeCursor::new(2, s.len(), true);
702+
assert_eq!(c.prev_boundary(&s[2..], 2), Err(GraphemeIncomplete::PrevChunk));
703+
assert_eq!(c.prev_boundary(&s[..2], 0), Ok(Some(1)));
704+
}

0 commit comments

Comments
 (0)
0