aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/functionplugger.cpp2
-rw-r--r--src/functionregex.cpp126
-rw-r--r--src/functionregex.h24
3 files changed, 152 insertions, 0 deletions
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;
15extern Bu::PluginInfo pluginFunctionTargets; 15extern Bu::PluginInfo pluginFunctionTargets;
16extern Bu::PluginInfo pluginFunctionToString; 16extern Bu::PluginInfo pluginFunctionToString;
17extern Bu::PluginInfo pluginFunctionUnlink; 17extern Bu::PluginInfo pluginFunctionUnlink;
18extern Bu::PluginInfo pluginFunctionRegEx;
18 19
19FunctionPlugger::FunctionPlugger() 20FunctionPlugger::FunctionPlugger()
20{ 21{
@@ -30,6 +31,7 @@ FunctionPlugger::FunctionPlugger()
30 registerBuiltinPlugin( &pluginFunctionTargets ); 31 registerBuiltinPlugin( &pluginFunctionTargets );
31 registerBuiltinPlugin( &pluginFunctionToString ); 32 registerBuiltinPlugin( &pluginFunctionToString );
32 registerBuiltinPlugin( &pluginFunctionUnlink ); 33 registerBuiltinPlugin( &pluginFunctionUnlink );
34 registerBuiltinPlugin( &pluginFunctionRegEx );
33 35
34 DIR *dir = opendir("/usr/lib/build"); 36 DIR *dir = opendir("/usr/lib/build");
35 if( !dir ) 37 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 @@
1#include "functionregex.h"
2
3#include <bu/regex.h>
4#include <bu/plugger.h>
5PluginInterface3( pluginFunctionRegEx, regex, FunctionRegEx, Function,
6 "Mike Buland", 0, 1 );
7
8FunctionRegEx::FunctionRegEx()
9{
10}
11
12FunctionRegEx::~FunctionRegEx()
13{
14}
15
16Bu::FString FunctionRegEx::getName() const
17{
18 return "regex";
19}
20
21Variable FunctionRegEx::call( Variable &input, VarList lParams )
22{
23 if( lParams.getSize() == 1 )
24 {
25 Bu::RegEx re( lParams.first().getString() );
26 switch( input.getType() )
27 {
28 case Variable::typeString:
29 return re.execute( input.getString() );
30
31 case Variable::typeList:
32 {
33 Variable vOut( Variable::typeList );
34 for( VarList::iterator i = input.begin(); i; i++ )
35 {
36 if( re.execute( (*i).toString() ) )
37 vOut.append( *i );
38 }
39 return vOut;
40 }
41 break;
42
43 default:
44 break;
45 }
46 }
47 else if( lParams.getSize() == 2 )
48 {
49 Bu::RegEx re( lParams.first().getString() );
50 Bu::FString sPat = lParams.last().getString();
51 switch( input.getType() )
52 {
53 case Variable::typeString:
54 if( re.execute( input.getString() ) )
55 {
56 return replace( re, input.getString(), sPat );
57 }
58 else
59 {
60 return input;
61 }
62 break;
63
64 case Variable::typeList:
65 {
66 Variable vOut( Variable::typeList );
67 for( VarList::iterator i = input.begin(); i; i++ )
68 {
69 if( re.execute( (*i).toString() ) )
70 vOut.append( replace( re, (*i).toString(), sPat ) );
71 else
72 vOut.append( *i );
73 }
74 return vOut;
75 }
76 break;
77
78 default:
79 break;
80 }
81 }
82 throw Bu::ExceptionBase(
83 "regex does not work on non-string or non-list types.");
84}
85
86Bu::FString FunctionRegEx::replace( Bu::RegEx &re, const Bu::FString &sSrc,
87 const Bu::FString &sPat )
88{
89 Bu::FString sOut;
90
91 int iStart, iEnd;
92 re.getSubStringRange( 0, iStart, iEnd ); // Get the range of the full match
93
94 if( iStart > 0 )
95 sOut.append( sSrc, 0, iStart );
96
97 for( Bu::FString::const_iterator i = sPat.begin(); i; i++ )
98 {
99 if( *i == '\\' )
100 {
101 i++;
102 if( *i <= '9' && *i >= '0' )
103 {
104 int iInd = *i-'0';
105 if( iInd < re.getNumSubStrings() )
106 sOut += re.getSubString( iInd );
107 }
108 else
109 {
110 sOut += *i;
111 }
112 }
113 else
114 {
115 sOut += *i;
116 }
117 }
118
119 if( iEnd < sSrc.getSize() )
120 {
121 sOut.append( sSrc, iEnd, -1 );
122 }
123
124 return sOut;
125}
126
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 @@
1#ifndef FUNCTION_REG_EX_H
2#define FUNCTION_REG_EX_H
3
4#include "function.h"
5
6namespace Bu
7{
8 class RegEx;
9}
10
11class FunctionRegEx : public Function
12{
13public:
14 FunctionRegEx();
15 virtual ~FunctionRegEx();
16
17 virtual Bu::FString getName() const;
18 virtual Variable call( Variable &input, VarList lParams );
19
20 Bu::FString replace( Bu::RegEx &re, const Bu::FString &sSrc,
21 const Bu::FString &sPat );
22};
23
24#endif