@@ -21,7 +21,7 @@ use js_sys::JsString;
2121pub struct Pattern {
2222 invalid_letters : Vec < char > ,
2323 pattern : Vec < char > ,
24- first_letter : char ,
24+ prefix : Box < str > ,
2525 /// true for normal hangman mode
2626 letters_in_pattern_have_no_other_occurrences : bool ,
2727 known_letters_count : usize ,
@@ -87,7 +87,10 @@ impl Pattern {
8787 }
8888 }
8989
90- let first_letter = * pattern_as_chars. first ( ) . unwrap_or ( & char:: WILDCARD ) ;
90+ let prefix = pattern_as_chars
91+ . iter ( )
92+ . take_while ( |ch| !ch. is_wildcard ( ) )
93+ . collect :: < Box < str > > ( ) ;
9194
9295 let mut invalid_ascii_letters = [ false ; 128 ] ;
9396 let mut invalid_letters_all_ascii: bool = true ;
@@ -107,7 +110,7 @@ impl Pattern {
107110 Ok ( Self {
108111 invalid_letters : invalid_letters_vec,
109112 pattern : pattern_as_chars,
110- first_letter ,
113+ prefix ,
111114 letters_in_pattern_have_no_other_occurrences,
112115 known_letters_count,
113116 invalid_ascii_letters,
@@ -117,23 +120,19 @@ impl Pattern {
117120
118121 #[ inline]
119122 #[ must_use]
120- fn first_letter_is_wildcard ( & self ) -> bool {
121- debug_assert_eq ! (
122- self . first_letter. is_wildcard( ) ,
123- self . first_letter. is_normalised_wildcard( )
124- ) ;
125- self . first_letter . is_normalised_wildcard ( )
123+ const fn first_letter_is_wildcard ( & self ) -> bool {
124+ self . prefix . is_empty ( )
126125 }
127126
128127 #[ must_use]
129128 #[ inline]
130- fn first_letter_matches < CC : InfallibleCharCollection + ?Sized > (
129+ fn prefix_matches < CC : InfallibleCharCollection + ?Sized > (
131130 & self ,
132131 word : & & CC ,
133132 ) -> bool {
134133 // This only makes sense if first_letter_is_wildcard is false
135134 debug_assert ! ( !self . first_letter_is_wildcard( ) ) ;
136- word. first_char ( ) == Some ( self . first_letter )
135+ word. starts_with ( & self . prefix )
137136 }
138137
139138 #[ inline]
@@ -309,8 +308,8 @@ impl Pattern {
309308 )
310309 } else {
311310 let mut filtered_words = all_words
312- . skip_while ( |word| !self . first_letter_matches ( word) )
313- . take_while ( |word| self . first_letter_matches ( word) )
311+ . skip_while ( |word| !self . prefix_matches ( word) )
312+ . take_while ( |word| self . prefix_matches ( word) )
314313 . filter ( |word| self . matches ( word) ) ;
315314 self . _collect_count_and_create_letter_frequency (
316315 & mut filtered_words,
0 commit comments