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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy