Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix crashes on prev_boundary #40

Merged
merged 3 commits into from
Mar 20, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)));
}