@@ -31,16 +31,16 @@ namespace shellanything
3131{
3232 static const size_t INVALID_WILDCARD_POSITION = (size_t )-1 ;
3333
34- size_t GetNextWildcardPosition (const size_t * wildcard_positions , size_t num_wildcards, size_t current)
34+ size_t GetNextWildcardPosition (const size_t * positions , size_t num_wildcards, size_t current)
3535 {
3636 for (size_t i=0 ; i<num_wildcards; i++)
3737 {
38- const size_t & pos = wildcard_positions [i];
38+ const size_t & pos = positions [i];
3939 if (pos == current)
4040 {
4141 if (i+1 < num_wildcards)
4242 {
43- return wildcard_positions [i+1 ];
43+ return positions [i+1 ];
4444 }
4545 return INVALID_WILDCARD_POSITION; // requested wildcard is the last one
4646 }
@@ -81,7 +81,7 @@ namespace shellanything
8181 bool sequence = (c == ' *' && i > 0 && str[i-1 ] == ' *' );
8282 if (!sequence)
8383 {
84- if (offsets)
84+ if (offsets != NULL )
8585 offsets[num_wildcard] = i;
8686
8787 num_wildcard++;
@@ -155,144 +155,143 @@ namespace shellanything
155155
156156 bool WildcardSolve ( const char * pattern,
157157 const char * value,
158- const size_t * wildcard_positions ,
158+ const size_t * positions ,
159159 size_t num_wildcards,
160- const int iIndexWild ,
161- const int iIndexValue ,
160+ size_t pattern_offset ,
161+ size_t value_offset ,
162162 WildcardList & matches)
163163 {
164- int indexWild = iIndexWild;
165- int indexValue = iIndexValue;
166- int wildLen = (int )std::string (pattern).size ();
167- int valueLen = (int )std::string (value).size ();
164+ size_t pattern_length = strlen (pattern);
165+ size_t value_length = strlen (value);
168166
169- // while wildcard and value not fully solved
170- while (indexWild < wildLen || indexValue < valueLen )
167+ // While pattern and value not fully solved
168+ while (pattern_offset < pattern_length || value_offset < value_length )
171169 {
172- const char & cWild = pattern[indexWild ];
173- const char & cValue = value[indexValue ];
174- if ( !IsWildcard (cWild ) )
170+ const char & pattern_char = pattern[pattern_offset ];
171+ const char & value_char = value[value_offset ];
172+ if ( !IsWildcard (pattern_char ) )
175173 {
176- if (cWild != cValue )
177- return false ; // characters don't match!
174+ if (pattern_char != value_char )
175+ return false ; // Characters don't match!
178176
179- // next char
180- indexWild ++;
181- indexValue ++;
177+ // Next characters
178+ pattern_offset ++;
179+ value_offset ++;
182180 }
183181 else
184182 {
185- // cWild is wildcard
186- if (cWild == ' ?' )
183+ // pattern_char is wildcard
184+ if (pattern_char == ' ?' )
187185 {
188- // save wildcard resolve information
186+ // Save wildcard info
189187 WILDCARD w;
190- w.character = cWild ;
191- w.index = indexWild ;
192- w.value = cValue ;
188+ w.character = pattern_char ;
189+ w.index = pattern_offset ;
190+ w.value = value_char ;
193191 matches.push_back (w);
194192
195- // next char
196- indexWild ++;
197- indexValue ++;
193+ // Next characters
194+ pattern_offset ++;
195+ value_offset ++;
198196 }
199- else if (cWild == ' *' )
197+ else if (pattern_char == ' *' )
200198 {
201- // if wildcard character * is the last one
202- if (indexWild +1 >= wildLen )
199+ // If wildcard character '*' is the last one
200+ if (pattern_offset +1 >= pattern_length )
203201 {
204- // automatically match the end of the string
202+ // Automatically match the end of the string
205203
206- // save wildcard resolve information
204+ // Save wildcard info
207205 WILDCARD w;
208- w.character = cWild ;
209- w.index = indexWild ;
210- w.value = &cValue ;
206+ w.character = pattern_char ;
207+ w.index = pattern_offset ;
208+ w.value = &value_char ;
211209 matches.push_back (w);
212210
213211 // If we reached a '*' sequence, move forward to the last '*' character of the sequence
214- while (pattern[indexWild ] == ' *' && (indexWild +1 < wildLen ) && pattern[indexWild +1 ] == ' *' )
212+ while (pattern[pattern_offset ] == ' *' && (pattern_offset +1 < pattern_length ) && pattern[pattern_offset +1 ] == ' *' )
215213 {
216- indexWild ++;
214+ pattern_offset ++;
217215 }
218216
219- // next char
220- indexWild ++;
221- indexValue += ( int ) w.value .size ();
217+ // Next characters
218+ pattern_offset ++;
219+ value_offset += w.value .size ();
222220 }
223221 else
224222 {
225- // wildcard character * is not the last one
223+ // Wildcard character '*' is not the last one
226224
227- // compute all possibilities that can fit in * and then use recursion to check if it can be resolved
228- // compute the possibilities in the from the longest to the shortest ("") for optimizing the replacement string
229- // since all wildcard caracters must be mapped, do not compute possibilities beyond the next wildcard character
225+ // Compute all possibles candidates that can fit in '*' and then use recursion to check if it can be resolved.
226+ // Compute the candidates from the longest to the shortest ("") for optimizing the replacement string.
227+ // Since all wildcard characters must be mapped, do not compute possibilities/candidates beyond the next wildcard character.
230228
231- // compute next character
232- const char & nextWild = (&cWild )[1 ];
229+ // Compute next character
230+ const char & pattern_char_next = (&pattern_char )[1 ];
233231
234- int nextWildcardPosition = ( int ) GetNextWildcardPosition (wildcard_positions , num_wildcards, indexWild );
232+ size_t next_wildcard_position = GetNextWildcardPosition (positions , num_wildcards, pattern_offset );
235233
236- // compute fixed characters between this wildcard and the last one (of the end of the file)
237- // ie: fgh in abc*fgh?j or abc*fgh
238- std::string fixedCharacters = &nextWild;
239- if (nextWildcardPosition != -1 )
234+ // Extract non-wildcard (fixed) characters between this wildcard and the next one (or the end of the string)
235+ // In order to match the current '*' character, these non-wildcard characters in the pattern will also have to match.
236+ // ie: Extract "fgh" in "abc*fgh?j" or "abc*fgh"
237+ std::string pattern_fixed_characters = &pattern_char_next;
238+ if (next_wildcard_position != INVALID_WILDCARD_POSITION)
240239 {
241- // there is at least another wildcard character after this one
242- fixedCharacters = &nextWild ;
243- size_t size = nextWildcardPosition - indexWild ;
244- fixedCharacters .resize (size);
240+ // There is at least another wildcard character after this one
241+ pattern_fixed_characters = &pattern_char_next ;
242+ size_t size = next_wildcard_position - pattern_offset ;
243+ pattern_fixed_characters .resize (size);
245244 }
246245
247- // compute maximum length of replacementString
248- int remainingCharactersInValue = valueLen-indexValue ;
249- int replacementStringMaxLength = remainingCharactersInValue-( int )fixedCharacters .size ();
246+ // Compute maximum length of candidate
247+ size_t remaining_characters_in_value = value_length-value_offset ;
248+ size_t candidate_max_length = remaining_characters_in_value-pattern_fixed_characters .size ();
250249
251- // compute all possibilities that can fit in * with a length in [0,replacementStringMaxLength ]
252- for (int length=replacementStringMaxLength ; length>=0 ; length--)
250+ // Compute all possible candidates that can fit in '*' with a length in [0,candidate_max_length ]
251+ for (size_t length=candidate_max_length ; length>=0 && length!=INVALID_WILDCARD_POSITION ; length--)
253252 {
254- // assuming replacement string is the right one
253+ // Assuming replacement string is the right one
255254
256- std::string replacementString = &cValue ;
257- replacementString .resize (length);
255+ std::string candidate = &value_char ;
256+ candidate .resize (length);
258257
259- WildcardList tmpList = matches;
258+ WildcardList sub_matches = matches;
260259
261- // save wildcard resolve information
260+ // Save wildcard information
262261 WILDCARD w;
263- w.character = cWild ;
264- w.index = indexWild ;
265- w.value = replacementString ;
266- tmpList .push_back (w);
262+ w.character = pattern_char ;
263+ w.index = pattern_offset ;
264+ w.value = candidate ;
265+ sub_matches .push_back (w);
267266
268- // next char
269- int tmpIndexWild = indexWild+ 1 ;
270- int tmpIndexValue = indexValue + ( int ) w.value .size ();
267+ // Next characters
268+ size_t sub_pattern_offset = pattern_offset + 1 ;
269+ size_t sub_value_offset = value_offset + w.value .size ();
271270
272- // execute recursive call
273- bool solved = WildcardSolve (pattern, value, wildcard_positions , num_wildcards, tmpIndexWild, tmpIndexValue, tmpList );
271+ // Execute recursive call
272+ bool solved = WildcardSolve (pattern, value, positions , num_wildcards, sub_pattern_offset, sub_value_offset, sub_matches );
274273 if (solved)
275274 {
276- // solved !
277- // refresh matches
278- matches = tmpList ;
275+ // Solved !
276+ // Keep the sub matches that we found
277+ matches = sub_matches ;
279278 return true ;
280279 }
281280 }
282281
283- // all possibilities were checked
284- // unable to solve * wildcard
282+ // All possibilities were checked
283+ // Unable to solve '*' wildcard
285284 return false ;
286285 }
287286 }
288287 }
289288 }
290289
291- if (indexWild == wildLen && indexValue == valueLen )
292- return true ; // solved
293- if ((indexWild == wildLen && indexValue < valueLen ) ||
294- (indexWild == wildLen && indexValue < valueLen ) )
295- return false ; // reached the end of wildcard or the end of value
290+ if (pattern_offset == pattern_length && value_offset == value_length )
291+ return true ; // Solved
292+ if ((pattern_offset == pattern_length && value_offset < value_length ) ||
293+ (pattern_offset == pattern_length && value_offset < value_length ) )
294+ return false ; // Reached the end of pattern or the end of value
296295 return false ; // ???
297296 }
298297
@@ -315,14 +314,14 @@ namespace shellanything
315314 return (std::string (pattern) == std::string (value));
316315
317316 // Allocate memory for all wildcard positions
318- size_t * wildcard_positions = new size_t [num_wildcards];
319- FindWildcardCharacters (simplified_pattern.c_str (), wildcard_positions , num_wildcards);
317+ size_t * positions = new size_t [num_wildcards];
318+ FindWildcardCharacters (simplified_pattern.c_str (), positions , num_wildcards* sizeof (positions[ 0 ]) );
320319
321320 // solve wildcards
322- bool solved = WildcardSolve (simplified_pattern.c_str (), value, wildcard_positions , num_wildcards, 0 , 0 , matches);
321+ bool solved = WildcardSolve (simplified_pattern.c_str (), value, positions , num_wildcards, 0 , 0 , matches);
323322
324- delete[] wildcard_positions ;
325- wildcard_positions = NULL ;
323+ delete[] positions ;
324+ positions = NULL ;
326325
327326 return solved;
328327 }
0 commit comments