Return empty iterator for empty string

This commit is contained in:
Dirkjan Ochtman 2023-03-10 11:07:27 +01:00
parent fbe7b9ad75
commit c34f7d5515
2 changed files with 7 additions and 1 deletions

View File

@ -62,7 +62,11 @@ impl Segmenter {
input: &str,
search: &'a mut Search,
) -> Result<impl Iterator<Item = &'a str> + ExactSizeIterator, InvalidCharacter> {
SegmentState::new(Ascii::new(input)?, self, search).run();
let state = SegmentState::new(Ascii::new(input)?, self, search);
if !input.is_empty() {
state.run();
}
Ok(search.result.iter().map(|v| v.as_str()))
}

View File

@ -3,6 +3,8 @@ use crate::{Search, Segmenter};
/// Run a segmenter against the built-in test cases
pub fn run(segmenter: &Segmenter) {
let mut search = Search::default();
assert_eq!(segmenter.segment("", &mut search).unwrap().len(), 0);
let mut success = true;
for test in TEST_CASES.iter().copied() {
success &= assert_segments(test, &mut search, segmenter);