Prevent implicit * from wildcard match in footprint

Footprint filters need to be able to match the start and end of strings.
The standard wildcard filter implictly adds "*" to the start and end of
match strings, so we create a derived class that requires an explicit
"*" or "?" to match wildcards.

Fixes: lp:1751565
* https://bugs.launchpad.net/kicad/+bug/1751565
This commit is contained in:
Seth Hillbrand 2018-04-19 13:42:25 -07:00
parent 2836d36de8
commit 62522ee450
4 changed files with 52 additions and 7 deletions

View File

@ -159,6 +159,44 @@ int EDA_PATTERN_MATCH_WILDCARD::Find( const wxString& aCandidate ) const
}
bool EDA_PATTERN_MATCH_WILDCARD_EXPLICIT::SetPattern( const wxString& aPattern )
{
m_wildcard_pattern = aPattern;
// Compile the wildcard string to a regular expression
wxString regex;
regex.Alloc( 2 * aPattern.Length() ); // no need to keep resizing, we know the size roughly
const wxString to_replace = wxT( ".*+?^${}()|[]/\\" );
regex += wxT( "^" );
for( wxString::const_iterator it = aPattern.begin(); it < aPattern.end(); ++it )
{
wxUniChar c = *it;
if( c == '?' )
{
regex += wxT( "." );
}
else if( c == '*' )
{
regex += wxT( ".*" );
}
else if( to_replace.Find( c ) != wxNOT_FOUND )
{
regex += "\\";
regex += c;
}
else
{
regex += c;
}
}
regex += wxT( "$" );
return EDA_PATTERN_MATCH_REGEX::SetPattern( regex );
}
bool EDA_PATTERN_MATCH_RELATIONAL::SetPattern( const wxString& aPattern )
{
bool matches = m_regex_search.Matches( aPattern );

View File

@ -128,7 +128,7 @@ bool FOOTPRINT_FILTER_IT::FootprintFilterMatch( FOOTPRINT_INFO& aItem )
// The matching is case insensitive
wxString name;
EDA_PATTERN_MATCH_WILDCARD patternFilter;
EDA_PATTERN_MATCH_WILDCARD_EXPLICIT patternFilter;
for( auto const& each_filter : m_filter->m_footprint_filters )
{
@ -203,7 +203,7 @@ void FOOTPRINT_FILTER::FilterByFootprintFilters( wxArrayString const& aFilters )
for( auto const& each_pattern : aFilters )
{
m_footprint_filters.push_back( std::make_unique<EDA_PATTERN_MATCH_WILDCARD>() );
m_footprint_filters.push_back( std::make_unique<EDA_PATTERN_MATCH_WILDCARD_EXPLICIT>() );
m_footprint_filters.back()->SetPattern( each_pattern.Lower() );
}

View File

@ -108,6 +108,13 @@ protected:
};
class EDA_PATTERN_MATCH_WILDCARD_EXPLICIT : public EDA_PATTERN_MATCH_WILDCARD
{
public:
virtual bool SetPattern( const wxString& aPattern ) override;
};
/**
* Relational match.
*

View File

@ -136,11 +136,11 @@ private:
FOOTPRINT_LIST* m_list;
wxString m_lib_name;
wxString m_filter_pattern;
int m_pin_count;
int m_filter_type;
EDA_PATTERN_MATCH_WILDCARD m_filter;
wxString m_lib_name;
wxString m_filter_pattern;
int m_pin_count;
int m_filter_type;
EDA_PATTERN_MATCH_WILDCARD_EXPLICIT m_filter;
std::vector<std::unique_ptr<EDA_PATTERN_MATCH>> m_footprint_filters;
};