@@ -49,7 +49,9 @@ impl<'a> Iterator for GraphemeIndices<'a> {
49
49
50
50
#[ inline]
51
51
fn next ( & mut self ) -> Option < ( usize , & ' a str ) > {
52
- self . iter . next ( ) . map ( |s| ( s. as_ptr ( ) as usize - self . start_offset , s) )
52
+ self . iter
53
+ . next ( )
54
+ . map ( |s| ( s. as_ptr ( ) as usize - self . start_offset , s) )
53
55
}
54
56
55
57
#[ inline]
@@ -61,7 +63,9 @@ impl<'a> Iterator for GraphemeIndices<'a> {
61
63
impl < ' a > DoubleEndedIterator for GraphemeIndices < ' a > {
62
64
#[ inline]
63
65
fn next_back ( & mut self ) -> Option < ( usize , & ' a str ) > {
64
- self . iter . next_back ( ) . map ( |s| ( s. as_ptr ( ) as usize - self . start_offset , s) )
66
+ self . iter
67
+ . next_back ( )
68
+ . map ( |s| ( s. as_ptr ( ) as usize - self . start_offset , s) )
65
69
}
66
70
}
67
71
@@ -126,7 +130,11 @@ impl<'a> DoubleEndedIterator for Graphemes<'a> {
126
130
if end == self . cursor . cur_cursor ( ) {
127
131
return None ;
128
132
}
129
- let prev = self . cursor_back . prev_boundary ( self . string , 0 ) . unwrap ( ) . unwrap ( ) ;
133
+ let prev = self
134
+ . cursor_back
135
+ . prev_boundary ( self . string , 0 )
136
+ . unwrap ( )
137
+ . unwrap ( ) ;
130
138
Some ( & self . string [ prev..end] )
131
139
}
132
140
}
@@ -143,7 +151,10 @@ pub fn new_graphemes<'b>(s: &'b str, is_extended: bool) -> Graphemes<'b> {
143
151
144
152
#[ inline]
145
153
pub fn new_grapheme_indices < ' b > ( s : & ' b str , is_extended : bool ) -> GraphemeIndices < ' b > {
146
- GraphemeIndices { start_offset : s. as_ptr ( ) as usize , iter : new_graphemes ( s, is_extended) }
154
+ GraphemeIndices {
155
+ start_offset : s. as_ptr ( ) as usize ,
156
+ iter : new_graphemes ( s, is_extended) ,
157
+ }
147
158
}
148
159
149
160
// maybe unify with PairResult?
@@ -215,7 +226,7 @@ pub enum GraphemeIncomplete {
215
226
/// current chunk, so the chunk after that is requested. This will only be
216
227
/// returned if the chunk ends before the `len` parameter provided on
217
228
/// creation of the cursor.
218
- NextChunk , // requesting chunk following the one given
229
+ NextChunk , // requesting chunk following the one given
219
230
220
231
/// An error returned when the chunk given does not contain the cursor position.
221
232
InvalidOffset ,
@@ -224,42 +235,42 @@ pub enum GraphemeIncomplete {
224
235
// An enum describing the result from lookup of a pair of categories.
225
236
#[ derive( PartialEq , Eq ) ]
226
237
enum PairResult {
227
- NotBreak , // definitely not a break
228
- Break , // definitely a break
229
- Extended , // a break iff not in extended mode
230
- Regional , // a break if preceded by an even number of RIS
231
- Emoji , // a break if preceded by emoji base and (Extend)*
238
+ NotBreak , // definitely not a break
239
+ Break , // definitely a break
240
+ Extended , // a break iff not in extended mode
241
+ Regional , // a break if preceded by an even number of RIS
242
+ Emoji , // a break if preceded by emoji base and (Extend)*
232
243
}
233
244
234
245
#[ inline]
235
246
fn check_pair ( before : GraphemeCat , after : GraphemeCat ) -> PairResult {
236
- use crate :: tables:: grapheme:: GraphemeCat :: * ;
237
247
use self :: PairResult :: * ;
248
+ use crate :: tables:: grapheme:: GraphemeCat :: * ;
238
249
match ( before, after) {
239
- ( GC_CR , GC_LF ) => NotBreak , // GB3
240
- ( GC_Control , _) => Break , // GB4
241
- ( GC_CR , _) => Break , // GB4
242
- ( GC_LF , _) => Break , // GB4
243
- ( _, GC_Control ) => Break , // GB5
244
- ( _, GC_CR ) => Break , // GB5
245
- ( _, GC_LF ) => Break , // GB5
246
- ( GC_L , GC_L ) => NotBreak , // GB6
247
- ( GC_L , GC_V ) => NotBreak , // GB6
248
- ( GC_L , GC_LV ) => NotBreak , // GB6
249
- ( GC_L , GC_LVT ) => NotBreak , // GB6
250
- ( GC_LV , GC_V ) => NotBreak , // GB7
251
- ( GC_LV , GC_T ) => NotBreak , // GB7
252
- ( GC_V , GC_V ) => NotBreak , // GB7
253
- ( GC_V , GC_T ) => NotBreak , // GB7
254
- ( GC_LVT , GC_T ) => NotBreak , // GB8
255
- ( GC_T , GC_T ) => NotBreak , // GB8
256
- ( _, GC_Extend ) => NotBreak , // GB9
257
- ( _, GC_ZWJ ) => NotBreak , // GB9
258
- ( _, GC_SpacingMark ) => Extended , // GB9a
259
- ( GC_Prepend , _) => Extended , // GB9b
260
- ( GC_ZWJ , GC_Extended_Pictographic ) => Emoji , // GB11
261
- ( GC_Regional_Indicator , GC_Regional_Indicator ) => Regional , // GB12, GB13
262
- ( _, _) => Break , // GB999
250
+ ( GC_CR , GC_LF ) => NotBreak , // GB3
251
+ ( GC_Control , _) => Break , // GB4
252
+ ( GC_CR , _) => Break , // GB4
253
+ ( GC_LF , _) => Break , // GB4
254
+ ( _, GC_Control ) => Break , // GB5
255
+ ( _, GC_CR ) => Break , // GB5
256
+ ( _, GC_LF ) => Break , // GB5
257
+ ( GC_L , GC_L ) => NotBreak , // GB6
258
+ ( GC_L , GC_V ) => NotBreak , // GB6
259
+ ( GC_L , GC_LV ) => NotBreak , // GB6
260
+ ( GC_L , GC_LVT ) => NotBreak , // GB6
261
+ ( GC_LV , GC_V ) => NotBreak , // GB7
262
+ ( GC_LV , GC_T ) => NotBreak , // GB7
263
+ ( GC_V , GC_V ) => NotBreak , // GB7
264
+ ( GC_V , GC_T ) => NotBreak , // GB7
265
+ ( GC_LVT , GC_T ) => NotBreak , // GB8
266
+ ( GC_T , GC_T ) => NotBreak , // GB8
267
+ ( _, GC_Extend ) => NotBreak , // GB9
268
+ ( _, GC_ZWJ ) => NotBreak , // GB9
269
+ ( _, GC_SpacingMark ) => Extended , // GB9a
270
+ ( GC_Prepend , _) => Extended , // GB9b
271
+ ( GC_ZWJ , GC_Extended_Pictographic ) => Emoji , // GB11
272
+ ( GC_Regional_Indicator , GC_Regional_Indicator ) => Regional , // GB12, GB13
273
+ ( _, _) => Break , // GB999
263
274
}
264
275
}
265
276
@@ -397,17 +408,19 @@ impl GraphemeCursor {
397
408
if self . is_extended && chunk_start + chunk. len ( ) == self . offset {
398
409
let ch = chunk. chars ( ) . rev ( ) . next ( ) . unwrap ( ) ;
399
410
if self . grapheme_category ( ch) == gr:: GC_Prepend {
400
- self . decide ( false ) ; // GB9b
411
+ self . decide ( false ) ; // GB9b
401
412
return ;
402
413
}
403
414
}
404
415
match self . state {
405
416
GraphemeState :: Regional => self . handle_regional ( chunk, chunk_start) ,
406
417
GraphemeState :: Emoji => self . handle_emoji ( chunk, chunk_start) ,
407
- _ => if self . cat_before . is_none ( ) && self . offset == chunk. len ( ) + chunk_start {
408
- let ch = chunk. chars ( ) . rev ( ) . next ( ) . unwrap ( ) ;
409
- self . cat_before = Some ( self . grapheme_category ( ch) ) ;
410
- } ,
418
+ _ => {
419
+ if self . cat_before . is_none ( ) && self . offset == chunk. len ( ) + chunk_start {
420
+ let ch = chunk. chars ( ) . rev ( ) . next ( ) . unwrap ( ) ;
421
+ self . cat_before = Some ( self . grapheme_category ( ch) ) ;
422
+ }
423
+ }
411
424
}
412
425
}
413
426
@@ -515,17 +528,21 @@ impl GraphemeCursor {
515
528
/// cursor.set_cursor(12);
516
529
/// assert_eq!(cursor.is_boundary(flags, 0), Ok(false));
517
530
/// ```
518
- pub fn is_boundary ( & mut self , chunk : & str , chunk_start : usize ) -> Result < bool , GraphemeIncomplete > {
531
+ pub fn is_boundary (
532
+ & mut self ,
533
+ chunk : & str ,
534
+ chunk_start : usize ,
535
+ ) -> Result < bool , GraphemeIncomplete > {
519
536
use crate :: tables:: grapheme as gr;
520
537
if self . state == GraphemeState :: Break {
521
- return Ok ( true )
538
+ return Ok ( true ) ;
522
539
}
523
540
if self . state == GraphemeState :: NotBreak {
524
- return Ok ( false )
541
+ return Ok ( false ) ;
525
542
}
526
543
if self . offset < chunk_start || self . offset >= chunk_start + chunk. len ( ) {
527
544
if self . offset > chunk_start + chunk. len ( ) || self . cat_after . is_none ( ) {
528
- return Err ( GraphemeIncomplete :: InvalidOffset )
545
+ return Err ( GraphemeIncomplete :: InvalidOffset ) ;
529
546
}
530
547
}
531
548
if let Some ( pre_context_offset) = self . pre_context_offset {
@@ -606,7 +623,11 @@ impl GraphemeCursor {
606
623
/// assert_eq!(cursor.next_boundary(&s[2..4], 2), Ok(Some(4)));
607
624
/// assert_eq!(cursor.next_boundary(&s[2..4], 2), Ok(None));
608
625
/// ```
609
- pub fn next_boundary ( & mut self , chunk : & str , chunk_start : usize ) -> Result < Option < usize > , GraphemeIncomplete > {
626
+ pub fn next_boundary (
627
+ & mut self ,
628
+ chunk : & str ,
629
+ chunk_start : usize ,
630
+ ) -> Result < Option < usize > , GraphemeIncomplete > {
610
631
if self . offset == self . len {
611
632
return Ok ( None ) ;
612
633
}
@@ -681,7 +702,11 @@ impl GraphemeCursor {
681
702
/// assert_eq!(cursor.prev_boundary(&s[0..2], 0), Ok(Some(0)));
682
703
/// assert_eq!(cursor.prev_boundary(&s[0..2], 0), Ok(None));
683
704
/// ```
684
- pub fn prev_boundary ( & mut self , chunk : & str , chunk_start : usize ) -> Result < Option < usize > , GraphemeIncomplete > {
705
+ pub fn prev_boundary (
706
+ & mut self ,
707
+ chunk : & str ,
708
+ chunk_start : usize ,
709
+ ) -> Result < Option < usize > , GraphemeIncomplete > {
685
710
if self . offset == 0 {
686
711
return Ok ( None ) ;
687
712
}
@@ -702,7 +727,11 @@ impl GraphemeCursor {
702
727
self . cat_after = self . cat_before . take ( ) ;
703
728
self . state = GraphemeState :: Unknown ;
704
729
if let Some ( ris_count) = self . ris_count {
705
- self . ris_count = if ris_count > 0 { Some ( ris_count - 1 ) } else { None } ;
730
+ self . ris_count = if ris_count > 0 {
731
+ Some ( ris_count - 1 )
732
+ } else {
733
+ None
734
+ } ;
706
735
}
707
736
if let Some ( prev_ch) = iter. next ( ) {
708
737
ch = prev_ch;
@@ -729,7 +758,10 @@ impl GraphemeCursor {
729
758
fn test_grapheme_cursor_ris_precontext ( ) {
730
759
let s = "\u{1f1fa} \u{1f1f8} \u{1f1fa} \u{1f1f8} \u{1f1fa} \u{1f1f8} " ;
731
760
let mut c = GraphemeCursor :: new ( 8 , s. len ( ) , true ) ;
732
- assert_eq ! ( c. is_boundary( & s[ 4 ..] , 4 ) , Err ( GraphemeIncomplete :: PreContext ( 4 ) ) ) ;
761
+ assert_eq ! (
762
+ c. is_boundary( & s[ 4 ..] , 4 ) ,
763
+ Err ( GraphemeIncomplete :: PreContext ( 4 ) )
764
+ ) ;
733
765
c. provide_context ( & s[ ..4 ] , 0 ) ;
734
766
assert_eq ! ( c. is_boundary( & s[ 4 ..] , 4 ) , Ok ( true ) ) ;
735
767
}
@@ -738,7 +770,10 @@ fn test_grapheme_cursor_ris_precontext() {
738
770
fn test_grapheme_cursor_chunk_start_require_precontext ( ) {
739
771
let s = "\r \n " ;
740
772
let mut c = GraphemeCursor :: new ( 1 , s. len ( ) , true ) ;
741
- assert_eq ! ( c. is_boundary( & s[ 1 ..] , 1 ) , Err ( GraphemeIncomplete :: PreContext ( 1 ) ) ) ;
773
+ assert_eq ! (
774
+ c. is_boundary( & s[ 1 ..] , 1 ) ,
775
+ Err ( GraphemeIncomplete :: PreContext ( 1 ) )
776
+ ) ;
742
777
c. provide_context ( & s[ ..1 ] , 0 ) ;
743
778
assert_eq ! ( c. is_boundary( & s[ 1 ..] , 1 ) , Ok ( false ) ) ;
744
779
}
@@ -747,14 +782,20 @@ fn test_grapheme_cursor_chunk_start_require_precontext() {
747
782
fn test_grapheme_cursor_prev_boundary ( ) {
748
783
let s = "abcd" ;
749
784
let mut c = GraphemeCursor :: new ( 3 , s. len ( ) , true ) ;
750
- assert_eq ! ( c. prev_boundary( & s[ 2 ..] , 2 ) , Err ( GraphemeIncomplete :: PrevChunk ) ) ;
785
+ assert_eq ! (
786
+ c. prev_boundary( & s[ 2 ..] , 2 ) ,
787
+ Err ( GraphemeIncomplete :: PrevChunk )
788
+ ) ;
751
789
assert_eq ! ( c. prev_boundary( & s[ ..2 ] , 0 ) , Ok ( Some ( 2 ) ) ) ;
752
790
}
753
791
754
792
#[ test]
755
793
fn test_grapheme_cursor_prev_boundary_chunk_start ( ) {
756
794
let s = "abcd" ;
757
795
let mut c = GraphemeCursor :: new ( 2 , s. len ( ) , true ) ;
758
- assert_eq ! ( c. prev_boundary( & s[ 2 ..] , 2 ) , Err ( GraphemeIncomplete :: PrevChunk ) ) ;
796
+ assert_eq ! (
797
+ c. prev_boundary( & s[ 2 ..] , 2 ) ,
798
+ Err ( GraphemeIncomplete :: PrevChunk )
799
+ ) ;
759
800
assert_eq ! ( c. prev_boundary( & s[ ..2 ] , 0 ) , Ok ( Some ( 1 ) ) ) ;
760
801
}
0 commit comments