RegularExpression.hxx.in 13 KB

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