RegularExpression.hxx.in 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing#kwsys for details. */
  3. // Original Copyright notice:
  4. // Copyright (C) 1991 Texas Instruments Incorporated.
  5. //
  6. // Permission is granted to any individual or institution to use, copy, modify,
  7. // and distribute this software, provided that this complete copyright and
  8. // permission notice is maintained, intact, in all copies and supporting
  9. // documentation.
  10. //
  11. // Texas Instruments Incorporated provides this software "as is" without
  12. // express or implied warranty.
  13. //
  14. // Created: MNF 06/13/89 Initial Design and Implementation
  15. // Updated: LGO 08/09/89 Inherit from Generic
  16. // Updated: MBN 09/07/89 Added conditional exception handling
  17. // Updated: MBN 12/15/89 Sprinkled "const" qualifiers all over the place!
  18. // Updated: DLS 03/22/91 New lite version
  19. //
  20. #ifndef @KWSYS_NAMESPACE@_RegularExpression_hxx
  21. #define @KWSYS_NAMESPACE@_RegularExpression_hxx
  22. #include <@KWSYS_NAMESPACE@/Configure.h>
  23. #include <@KWSYS_NAMESPACE@/Configure.hxx>
  24. #include <string>
  25. /* Disable useless Borland warnings. KWSys tries not to force things
  26. on its includers, but there is no choice here. */
  27. #if defined(__BORLANDC__)
  28. # pragma warn - 8027 /* function not inlined. */
  29. #endif
  30. namespace @KWSYS_NAMESPACE@ {
  31. // Forward declaration
  32. class RegularExpression;
  33. /** \class RegularExpressionMatch
  34. * \brief Stores the pattern matches of a RegularExpression
  35. */
  36. class @KWSYS_NAMESPACE@_EXPORT RegularExpressionMatch
  37. {
  38. public:
  39. RegularExpressionMatch();
  40. bool isValid() const;
  41. void clear();
  42. std::string::size_type start() const;
  43. std::string::size_type end() const;
  44. std::string::size_type start(int n) const;
  45. std::string::size_type end(int n) const;
  46. std::string match(int n) const;
  47. enum
  48. {
  49. NSUBEXP = 10
  50. };
  51. private:
  52. friend class RegularExpression;
  53. const char* startp[NSUBEXP];
  54. const char* endp[NSUBEXP];
  55. const char* searchstring;
  56. };
  57. /**
  58. * \brief Creates an invalid match object
  59. */
  60. inline RegularExpressionMatch::RegularExpressionMatch()
  61. {
  62. startp[0] = 0;
  63. endp[0] = 0;
  64. searchstring = 0;
  65. }
  66. /**
  67. * \brief Returns true if the match pointers are valid
  68. */
  69. inline bool RegularExpressionMatch::isValid() const
  70. {
  71. return (this->startp[0] != 0);
  72. }
  73. /**
  74. * \brief Resets to the (invalid) construction state.
  75. */
  76. inline void RegularExpressionMatch::clear()
  77. {
  78. startp[0] = 0;
  79. endp[0] = 0;
  80. searchstring = 0;
  81. }
  82. /**
  83. * \brief Returns the start index of the full match.
  84. */
  85. inline std::string::size_type RegularExpressionMatch::start() const
  86. {
  87. return static_cast<std::string::size_type>(this->startp[0] - searchstring);
  88. }
  89. /**
  90. * \brief Returns the end index of the full match.
  91. */
  92. inline std::string::size_type RegularExpressionMatch::end() const
  93. {
  94. return static_cast<std::string::size_type>(this->endp[0] - searchstring);
  95. }
  96. /**
  97. * \brief Returns the start index of nth submatch.
  98. * start(0) is the start of the full match.
  99. */
  100. inline std::string::size_type RegularExpressionMatch::start(int n) const
  101. {
  102. return static_cast<std::string::size_type>(this->startp[n] -
  103. this->searchstring);
  104. }
  105. /**
  106. * \brief Returns the end index of nth submatch.
  107. * end(0) is the end of the full match.
  108. */
  109. inline std::string::size_type RegularExpressionMatch::end(int n) const
  110. {
  111. return static_cast<std::string::size_type>(this->endp[n] -
  112. this->searchstring);
  113. }
  114. /**
  115. * \brief Returns the nth submatch as a string.
  116. */
  117. inline std::string RegularExpressionMatch::match(int n) const
  118. {
  119. if (this->startp[n] == 0) {
  120. return std::string();
  121. } else {
  122. return std::string(
  123. this->startp[n],
  124. static_cast<std::string::size_type>(this->endp[n] - this->startp[n]));
  125. }
  126. }
  127. /** \class RegularExpression
  128. * \brief Implements pattern matching with regular expressions.
  129. *
  130. * This is the header file for the regular expression class. An object of
  131. * this class contains a regular expression, in a special "compiled" format.
  132. * This compiled format consists of several slots all kept as the objects
  133. * private data. The RegularExpression class provides a convenient way to
  134. * represent regular expressions. It makes it easy to search for the same
  135. * regular expression in many different strings without having to compile a
  136. * string to regular expression format more than necessary.
  137. *
  138. * This class implements pattern matching via regular expressions.
  139. * A regular expression allows a programmer to specify complex
  140. * patterns that can be searched for and matched against the
  141. * character string of a string object. In its simplest form, a
  142. * regular expression is a sequence of characters used to
  143. * search for exact character matches. However, many times the
  144. * exact sequence to be found is not known, or only a match at
  145. * the beginning or end of a string is desired. The RegularExpression regu-
  146. * lar expression class implements regular expression pattern
  147. * matching as is found and implemented in many UNIX commands
  148. * and utilities.
  149. *
  150. * Example: The perl code
  151. *
  152. * $filename =~ m"([a-z]+)\.cc";
  153. * print $1;
  154. *
  155. * Is written as follows in C++
  156. *
  157. * RegularExpression re("([a-z]+)\\.cc");
  158. * re.find(filename);
  159. * cerr << re.match(1);
  160. *
  161. *
  162. * The regular expression class provides a convenient mechanism
  163. * for specifying and manipulating regular expressions. The
  164. * regular expression object allows specification of such pat-
  165. * terns by using the following regular expression metacharac-
  166. * ters:
  167. *
  168. * ^ Matches at beginning of a line
  169. *
  170. * $ Matches at end of a line
  171. *
  172. * . Matches any single character
  173. *
  174. * [ ] Matches any character(s) inside the brackets
  175. *
  176. * [^ ] Matches any character(s) not inside the brackets
  177. *
  178. * - Matches any character in range on either side of a dash
  179. *
  180. * * Matches preceding pattern zero or more times
  181. *
  182. * + Matches preceding pattern one or more times
  183. *
  184. * ? Matches preceding pattern zero or once only
  185. *
  186. * () Saves a matched expression and uses it in a later match
  187. *
  188. * Note that more than one of these metacharacters can be used
  189. * in a single regular expression in order to create complex
  190. * search patterns. For example, the pattern [^ab1-9] says to
  191. * match any character sequence that does not begin with the
  192. * characters "ab" followed by numbers in the series one
  193. * through nine.
  194. *
  195. * There are three constructors for RegularExpression. One just creates an
  196. * empty RegularExpression object. Another creates a RegularExpression
  197. * object and initializes it with a regular expression that is given in the
  198. * form of a char*. The third takes a reference to a RegularExpression
  199. * object as an argument and creates an object initialized with the
  200. * information from the given RegularExpression object.
  201. *
  202. * The find member function finds the first occurrence of the regular
  203. * expression of that object in the string given to find as an argument. Find
  204. * returns a boolean, and if true, mutates the private data appropriately.
  205. * Find sets pointers to the beginning and end of the thing last found, they
  206. * are pointers into the actual string that was searched. The start and end
  207. * member functions return indices into the searched string that correspond
  208. * to the beginning and end pointers respectively. The compile member
  209. * function takes a char* and puts the compiled version of the char* argument
  210. * into the object's private data fields. The == and != operators only check
  211. * the to see if the compiled regular expression is the same, and the
  212. * deep_equal functions also checks to see if the start and end pointers are
  213. * the same. The is_valid function returns false if program is set to NULL,
  214. * (i.e. there is no valid compiled expression). The set_invalid function
  215. * sets the program to NULL (Warning: this deletes the compiled expression).
  216. * The following examples may help clarify regular expression usage:
  217. *
  218. * * The regular expression "^hello" matches a "hello" only at the
  219. * beginning of a line. It would match "hello there" but not "hi,
  220. * hello there".
  221. *
  222. * * The regular expression "long$" matches a "long" only at the end
  223. * of a line. It would match "so long\0", but not "long ago".
  224. *
  225. * * The regular expression "t..t..g" will match anything that has a
  226. * "t" then any two characters, another "t", any two characters and
  227. * then a "g". It will match "testing", or "test again" but would
  228. * not match "toasting"
  229. *
  230. * * The regular expression "[1-9ab]" matches any number one through
  231. * nine, and the characters "a" and "b". It would match "hello 1"
  232. * or "begin", but would not match "no-match".
  233. *
  234. * * The regular expression "[^1-9ab]" matches any character that is
  235. * not a number one through nine, or an "a" or "b". It would NOT
  236. * match "hello 1" or "begin", but would match "no-match".
  237. *
  238. * * The regular expression "br* " matches something that begins with
  239. * a "b", is followed by zero or more "r"s, and ends in a space. It
  240. * would match "brrrrr ", and "b ", but would not match "brrh ".
  241. *
  242. * * The regular expression "br+ " matches something that begins with
  243. * a "b", is followed by one or more "r"s, and ends in a space. It
  244. * would match "brrrrr ", and "br ", but would not match "b " or
  245. * "brrh ".
  246. *
  247. * * The regular expression "br? " matches something that begins with
  248. * a "b", is followed by zero or one "r"s, and ends in a space. It
  249. * would match "br ", and "b ", but would not match "brrrr " or
  250. * "brrh ".
  251. *
  252. * * The regular expression "(..p)b" matches something ending with pb
  253. * and beginning with whatever the two characters before the first p
  254. * encountered in the line were. It would find "repb" in "rep drepa
  255. * qrepb". The regular expression "(..p)a" would find "repa qrepb"
  256. * in "rep drepa qrepb"
  257. *
  258. * * The regular expression "d(..p)" matches something ending with p,
  259. * beginning with d, and having two characters in between that are
  260. * the same as the two characters before the first p encountered in
  261. * the line. It would match "drepa qrepb" in "rep drepa qrepb".
  262. *
  263. * All methods of RegularExpression can be called simultaneously from
  264. * different threads but only if each invocation uses an own instance of
  265. * RegularExpression.
  266. */
  267. class @KWSYS_NAMESPACE@_EXPORT RegularExpression
  268. {
  269. public:
  270. /**
  271. * Instantiate RegularExpression with program=NULL.
  272. */
  273. inline RegularExpression();
  274. /**
  275. * Instantiate RegularExpression with compiled char*.
  276. */
  277. inline RegularExpression(char const*);
  278. /**
  279. * Instantiate RegularExpression as a copy of another regular expression.
  280. */
  281. RegularExpression(RegularExpression const&);
  282. /**
  283. * Instantiate RegularExpression with compiled string.
  284. */
  285. inline RegularExpression(std::string const&);
  286. /**
  287. * Destructor.
  288. */
  289. inline ~RegularExpression();
  290. /**
  291. * Compile a regular expression into internal code
  292. * for later pattern matching.
  293. */
  294. bool compile(char const*);
  295. /**
  296. * Compile a regular expression into internal code
  297. * for later pattern matching.
  298. */
  299. inline bool compile(std::string const&);
  300. /**
  301. * Matches the regular expression to the given string.
  302. * Returns true if found, and sets start and end indexes
  303. * in the RegularExpressionMatch instance accordingly.
  304. *
  305. * This method is thread safe when called with different
  306. * RegularExpressionMatch instances.
  307. */
  308. bool find(char const*, RegularExpressionMatch&) const;
  309. /**
  310. * Matches the regular expression to the given string.
  311. * Returns true if found, and sets start and end indexes accordingly.
  312. */
  313. inline bool find(char const*);
  314. /**
  315. * Matches the regular expression to the given std string.
  316. * Returns true if found, and sets start and end indexes accordingly.
  317. */
  318. inline bool find(std::string const&);
  319. /**
  320. * Match indices
  321. */
  322. inline RegularExpressionMatch const& regMatch() const;
  323. inline std::string::size_type start() const;
  324. inline std::string::size_type end() const;
  325. inline std::string::size_type start(int n) const;
  326. inline std::string::size_type end(int n) const;
  327. /**
  328. * Match strings
  329. */
  330. inline std::string match(int n) const;
  331. /**
  332. * Copy the given regular expression.
  333. */
  334. RegularExpression& operator=(const RegularExpression& rxp);
  335. /**
  336. * Returns true if two regular expressions have the same
  337. * compiled program for pattern matching.
  338. */
  339. bool operator==(RegularExpression const&) const;
  340. /**
  341. * Returns true if two regular expressions have different
  342. * compiled program for pattern matching.
  343. */
  344. inline bool operator!=(RegularExpression const&) const;
  345. /**
  346. * Returns true if have the same compiled regular expressions
  347. * and the same start and end pointers.
  348. */
  349. bool deep_equal(RegularExpression const&) const;
  350. /**
  351. * True if the compiled regexp is valid.
  352. */
  353. inline bool is_valid() const;
  354. /**
  355. * Marks the regular expression as invalid.
  356. */
  357. inline void set_invalid();
  358. private:
  359. RegularExpressionMatch regmatch;
  360. char regstart; // Internal use only
  361. char reganch; // Internal use only
  362. const char* regmust; // Internal use only
  363. std::string::size_type regmlen; // Internal use only
  364. char* program;
  365. int progsize;
  366. };
  367. /**
  368. * Create an empty regular expression.
  369. */
  370. inline RegularExpression::RegularExpression()
  371. {
  372. this->program = 0;
  373. }
  374. /**
  375. * Creates a regular expression from string s, and
  376. * compiles s.
  377. */
  378. inline RegularExpression::RegularExpression(const char* s)
  379. {
  380. this->program = 0;
  381. if (s) {
  382. this->compile(s);
  383. }
  384. }
  385. /**
  386. * Creates a regular expression from string s, and
  387. * compiles s.
  388. */
  389. inline RegularExpression::RegularExpression(const std::string& s)
  390. {
  391. this->program = 0;
  392. this->compile(s);
  393. }
  394. /**
  395. * Destroys and frees space allocated for the regular expression.
  396. */
  397. inline RegularExpression::~RegularExpression()
  398. {
  399. //#ifndef _WIN32
  400. delete[] this->program;
  401. //#endif
  402. }
  403. /**
  404. * Compile a regular expression into internal code
  405. * for later pattern matching.
  406. */
  407. inline bool RegularExpression::compile(std::string const& s)
  408. {
  409. return this->compile(s.c_str());
  410. }
  411. /**
  412. * Matches the regular expression to the given std string.
  413. * Returns true if found, and sets start and end indexes accordingly.
  414. */
  415. inline bool RegularExpression::find(const char* s)
  416. {
  417. return this->find(s, this->regmatch);
  418. }
  419. /**
  420. * Matches the regular expression to the given std string.
  421. * Returns true if found, and sets start and end indexes accordingly.
  422. */
  423. inline bool RegularExpression::find(std::string const& s)
  424. {
  425. return this->find(s.c_str());
  426. }
  427. /**
  428. * Returns the internal match object
  429. */
  430. inline RegularExpressionMatch const& RegularExpression::regMatch() const
  431. {
  432. return this->regmatch;
  433. }
  434. /**
  435. * Returns the start index of the full match.
  436. */
  437. inline std::string::size_type RegularExpression::start() const
  438. {
  439. return regmatch.start();
  440. }
  441. /**
  442. * Returns the end index of the full match.
  443. */
  444. inline std::string::size_type RegularExpression::end() const
  445. {
  446. return regmatch.end();
  447. }
  448. /**
  449. * Return start index of nth submatch. start(0) is the start of the full match.
  450. */
  451. inline std::string::size_type RegularExpression::start(int n) const
  452. {
  453. return regmatch.start(n);
  454. }
  455. /**
  456. * Return end index of nth submatch. end(0) is the end of the full match.
  457. */
  458. inline std::string::size_type RegularExpression::end(int n) const
  459. {
  460. return regmatch.end(n);
  461. }
  462. /**
  463. * Return nth submatch as a string.
  464. */
  465. inline std::string RegularExpression::match(int n) const
  466. {
  467. return regmatch.match(n);
  468. }
  469. /**
  470. * Returns true if two regular expressions have different
  471. * compiled program for pattern matching.
  472. */
  473. inline bool RegularExpression::operator!=(const RegularExpression& r) const
  474. {
  475. return (!(*this == r));
  476. }
  477. /**
  478. * Returns true if a valid regular expression is compiled
  479. * and ready for pattern matching.
  480. */
  481. inline bool RegularExpression::is_valid() const
  482. {
  483. return (this->program != 0);
  484. }
  485. inline void RegularExpression::set_invalid()
  486. {
  487. //#ifndef _WIN32
  488. delete[] this->program;
  489. //#endif
  490. this->program = 0;
  491. }
  492. } // namespace @KWSYS_NAMESPACE@
  493. #endif