Skip to content

Commit

Permalink
Simplify text split_str_by_separator
Browse files Browse the repository at this point in the history
  • Loading branch information
benbrandt committed May 5, 2024
1 parent 9676ab1 commit c07e5ed
Showing 1 changed file with 21 additions and 27 deletions.
48 changes: 21 additions & 27 deletions src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,33 +124,27 @@ impl TextLevel {
let mut final_match = false;
separator_ranges
.batching(move |it| {
loop {
match it.next() {
// If we've hit the end, actually return None
None if final_match => return None,
// First time we hit None, return the final section of the text
None => {
final_match = true;
return text.get(cursor..).map(|t| Either::Left(once((cursor, t))));
}
// Return text preceding match + the match
Some(range) => {
if range.start < cursor {
continue;
}

let offset = cursor;
let prev_section = text
.get(offset..range.start)
.expect("invalid character sequence");
let separator = text
.get(range.start..range.end)
.expect("invalid character sequence");
cursor = range.end;
return Some(Either::Right(
[(offset, prev_section), (range.start, separator)].into_iter(),
));
}
match it.next() {
// If we've hit the end, actually return None
None if final_match => None,
// First time we hit None, return the final section of the text
None => {
final_match = true;
return text.get(cursor..).map(|t| Either::Left(once((cursor, t))));
}
// Return text preceding match + the match
Some(range) => {
let offset = cursor;
let prev_section = text
.get(offset..range.start)
.expect("invalid character sequence");
let separator = text
.get(range.start..range.end)
.expect("invalid character sequence");
cursor = range.end;
Some(Either::Right(
[(offset, prev_section), (range.start, separator)].into_iter(),
))
}
}
})
Expand Down

0 comments on commit c07e5ed

Please sign in to comment.