Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make word separators and splitters more flexible #402

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
91 changes: 63 additions & 28 deletions src/word_splitters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,36 +190,71 @@ where
I: IntoIterator<Item = Word<'a>>,
WordSplit: WordSplitter,
{
words.into_iter().flat_map(move |word| {
let mut prev = 0;
let mut split_points = word_splitter.split_points(&word).into_iter();
std::iter::from_fn(move || {
if let Some(idx) = split_points.next() {
let need_hyphen = !word[..idx].ends_with('-');
let w = Word {
word: &word.word[prev..idx],
width: display_width(&word[prev..idx]),
whitespace: "",
penalty: if need_hyphen { "-" } else { "" },
};
prev = idx;
return Some(w);
}
words
.into_iter()
.flat_map(move |word| Fragments::new(word, word_splitter))
}

if prev < word.word.len() || prev == 0 {
let w = Word {
word: &word.word[prev..],
width: display_width(&word[prev..]),
whitespace: word.whitespace,
penalty: word.penalty,
};
prev = word.word.len() + 1;
return Some(w);
}
#[allow(missing_docs)]
#[derive(Debug)]
pub struct Fragments<'a, I: Iterator<Item = usize>> {
word: Word<'a>,
split_points: I,
prev: usize,
}

None
})
})
impl<'a> Fragments<'a, std::vec::IntoIter<usize>> {
#[allow(missing_docs)]
pub fn new(word: Word<'a>, word_splitter: &impl WordSplitter) -> Self {
let split_points = word_splitter.split_points(&word).into_iter();
Self {
word,
split_points,
prev: 0,
}
}
}

impl<'a, I: Iterator<Item = usize>> Fragments<'a, I> {
fn split(&self, range: std::ops::Range<usize>, keep_ending: bool) -> Word<'a> {
let word = &self.word.word[range];
Word {
word,
width: display_width(word),
whitespace: if keep_ending {
self.word.whitespace
} else {
""
},
penalty: if keep_ending {
self.word.penalty
} else if word.ends_with('-') {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This condition should be negated – fixed in the next commit.

"-"
} else {
""
},
}
}
}

impl<'a, I: Iterator<Item = usize>> Iterator for Fragments<'a, I> {
type Item = Word<'a>;

fn next(&mut self) -> Option<Self::Item> {
if let Some(idx) = self.split_points.next() {
let w = self.split(self.prev..idx, false);
self.prev = idx;
return Some(w);
}

if self.prev < self.word.word.len() || self.prev == 0 {
let w = self.split(self.prev..self.word.len(), true);
self.prev = self.word.word.len() + 1;
return Some(w);
}

None
}
}

#[cfg(test)]
Expand Down