@@ -48,8 +48,10 @@ impl BuiltIn {
48
48
. for_each ( |mut s| case_correct ( & mut s, word_token. case ( ) ) ) ;
49
49
Some ( corrections)
50
50
}
51
+ }
51
52
52
- #[ cfg( feature = "dict" ) ]
53
+ #[ cfg( feature = "dict" ) ]
54
+ impl BuiltIn {
53
55
// Not using `Status` to avoid the allocations
54
56
fn correct_with_dict ( & self , word : & str ) -> Option < & ' static [ & ' static str ] > {
55
57
if typos_dict:: WORD_RANGE . contains ( & word. len ( ) ) {
@@ -58,53 +60,55 @@ impl BuiltIn {
58
60
None
59
61
}
60
62
}
63
+ }
61
64
62
- #[ cfg( not( feature = "dict" ) ) ]
65
+ #[ cfg( not( feature = "dict" ) ) ]
66
+ impl BuiltIn {
63
67
fn correct_with_dict ( & self , _word : & str ) -> Option < & ' static [ & ' static str ] > {
64
68
None
65
69
}
70
+ }
66
71
67
- #[ cfg( feature = "vars" ) ]
72
+ #[ cfg( feature = "vars" ) ]
73
+ impl BuiltIn {
68
74
fn chain_with_vars ( & self , corrections : & ' static [ & ' static str ] ) -> Status < ' static > {
69
- let mut chained: Vec < _ > = corrections
70
- . iter ( )
71
- . flat_map ( |c| match self . correct_with_vars ( c) {
72
- Some ( Status :: Valid ) | None => vec ! [ Cow :: Borrowed ( * c) ] ,
73
- Some ( Status :: Corrections ( vars) ) => vars,
74
- Some ( Status :: Invalid ) => {
75
- unreachable ! ( "correct_with_vars should always have valid suggestions" )
76
- }
77
- } )
78
- . collect ( ) ;
79
- if chained. len ( ) != 1 {
80
- chained. sort_unstable ( ) ;
81
- chained. dedup ( ) ;
75
+ if self . is_vars_enabled ( ) {
76
+ let mut chained: Vec < _ > = corrections
77
+ . iter ( )
78
+ . flat_map ( |c| match self . correct_with_vars ( c) {
79
+ Some ( Status :: Valid ) | None => vec ! [ Cow :: Borrowed ( * c) ] ,
80
+ Some ( Status :: Corrections ( vars) ) => vars,
81
+ Some ( Status :: Invalid ) => {
82
+ unreachable ! ( "correct_with_vars should always have valid suggestions" )
83
+ }
84
+ } )
85
+ . collect ( ) ;
86
+ if chained. len ( ) != 1 {
87
+ chained. sort_unstable ( ) ;
88
+ chained. dedup ( ) ;
89
+ }
90
+ debug_assert ! ( !chained. is_empty( ) ) ;
91
+ Status :: Corrections ( chained)
92
+ } else {
93
+ Status :: Corrections ( corrections. iter ( ) . map ( |c| Cow :: Borrowed ( * c) ) . collect ( ) )
82
94
}
83
- debug_assert ! ( !chained. is_empty( ) ) ;
84
- Status :: Corrections ( chained)
85
- }
86
-
87
- #[ cfg( not( feature = "vars" ) ) ]
88
- fn chain_with_vars ( & self , corrections : & ' static [ & ' static str ] ) -> Status < ' static > {
89
- Status :: Corrections ( corrections. iter ( ) . map ( |c| Cow :: Borrowed ( * c) ) . collect ( ) )
90
95
}
91
96
92
- #[ cfg( feature = "vars" ) ]
93
97
fn correct_with_vars ( & self , word : & str ) -> Option < Status < ' static > > {
94
- if typos_vars:: WORD_RANGE . contains ( & word. len ( ) ) {
98
+ if self . is_vars_enabled ( ) && typos_vars:: WORD_RANGE . contains ( & word. len ( ) ) {
95
99
map_lookup ( & typos_vars:: VARS_DICTIONARY , word)
96
100
. map ( |variants| self . select_variant ( variants) )
97
101
} else {
98
102
None
99
103
}
100
104
}
101
105
102
- #[ cfg( not( feature = "vars" ) ) ]
103
- fn correct_with_vars ( & self , _word : & str ) -> Option < Status < ' static > > {
104
- None
106
+ fn is_vars_enabled ( & self ) -> bool {
107
+ #![ allow( clippy:: assertions_on_constants) ]
108
+ debug_assert ! ( typos_vars:: NO_INVALID ) ;
109
+ self . locale . is_some ( )
105
110
}
106
111
107
- #[ cfg( feature = "vars" ) ]
108
112
fn select_variant (
109
113
& self ,
110
114
vars : & ' static [ ( u8 , & ' static typos_vars:: VariantsMap ) ] ,
@@ -148,6 +152,17 @@ impl BuiltIn {
148
152
}
149
153
}
150
154
155
+ #[ cfg( not( feature = "vars" ) ) ]
156
+ impl BuiltIn {
157
+ fn chain_with_vars ( & self , corrections : & ' static [ & ' static str ] ) -> Status < ' static > {
158
+ Status :: Corrections ( corrections. iter ( ) . map ( |c| Cow :: Borrowed ( * c) ) . collect ( ) )
159
+ }
160
+
161
+ fn correct_with_vars ( & self , _word : & str ) -> Option < Status < ' static > > {
162
+ None
163
+ }
164
+ }
165
+
151
166
impl typos:: Dictionary for BuiltIn {
152
167
fn correct_ident < ' s , ' w > ( & ' s self , ident : typos:: tokens:: Identifier < ' w > ) -> Option < Status < ' s > > {
153
168
BuiltIn :: correct_ident ( self , ident)
@@ -296,7 +311,7 @@ mod test {
296
311
typos:: tokens:: Case :: Lower ,
297
312
0 ,
298
313
) ) ;
299
- assert_eq ! ( correction, Some ( Status :: Valid ) ) ;
314
+ assert_eq ! ( correction, None ) ;
300
315
}
301
316
302
317
#[ cfg( feature = "vars" ) ]
0 commit comments