@@ -58,7 +58,7 @@ use sparse::SparseSet;
58
58
/// Return true if and only if the given program can be executed by a DFA.
59
59
///
60
60
/// Generally, a DFA is always possible. A pathological case where it is not
61
- /// possible is if the number of NFA states exceeds u32::MAX, in which case,
61
+ /// possible is if the number of NFA states exceeds ` u32::MAX` , in which case,
62
62
/// this function will return false.
63
63
///
64
64
/// This function will also return false if the given program has any Unicode
@@ -104,7 +104,7 @@ pub struct Cache {
104
104
qnext : SparseSet ,
105
105
}
106
106
107
- /// CacheInner is logically just a part of Cache, but groups together fields
107
+ /// ` CacheInner` is logically just a part of Cache, but groups together fields
108
108
/// that aren't passed as function parameters throughout search. (This split
109
109
/// is mostly an artifact of the borrow checker. It is happily paid.)
110
110
#[ derive( Clone , Debug ) ]
@@ -162,8 +162,8 @@ struct CacheInner {
162
162
/// It is laid out in row-major order, with states as rows and byte class
163
163
/// transitions as columns.
164
164
///
165
- /// The transition table is responsible for producing valid StatePtrs. A
166
- /// StatePtr points to the start of a particular row in this table. When
165
+ /// The transition table is responsible for producing valid ` StatePtrs` . A
166
+ /// ` StatePtr` points to the start of a particular row in this table. When
167
167
/// indexing to find the next state this allows us to avoid a multiplication
168
168
/// when computing an index into the table.
169
169
#[ derive( Clone ) ]
@@ -252,7 +252,7 @@ impl<T> Result<T> {
252
252
}
253
253
}
254
254
255
- /// State is a DFA state. It contains an ordered set of NFA states (not
255
+ /// ` State` is a DFA state. It contains an ordered set of NFA states (not
256
256
/// necessarily complete) and a smattering of flags.
257
257
///
258
258
/// The flags are packed into the first byte of data.
@@ -271,7 +271,7 @@ struct State{
271
271
data : Box < [ u8 ] > ,
272
272
}
273
273
274
- /// InstPtr is a 32 bit pointer into a sequence of opcodes (i.e., it indexes
274
+ /// `InstPt`r is a 32 bit pointer into a sequence of opcodes (i.e., it indexes
275
275
/// an NFA state).
276
276
///
277
277
/// Throughout this library, this is usually set to `usize`, but we force a
@@ -322,7 +322,8 @@ impl State {
322
322
}
323
323
}
324
324
325
- /// StatePtr is a 32 bit pointer to the start of a row in the transition table.
325
+ /// `StatePtr` is a 32 bit pointer to the start of a row in the transition
326
+ /// table.
326
327
///
327
328
/// It has many special values. There are two types of special values:
328
329
/// sentinels and flags.
@@ -345,7 +346,8 @@ impl State {
345
346
///
346
347
/// The other type of state pointer is a state pointer with special flag bits.
347
348
/// There are two flags: a start flag and a match flag. The lower bits of both
348
- /// kinds always contain a "valid" StatePtr (indicated by the STATE_MAX mask).
349
+ /// kinds always contain a "valid" `StatePtr` (indicated by the `STATE_MAX`
350
+ /// mask).
349
351
///
350
352
/// The start flag means that the state is a start state, and therefore may be
351
353
/// subject to special prefix scanning optimizations.
@@ -482,7 +484,7 @@ impl<'a> Fsm<'a> {
482
484
Some ( STATE_DEAD ) => return Result :: NoMatch ( at) ,
483
485
Some ( si) => si,
484
486
} ;
485
- debug_assert ! ( dfa. start != STATE_UNKNOWN ) ;
487
+ debug_assert_ne ! ( dfa. start, STATE_UNKNOWN ) ;
486
488
dfa. exec_at ( & mut cache. qcur , & mut cache. qnext , text)
487
489
}
488
490
@@ -515,7 +517,7 @@ impl<'a> Fsm<'a> {
515
517
Some ( STATE_DEAD ) => return Result :: NoMatch ( at) ,
516
518
Some ( si) => si,
517
519
} ;
518
- debug_assert ! ( dfa. start != STATE_UNKNOWN ) ;
520
+ debug_assert_ne ! ( dfa. start, STATE_UNKNOWN ) ;
519
521
dfa. exec_at_reverse ( & mut cache. qcur , & mut cache. qnext , text)
520
522
}
521
523
@@ -527,7 +529,7 @@ impl<'a> Fsm<'a> {
527
529
text : & [ u8 ] ,
528
530
at : usize ,
529
531
) -> Result < usize > {
530
- debug_assert ! ( matches. len( ) == prog. matches. len( ) ) ;
532
+ debug_assert_eq ! ( matches. len( ) , prog. matches. len( ) ) ;
531
533
let mut cache = cache. borrow_mut ( ) ;
532
534
let mut cache = & mut cache. dfa ;
533
535
let mut dfa = Fsm {
@@ -549,14 +551,14 @@ impl<'a> Fsm<'a> {
549
551
Some ( STATE_DEAD ) => return Result :: NoMatch ( at) ,
550
552
Some ( si) => si,
551
553
} ;
552
- debug_assert ! ( dfa. start != STATE_UNKNOWN ) ;
554
+ debug_assert_ne ! ( dfa. start, STATE_UNKNOWN ) ;
553
555
let result = dfa. exec_at ( & mut cache. qcur , & mut cache. qnext , text) ;
554
556
if result. is_match ( ) {
555
557
if matches. len ( ) == 1 {
556
558
matches[ 0 ] = true ;
557
559
} else {
558
- debug_assert ! ( dfa. last_match_si != STATE_UNKNOWN ) ;
559
- debug_assert ! ( dfa. last_match_si != STATE_DEAD ) ;
560
+ debug_assert_ne ! ( dfa. last_match_si, STATE_UNKNOWN ) ;
561
+ debug_assert_ne ! ( dfa. last_match_si, STATE_DEAD ) ;
560
562
for ip in dfa. state ( dfa. last_match_si ) . inst_ptrs ( ) {
561
563
if let Inst :: Match ( slot) = dfa. prog [ ip] {
562
564
matches[ slot] = true ;
@@ -728,7 +730,7 @@ impl<'a> Fsm<'a> {
728
730
Some ( STATE_DEAD ) => return result. set_non_match ( at) ,
729
731
Some ( si) => si,
730
732
} ;
731
- debug_assert ! ( next_si != STATE_UNKNOWN ) ;
733
+ debug_assert_ne ! ( next_si, STATE_UNKNOWN ) ;
732
734
if next_si & STATE_MATCH > 0 {
733
735
next_si &= !STATE_MATCH ;
734
736
result = Result :: Match ( at - 1 ) ;
@@ -752,7 +754,7 @@ impl<'a> Fsm<'a> {
752
754
Some ( STATE_DEAD ) => return result. set_non_match ( text. len ( ) ) ,
753
755
Some ( si) => si & !STATE_START ,
754
756
} ;
755
- debug_assert ! ( prev_si != STATE_UNKNOWN ) ;
757
+ debug_assert_ne ! ( prev_si, STATE_UNKNOWN ) ;
756
758
if prev_si & STATE_MATCH > 0 {
757
759
prev_si &= !STATE_MATCH ;
758
760
self . last_match_si = prev_si;
@@ -833,7 +835,7 @@ impl<'a> Fsm<'a> {
833
835
Some ( STATE_DEAD ) => return result. set_non_match ( at) ,
834
836
Some ( si) => si,
835
837
} ;
836
- debug_assert ! ( next_si != STATE_UNKNOWN ) ;
838
+ debug_assert_ne ! ( next_si, STATE_UNKNOWN ) ;
837
839
if next_si & STATE_MATCH > 0 {
838
840
next_si &= !STATE_MATCH ;
839
841
result = Result :: Match ( at + 1 ) ;
@@ -854,7 +856,7 @@ impl<'a> Fsm<'a> {
854
856
Some ( STATE_DEAD ) => return result. set_non_match ( 0 ) ,
855
857
Some ( si) => si,
856
858
} ;
857
- debug_assert ! ( prev_si != STATE_UNKNOWN ) ;
859
+ debug_assert_ne ! ( prev_si, STATE_UNKNOWN ) ;
858
860
if prev_si & STATE_MATCH > 0 {
859
861
prev_si &= !STATE_MATCH ;
860
862
self . last_match_si = prev_si;
@@ -998,16 +1000,19 @@ impl<'a> Fsm<'a> {
998
1000
}
999
1001
}
1000
1002
}
1001
- let mut cache = true ;
1002
- if b. is_eof ( ) && self . prog . matches . len ( ) > 1 {
1003
+
1004
+ let cache = if b. is_eof ( ) && self . prog . matches . len ( ) > 1 {
1003
1005
// If we're processing the last byte of the input and we're
1004
1006
// matching a regex set, then make the next state contain the
1005
1007
// previous states transitions. We do this so that the main
1006
1008
// matching loop can extract all of the match instructions.
1007
1009
mem:: swap ( qcur, qnext) ;
1008
1010
// And don't cache this state because it's totally bunk.
1009
- cache = false ;
1010
- }
1011
+ false
1012
+ } else {
1013
+ true
1014
+ } ;
1015
+
1011
1016
// We've now built up the set of NFA states that ought to comprise the
1012
1017
// next DFA state, so try to find it in the cache, and if it doesn't
1013
1018
// exist, cache it.
@@ -1030,9 +1035,9 @@ impl<'a> Fsm<'a> {
1030
1035
next = self . start_ptr ( next) ;
1031
1036
}
1032
1037
if next <= STATE_MAX && self . state ( next) . flags ( ) . is_match ( ) {
1033
- next = STATE_MATCH | next ;
1038
+ next | = STATE_MATCH ;
1034
1039
}
1035
- debug_assert ! ( next != STATE_UNKNOWN ) ;
1040
+ debug_assert_ne ! ( next, STATE_UNKNOWN ) ;
1036
1041
// And now store our state in the current state's next list.
1037
1042
if cache {
1038
1043
let cls = self . byte_class ( b) ;
@@ -1113,9 +1118,9 @@ impl<'a> Fsm<'a> {
1113
1118
NotWordBoundary if flags. not_word_boundary => {
1114
1119
self . cache . stack . push ( inst. goto as InstPtr ) ;
1115
1120
}
1116
- StartLine | EndLine | StartText | EndText => { }
1117
- WordBoundaryAscii | NotWordBoundaryAscii => { }
1118
- WordBoundary | NotWordBoundary => { }
1121
+ StartLine | EndLine | StartText | EndText
1122
+ | WordBoundaryAscii | NotWordBoundaryAscii
1123
+ | WordBoundary | NotWordBoundary => { }
1119
1124
}
1120
1125
}
1121
1126
Save ( ref inst) => self . cache . stack . push ( inst. goto as InstPtr ) ,
@@ -1167,11 +1172,10 @@ impl<'a> Fsm<'a> {
1167
1172
return Some ( si) ;
1168
1173
}
1169
1174
// If the cache has gotten too big, wipe it.
1170
- if self . approximate_size ( ) > self . prog . dfa_size_limit {
1171
- if !self . clear_cache_and_save ( current_state) {
1172
- // Ooops. DFA is giving up.
1173
- return None ;
1174
- }
1175
+ if self . approximate_size ( ) > self . prog . dfa_size_limit
1176
+ && !self . clear_cache_and_save ( current_state) {
1177
+ // Ooops. DFA is giving up.
1178
+ return None ;
1175
1179
}
1176
1180
// Allocate room for our state and add it.
1177
1181
self . add_state ( key)
@@ -1210,8 +1214,7 @@ impl<'a> Fsm<'a> {
1210
1214
let ip = usize_to_u32 ( ip) ;
1211
1215
match self . prog [ ip as usize ] {
1212
1216
Char ( _) | Ranges ( _) => unreachable ! ( ) ,
1213
- Save ( _) => { }
1214
- Split ( _) => { }
1217
+ Save ( _) | Split ( _) => { }
1215
1218
Bytes ( _) => push_inst_ptr ( & mut insts, & mut prev, ip) ,
1216
1219
EmptyLook ( _) => {
1217
1220
state_flags. set_empty ( ) ;
@@ -1301,7 +1304,7 @@ impl<'a> Fsm<'a> {
1301
1304
self . cache . trans . clear ( ) ;
1302
1305
self . cache . states . clear ( ) ;
1303
1306
self . cache . compiled . clear ( ) ;
1304
- for s in self . cache . start_states . iter_mut ( ) {
1307
+ for s in & mut self . cache . start_states {
1305
1308
* s = STATE_UNKNOWN ;
1306
1309
}
1307
1310
// The unwraps are OK because we just cleared the cache and therefore
@@ -1374,7 +1377,7 @@ impl<'a> Fsm<'a> {
1374
1377
// matches are delayed by one byte, start states can never be match
1375
1378
// states.
1376
1379
let flagi = {
1377
- ( ( ( empty_flags. start as u8 ) << 0 ) |
1380
+ ( ( empty_flags. start as u8 ) |
1378
1381
( ( empty_flags. end as u8 ) << 1 ) |
1379
1382
( ( empty_flags. start_line as u8 ) << 2 ) |
1380
1383
( ( empty_flags. end_line as u8 ) << 3 ) |
@@ -1411,9 +1414,9 @@ impl<'a> Fsm<'a> {
1411
1414
let mut empty_flags = EmptyFlags :: default ( ) ;
1412
1415
let mut state_flags = StateFlags :: default ( ) ;
1413
1416
empty_flags. start = at == 0 ;
1414
- empty_flags. end = text. len ( ) == 0 ;
1417
+ empty_flags. end = text. is_empty ( ) ;
1415
1418
empty_flags. start_line = at == 0 || text[ at - 1 ] == b'\n' ;
1416
- empty_flags. end_line = text. len ( ) == 0 ;
1419
+ empty_flags. end_line = text. is_empty ( ) ;
1417
1420
1418
1421
let is_word_last = at > 0 && Byte :: byte ( text[ at - 1 ] ) . is_ascii_word ( ) ;
1419
1422
let is_word = at < text. len ( ) && Byte :: byte ( text[ at] ) . is_ascii_word ( ) ;
@@ -1440,9 +1443,9 @@ impl<'a> Fsm<'a> {
1440
1443
let mut empty_flags = EmptyFlags :: default ( ) ;
1441
1444
let mut state_flags = StateFlags :: default ( ) ;
1442
1445
empty_flags. start = at == text. len ( ) ;
1443
- empty_flags. end = text. len ( ) == 0 ;
1446
+ empty_flags. end = text. is_empty ( ) ;
1444
1447
empty_flags. start_line = at == text. len ( ) || text[ at] == b'\n' ;
1445
- empty_flags. end_line = text. len ( ) == 0 ;
1448
+ empty_flags. end_line = text. is_empty ( ) ;
1446
1449
1447
1450
let is_word_last =
1448
1451
at < text. len ( ) && Byte :: byte ( text[ at] ) . is_ascii_word ( ) ;
@@ -1499,10 +1502,8 @@ impl<'a> Fsm<'a> {
1499
1502
self . cache . states . push ( state. clone ( ) ) ;
1500
1503
self . cache . compiled . insert ( state, si) ;
1501
1504
// Transition table and set of states and map should all be in sync.
1502
- debug_assert ! ( self . cache. states. len( )
1503
- == self . cache. trans. num_states( ) ) ;
1504
- debug_assert ! ( self . cache. states. len( )
1505
- == self . cache. compiled. len( ) ) ;
1505
+ debug_assert_eq ! ( self . cache. states. len( ) , self . cache. trans. num_states( ) ) ;
1506
+ debug_assert_eq ! ( self . cache. states. len( ) , self . cache. compiled. len( ) ) ;
1506
1507
Some ( si)
1507
1508
}
1508
1509
0 commit comments