Skip to content

Commit 04e73e5

Browse files
committed
Fix bug in MergeIter::size_hint
1 parent 0db3339 commit 04e73e5

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

regex-syntax/src/hir/interval.rs

+16-2
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,8 @@ impl Bound for char {
614614
}
615615
}
616616

617+
/// Iterator that, given a pair of sorted iterators, merges their items into
618+
/// a single sorted sequence
617619
struct MergeIter<I: Iterator> {
618620
left: I,
619621
right: I,
@@ -692,12 +694,24 @@ where
692694
}
693695

694696
fn size_hint(&self) -> (usize, Option<usize>) {
697+
use MergeIterState::*;
698+
695699
// Fundamentally this is a spicy concatenation, so add the sizes together
700+
let extra_element = match self.state {
701+
LeftExhausted => 0,
702+
RightExhausted => 0,
703+
LeftItem(_) => 1,
704+
RightItem(_) => 1,
705+
};
706+
696707
let (min1, max1) = self.left.size_hint();
697708
let (min2, max2) = self.right.size_hint();
698709

699-
let min = min1.saturating_add(min2);
700-
let max = max1.and_then(|max| max.checked_add(max2?));
710+
let min = min1.saturating_add(min2).saturating_add(extra_element);
711+
712+
let max = max1
713+
.and_then(|max| max.checked_add(max2?))
714+
.and_then(|max| max.checked_add(extra_element));
701715

702716
(min, max)
703717
}

0 commit comments

Comments
 (0)