RegularExpression.hxx.in 14 KB

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