@@ -1376,7 +1376,7 @@ impl BoundedBacktracker {
1376
1376
for slot in slots. iter_mut ( ) {
1377
1377
* slot = None ;
1378
1378
}
1379
- cache. setup_search ( & self . nfa , input) ?;
1379
+ cache. setup_search ( & self , input) ?;
1380
1380
if input. is_done ( ) {
1381
1381
return Ok ( None ) ;
1382
1382
}
@@ -1750,11 +1750,11 @@ impl Cache {
1750
1750
/// in the BoundedBacktracker.
1751
1751
fn setup_search (
1752
1752
& mut self ,
1753
- nfa : & NFA ,
1753
+ re : & BoundedBacktracker ,
1754
1754
input : & Input < ' _ > ,
1755
1755
) -> Result < ( ) , MatchError > {
1756
1756
self . stack . clear ( ) ;
1757
- self . visited . setup_search ( nfa , input) ?;
1757
+ self . visited . setup_search ( re , input) ?;
1758
1758
Ok ( ( ) )
1759
1759
}
1760
1760
}
@@ -1836,23 +1836,9 @@ impl Visited {
1836
1836
true
1837
1837
}
1838
1838
1839
- /// Returns the capacity of this visited set in terms of the number of bits
1840
- /// it has to track (StateID, offset) pairs.
1841
- fn capacity ( & self ) -> usize {
1842
- self . bitset . len ( ) * Visited :: BLOCK_SIZE
1843
- }
1844
-
1845
1839
/// Reset this visited set to work with the given bounded backtracker.
1846
- fn reset ( & mut self , re : & BoundedBacktracker ) {
1847
- // The capacity given in the config is "bytes of heap memory," but the
1848
- // capacity we use here is "number of bits." So convert the capacity in
1849
- // bytes to the capacity in bits.
1850
- let capacity = 8 * re. get_config ( ) . get_visited_capacity ( ) ;
1851
- let blocks = div_ceil ( capacity, Visited :: BLOCK_SIZE ) ;
1852
- self . bitset . resize ( blocks, 0 ) ;
1853
- // N.B. 'stride' is set in 'setup_search', since it isn't known until
1854
- // we know the length of the haystack. (That is also when we return an
1855
- // error if the haystack is too big.)
1840
+ fn reset ( & mut self , _: & BoundedBacktracker ) {
1841
+ self . bitset . truncate ( 0 ) ;
1856
1842
}
1857
1843
1858
1844
/// Setup this visited set to work for a search using the given NFA
@@ -1861,7 +1847,7 @@ impl Visited {
1861
1847
/// result in panics or silently incorrect search behavior.
1862
1848
fn setup_search (
1863
1849
& mut self ,
1864
- nfa : & NFA ,
1850
+ re : & BoundedBacktracker ,
1865
1851
input : & Input < ' _ > ,
1866
1852
) -> Result < ( ) , MatchError > {
1867
1853
// Our haystack length is only the length of the span of the entire
@@ -1872,19 +1858,23 @@ impl Visited {
1872
1858
// search loop includes the position at input.end(). (And it does this
1873
1859
// because matches are delayed by one byte to account for look-around.)
1874
1860
self . stride = haylen + 1 ;
1875
- let capacity = match nfa. states ( ) . len ( ) . checked_mul ( self . stride ) {
1876
- None => return Err ( err ( ) ) ,
1877
- Some ( capacity) => capacity,
1878
- } ;
1879
- if capacity > self . capacity ( ) {
1861
+ let needed_capacity =
1862
+ match re. get_nfa ( ) . states ( ) . len ( ) . checked_mul ( self . stride ) {
1863
+ None => return Err ( err ( ) ) ,
1864
+ Some ( capacity) => capacity,
1865
+ } ;
1866
+ let max_capacity = 8 * re. get_config ( ) . get_visited_capacity ( ) ;
1867
+ if needed_capacity > max_capacity {
1880
1868
return Err ( err ( ) ) ;
1881
1869
}
1882
- // We only need to zero out our desired capacity, not our total
1883
- // capacity in this set.
1884
- let blocks = div_ceil ( capacity, Visited :: BLOCK_SIZE ) ;
1885
- for block in self . bitset . iter_mut ( ) . take ( blocks) {
1870
+ let needed_blocks = div_ceil ( needed_capacity, Visited :: BLOCK_SIZE ) ;
1871
+ self . bitset . truncate ( needed_blocks) ;
1872
+ for block in self . bitset . iter_mut ( ) {
1886
1873
* block = 0 ;
1887
1874
}
1875
+ if needed_blocks > self . bitset . len ( ) {
1876
+ self . bitset . resize ( needed_blocks, 0 ) ;
1877
+ }
1888
1878
Ok ( ( ) )
1889
1879
}
1890
1880
0 commit comments