@@ -1185,7 +1185,10 @@ impl NFA {
1185
1185
+ self . 0 . states . len ( ) * size_of :: < State > ( )
1186
1186
+ self . 0 . start_pattern . len ( ) * size_of :: < StateID > ( )
1187
1187
+ self . 0 . group_info . memory_usage ( )
1188
- + self . 0 . start_look_behind . len ( ) * size_of :: < StateID > ( )
1188
+ + self . 0 . lookbehinds . iter ( )
1189
+ . map ( |b|
1190
+ b. try_fold ( 0 , & |acc, _| Some ( acc + 1 ) ) . unwrap ( )
1191
+ ) . sum :: < usize > ( ) * size_of :: < LookBehindTree > ( )
1189
1192
+ self . 0 . memory_extra
1190
1193
}
1191
1194
}
@@ -1319,23 +1322,30 @@ impl LookBehindTree {
1319
1322
}
1320
1323
1321
1324
/// Calls `fun` on this look-behind tree and all of its children in pre-order.
1322
- /// `fun` should return `true ` if the traversal should continue and `false `
1325
+ /// `fun` should return `Some ` if the traversal should continue and `None `
1323
1326
/// if it should stop.
1324
1327
///
1325
- /// The return value indicates whether the traversal was at any point stopped.
1326
- pub fn preorder ( & self , fun : & impl Fn ( & LookBehindTree ) -> bool ) -> bool {
1327
- if !fun ( self ) {
1328
- return false ;
1329
- }
1330
- for child in & self . children {
1331
- if !child. preorder ( fun) {
1332
- return false ;
1333
- }
1328
+ /// The return value is the fold of all `Some`s, or `None` if at any point `None`
1329
+ /// was returned.
1330
+ pub fn try_fold < A > (
1331
+ & self ,
1332
+ acc : A ,
1333
+ fun : & impl Fn ( A , & LookBehindTree ) -> Option < A > ,
1334
+ ) -> Option < A > {
1335
+ if let Some ( acc) = fun ( acc, self ) {
1336
+ self . children
1337
+ . iter ( )
1338
+ . try_fold ( acc, |acc, child| child. try_fold ( acc, fun) )
1339
+ } else {
1340
+ None
1334
1341
}
1335
- true
1336
1342
}
1337
1343
1338
- /// Like [`preorder`], but allows mutating the nodes.
1344
+ /// Calls `fun` on this look-behind tree and all of its children in pre-order.
1345
+ /// `fun` should return `true` if the traversal should continue and `false`
1346
+ /// if it should stop.
1347
+ ///
1348
+ /// The return value indicates whether the traversal was at any point stopped.
1339
1349
pub fn preorder_mut (
1340
1350
& mut self ,
1341
1351
fun : & impl Fn ( & mut LookBehindTree ) -> bool ,
@@ -1577,11 +1587,16 @@ impl fmt::Debug for Inner {
1577
1587
'^'
1578
1588
} else if sid == self . start_unanchored {
1579
1589
'>'
1580
- } else if self
1581
- . lookbehinds
1582
- . iter ( )
1583
- . any ( |i| !i. preorder ( & |e| e. start_id ( ) != sid) )
1584
- {
1590
+ } else if self . lookbehinds . iter ( ) . any ( |i| {
1591
+ i. try_fold ( ( ) , & |_, e| {
1592
+ if e. start_id ( ) == sid {
1593
+ None
1594
+ } else {
1595
+ Some ( ( ) )
1596
+ }
1597
+ } )
1598
+ . is_none ( )
1599
+ } ) {
1585
1600
'<'
1586
1601
} else {
1587
1602
' '
0 commit comments