20
20
21
21
using System ;
22
22
using System . Runtime . Serialization ;
23
- #if ! MONO_2_0
24
- using Microsoft . VisualBasic ;
25
- using Microsoft . VisualBasic . CompilerServices ;
26
- #endif
23
+ using System . Text ;
24
+ using System . Text . RegularExpressions ;
27
25
28
26
namespace Spring . Expressions
29
27
{
@@ -37,7 +35,7 @@ public class OpLike : BinaryOperator
37
35
/// <summary>
38
36
/// Create a new instance
39
37
/// </summary>
40
- public OpLike ( ) : base ( )
38
+ public OpLike ( )
41
39
{
42
40
}
43
41
@@ -48,7 +46,7 @@ protected OpLike(SerializationInfo info, StreamingContext context)
48
46
: base ( info , context )
49
47
{
50
48
}
51
-
49
+
52
50
/// <summary>
53
51
/// Returns a value for the logical LIKE operator node.
54
52
/// </summary>
@@ -59,14 +57,100 @@ protected OpLike(SerializationInfo info, StreamingContext context)
59
57
/// </returns>
60
58
protected override object Get ( object context , EvaluationContext evalContext )
61
59
{
62
- #if ! MONO_2_0
63
- string text = GetLeftValue ( context , evalContext ) as string ;
64
- string pattern = GetRightValue ( context , evalContext ) as string ;
65
-
66
- return LikeOperator . LikeString ( text , pattern , CompareMethod . Text ) ;
67
- #else
68
- throw new NotSupportedException ( "'like' operator is only supported in .NET 2.0 or higher." ) ;
69
- #endif
60
+ string text = GetLeftValue ( context , evalContext ) as string ;
61
+ string pattern = GetRightValue ( context , evalContext ) as string ;
62
+
63
+ return LikeString ( text , pattern ) ;
64
+ }
65
+
66
+ private static bool LikeString ( string Source , string Pattern )
67
+ {
68
+ if ( string . IsNullOrEmpty ( Source ) && string . IsNullOrEmpty ( Pattern ) )
69
+ {
70
+ return true ;
71
+ // LAMESPEC : MSDN states "if either string or pattern is an empty string, the result is False."
72
+ // but "" Like "[]" returns True
73
+ }
74
+
75
+ if ( ( string . IsNullOrEmpty ( Source ) || string . IsNullOrEmpty ( Pattern ) ) && string . Compare ( Pattern , "[]" ) != 0 )
76
+ {
77
+ return false ;
78
+ }
79
+
80
+ RegexOptions options = RegexOptions . Singleline | RegexOptions . CultureInvariant | RegexOptions . IgnoreCase ;
81
+
82
+ string regexString = ConvertLikeExpression ( Pattern ) ;
83
+ Regex regexpr = new Regex ( regexString , options ) ;
84
+
85
+ //Console.WriteLine("{0} --> {1}", Pattern, regexString)
86
+
87
+ return regexpr . IsMatch ( Source ) ;
88
+ }
89
+
90
+ private static string ConvertLikeExpression ( string expression )
91
+ {
92
+ StringBuilder sb = new StringBuilder ( ) ;
93
+
94
+ sb . Append ( "^" ) ;
95
+
96
+ for ( int pos = 0 ; pos <= expression . Length - 1 ; pos ++ )
97
+ {
98
+ switch ( expression [ pos ] )
99
+ {
100
+ case '?' :
101
+ sb . Append ( '.' ) ;
102
+ break ;
103
+ case '*' :
104
+ sb . Append ( '.' ) . Append ( '*' ) ;
105
+ break ;
106
+ case '#' :
107
+ sb . Append ( "\\ d{1}" ) ;
108
+ break ;
109
+ case '[' :
110
+ StringBuilder gsb = ConvertGroupSubexpression ( expression , ref pos ) ;
111
+ // skip groups of form [], i.e. empty strings
112
+ if ( gsb . Length > 2 )
113
+ {
114
+ sb . Append ( gsb ) ;
115
+ }
116
+ break ;
117
+ default :
118
+ sb . Append ( Regex . Escape ( expression [ pos ] . ToString ( ) ) ) ;
119
+ break ;
120
+ }
121
+ }
122
+
123
+ sb . Append ( "$" ) ;
124
+
125
+ return sb . ToString ( ) ;
126
+ }
127
+
128
+ private static StringBuilder ConvertGroupSubexpression ( string carr , ref int pos )
129
+ {
130
+ StringBuilder sb = new StringBuilder ( ) ;
131
+ bool negate = false ;
132
+
133
+ while ( carr [ pos ] != ']' )
134
+ {
135
+ if ( negate )
136
+ {
137
+ sb . Append ( '^' ) ;
138
+ negate = false ;
139
+ }
140
+ if ( carr [ pos ] == '!' )
141
+ {
142
+ sb . Remove ( 1 , sb . Length - 1 ) ;
143
+ negate = true ;
144
+ }
145
+ else
146
+ {
147
+ sb . Append ( carr [ pos ] ) ;
148
+ }
149
+ pos = pos + 1 ;
150
+ }
151
+ sb . Append ( ']' ) ;
152
+
153
+ return sb ;
70
154
}
71
155
}
72
- }
156
+ }
0 commit comments