Simplify top-level loop

This commit is contained in:
Dirkjan Ochtman 2020-11-26 10:18:44 +01:00
parent 2f9cb95b5c
commit b7daaff47a
1 changed files with 7 additions and 12 deletions

View File

@ -97,24 +97,19 @@ impl<'a> SegmentState<'a> {
/// Returns a list of words that is the best segmentation of `text` /// Returns a list of words that is the best segmentation of `text`
fn run(mut self) { fn run(mut self) {
let (mut start, mut end) = (0, 0); let (mut start, mut end) = (0, 0);
loop { while end < self.text.len() {
end = self.text.len().min(end + SEGMENT_SIZE); end = self.text.len().min(end + SEGMENT_SIZE);
let prefix = &self.text[start..end]; let prefix = &self.text[start..end];
if self.search(0, &prefix, None).1 { if !self.search(0, &prefix, None).1 {
let splits = &self.best[0]; continue;
for split in &splits[..splits.len().saturating_sub(5)] {
self.result.push(self.text[start..start + split].into());
start += split;
}
} }
if end == self.text.len() { let mut splits = &self.best[0][..];
break; if end < self.text.len() {
} splits = &splits[..splits.len().saturating_sub(5)];
} }
if self.search(0, &self.text[start..], None).1 { for split in splits {
for split in &self.best[0] {
self.result.push(self.text[start..start + split].into()); self.result.push(self.text[start..start + split].into());
start += split; start += split;
} }