Skip to content

Commit

Permalink
Merge pull request #40 from jmuk/prev_boundary
Browse files Browse the repository at this point in the history
fix crashes on prev_boundary
  • Loading branch information
Manishearth authored Mar 20, 2018
2 parents c3db7a7 + 1f2f591 commit 6471399
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/grapheme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,9 @@ impl GraphemeCursor {
if self.offset == 0 {
return Ok(None);
}
if self.offset == chunk_start {
return Err(GraphemeIncomplete::PrevChunk);
}
let mut iter = chunk[..self.offset - chunk_start].chars().rev();
let mut ch = iter.next().unwrap();
loop {
Expand All @@ -652,6 +655,7 @@ impl GraphemeCursor {
self.decide(true);
} else {
self.resuming = true;
self.cat_after = Some(gr::grapheme_category(ch));
return Err(GraphemeIncomplete::PrevChunk);
}
}
Expand Down Expand Up @@ -682,3 +686,19 @@ fn test_grapheme_cursor_chunk_start_require_precontext() {
c.provide_context(&s[..1], 0);
assert_eq!(c.is_boundary(&s[1..], 1), Ok(false));
}

#[test]
fn test_grapheme_cursor_prev_boundary() {
let s = "abcd";
let mut c = GraphemeCursor::new(3, s.len(), true);
assert_eq!(c.prev_boundary(&s[2..], 2), Err(GraphemeIncomplete::PrevChunk));
assert_eq!(c.prev_boundary(&s[..2], 0), Ok(Some(2)));
}

#[test]
fn test_grapheme_cursor_prev_boundary_chunk_start() {
let s = "abcd";
let mut c = GraphemeCursor::new(2, s.len(), true);
assert_eq!(c.prev_boundary(&s[2..], 2), Err(GraphemeIncomplete::PrevChunk));
assert_eq!(c.prev_boundary(&s[..2], 0), Ok(Some(1)));
}

0 comments on commit 6471399

Please sign in to comment.