RegularExpression.hxx.in 16 KB

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