You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
170 lines
5.8 KiB
C++
170 lines
5.8 KiB
C++
#include "tdestringmatcher.h"
|
|
|
|
#include <tqregexp.h>
|
|
#include <kdebug.h>
|
|
|
|
class TDEStringMatcher::TDEStringMatcherPrivate {
|
|
public:
|
|
TQString patternString;
|
|
}; // FIXME: This may be too small to warrant a private class :\
|
|
|
|
TDEStringMatcher::TDEStringMatcher()
|
|
{
|
|
p = new TDEStringMatcherPrivate;
|
|
TSMTRACE << "TDEStringMatcher::TDEStringMatcher: New instance created: " << this << endl;
|
|
}
|
|
|
|
TDEStringMatcher::~TDEStringMatcher()
|
|
{
|
|
patternList.setAutoDelete( true );
|
|
patternList.clear();
|
|
delete p;
|
|
TSMTRACE << "TDEStringMatcher::TDEStringMatcher: Instance destroyed: " << this << endl;
|
|
}
|
|
|
|
TQString TDEStringMatcher::getPatternString()
|
|
{
|
|
return p->patternString;
|
|
}
|
|
|
|
bool TDEStringMatcher::generatePatternList( TQString newPatternString )
|
|
{
|
|
if ( newPatternString == p->patternString )
|
|
return true;
|
|
TSMTRACE << "TDEStringMatcher::generatePatternList: Proposed pattern string: <" << newPatternString << ">" << endl;
|
|
if ( newPatternString.length() < 2 ) {
|
|
TSMTRACE << " Input string too short to be interpreted, patterns will be cleared" << endl;
|
|
patternList.clear();
|
|
p->patternString = "" ;
|
|
#ifdef TSMSIGNALS
|
|
emit patternsChanged();
|
|
#endif // TSMSIGNALS
|
|
return true;
|
|
}
|
|
TQChar patternStringDivider = newPatternString[0];
|
|
TSMTRACE << " patternStringDivider = '" << patternStringDivider << "'" << endl;
|
|
TQStringList specList = TQStringList::split( patternStringDivider, newPatternString.mid(1), true );
|
|
|
|
TQRegExp rxWork;
|
|
TQPtrList<TQRegExp> rxPatternList;
|
|
|
|
for ( const TQString &specification : specList ) {
|
|
TSMTRACE << " Processing specification string: '" << specification << "'" << endl;
|
|
TQChar specificationType = specification[0].lower();
|
|
switch ( specificationType ) {
|
|
case 'o' : {
|
|
TQString optionString = specification.mid(1).lower();
|
|
TSMTRACE << " Processing match option string: '" << optionString << "'" << endl;
|
|
for ( int i = 0 ; i < optionString.length() ; i++ ) {
|
|
TQChar optionChar = optionString[i];
|
|
TSMTRACE << " Option character: '" << optionChar << "'" << endl;
|
|
switch ( optionChar ) {
|
|
case 'w' : rxWork.setWildcard( true ); break;
|
|
case 'r' : rxWork.setWildcard( false ); break;
|
|
case 'c' : rxWork.setCaseSensitive( true ); break;
|
|
case 'i' : rxWork.setCaseSensitive( false ); break;
|
|
case 'm' : rxWork.setMinimal( true ); break;
|
|
case 'g' : rxWork.setMinimal( false ); break;
|
|
default: break;
|
|
}
|
|
}
|
|
TSMTRACE << " Wildcard/CaseSensitive settings: " << rxWork.wildcard() << "/" << rxWork.caseSensitive() << endl;
|
|
|
|
}
|
|
break;
|
|
|
|
case 'p' : {
|
|
TQString pattern = specification.mid(1);
|
|
TSMTRACE << " Processing match pattern: '" << pattern << "'" << endl;
|
|
if ( pattern.isEmpty() ) {
|
|
TSMTRACE << " Empty patterns are not allowed" << endl;
|
|
rxPatternList.clear();
|
|
return false;
|
|
}
|
|
rxWork.setPattern( pattern );
|
|
if (! rxWork.isValid() ) {
|
|
TSMTRACE << " Invalid pattern" << endl;
|
|
rxPatternList.clear();
|
|
return false;
|
|
}
|
|
TQRegExp *rxPattern = new TQRegExp( rxWork );
|
|
rxPatternList.append( rxPattern );
|
|
}
|
|
break;
|
|
|
|
default :
|
|
TSMTRACE << " Ignoring unknown specification type '" << specificationType << "'" << endl;
|
|
//-Relax, don't overreact: rxPatternList.clear();
|
|
//-Relax, don't overreact: return false;
|
|
break;
|
|
}
|
|
}
|
|
|
|
// patternList.clear(); // no need to do this?
|
|
patternList.setAutoDelete( true );
|
|
patternList = rxPatternList;
|
|
p->patternString = newPatternString;
|
|
// rxPatternList.clear(); // no need to do this?
|
|
|
|
TSMTRACE << " Final patternString: '" << p->patternString << "'" << endl;
|
|
TSMTRACE << " Number of regex match patterns in list: '" << patternList.count() << "'" << endl;
|
|
|
|
#ifdef TSMSIGNALS
|
|
TSMTRACE << " Notifying slots of pattern change" << endl;
|
|
emit patternsChanged();
|
|
TSMTRACE << " All slots have been notified" << endl;
|
|
#endif // TSMSIGNALS
|
|
TSMTRACE << "TDEStringMatcher::generatePatternList: Patterns were successfully regenerated" << endl << endl;
|
|
return true;
|
|
}
|
|
|
|
bool TDEStringMatcher::matchAny( const TQString& stringToMatch )
|
|
{
|
|
//-Debug: TSMTRACE << "Attempting to match string '" << stringToMatch << "' against stored patterns" << endl;
|
|
for ( const TQRegExp *rxPattern : patternList ) {
|
|
if (
|
|
( rxPattern->wildcard() && rxPattern->exactMatch( stringToMatch ) ) ||
|
|
( ! rxPattern->wildcard() && rxPattern->search( stringToMatch ) >= 0)
|
|
)
|
|
{
|
|
//-Debug: TSMTRACE << "String matched pattern: '" << rxPattern->pattern() << "'" << endl;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
if ( patternList.isEmpty() ) {
|
|
//-Debug: TSMTRACE << "Match failed on empty pattern list!" << endl;
|
|
return false;
|
|
}
|
|
else {
|
|
//-Debug: TSMTRACE << "Match failed, no pattern matched!" << endl;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
bool TDEStringMatcher::matchAll( const TQString& stringToMatch )
|
|
{
|
|
//-Debug: TSMTRACE << "Attempting to match string '" << stringToMatch << "' against ALL stored patterns" << endl;
|
|
for ( const TQRegExp *rxPattern : patternList ) {
|
|
if ( !
|
|
( rxPattern->wildcard() && rxPattern->exactMatch( stringToMatch ) ) ||
|
|
( ! rxPattern->wildcard() && rxPattern->search( stringToMatch ) >= 0)
|
|
)
|
|
{
|
|
//-Debug: TSMTRACE << "String failed to match pattern: '" << rxPattern->pattern() << "'" << endl;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
if ( patternList.isEmpty() ) {
|
|
//-Debug: TSMTRACE << "Match failed on empty pattern list!" << endl;
|
|
return false;
|
|
}
|
|
else {
|
|
//-Debug: TSMTRACE << "Match succeeded, all patterns matched!" << endl;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
#include "tdestringmatcher.moc"
|