~ubuntu-branches/ubuntu/jaunty/cmake/jaunty-security

« back to all changes in this revision

Viewing changes to Source/cmRegularExpression.h

  • Committer: Bazaar Package Importer
  • Author(s): A. Maitland Bottoms
  • Date: 2005-03-02 09:22:44 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20050302092244-y6o9j8wr27vqcqvx
Tags: 2.0.5-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*=========================================================================
2
 
 
3
 
  Program:   Insight Segmentation & Registration Toolkit
4
 
  Module:    $RCSfile: cmRegularExpression.h,v $
5
 
  Language:  C++
6
 
  Date:      $Date: 2001/04/27 12:01:17 $
7
 
  Version:   $Revision: 1.5 $
8
 
 
9
 
Copyright (c) 2001 Insight Consortium
10
 
All rights reserved.
11
 
 
12
 
Redistribution and use in source and binary forms, with or without
13
 
modification, are permitted provided that the following conditions are met:
14
 
 
15
 
 * Redistributions of source code must retain the above copyright notice,
16
 
   this list of conditions and the following disclaimer.
17
 
 
18
 
 * Redistributions in binary form must reproduce the above copyright notice,
19
 
   this list of conditions and the following disclaimer in the documentation
20
 
   and/or other materials provided with the distribution.
21
 
 
22
 
 * The name of the Insight Consortium, nor the names of any consortium members,
23
 
   nor of any contributors, may be used to endorse or promote products derived
24
 
   from this software without specific prior written permission.
25
 
 
26
 
  * Modified source versions must be plainly marked as such, and must not be
27
 
    misrepresented as being the original software.
28
 
 
29
 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS ``AS IS''
30
 
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31
 
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32
 
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
33
 
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34
 
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35
 
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
36
 
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
37
 
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38
 
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39
 
 
40
 
=========================================================================*/
41
 
// Original Copyright notice:
42
 
// Copyright (C) 1991 Texas Instruments Incorporated.
43
 
//
44
 
// Permission is granted to any individual or institution to use, copy, modify,
45
 
// and distribute this software, provided that this complete copyright and
46
 
// permission notice is maintained, intact, in all copies and supporting
47
 
// documentation.
48
 
//
49
 
// Texas Instruments Incorporated provides this software "as is" without
50
 
// express or implied warranty.
51
 
//
52
 
// Created: MNF 06/13/89  Initial Design and Implementation
53
 
// Updated: LGO 08/09/89  Inherit from Generic
54
 
// Updated: MBN 09/07/89  Added conditional exception handling
55
 
// Updated: MBN 12/15/89  Sprinkled "const" qualifiers all over the place!
56
 
// Updated: DLS 03/22/91  New lite version
57
 
//
58
 
 
59
 
#ifndef cmRegularExpression_h
60
 
#define cmRegularExpression_h
61
 
 
62
 
#include "cmStandardIncludes.h"
63
 
 
64
 
const int NSUBEXP = 10;
65
 
 
66
 
/** \class cmRegularExpression
67
 
 * \brief Implements pattern matching with regular expressions.
68
 
 *
69
 
 * This is the header file for the regular expression class.  An object of
70
 
 * this class contains a regular expression, in a special "compiled" format.
71
 
 * This compiled format consists of several slots all kept as the objects
72
 
 * private data.  The cmRegularExpression class provides a convenient way to
73
 
 * represent regular expressions.  It makes it easy to search for the same
74
 
 * regular expression in many different strings without having to compile a
75
 
 * string to regular expression format more than necessary.
76
 
 *
77
 
 * This class implements pattern matching via regular expressions.
78
 
 * A regular expression allows a programmer to specify  complex
79
 
 * patterns  that  can  be searched for and matched against the
80
 
 * character string of a string object. In its simplest form, a
81
 
 * regular  expression  is  a  sequence  of  characters used to
82
 
 * search for exact character matches. However, many times  the
83
 
 * exact  sequence to be found is not known, or only a match at
84
 
 * the beginning or end of a string is desired. The cmRegularExpression regu-
85
 
 * lar  expression  class implements regular expression pattern
86
 
 * matching as is found and implemented in many  UNIX  commands
87
 
 * and utilities.
88
 
 *
89
 
 * Example: The perl code
90
 
 * 
91
 
 *    $filename =~ m"([a-z]+)\.cc";
92
 
 *    print $1;
93
 
 *    
94
 
 * Is written as follows in C++
95
 
 *
96
 
 *    cmRegularExpression re("([a-z]+)\\.cc");
97
 
 *    re.find(filename);
98
 
 *    cerr << re.match(1);
99
 
 *
100
 
 *
101
 
 * The regular expression class provides a convenient mechanism
102
 
 * for  specifying  and  manipulating  regular expressions. The
103
 
 * regular expression object allows specification of such  pat-
104
 
 * terns  by using the following regular expression metacharac-
105
 
 * ters:
106
 
 *
107
 
 *  ^        Matches at beginning of a line
108
 
 *
109
 
 *  $        Matches at end of a line
110
 
 *
111
 
 * .         Matches any single character
112
 
 *
113
 
 * [ ]       Matches any character(s) inside the brackets
114
 
 *
115
 
 * [^ ]      Matches any character(s) not inside the brackets
116
 
 *
117
 
 *  -        Matches any character in range on either side of a dash
118
 
 *
119
 
 *  *        Matches preceding pattern zero or more times
120
 
 *
121
 
 *  +        Matches preceding pattern one or more times
122
 
 *
123
 
 *  ?        Matches preceding pattern zero or once only
124
 
 *
125
 
 * ()        Saves a matched expression and uses it in a  later match
126
 
 *
127
 
 * Note that more than one of these metacharacters can be  used
128
 
 * in  a  single  regular expression in order to create complex
129
 
 * search patterns. For example, the pattern [^ab1-9]  says  to
130
 
 * match  any  character  sequence that does not begin with the
131
 
 * characters "ab"  followed  by  numbers  in  the  series  one
132
 
 * through nine.
133
 
 *
134
 
 * There are three constructors for cmRegularExpression.  One just creates an
135
 
 * empty cmRegularExpression object.  Another creates a cmRegularExpression
136
 
 * object and initializes it with a regular expression that is given in the
137
 
 * form of a char*.  The third takes a reference to a cmRegularExpression
138
 
 * object as an argument and creates an object initialized with the
139
 
 * information from the given cmRegularExpression object.
140
 
 *
141
 
 * The  find  member function  finds   the  first  occurence   of  the regualr
142
 
 * expression of that object in the string given to find as an argument.  Find
143
 
 * returns a boolean, and  if true,  mutates  the private  data appropriately.
144
 
 * Find sets pointers to the beginning and end of  the thing last  found, they
145
 
 * are pointers into the actual string  that was searched.   The start and end
146
 
 * member functions return indicies  into the searched string that  correspond
147
 
 * to the beginning   and  end pointers  respectively.   The    compile member
148
 
 * function takes a char* and puts the  compiled version of the char* argument
149
 
 * into the object's private data fields.  The == and  != operators only check
150
 
 * the  to see  if   the compiled  regular  expression   is the same, and  the
151
 
 * deep_equal functions also checks  to see if the  start and end pointers are
152
 
 * the same.  The is_valid  function returns false if  program is set to NULL,
153
 
 * (i.e. there is no valid compiled exression).  The set_invalid function sets
154
 
 * the  program to NULL  (Warning: this deletes the compiled  expression). The
155
 
 * following examples may help clarify regular expression usage:
156
 
 *
157
 
 *   *  The regular expression  "^hello" matches  a "hello"  only at  the
158
 
 *      beginning of a  line.  It would match "hello  there" but not "hi,
159
 
 *      hello there".
160
 
 *
161
 
 *   *  The regular expression "long$" matches a  "long"  only at the end
162
 
 *      of a line. It would match "so long\0", but not "long ago".
163
 
 *
164
 
 *   *  The regular expression "t..t..g"  will match anything that  has a
165
 
 *      "t" then any two characters, another "t", any  two characters and
166
 
 *      then a "g".   It will match  "testing", or "test again" but would
167
 
 *      not match "toasting"
168
 
 *
169
 
 *   *  The regular  expression "[1-9ab]" matches any  number one through
170
 
 *      nine, and the characters  "a" and  "b".  It would match "hello 1"
171
 
 *      or "begin", but would not match "no-match".
172
 
 *
173
 
 *   *  The  regular expression "[^1-9ab]"  matches any character that is
174
 
 *      not a number one  through nine, or  an "a" or "b".   It would NOT
175
 
 *      match "hello 1" or "begin", but would match "no-match".
176
 
 *
177
 
 *   *  The regular expression "br* " matches  something that begins with
178
 
 *      a "b", is followed by zero or more "r"s, and ends in a space.  It
179
 
 *      would match "brrrrr ", and "b ", but would not match "brrh ".
180
 
 *
181
 
 *   *  The regular expression "br+ " matches something  that begins with
182
 
 *      a "b", is followed by one or more "r"s, and ends in  a space.  It
183
 
 *      would match "brrrrr ",  and  "br ", but would not  match "b  " or
184
 
 *      "brrh ".
185
 
 *
186
 
 *   *  The regular expression "br? " matches  something that begins with
187
 
 *      a "b", is followed by zero or one "r"s, and ends in  a space.  It
188
 
 *      would  match  "br ", and "b  ", but would not match  "brrrr "  or
189
 
 *      "brrh ".
190
 
 *
191
 
 *   *  The regular expression "(..p)b" matches  something ending with pb
192
 
 *      and beginning with whatever the two characters before the first p
193
 
 *      encounterd in the line were.  It would find  "repb" in "rep drepa
194
 
 *      qrepb".  The regular expression "(..p)a"  would find "repa qrepb"
195
 
 *      in "rep drepa qrepb"
196
 
 *
197
 
 *   *  The regular expression "d(..p)" matches something ending  with p,
198
 
 *      beginning with d, and having  two characters  in between that are
199
 
 *      the same as the two characters before  the first p  encounterd in
200
 
 *      the line.  It would match "drepa qrepb" in "rep drepa qrepb".
201
 
 *
202
 
 */
203
 
class cmRegularExpression 
204
 
{
205
 
public:
206
 
  /**
207
 
   * Instantiate cmRegularExpression with program=NULL.
208
 
   */
209
 
  inline cmRegularExpression ();        
210
 
 
211
 
  /**
212
 
   * Instantiate cmRegularExpression with compiled char*.
213
 
   */
214
 
  inline cmRegularExpression (char const*);
215
 
  
216
 
  /**
217
 
   * Instantiate cmRegularExpression as a copy of another regular expression.
218
 
   */
219
 
  cmRegularExpression (cmRegularExpression const&);
220
 
 
221
 
  /**
222
 
   * Destructor.
223
 
   */
224
 
  inline ~cmRegularExpression();
225
 
 
226
 
  /**
227
 
   * Compile a regular expression into internal code
228
 
   * for later pattern matching.
229
 
   */
230
 
  void compile (char const*);
231
 
 
232
 
  /**
233
 
   * Matches the regular expression to the given string.
234
 
   * Returns true if found, and sets start and end indexes accordingly.
235
 
   */
236
 
  bool find (char const*);
237
 
 
238
 
  /**
239
 
   * Matches the regular expression to the given std string.
240
 
   * Returns true if found, and sets start and end indexes accordingly.
241
 
   */
242
 
  bool find (std::string const&);               
243
 
 
244
 
  /**
245
 
   * Index to start of first find.
246
 
   */
247
 
  inline long start() const;
248
 
 
249
 
  /**
250
 
   * Index to end of first find.
251
 
   */
252
 
  inline long end() const;
253
 
 
254
 
  /**
255
 
   * Returns true if two regular expressions have the same
256
 
   * compiled program for pattern matching.
257
 
   */
258
 
  bool operator== (cmRegularExpression const&) const;
259
 
 
260
 
  /**
261
 
   * Returns true if two regular expressions have different
262
 
   * compiled program for pattern matching.
263
 
   */
264
 
  inline bool operator!= (cmRegularExpression const&) const;
265
 
 
266
 
  /**
267
 
   * Returns true if have the same compiled regular expressions
268
 
   * and the same start and end pointers.
269
 
   */
270
 
  bool deep_equal (cmRegularExpression const&) const;
271
 
  
272
 
  /**
273
 
   * True if the compiled regexp is valid.
274
 
   */
275
 
  inline bool is_valid() const;
276
 
 
277
 
  /**
278
 
   * Marks the regular expression as invalid.
279
 
   */
280
 
  inline void set_invalid();            
281
 
 
282
 
  /**
283
 
   * Destructor.
284
 
   */
285
 
  // awf added
286
 
  int start(int n) const;
287
 
  int end(int n) const;
288
 
  std::string match(int n) const;
289
 
  
290
 
private: 
291
 
  const char* startp[NSUBEXP];
292
 
  const char* endp[NSUBEXP];
293
 
  char  regstart;                       // Internal use only
294
 
  char  reganch;                        // Internal use only
295
 
  const char* regmust;                  // Internal use only
296
 
  int   regmlen;                        // Internal use only
297
 
  char* program;   
298
 
  int   progsize;
299
 
  const char* searchstring;
300
 
}; 
301
 
 
302
 
/**
303
 
 * Create an empty regular expression.
304
 
 */
305
 
inline cmRegularExpression::cmRegularExpression () 
306
 
307
 
  this->program = NULL;
308
 
}
309
 
 
310
 
/**
311
 
 * Creates a regular expression from string s, and
312
 
 * compiles s.
313
 
 */
314
 
inline cmRegularExpression::cmRegularExpression (const char* s) 
315
 
{  
316
 
  this->program = NULL;
317
 
  compile(s);
318
 
}
319
 
 
320
 
/**
321
 
 * Destroys and frees space allocated for the regular expression.
322
 
 */
323
 
inline cmRegularExpression::~cmRegularExpression () 
324
 
{
325
 
//#ifndef WIN32
326
 
  delete [] this->program;
327
 
//#endif
328
 
}
329
 
 
330
 
/**
331
 
 * Set the start position for the regular expression.
332
 
 */
333
 
inline long cmRegularExpression::start () const 
334
 
{
335
 
  return(this->startp[0] - searchstring);
336
 
}
337
 
 
338
 
 
339
 
/**
340
 
 * Returns the start/end index of the last item found.
341
 
 */
342
 
inline long cmRegularExpression::end () const 
343
 
{
344
 
  return(this->endp[0] - searchstring);
345
 
}
346
 
 
347
 
/**
348
 
 * Returns true if two regular expressions have different
349
 
 * compiled program for pattern matching.
350
 
 */
351
 
inline bool cmRegularExpression::operator!= (const cmRegularExpression& r) const 
352
 
{
353
 
  return(!(*this == r));
354
 
}
355
 
 
356
 
/**
357
 
 * Returns true if a valid regular expression is compiled
358
 
 * and ready for pattern matching.
359
 
 */
360
 
inline bool cmRegularExpression::is_valid () const 
361
 
{
362
 
  return (this->program != NULL);
363
 
}
364
 
 
365
 
 
366
 
inline void cmRegularExpression::set_invalid () 
367
 
{
368
 
//#ifndef WIN32
369
 
  delete [] this->program;
370
 
//#endif
371
 
  this->program = NULL;
372
 
}
373
 
 
374
 
/**
375
 
 * Return start index of nth submatch. start(0) is the start of the full match.
376
 
 */
377
 
inline int cmRegularExpression::start(int n) const
378
 
{
379
 
  return this->startp[n] - searchstring;
380
 
}
381
 
 
382
 
 
383
 
/**
384
 
 * Return end index of nth submatch. end(0) is the end of the full match.
385
 
 */
386
 
inline int cmRegularExpression::end(int n) const
387
 
{
388
 
  return this->endp[n] - searchstring;
389
 
}
390
 
 
391
 
/**
392
 
 * Return nth submatch as a string.
393
 
 */
394
 
inline std::string cmRegularExpression::match(int n) const
395
 
{
396
 
  return std::string(this->startp[n], this->endp[n] - this->startp[n]);
397
 
}
398
 
 
399
 
#endif // cmRegularExpressionh