Skip to content

Commit 733c6f6

Browse files
committed
Make string splitting more uniform
1 parent f877b32 commit 733c6f6

File tree

1 file changed

+72
-55
lines changed

1 file changed

+72
-55
lines changed

src/str.rs

Lines changed: 72 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,17 @@ fn find_char_midpoint(chars: &str) -> usize {
4343
.unwrap_or(0)
4444
}
4545

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+
4657

4758
/// Parallel extensions for strings.
4859
pub trait ParallelString {
@@ -376,14 +387,13 @@ impl<'ch> ParallelIterator for Chars<'ch> {
376387
impl<'ch> UnindexedProducer for CharsProducer<'ch> {
377388
type Item = char;
378389

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),
387397
}
388398
}
389399

@@ -422,15 +432,19 @@ impl<'ch> ParallelIterator for CharIndices<'ch> {
422432
impl<'ch> UnindexedProducer for CharIndicesProducer<'ch> {
423433
type Item = (usize, char);
424434

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),
434448
}
435449
}
436450

@@ -468,14 +482,13 @@ impl<'ch> ParallelIterator for Bytes<'ch> {
468482
impl<'ch> UnindexedProducer for BytesProducer<'ch> {
469483
type Item = u8;
470484

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),
479492
}
480493
}
481494

@@ -511,14 +524,13 @@ impl<'ch> ParallelIterator for EncodeUtf16<'ch> {
511524
impl<'ch> UnindexedProducer for EncodeUtf16Producer<'ch> {
512525
type Item = u16;
513526

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),
522534
}
523535
}
524536

@@ -733,15 +745,19 @@ impl<'ch, P: Pattern> ParallelIterator for Matches<'ch, P> {
733745
impl<'ch, 'pat, P: Pattern> UnindexedProducer for MatchesProducer<'ch, 'pat, P> {
734746
type Item = &'ch str;
735747

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),
745761
}
746762
}
747763

@@ -786,19 +802,20 @@ impl<'ch, P: Pattern> ParallelIterator for MatchIndices<'ch, P> {
786802
impl<'ch, 'pat, P: Pattern> UnindexedProducer for MatchIndicesProducer<'ch, 'pat, P> {
787803
type Item = (usize, &'ch str);
788804

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),
802819
}
803820
}
804821

0 commit comments

Comments
 (0)