@@ -43,6 +43,17 @@ fn find_char_midpoint(chars: &str) -> usize {
43
43
. unwrap_or ( 0 )
44
44
}
45
45
46
+ /// Try to split a string near the midpoint.
47
+ #[ inline]
48
+ fn split ( chars : & str ) -> Option < ( & str , & str ) > {
49
+ let index = find_char_midpoint ( chars) ;
50
+ if index > 0 {
51
+ Some ( chars. split_at ( index) )
52
+ } else {
53
+ None
54
+ }
55
+ }
56
+
46
57
47
58
/// Parallel extensions for strings.
48
59
pub trait ParallelString {
@@ -376,14 +387,13 @@ impl<'ch> ParallelIterator for Chars<'ch> {
376
387
impl < ' ch > UnindexedProducer for CharsProducer < ' ch > {
377
388
type Item = char ;
378
389
379
- fn split ( mut self ) -> ( Self , Option < Self > ) {
380
- let index = find_char_midpoint ( self . chars ) ;
381
- if index > 0 {
382
- let ( left, right) = self . chars . split_at ( index) ;
383
- self . chars = left;
384
- ( self , Some ( CharsProducer { chars : right } ) )
385
- } else {
386
- ( self , None )
390
+ fn split ( self ) -> ( Self , Option < Self > ) {
391
+ match split ( self . chars ) {
392
+ Some ( ( left, right) ) => (
393
+ CharsProducer { chars : left } ,
394
+ Some ( CharsProducer { chars : right } ) ,
395
+ ) ,
396
+ None => ( self , None ) ,
387
397
}
388
398
}
389
399
@@ -422,15 +432,19 @@ impl<'ch> ParallelIterator for CharIndices<'ch> {
422
432
impl < ' ch > UnindexedProducer for CharIndicesProducer < ' ch > {
423
433
type Item = ( usize , char ) ;
424
434
425
- fn split ( mut self ) -> ( Self , Option < Self > ) {
426
- let index = find_char_midpoint ( self . chars ) ;
427
- if index > 0 {
428
- let ( left, right) = self . chars . split_at ( index) ;
429
- let right_index = self . index + index;
430
- self . chars = left;
431
- ( self , Some ( CharIndicesProducer { index : right_index, chars : right } ) )
432
- } else {
433
- ( self , None )
435
+ fn split ( self ) -> ( Self , Option < Self > ) {
436
+ match split ( self . chars ) {
437
+ Some ( ( left, right) ) => (
438
+ CharIndicesProducer {
439
+ chars : left,
440
+ ..self
441
+ } ,
442
+ Some ( CharIndicesProducer {
443
+ chars : right,
444
+ index : self . index + left. len ( ) ,
445
+ } ) ,
446
+ ) ,
447
+ None => ( self , None ) ,
434
448
}
435
449
}
436
450
@@ -468,14 +482,13 @@ impl<'ch> ParallelIterator for Bytes<'ch> {
468
482
impl < ' ch > UnindexedProducer for BytesProducer < ' ch > {
469
483
type Item = u8 ;
470
484
471
- fn split ( mut self ) -> ( Self , Option < Self > ) {
472
- let index = find_char_midpoint ( self . chars ) ;
473
- if index > 0 {
474
- let ( left, right) = self . chars . split_at ( index) ;
475
- self . chars = left;
476
- ( self , Some ( BytesProducer { chars : right } ) )
477
- } else {
478
- ( self , None )
485
+ fn split ( self ) -> ( Self , Option < Self > ) {
486
+ match split ( self . chars ) {
487
+ Some ( ( left, right) ) => (
488
+ BytesProducer { chars : left } ,
489
+ Some ( BytesProducer { chars : right } ) ,
490
+ ) ,
491
+ None => ( self , None ) ,
479
492
}
480
493
}
481
494
@@ -511,14 +524,13 @@ impl<'ch> ParallelIterator for EncodeUtf16<'ch> {
511
524
impl < ' ch > UnindexedProducer for EncodeUtf16Producer < ' ch > {
512
525
type Item = u16 ;
513
526
514
- fn split ( mut self ) -> ( Self , Option < Self > ) {
515
- let index = find_char_midpoint ( self . chars ) ;
516
- if index > 0 {
517
- let ( left, right) = self . chars . split_at ( index) ;
518
- self . chars = left;
519
- ( self , Some ( EncodeUtf16Producer { chars : right } ) )
520
- } else {
521
- ( self , None )
527
+ fn split ( self ) -> ( Self , Option < Self > ) {
528
+ match split ( self . chars ) {
529
+ Some ( ( left, right) ) => (
530
+ EncodeUtf16Producer { chars : left } ,
531
+ Some ( EncodeUtf16Producer { chars : right } ) ,
532
+ ) ,
533
+ None => ( self , None ) ,
522
534
}
523
535
}
524
536
@@ -733,15 +745,19 @@ impl<'ch, P: Pattern> ParallelIterator for Matches<'ch, P> {
733
745
impl < ' ch , ' pat , P : Pattern > UnindexedProducer for MatchesProducer < ' ch , ' pat , P > {
734
746
type Item = & ' ch str ;
735
747
736
- fn split ( mut self ) -> ( Self , Option < Self > ) {
737
- let index = find_char_midpoint ( self . chars ) ;
738
- if index > 0 {
739
- let ( left, right) = self . chars . split_at ( index) ;
740
- let pattern = self . pattern ;
741
- self . chars = left;
742
- ( self , Some ( MatchesProducer { chars : right, pattern : pattern } ) )
743
- } else {
744
- ( self , None )
748
+ fn split ( self ) -> ( Self , Option < Self > ) {
749
+ match split ( self . chars ) {
750
+ Some ( ( left, right) ) => (
751
+ MatchesProducer {
752
+ chars : left,
753
+ ..self
754
+ } ,
755
+ Some ( MatchesProducer {
756
+ chars : right,
757
+ ..self
758
+ } ) ,
759
+ ) ,
760
+ None => ( self , None ) ,
745
761
}
746
762
}
747
763
@@ -786,19 +802,20 @@ impl<'ch, P: Pattern> ParallelIterator for MatchIndices<'ch, P> {
786
802
impl < ' ch , ' pat , P : Pattern > UnindexedProducer for MatchIndicesProducer < ' ch , ' pat , P > {
787
803
type Item = ( usize , & ' ch str ) ;
788
804
789
- fn split ( mut self ) -> ( Self , Option < Self > ) {
790
- let index = find_char_midpoint ( self . chars ) ;
791
- if index > 0 {
792
- let ( left, right) = self . chars . split_at ( index) ;
793
- let right_producer = MatchIndicesProducer {
794
- index : self . index + index,
795
- chars : right,
796
- pattern : self . pattern ,
797
- } ;
798
- self . chars = left;
799
- ( self , Some ( right_producer) )
800
- } else {
801
- ( self , None )
805
+ fn split ( self ) -> ( Self , Option < Self > ) {
806
+ match split ( self . chars ) {
807
+ Some ( ( left, right) ) => (
808
+ MatchIndicesProducer {
809
+ chars : left,
810
+ ..self
811
+ } ,
812
+ Some ( MatchIndicesProducer {
813
+ chars : right,
814
+ index : self . index + left. len ( ) ,
815
+ ..self
816
+ } ) ,
817
+ ) ,
818
+ None => ( self , None ) ,
802
819
}
803
820
}
804
821
0 commit comments