sub_match.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. ///////////////////////////////////////////////////////////////////////////////
  2. /// \file sub_match.hpp
  3. /// Contains the definition of the class template sub_match\<\>
  4. /// and associated helper functions
  5. //
  6. // Copyright 2008 Eric Niebler. Distributed under the Boost
  7. // Software License, Version 1.0. (See accompanying file
  8. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. #ifndef BOOST_XPRESSIVE_SUB_MATCH_HPP_EAN_10_04_2005
  10. #define BOOST_XPRESSIVE_SUB_MATCH_HPP_EAN_10_04_2005
  11. // MS compatible compilers support #pragma once
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1020)
  13. # pragma once
  14. #endif
  15. #include <iosfwd>
  16. #include <string>
  17. #include <utility>
  18. #include <iterator>
  19. #include <algorithm>
  20. #include <boost/mpl/assert.hpp>
  21. #include <boost/type_traits/is_same.hpp>
  22. #include <boost/iterator/iterator_traits.hpp>
  23. #include <boost/xpressive/detail/detail_fwd.hpp>
  24. //{{AFX_DOC_COMMENT
  25. ///////////////////////////////////////////////////////////////////////////////
  26. // This is a hack to get Doxygen to show the inheritance relation between
  27. // sub_match<T> and std::pair<T,T>.
  28. #ifdef BOOST_XPRESSIVE_DOXYGEN_INVOKED
  29. /// INTERNAL ONLY
  30. namespace std
  31. {
  32. /// INTERNAL ONLY
  33. template<typename, typename> struct pair {};
  34. }
  35. #endif
  36. //}}AFX_DOC_COMMENT
  37. namespace boost { namespace xpressive
  38. {
  39. ///////////////////////////////////////////////////////////////////////////////
  40. // sub_match
  41. //
  42. /// \brief Class template \c sub_match denotes the sequence of characters matched by a particular
  43. /// marked sub-expression.
  44. ///
  45. /// When the marked sub-expression denoted by an object of type \c sub_match\<\> participated in a
  46. /// regular expression match then member \c matched evaluates to \c true, and members \c first and \c second
  47. /// denote the range of characters <tt>[first,second)</tt> which formed that match. Otherwise \c matched is \c false,
  48. /// and members \c first and \c second contained undefined values.
  49. ///
  50. /// If an object of type \c sub_match\<\> represents sub-expression 0 - that is to say the whole match -
  51. /// then member \c matched is always \c true, unless a partial match was obtained as a result of the flag
  52. /// \c match_partial being passed to a regular expression algorithm, in which case member \c matched is
  53. /// \c false, and members \c first and \c second represent the character range that formed the partial match.
  54. template<typename BidiIter>
  55. struct sub_match
  56. : std::pair<BidiIter, BidiIter>
  57. {
  58. private:
  59. /// INTERNAL ONLY
  60. ///
  61. struct dummy { int i_; };
  62. typedef int dummy::*bool_type;
  63. public:
  64. typedef typename iterator_value<BidiIter>::type value_type;
  65. typedef typename iterator_difference<BidiIter>::type difference_type;
  66. typedef typename detail::string_type<value_type>::type string_type;
  67. typedef BidiIter iterator;
  68. sub_match()
  69. : std::pair<BidiIter, BidiIter>()
  70. , matched(false)
  71. {
  72. }
  73. sub_match(BidiIter first, BidiIter second, bool matched_ = false)
  74. : std::pair<BidiIter, BidiIter>(first, second)
  75. , matched(matched_)
  76. {
  77. }
  78. string_type str() const
  79. {
  80. return this->matched ? string_type(this->first, this->second) : string_type();
  81. }
  82. operator string_type() const
  83. {
  84. return this->matched ? string_type(this->first, this->second) : string_type();
  85. }
  86. difference_type length() const
  87. {
  88. return this->matched ? std::distance(this->first, this->second) : 0;
  89. }
  90. operator bool_type() const
  91. {
  92. return this->matched ? &dummy::i_ : 0;
  93. }
  94. bool operator !() const
  95. {
  96. return !this->matched;
  97. }
  98. /// \brief Performs a lexicographic string comparison
  99. /// \param str the string against which to compare
  100. /// \return the results of <tt>(*this).str().compare(str)</tt>
  101. int compare(string_type const &str) const
  102. {
  103. return this->str().compare(str);
  104. }
  105. /// \overload
  106. ///
  107. int compare(sub_match const &sub) const
  108. {
  109. return this->str().compare(sub.str());
  110. }
  111. /// \overload
  112. ///
  113. int compare(value_type const *ptr) const
  114. {
  115. return this->str().compare(ptr);
  116. }
  117. /// \brief true if this sub-match participated in the full match.
  118. bool matched;
  119. };
  120. ///////////////////////////////////////////////////////////////////////////////
  121. /// \brief \c range_begin() to make \c sub_match\<\> a valid range
  122. /// \param sub the \c sub_match\<\> object denoting the range
  123. /// \return \c sub.first
  124. /// \pre \c sub.first is not singular
  125. template<typename BidiIter>
  126. inline BidiIter range_begin(sub_match<BidiIter> &sub)
  127. {
  128. return sub.first;
  129. }
  130. /// \overload
  131. ///
  132. template<typename BidiIter>
  133. inline BidiIter range_begin(sub_match<BidiIter> const &sub)
  134. {
  135. return sub.first;
  136. }
  137. ///////////////////////////////////////////////////////////////////////////////
  138. /// \brief \c range_end() to make \c sub_match\<\> a valid range
  139. /// \param sub the \c sub_match\<\> object denoting the range
  140. /// \return \c sub.second
  141. /// \pre \c sub.second is not singular
  142. template<typename BidiIter>
  143. inline BidiIter range_end(sub_match<BidiIter> &sub)
  144. {
  145. return sub.second;
  146. }
  147. /// \overload
  148. ///
  149. template<typename BidiIter>
  150. inline BidiIter range_end(sub_match<BidiIter> const &sub)
  151. {
  152. return sub.second;
  153. }
  154. ///////////////////////////////////////////////////////////////////////////////
  155. /// \brief insertion operator for sending sub-matches to ostreams
  156. /// \param sout output stream.
  157. /// \param sub sub_match object to be written to the stream.
  158. /// \return sout \<\< sub.str()
  159. template<typename BidiIter, typename Char, typename Traits>
  160. inline std::basic_ostream<Char, Traits> &operator <<
  161. (
  162. std::basic_ostream<Char, Traits> &sout
  163. , sub_match<BidiIter> const &sub
  164. )
  165. {
  166. typedef typename iterator_value<BidiIter>::type char_type;
  167. BOOST_MPL_ASSERT_MSG(
  168. (boost::is_same<Char, char_type>::value)
  169. , CHARACTER_TYPES_OF_STREAM_AND_SUB_MATCH_MUST_MATCH
  170. , (Char, char_type)
  171. );
  172. if(sub.matched)
  173. {
  174. std::ostream_iterator<char_type, Char, Traits> iout(sout);
  175. std::copy(sub.first, sub.second, iout);
  176. }
  177. return sout;
  178. }
  179. // BUGBUG make these more efficient
  180. template<typename BidiIter>
  181. bool operator == (sub_match<BidiIter> const &lhs, sub_match<BidiIter> const &rhs)
  182. {
  183. return lhs.compare(rhs) == 0;
  184. }
  185. template<typename BidiIter>
  186. bool operator != (sub_match<BidiIter> const &lhs, sub_match<BidiIter> const &rhs)
  187. {
  188. return lhs.compare(rhs) != 0;
  189. }
  190. template<typename BidiIter>
  191. bool operator < (sub_match<BidiIter> const &lhs, sub_match<BidiIter> const &rhs)
  192. {
  193. return lhs.compare(rhs) < 0;
  194. }
  195. template<typename BidiIter>
  196. bool operator <= (sub_match<BidiIter> const &lhs, sub_match<BidiIter> const &rhs)
  197. {
  198. return lhs.compare(rhs) <= 0;
  199. }
  200. template<typename BidiIter>
  201. bool operator >= (sub_match<BidiIter> const &lhs, sub_match<BidiIter> const &rhs)
  202. {
  203. return lhs.compare(rhs) >= 0;
  204. }
  205. template<typename BidiIter>
  206. bool operator > (sub_match<BidiIter> const &lhs, sub_match<BidiIter> const &rhs)
  207. {
  208. return lhs.compare(rhs) > 0;
  209. }
  210. template<typename BidiIter>
  211. bool operator == (typename iterator_value<BidiIter>::type const *lhs, sub_match<BidiIter> const &rhs)
  212. {
  213. return lhs == rhs.str();
  214. }
  215. template<typename BidiIter>
  216. bool operator != (typename iterator_value<BidiIter>::type const *lhs, sub_match<BidiIter> const &rhs)
  217. {
  218. return lhs != rhs.str();
  219. }
  220. template<typename BidiIter>
  221. bool operator < (typename iterator_value<BidiIter>::type const *lhs, sub_match<BidiIter> const &rhs)
  222. {
  223. return lhs < rhs.str();
  224. }
  225. template<typename BidiIter>
  226. bool operator > (typename iterator_value<BidiIter>::type const *lhs, sub_match<BidiIter> const &rhs)
  227. {
  228. return lhs> rhs.str();
  229. }
  230. template<typename BidiIter>
  231. bool operator >= (typename iterator_value<BidiIter>::type const *lhs, sub_match<BidiIter> const &rhs)
  232. {
  233. return lhs >= rhs.str();
  234. }
  235. template<typename BidiIter>
  236. bool operator <= (typename iterator_value<BidiIter>::type const *lhs, sub_match<BidiIter> const &rhs)
  237. {
  238. return lhs <= rhs.str();
  239. }
  240. template<typename BidiIter>
  241. bool operator == (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const *rhs)
  242. {
  243. return lhs.str() == rhs;
  244. }
  245. template<typename BidiIter>
  246. bool operator != (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const *rhs)
  247. {
  248. return lhs.str() != rhs;
  249. }
  250. template<typename BidiIter>
  251. bool operator < (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const *rhs)
  252. {
  253. return lhs.str() < rhs;
  254. }
  255. template<typename BidiIter>
  256. bool operator > (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const *rhs)
  257. {
  258. return lhs.str() > rhs;
  259. }
  260. template<typename BidiIter>
  261. bool operator >= (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const *rhs)
  262. {
  263. return lhs.str() >= rhs;
  264. }
  265. template<typename BidiIter>
  266. bool operator <= (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const *rhs)
  267. {
  268. return lhs.str() <= rhs;
  269. }
  270. template<typename BidiIter>
  271. bool operator == (typename iterator_value<BidiIter>::type const &lhs, sub_match<BidiIter> const &rhs)
  272. {
  273. return lhs == rhs.str();
  274. }
  275. template<typename BidiIter>
  276. bool operator != (typename iterator_value<BidiIter>::type const &lhs, sub_match<BidiIter> const &rhs)
  277. {
  278. return lhs != rhs.str();
  279. }
  280. template<typename BidiIter>
  281. bool operator < (typename iterator_value<BidiIter>::type const &lhs, sub_match<BidiIter> const &rhs)
  282. {
  283. return lhs < rhs.str();
  284. }
  285. template<typename BidiIter>
  286. bool operator > (typename iterator_value<BidiIter>::type const &lhs, sub_match<BidiIter> const &rhs)
  287. {
  288. return lhs> rhs.str();
  289. }
  290. template<typename BidiIter>
  291. bool operator >= (typename iterator_value<BidiIter>::type const &lhs, sub_match<BidiIter> const &rhs)
  292. {
  293. return lhs >= rhs.str();
  294. }
  295. template<typename BidiIter>
  296. bool operator <= (typename iterator_value<BidiIter>::type const &lhs, sub_match<BidiIter> const &rhs)
  297. {
  298. return lhs <= rhs.str();
  299. }
  300. template<typename BidiIter>
  301. bool operator == (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const &rhs)
  302. {
  303. return lhs.str() == rhs;
  304. }
  305. template<typename BidiIter>
  306. bool operator != (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const &rhs)
  307. {
  308. return lhs.str() != rhs;
  309. }
  310. template<typename BidiIter>
  311. bool operator < (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const &rhs)
  312. {
  313. return lhs.str() < rhs;
  314. }
  315. template<typename BidiIter>
  316. bool operator > (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const &rhs)
  317. {
  318. return lhs.str() > rhs;
  319. }
  320. template<typename BidiIter>
  321. bool operator >= (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const &rhs)
  322. {
  323. return lhs.str() >= rhs;
  324. }
  325. template<typename BidiIter>
  326. bool operator <= (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const &rhs)
  327. {
  328. return lhs.str() <= rhs;
  329. }
  330. // Operator+ convenience function
  331. template<typename BidiIter>
  332. typename sub_match<BidiIter>::string_type
  333. operator + (sub_match<BidiIter> const &lhs, sub_match<BidiIter> const &rhs)
  334. {
  335. return lhs.str() + rhs.str();
  336. }
  337. template<typename BidiIter>
  338. typename sub_match<BidiIter>::string_type
  339. operator + (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const &rhs)
  340. {
  341. return lhs.str() + rhs;
  342. }
  343. template<typename BidiIter>
  344. typename sub_match<BidiIter>::string_type
  345. operator + (typename iterator_value<BidiIter>::type const &lhs, sub_match<BidiIter> const &rhs)
  346. {
  347. return lhs + rhs.str();
  348. }
  349. template<typename BidiIter>
  350. typename sub_match<BidiIter>::string_type
  351. operator + (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const *rhs)
  352. {
  353. return lhs.str() + rhs;
  354. }
  355. template<typename BidiIter>
  356. typename sub_match<BidiIter>::string_type
  357. operator + (typename iterator_value<BidiIter>::type const *lhs, sub_match<BidiIter> const &rhs)
  358. {
  359. return lhs + rhs.str();
  360. }
  361. template<typename BidiIter>
  362. typename sub_match<BidiIter>::string_type
  363. operator + (sub_match<BidiIter> const &lhs, typename sub_match<BidiIter>::string_type const &rhs)
  364. {
  365. return lhs.str() + rhs;
  366. }
  367. template<typename BidiIter>
  368. typename sub_match<BidiIter>::string_type
  369. operator + (typename sub_match<BidiIter>::string_type const &lhs, sub_match<BidiIter> const &rhs)
  370. {
  371. return lhs + rhs.str();
  372. }
  373. }} // namespace boost::xpressive
  374. // Hook the Boost.Range customization points to make sub_match a valid range.
  375. namespace boost
  376. {
  377. /// INTERNAL ONLY
  378. ///
  379. template<typename Range>
  380. struct range_mutable_iterator;
  381. /// INTERNAL ONLY
  382. ///
  383. template<typename BidiIter>
  384. struct range_mutable_iterator<xpressive::sub_match<BidiIter> >
  385. {
  386. typedef BidiIter type;
  387. };
  388. /// INTERNAL ONLY
  389. ///
  390. template<typename Range>
  391. struct range_const_iterator;
  392. /// INTERNAL ONLY
  393. ///
  394. template<typename BidiIter>
  395. struct range_const_iterator<xpressive::sub_match<BidiIter> >
  396. {
  397. typedef BidiIter type;
  398. };
  399. }
  400. #endif