use iter::plumbing::*;
use iter::*;
use split_producer::*;
#[inline]
fn is_char_boundary(b: u8) -> bool {
(b as i8) >= -0x40
}
#[inline]
fn find_char_midpoint(chars: &str) -> usize {
let mid = chars.len() / 2;
let (left, right) = chars.as_bytes().split_at(mid);
match right.iter().cloned().position(is_char_boundary) {
Some(i) => mid + i,
None => left
.iter()
.cloned()
.rposition(is_char_boundary)
.unwrap_or(0),
}
}
#[inline]
fn split(chars: &str) -> Option<(&str, &str)> {
let index = find_char_midpoint(chars);
if index > 0 {
Some(chars.split_at(index))
} else {
None
}
}
pub trait ParallelString {
fn as_parallel_string(&self) -> &str;
fn par_chars(&self) -> Chars<'_> {
Chars {
chars: self.as_parallel_string(),
}
}
fn par_char_indices(&self) -> CharIndices<'_> {
CharIndices {
chars: self.as_parallel_string(),
}
}
fn par_bytes(&self) -> Bytes<'_> {
Bytes {
chars: self.as_parallel_string(),
}
}
fn par_encode_utf16(&self) -> EncodeUtf16<'_> {
EncodeUtf16 {
chars: self.as_parallel_string(),
}
}
fn par_split<P: Pattern>(&self, separator: P) -> Split<'_, P> {
Split::new(self.as_parallel_string(), separator)
}
fn par_split_terminator<P: Pattern>(&self, terminator: P) -> SplitTerminator<'_, P> {
SplitTerminator::new(self.as_parallel_string(), terminator)
}
fn par_lines(&self) -> Lines<'_> {
Lines(self.as_parallel_string())
}
fn par_split_whitespace(&self) -> SplitWhitespace<'_> {
SplitWhitespace(self.as_parallel_string())
}
fn par_matches<P: Pattern>(&self, pattern: P) -> Matches<'_, P> {
Matches {
chars: self.as_parallel_string(),
pattern,
}
}
fn par_match_indices<P: Pattern>(&self, pattern: P) -> MatchIndices<'_, P> {
MatchIndices {
chars: self.as_parallel_string(),
pattern,
}
}
}
impl ParallelString for str {
#[inline]
fn as_parallel_string(&self) -> &str {
self
}
}
mod private {
use iter::plumbing::Folder;
pub trait Pattern: Sized + Sync + Send {
private_decl! {}
fn find_in(&self, haystack: &str) -> Option<usize>;
fn rfind_in(&self, haystack: &str) -> Option<usize>;
fn is_suffix_of(&self, haystack: &str) -> bool;
fn fold_splits<'ch, F>(&self, haystack: &'ch str, folder: F, skip_last: bool) -> F
where
F: Folder<&'ch str>;
fn fold_matches<'ch, F>(&self, haystack: &'ch str, folder: F) -> F
where
F: Folder<&'ch str>;
fn fold_match_indices<'ch, F>(&self, haystack: &'ch str, folder: F, base: usize) -> F
where
F: Folder<(usize, &'ch str)>;
}
}
use self::private::Pattern;
impl Pattern for char {
private_impl! {}
#[inline]
fn find_in(&self, chars: &str) -> Option<usize> {
chars.find(*self)
}
#[inline]
fn rfind_in(&self, chars: &str) -> Option<usize> {
chars.rfind(*self)
}
#[inline]
fn is_suffix_of(&self, chars: &str) -> bool {
chars.ends_with(*self)
}
fn fold_splits<'ch, F>(&self, chars: &'ch str, folder: F, skip_last: bool) -> F
where
F: Folder<&'ch str>,
{
let mut split = chars.split(*self);
if skip_last {
split.next_back();
}
folder.consume_iter(split)
}
fn fold_matches<'ch, F>(&self, chars: &'ch str, folder: F) -> F
where
F: Folder<&'ch str>,
{
folder.consume_iter(chars.matches(*self))
}
fn fold_match_indices<'ch, F>(&self, chars: &'ch str, folder: F, base: usize) -> F
where
F: Folder<(usize, &'ch str)>,
{
folder.consume_iter(chars.match_indices(*self).map(move |(i, s)| (base + i, s)))
}
}
impl<FN: Sync + Send + Fn(char) -> bool> Pattern for FN {
private_impl! {}
fn find_in(&self, chars: &str) -> Option<usize> {
chars.find(self)
}
fn rfind_in(&self, chars: &str) -> Option<usize> {
chars.rfind(self)
}
fn is_suffix_of(&self, chars: &str) -> bool {
chars.ends_with(self)
}
fn fold_splits<'ch, F>(&self, chars: &'ch str, folder: F, skip_last: bool) -> F
where
F: Folder<&'ch str>,
{
let mut split = chars.split(self);
if skip_last {
split.next_back();
}
folder.consume_iter(split)
}
fn fold_matches<'ch, F>(&self, chars: &'ch str, folder: F) -> F
where
F: Folder<&'ch str>,
{
folder.consume_iter(chars.matches(self))
}
fn fold_match_indices<'ch, F>(&self, chars: &'ch str, folder: F, base: usize) -> F
where
F: Folder<(usize, &'ch str)>,
{
folder.consume_iter(chars.match_indices(self).map(move |(i, s)| (base + i, s)))
}
}
#[derive(Debug, Clone)]
pub struct Chars<'ch> {
chars: &'ch str,
}
struct CharsProducer<'ch> {
chars: &'ch str,
}
impl<'ch> ParallelIterator for Chars<'ch> {
type Item = char;
fn drive_unindexed<C>(self, consumer: C) -> C::Result
where
C: UnindexedConsumer<Self::Item>,
{
bridge_unindexed(CharsProducer { chars: self.chars }, consumer)
}
}
impl<'ch> UnindexedProducer for CharsProducer<'ch> {
type Item = char;
fn split(self) -> (Self, Option<Self>) {
match split(self.chars) {
Some((left, right)) => (
CharsProducer { chars: left },
Some(CharsProducer { chars: right }),
),
None => (self, None),
}
}
fn fold_with<F>(self, folder: F) -> F
where
F: Folder<Self::Item>,
{
folder.consume_iter(self.chars.chars())
}
}
#[derive(Debug, Clone)]
pub struct CharIndices<'ch> {
chars: &'ch str,
}
struct CharIndicesProducer<'ch> {
index: usize,
chars: &'ch str,
}
impl<'ch> ParallelIterator for CharIndices<'ch> {
type Item = (usize, char);
fn drive_unindexed<C>(self, consumer: C) -> C::Result
where
C: UnindexedConsumer<Self::Item>,
{
let producer = CharIndicesProducer {
index: 0,
chars: self.chars,
};
bridge_unindexed(producer, consumer)
}
}
impl<'ch> UnindexedProducer for CharIndicesProducer<'ch> {
type Item = (usize, char);
fn split(self) -> (Self, Option<Self>) {
match split(self.chars) {
Some((left, right)) => (
CharIndicesProducer {
chars: left,
..self
},
Some(CharIndicesProducer {
chars: right,
index: self.index + left.len(),
}),
),
None => (self, None),
}
}
fn fold_with<F>(self, folder: F) -> F
where
F: Folder<Self::Item>,
{
let base = self.index;
folder.consume_iter(self.chars.char_indices().map(move |(i, c)| (base + i, c)))
}
}
#[derive(Debug, Clone)]
pub struct Bytes<'ch> {
chars: &'ch str,
}
struct BytesProducer<'ch> {
chars: &'ch str,
}
impl<'ch> ParallelIterator for Bytes<'ch> {
type Item = u8;
fn drive_unindexed<C>(self, consumer: C) -> C::Result
where
C: UnindexedConsumer<Self::Item>,
{
bridge_unindexed(BytesProducer { chars: self.chars }, consumer)
}
}
impl<'ch> UnindexedProducer for BytesProducer<'ch> {
type Item = u8;
fn split(self) -> (Self, Option<Self>) {
match split(self.chars) {
Some((left, right)) => (
BytesProducer { chars: left },
Some(BytesProducer { chars: right }),
),
None => (self, None),
}
}
fn fold_with<F>(self, folder: F) -> F
where
F: Folder<Self::Item>,
{
folder.consume_iter(self.chars.bytes())
}
}
#[derive(Debug, Clone)]
pub struct EncodeUtf16<'ch> {
chars: &'ch str,
}
struct EncodeUtf16Producer<'ch> {
chars: &'ch str,
}
impl<'ch> ParallelIterator for EncodeUtf16<'ch> {
type Item = u16;
fn drive_unindexed<C>(self, consumer: C) -> C::Result
where
C: UnindexedConsumer<Self::Item>,
{
bridge_unindexed(EncodeUtf16Producer { chars: self.chars }, consumer)
}
}
impl<'ch> UnindexedProducer for EncodeUtf16Producer<'ch> {
type Item = u16;
fn split(self) -> (Self, Option<Self>) {
match split(self.chars) {
Some((left, right)) => (
EncodeUtf16Producer { chars: left },
Some(EncodeUtf16Producer { chars: right }),
),
None => (self, None),
}
}
fn fold_with<F>(self, folder: F) -> F
where
F: Folder<Self::Item>,
{
folder.consume_iter(self.chars.encode_utf16())
}
}
#[derive(Debug, Clone)]
pub struct Split<'ch, P: Pattern> {
chars: &'ch str,
separator: P,
}
impl<'ch, P: Pattern> Split<'ch, P> {
fn new(chars: &'ch str, separator: P) -> Self {
Split { chars, separator }
}
}
impl<'ch, P: Pattern> ParallelIterator for Split<'ch, P> {
type Item = &'ch str;
fn drive_unindexed<C>(self, consumer: C) -> C::Result
where
C: UnindexedConsumer<Self::Item>,
{
let producer = SplitProducer::new(self.chars, &self.separator);
bridge_unindexed(producer, consumer)
}
}
impl<'ch, P: Pattern> Fissile<P> for &'ch str {
fn length(&self) -> usize {
self.len()
}
fn midpoint(&self, end: usize) -> usize {
find_char_midpoint(&self[..end])
}
fn find(&self, separator: &P, start: usize, end: usize) -> Option<usize> {
separator.find_in(&self[start..end])
}
fn rfind(&self, separator: &P, end: usize) -> Option<usize> {
separator.rfind_in(&self[..end])
}
fn split_once(self, index: usize) -> (Self, Self) {
let (left, right) = self.split_at(index);
let mut right_iter = right.chars();
right_iter.next();
(left, right_iter.as_str())
}
fn fold_splits<F>(self, separator: &P, folder: F, skip_last: bool) -> F
where
F: Folder<Self>,
{
separator.fold_splits(self, folder, skip_last)
}
}
#[derive(Debug, Clone)]
pub struct SplitTerminator<'ch, P: Pattern> {
chars: &'ch str,
terminator: P,
}
struct SplitTerminatorProducer<'ch, 'sep, P: Pattern + 'sep> {
splitter: SplitProducer<'sep, P, &'ch str>,
skip_last: bool,
}
impl<'ch, P: Pattern> SplitTerminator<'ch, P> {
fn new(chars: &'ch str, terminator: P) -> Self {
SplitTerminator { chars, terminator }
}
}
impl<'ch, 'sep, P: Pattern + 'sep> SplitTerminatorProducer<'ch, 'sep, P> {
fn new(chars: &'ch str, terminator: &'sep P) -> Self {
SplitTerminatorProducer {
splitter: SplitProducer::new(chars, terminator),
skip_last: chars.is_empty() || terminator.is_suffix_of(chars),
}
}
}
impl<'ch, P: Pattern> ParallelIterator for SplitTerminator<'ch, P> {
type Item = &'ch str;
fn drive_unindexed<C>(self, consumer: C) -> C::Result
where
C: UnindexedConsumer<Self::Item>,
{
let producer = SplitTerminatorProducer::new(self.chars, &self.terminator);
bridge_unindexed(producer, consumer)
}
}
impl<'ch, 'sep, P: Pattern + 'sep> UnindexedProducer for SplitTerminatorProducer<'ch, 'sep, P> {
type Item = &'ch str;
fn split(mut self) -> (Self, Option<Self>) {
let (left, right) = self.splitter.split();
self.splitter = left;
let right = right.map(|right| {
let skip_last = self.skip_last;
self.skip_last = false;
SplitTerminatorProducer {
splitter: right,
skip_last,
}
});
(self, right)
}
fn fold_with<F>(self, folder: F) -> F
where
F: Folder<Self::Item>,
{
self.splitter.fold_with(folder, self.skip_last)
}
}
#[derive(Debug, Clone)]
pub struct Lines<'ch>(&'ch str);
impl<'ch> ParallelIterator for Lines<'ch> {
type Item = &'ch str;
fn drive_unindexed<C>(self, consumer: C) -> C::Result
where
C: UnindexedConsumer<Self::Item>,
{
self.0
.par_split_terminator('\n')
.map(|line| {
if line.ends_with('\r') {
&line[..line.len() - 1]
} else {
line
}
})
.drive_unindexed(consumer)
}
}
#[derive(Debug, Clone)]
pub struct SplitWhitespace<'ch>(&'ch str);
impl<'ch> ParallelIterator for SplitWhitespace<'ch> {
type Item = &'ch str;
fn drive_unindexed<C>(self, consumer: C) -> C::Result
where
C: UnindexedConsumer<Self::Item>,
{
self.0
.par_split(char::is_whitespace)
.filter(|string| !string.is_empty())
.drive_unindexed(consumer)
}
}
#[derive(Debug, Clone)]
pub struct Matches<'ch, P: Pattern> {
chars: &'ch str,
pattern: P,
}
struct MatchesProducer<'ch, 'pat, P: Pattern + 'pat> {
chars: &'ch str,
pattern: &'pat P,
}
impl<'ch, P: Pattern> ParallelIterator for Matches<'ch, P> {
type Item = &'ch str;
fn drive_unindexed<C>(self, consumer: C) -> C::Result
where
C: UnindexedConsumer<Self::Item>,
{
let producer = MatchesProducer {
chars: self.chars,
pattern: &self.pattern,
};
bridge_unindexed(producer, consumer)
}
}
impl<'ch, 'pat, P: Pattern> UnindexedProducer for MatchesProducer<'ch, 'pat, P> {
type Item = &'ch str;
fn split(self) -> (Self, Option<Self>) {
match split(self.chars) {
Some((left, right)) => (
MatchesProducer {
chars: left,
..self
},
Some(MatchesProducer {
chars: right,
..self
}),
),
None => (self, None),
}
}
fn fold_with<F>(self, folder: F) -> F
where
F: Folder<Self::Item>,
{
self.pattern.fold_matches(self.chars, folder)
}
}
#[derive(Debug, Clone)]
pub struct MatchIndices<'ch, P: Pattern> {
chars: &'ch str,
pattern: P,
}
struct MatchIndicesProducer<'ch, 'pat, P: Pattern + 'pat> {
index: usize,
chars: &'ch str,
pattern: &'pat P,
}
impl<'ch, P: Pattern> ParallelIterator for MatchIndices<'ch, P> {
type Item = (usize, &'ch str);
fn drive_unindexed<C>(self, consumer: C) -> C::Result
where
C: UnindexedConsumer<Self::Item>,
{
let producer = MatchIndicesProducer {
index: 0,
chars: self.chars,
pattern: &self.pattern,
};
bridge_unindexed(producer, consumer)
}
}
impl<'ch, 'pat, P: Pattern> UnindexedProducer for MatchIndicesProducer<'ch, 'pat, P> {
type Item = (usize, &'ch str);
fn split(self) -> (Self, Option<Self>) {
match split(self.chars) {
Some((left, right)) => (
MatchIndicesProducer {
chars: left,
..self
},
Some(MatchIndicesProducer {
chars: right,
index: self.index + left.len(),
..self
}),
),
None => (self, None),
}
}
fn fold_with<F>(self, folder: F) -> F
where
F: Folder<Self::Item>,
{
self.pattern
.fold_match_indices(self.chars, folder, self.index)
}
}