/Src/Dependencies/Boost/boost/xpressive/sub_match.hpp

http://hadesmem.googlecode.com/ · C++ Header · 392 lines · 285 code · 60 blank · 47 comment · 22 complexity · c55c80ebe95c2b71060798d5405b203d MD5 · raw file

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