cmRegularExpression.h 12 KB

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