@@ -360,7 +360,10 @@ impl GraphemeCursor {
360
360
match self . state {
361
361
GraphemeState :: Regional => self . handle_regional ( chunk, chunk_start) ,
362
362
GraphemeState :: Emoji => self . handle_emoji ( chunk, chunk_start) ,
363
- _ => panic ! ( "invalid state" )
363
+ _ => if self . cat_before . is_none ( ) && self . offset == chunk. len ( ) + chunk_start {
364
+ let ch = chunk. chars ( ) . rev ( ) . next ( ) . unwrap ( ) ;
365
+ self . cat_before = Some ( gr:: grapheme_category ( ch) ) ;
366
+ } ,
364
367
}
365
368
}
366
369
@@ -406,6 +409,7 @@ impl GraphemeCursor {
406
409
return ;
407
410
}
408
411
self . pre_context_offset = Some ( chunk_start) ;
412
+ self . state = GraphemeState :: Regional ;
409
413
}
410
414
411
415
fn handle_emoji ( & mut self , chunk : & str , chunk_start : usize ) {
@@ -428,6 +432,7 @@ impl GraphemeCursor {
428
432
return ;
429
433
}
430
434
self . pre_context_offset = Some ( chunk_start) ;
435
+ self . state = GraphemeState :: Emoji ;
431
436
}
432
437
433
438
/// Determine whether the current cursor location is a grapheme cluster boundary.
@@ -664,6 +669,23 @@ impl GraphemeCursor {
664
669
}
665
670
}
666
671
672
+ fn test_grapheme_cursor_ris_precontext ( ) {
673
+ let s = "\u{1f1fa} \u{1f1f8} \u{1f1fa} \u{1f1f8} \u{1f1fa} \u{1f1f8} " ;
674
+ let mut c = GraphemeCursor :: new ( 8 , s. len ( ) , true ) ;
675
+ assert_eq ! ( c. is_boundary( & s[ 4 ..] , 4 ) , Err ( GraphemeIncomplete :: PreContext ( 4 ) ) ) ;
676
+ c. provide_context ( & s[ ..4 ] , 0 ) ;
677
+ assert_eq ! ( c. is_boundary( & s[ 4 ..] , 4 ) , Ok ( true ) ) ;
678
+ }
679
+
680
+ #[ test]
681
+ fn test_grapheme_cursor_chunk_start_require_precontext ( ) {
682
+ let s = "\r \n " ;
683
+ let mut c = GraphemeCursor :: new ( 1 , s. len ( ) , true ) ;
684
+ assert_eq ! ( c. is_boundary( & s[ 1 ..] , 1 ) , Err ( GraphemeIncomplete :: PreContext ( 1 ) ) ) ;
685
+ c. provide_context ( & s[ ..1 ] , 0 ) ;
686
+ assert_eq ! ( c. is_boundary( & s[ 1 ..] , 1 ) , Ok ( false ) ) ;
687
+ }
688
+
667
689
#[ test]
668
690
fn test_grapheme_cursor_prev_boundary ( ) {
669
691
let s = "abcd" ;
0 commit comments