(file) Return to CQLRegularExpression.cpp CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / CQL

  1 martin 1.8 //%LICENSE////////////////////////////////////////////////////////////////
  2 martin 1.9 //
  3 martin 1.8 // Licensed to The Open Group (TOG) under one or more contributor license
  4            // agreements.  Refer to the OpenPegasusNOTICE.txt file distributed with
  5            // this work for additional information regarding copyright ownership.
  6            // Each contributor licenses this file to you under the OpenPegasus Open
  7            // Source License; you may not use this file except in compliance with the
  8            // License.
  9 martin 1.9 //
 10 martin 1.8 // Permission is hereby granted, free of charge, to any person obtaining a
 11            // copy of this software and associated documentation files (the "Software"),
 12            // to deal in the Software without restriction, including without limitation
 13            // the rights to use, copy, modify, merge, publish, distribute, sublicense,
 14            // and/or sell copies of the Software, and to permit persons to whom the
 15            // Software is furnished to do so, subject to the following conditions:
 16 martin 1.9 //
 17 martin 1.8 // The above copyright notice and this permission notice shall be included
 18            // in all copies or substantial portions of the Software.
 19 martin 1.9 //
 20 martin 1.8 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 21 martin 1.9 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 22 martin 1.8 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 23            // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 24            // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 25            // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 26            // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 27 martin 1.9 //
 28 martin 1.8 //////////////////////////////////////////////////////////////////////////
 29 chuck  1.2 //
 30            //%/////////////////////////////////////////////////////////////////////////////
 31            
 32            #include "CQLRegularExpression.h"
 33            #include <Pegasus/Common/Char16.h>
 34            #include <Pegasus/Common/CommonUTF.h>
 35            
 36            PEGASUS_NAMESPACE_BEGIN
 37            
 38 s.kodali 1.7 CQLRegularExpression::CQLRegularExpression(const String& pattern):
 39 kumpf    1.10     pattern(pattern)
 40 chuck    1.2  {
 41               }
 42               
 43               CQLRegularExpression::~CQLRegularExpression()
 44               {
 45               }
 46               
 47               
 48 s.kodali 1.7  Boolean CQLRegularExpression::match(const String& string)
 49 kumpf    1.10 
 50 chuck    1.2  {
 51                   Uint32 patIndex = 0;
 52                   Uint32 strIndex = 0;
 53               
 54                   // if either pattern or string are "EMPTY" you have an invalid String
 55 karl     1.6      if (pattern == String::EMPTY)
 56                   {
 57 chuck    1.2          return false;
 58                   }
 59               
 60 karl     1.6      if (string == String::EMPTY)
 61                   {
 62 chuck    1.2          return false;
 63                   }
 64               
 65 karl     1.6      while (true)
 66                   {
 67                       if ( (string.size() == strIndex) && (pattern.size() == patIndex))
 68                       {
 69 chuck    1.2              return true;
 70 karl     1.6          }
 71                       else if ((string.size() == strIndex) || (pattern.size() == patIndex))
 72                       {
 73 chuck    1.2              return false;
 74                       }
 75               
 76                       //  Check if pattern equal to  '.'
 77 karl     1.6          if (pattern[patIndex] == '.')
 78                       {
 79 chuck    1.2              //assumes a valid multi-byte pair has been passed
 80 kumpf    1.10             if ((((Uint16)pattern[patIndex] >= FIRST_HIGH_SURROGATE) &&
 81 chuck    1.2                   ((Uint16)pattern[patIndex] <= LAST_HIGH_SURROGATE)) ||
 82 kumpf    1.10                 (((Uint16)pattern[patIndex] >= FIRST_LOW_SURROGATE) &&
 83 karl     1.6                   ((Uint16)pattern[patIndex] <= LAST_LOW_SURROGATE)))
 84                           {
 85 chuck    1.2                  patIndex ++;
 86                               strIndex ++;
 87                           }
 88               
 89                           strIndex ++;
 90                           patIndex ++;
 91               
 92                           // Check if pattern equal to '*'
 93 karl     1.6          }
 94                       else if (pattern[patIndex] == '*')
 95                       {
 96                           if(patIndex == 0)
 97                               return false;
 98                           if (pattern[patIndex-1] == '.')
 99                           {
100                               if ((patIndex > 1) && pattern[patIndex-2] =='\\')
101                               {
102                                   if (string[strIndex] != '.')
103                                   {
104 chuck    1.2                          return false;
105                                   }
106 karl     1.6                  }
107                               else if (pattern.size()-1 == patIndex)
108                               {
109 chuck    1.2                      return true;
110 karl     1.6                  }
111                               else if (string.size()-1 == strIndex)
112                               {
113 chuck    1.2                      return false;
114                               }
115 karl     1.6              }
116                           else if (pattern[patIndex-1] == '\\')
117                           {
118                               if (pattern[patIndex-2] == '.')
119                               {
120                                   if (string[strIndex] != '*')
121                                   {
122 chuck    1.2                          return false;
123                                   }
124                               }
125 karl     1.6              }
126                           else if (
127 kumpf    1.10                  (((Uint16)pattern[patIndex-2] >= FIRST_HIGH_SURROGATE) &&
128 karl     1.6                      ((Uint16)pattern[patIndex-2] <= LAST_HIGH_SURROGATE)) ||
129 kumpf    1.10                  (((Uint16)pattern[patIndex-2] >= FIRST_LOW_SURROGATE) &&
130 karl     1.6                      ((Uint16)pattern[patIndex-2] <= LAST_LOW_SURROGATE)))
131                           {
132 chuck    1.2  
133 karl     1.6                  if (pattern[patIndex-2] != string[strIndex])
134                               {
135 chuck    1.2                      return false;
136 karl     1.6                  } else if (pattern[patIndex-1] != string[strIndex+1])
137                               {
138 chuck    1.2                      return false;
139 karl     1.6                  }
140                               else
141                               {
142 chuck    1.2                      strIndex ++;
143                               }
144 karl     1.6              }
145                           else if (pattern[patIndex-1] != string[strIndex])
146                           {
147 chuck    1.2                  return false;
148                           }
149 karl     1.6              while (true)
150                           {
151 chuck    1.2                  strIndex ++;
152               
153 karl     1.6                  if (pattern[patIndex-1] == '.')
154                               {
155                                   if ((patIndex > 1) && (pattern[patIndex-2] =='\\'))
156                                   {
157                                       if (string[strIndex] != '.')
158                                       {
159 chuck    1.2                              patIndex ++;
160                                           break;
161                                       }
162 karl     1.6                      }
163                                   else if (pattern[patIndex+1] == string[strIndex])
164                                   {
165 chuck    1.2                          //make copies of the indexes in case you do not reach
166                                       //the end of the string
167                                       int stringOrig = strIndex;
168                                       int patternOrig = patIndex;
169                                       patIndex++;
170               
171 kumpf    1.10                         if (strIndex == string.size()-1 &&
172 karl     1.6                                  patIndex == pattern.size()-1)
173                                       {
174 chuck    1.2                              return true;
175                                       }
176 karl     1.6                          while (true)
177                                       {
178 chuck    1.2                              strIndex++;
179                                           patIndex ++;
180 karl     1.6                              if (pattern[patIndex] != string[strIndex])
181                                           {
182 chuck    1.2                                  strIndex = stringOrig + 1;
183                                               patIndex = patternOrig;
184                                               break;
185 kumpf    1.10                             } else if (strIndex == string.size()-1 &&
186 karl     1.6                                     patIndex == pattern.size()-1)
187                                           {
188 chuck    1.2                                  break;
189                                           }
190                                           patIndex++;
191                                       }
192                                   }
193 karl     1.6                  }
194                               else if (pattern[patIndex-1] == '\\')
195                               {
196                                   if (pattern[patIndex-2] == '.')
197                                   {
198                                       if (string[strIndex] != '*')
199                                       {
200 chuck    1.2                              patIndex ++;
201                                           break;
202                                       }
203 kumpf    1.10                         if (strIndex == string.size()-1 &&
204 karl     1.6                              patIndex == pattern.size()-1)
205                                       {
206 chuck    1.2                              return true;
207                                       }
208                                       while (true) {
209                                           strIndex ++;
210 karl     1.6                              if (string[strIndex] != '*')
211                                           {
212 chuck    1.2                                  patIndex ++;
213                                               break;
214                                           }
215 kumpf    1.10                             if (strIndex == string.size()-1 &&
216 karl     1.6                                  patIndex == pattern.size()-1)
217                                           {
218 chuck    1.2                                  return true;
219                                           }
220                                       }
221                                   }
222 karl     1.6                  }
223                               else if (
224 kumpf    1.10                      (((Uint16)pattern[patIndex-2] >= FIRST_HIGH_SURROGATE) &&
225 karl     1.6                          ((Uint16)pattern[patIndex-2] <= LAST_HIGH_SURROGATE)) ||
226 kumpf    1.10                      (((Uint16)pattern[patIndex-2] >= FIRST_LOW_SURROGATE) &&
227 karl     1.6                          ((Uint16)pattern[patIndex-2] <= LAST_LOW_SURROGATE)))
228                               {
229                                   if (pattern[patIndex-2] != string[strIndex])
230                                   {
231 chuck    1.2                          patIndex ++;
232                                       break;
233 karl     1.6                      }
234                                   else if (pattern[patIndex-1] != string[strIndex+1])
235                                   {
236 chuck    1.2                          patIndex ++;
237                                       break;
238 karl     1.6                      }
239                                   else
240                                   {
241 chuck    1.2                          strIndex ++;
242                                   }
243 karl     1.6                  }
244                               else if (pattern[patIndex-1] != string[strIndex])
245                               {
246 chuck    1.2                      patIndex ++;
247                                   break;
248                               }
249               
250 kumpf    1.10                 if (strIndex == string.size()-1 &&
251 karl     1.6                      patIndex == pattern.size()-1)
252                               {
253 chuck    1.2                      return true;
254 karl     1.6                  } else if (strIndex == string.size()-1)
255                               {
256 chuck    1.2                      return false;
257                               }
258               
259                           }
260                           // check if pattern equal to '\'
261 karl     1.6          }
262                       else if (pattern[patIndex] == '\\')
263                       {
264 chuck    1.2              patIndex ++;
265 kumpf    1.10             if ((((Uint16)pattern[patIndex] >= FIRST_HIGH_SURROGATE) &&
266 chuck    1.2                   ((Uint16)pattern[patIndex] <= LAST_HIGH_SURROGATE)) ||
267 kumpf    1.10                 (((Uint16)pattern[patIndex] >= FIRST_LOW_SURROGATE) &&
268 karl     1.6                   ((Uint16)pattern[patIndex] <= LAST_LOW_SURROGATE)))
269                           {
270 chuck    1.2  
271 karl     1.6                  if (pattern[patIndex] != string[strIndex])
272                               {
273 chuck    1.2                      return false;
274 karl     1.6                  }
275                               else if (pattern[patIndex+1] != string[strIndex+1])
276                               {
277 chuck    1.2                      return false;
278 karl     1.6                  }
279                               else
280                               {
281 chuck    1.2                      patIndex ++;
282                                   strIndex ++;
283                               }
284 karl     1.6              }
285                           else
286                           {
287 chuck    1.2                  if (pattern[patIndex] != string[strIndex]) {
288                                   return false;
289                               }
290 kumpf    1.10                 if (strIndex == string.size()-1 &&
291 karl     1.6                      patIndex == pattern.size()-1)
292                               {
293 chuck    1.2                      return true;
294                               }
295                               strIndex ++;
296                               patIndex ++;
297               
298 kumpf    1.10             }
299 chuck    1.2  
300 kumpf    1.10             //default
301 karl     1.6          }
302                       else
303                       {
304 kumpf    1.10             if ((((Uint16)pattern[patIndex] >= FIRST_HIGH_SURROGATE) &&
305 chuck    1.2                   ((Uint16)pattern[patIndex] <= LAST_HIGH_SURROGATE)) ||
306 kumpf    1.10                 (((Uint16)pattern[patIndex] >= FIRST_LOW_SURROGATE) &&
307 karl     1.6                   ((Uint16)pattern[patIndex] <= LAST_LOW_SURROGATE)))
308                           {
309                               if (pattern[patIndex] != string[strIndex])
310                               {
311 chuck    1.2                      return false;
312 karl     1.6                  }
313                               else if (pattern[patIndex+1] != string[strIndex+1])
314                               {
315 chuck    1.2                      return false;
316                               } else {
317                                   patIndex ++;
318                                   strIndex ++;
319                               }
320 karl     1.6              }
321                           else if (pattern[patIndex] != string[strIndex])
322                           {
323 chuck    1.2                  return false;
324                           }
325                           patIndex ++;
326                           strIndex ++;
327                       }
328                   }
329 carson.hovey 1.4      PEGASUS_UNREACHABLE( return false; )
330 chuck        1.2  }
331                   
332                   PEGASUS_NAMESPACE_END
333                   
334                   
335                   

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2