sub_match.hpp 13 KB

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