From cbd0823fde1b3feb4cfa9ef3a5affca5a4554d5e Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 7 Jan 2010 00:36:55 +0000 Subject: Updated the general rules to use the new regex function, they're safer now, and everything works. Unfortunately, with this release, you'll have to rebuild with the shell script... --- src/functionplugger.cpp | 2 + src/functionregex.cpp | 126 ++++++++++++++++++++++++++++++++++++++++++++++++ src/functionregex.h | 24 +++++++++ 3 files changed, 152 insertions(+) create mode 100644 src/functionregex.cpp create mode 100644 src/functionregex.h (limited to 'src') diff --git a/src/functionplugger.cpp b/src/functionplugger.cpp index 83435ae..c8d3fd2 100644 --- a/src/functionplugger.cpp +++ b/src/functionplugger.cpp @@ -15,6 +15,7 @@ extern Bu::PluginInfo pluginFunctionReplace; extern Bu::PluginInfo pluginFunctionTargets; extern Bu::PluginInfo pluginFunctionToString; extern Bu::PluginInfo pluginFunctionUnlink; +extern Bu::PluginInfo pluginFunctionRegEx; FunctionPlugger::FunctionPlugger() { @@ -30,6 +31,7 @@ FunctionPlugger::FunctionPlugger() registerBuiltinPlugin( &pluginFunctionTargets ); registerBuiltinPlugin( &pluginFunctionToString ); registerBuiltinPlugin( &pluginFunctionUnlink ); + registerBuiltinPlugin( &pluginFunctionRegEx ); DIR *dir = opendir("/usr/lib/build"); if( !dir ) diff --git a/src/functionregex.cpp b/src/functionregex.cpp new file mode 100644 index 0000000..f0abc35 --- /dev/null +++ b/src/functionregex.cpp @@ -0,0 +1,126 @@ +#include "functionregex.h" + +#include +#include +PluginInterface3( pluginFunctionRegEx, regex, FunctionRegEx, Function, + "Mike Buland", 0, 1 ); + +FunctionRegEx::FunctionRegEx() +{ +} + +FunctionRegEx::~FunctionRegEx() +{ +} + +Bu::FString FunctionRegEx::getName() const +{ + return "regex"; +} + +Variable FunctionRegEx::call( Variable &input, VarList lParams ) +{ + if( lParams.getSize() == 1 ) + { + Bu::RegEx re( lParams.first().getString() ); + switch( input.getType() ) + { + case Variable::typeString: + return re.execute( input.getString() ); + + case Variable::typeList: + { + Variable vOut( Variable::typeList ); + for( VarList::iterator i = input.begin(); i; i++ ) + { + if( re.execute( (*i).toString() ) ) + vOut.append( *i ); + } + return vOut; + } + break; + + default: + break; + } + } + else if( lParams.getSize() == 2 ) + { + Bu::RegEx re( lParams.first().getString() ); + Bu::FString sPat = lParams.last().getString(); + switch( input.getType() ) + { + case Variable::typeString: + if( re.execute( input.getString() ) ) + { + return replace( re, input.getString(), sPat ); + } + else + { + return input; + } + break; + + case Variable::typeList: + { + Variable vOut( Variable::typeList ); + for( VarList::iterator i = input.begin(); i; i++ ) + { + if( re.execute( (*i).toString() ) ) + vOut.append( replace( re, (*i).toString(), sPat ) ); + else + vOut.append( *i ); + } + return vOut; + } + break; + + default: + break; + } + } + throw Bu::ExceptionBase( + "regex does not work on non-string or non-list types."); +} + +Bu::FString FunctionRegEx::replace( Bu::RegEx &re, const Bu::FString &sSrc, + const Bu::FString &sPat ) +{ + Bu::FString sOut; + + int iStart, iEnd; + re.getSubStringRange( 0, iStart, iEnd ); // Get the range of the full match + + if( iStart > 0 ) + sOut.append( sSrc, 0, iStart ); + + for( Bu::FString::const_iterator i = sPat.begin(); i; i++ ) + { + if( *i == '\\' ) + { + i++; + if( *i <= '9' && *i >= '0' ) + { + int iInd = *i-'0'; + if( iInd < re.getNumSubStrings() ) + sOut += re.getSubString( iInd ); + } + else + { + sOut += *i; + } + } + else + { + sOut += *i; + } + } + + if( iEnd < sSrc.getSize() ) + { + sOut.append( sSrc, iEnd, -1 ); + } + + return sOut; +} + diff --git a/src/functionregex.h b/src/functionregex.h new file mode 100644 index 0000000..2096a64 --- /dev/null +++ b/src/functionregex.h @@ -0,0 +1,24 @@ +#ifndef FUNCTION_REG_EX_H +#define FUNCTION_REG_EX_H + +#include "function.h" + +namespace Bu +{ + class RegEx; +} + +class FunctionRegEx : public Function +{ +public: + FunctionRegEx(); + virtual ~FunctionRegEx(); + + virtual Bu::FString getName() const; + virtual Variable call( Variable &input, VarList lParams ); + + Bu::FString replace( Bu::RegEx &re, const Bu::FString &sSrc, + const Bu::FString &sPat ); +}; + +#endif -- cgit v1.2.3