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