RegularExpression.hxx.in 12 KB

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