regex_token_iterator.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. *
  3. * Copyright (c) 2003
  4. * John Maddock
  5. *
  6. * Use, modification and distribution are subject to the
  7. * Boost 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. *
  10. */
  11. /*
  12. * LOCATION: see http://www.boost.org for most recent version.
  13. * FILE regex_token_iterator.hpp
  14. * VERSION see <boost/version.hpp>
  15. * DESCRIPTION: Provides regex_token_iterator implementation.
  16. */
  17. #ifndef BOOST_REGEX_V5_REGEX_TOKEN_ITERATOR_HPP
  18. #define BOOST_REGEX_V5_REGEX_TOKEN_ITERATOR_HPP
  19. #include <memory>
  20. namespace boost{
  21. template <class BidirectionalIterator,
  22. class charT,
  23. class traits>
  24. class regex_token_iterator_implementation
  25. {
  26. typedef basic_regex<charT, traits> regex_type;
  27. typedef sub_match<BidirectionalIterator> value_type;
  28. match_results<BidirectionalIterator> what; // current match
  29. BidirectionalIterator base; // start of search area
  30. BidirectionalIterator end; // end of search area
  31. const regex_type re; // the expression
  32. match_flag_type flags; // match flags
  33. value_type result; // the current string result
  34. int N; // the current sub-expression being enumerated
  35. std::vector<int> subs; // the sub-expressions to enumerate
  36. public:
  37. regex_token_iterator_implementation(const regex_type* p, BidirectionalIterator last, int sub, match_flag_type f)
  38. : end(last), re(*p), flags(f), N(0){ subs.push_back(sub); }
  39. regex_token_iterator_implementation(const regex_type* p, BidirectionalIterator last, const std::vector<int>& v, match_flag_type f)
  40. : end(last), re(*p), flags(f), N(0), subs(v){}
  41. template <std::size_t CN>
  42. regex_token_iterator_implementation(const regex_type* p, BidirectionalIterator last, const int (&submatches)[CN], match_flag_type f)
  43. : end(last), re(*p), flags(f), N(0)
  44. {
  45. for(std::size_t i = 0; i < CN; ++i)
  46. {
  47. subs.push_back(submatches[i]);
  48. }
  49. }
  50. regex_token_iterator_implementation(const regex_token_iterator_implementation& other) = default;
  51. bool init(BidirectionalIterator first)
  52. {
  53. N = 0;
  54. base = first;
  55. if(regex_search(first, end, what, re, flags, base) == true)
  56. {
  57. N = 0;
  58. result = ((subs[N] == -1) ? what.prefix() : what[(int)subs[N]]);
  59. return true;
  60. }
  61. else if((subs[N] == -1) && (first != end))
  62. {
  63. result.first = first;
  64. result.second = end;
  65. result.matched = (first != end);
  66. N = -1;
  67. return true;
  68. }
  69. return false;
  70. }
  71. bool compare(const regex_token_iterator_implementation& that)
  72. {
  73. if(this == &that) return true;
  74. return (&re.get_data() == &that.re.get_data())
  75. && (end == that.end)
  76. && (flags == that.flags)
  77. && (N == that.N)
  78. && (what[0].first == that.what[0].first)
  79. && (what[0].second == that.what[0].second);
  80. }
  81. const value_type& get()
  82. { return result; }
  83. bool next()
  84. {
  85. if(N == -1)
  86. return false;
  87. if(N+1 < (int)subs.size())
  88. {
  89. ++N;
  90. result =((subs[N] == -1) ? what.prefix() : what[subs[N]]);
  91. return true;
  92. }
  93. //if(what.prefix().first != what[0].second)
  94. // flags |= /*match_prev_avail |*/ regex_constants::match_not_bob;
  95. BidirectionalIterator last_end(what[0].second);
  96. if(regex_search(last_end, end, what, re, ((what[0].first == what[0].second) ? flags | regex_constants::match_not_initial_null : flags), base))
  97. {
  98. N =0;
  99. result =((subs[N] == -1) ? what.prefix() : what[subs[N]]);
  100. return true;
  101. }
  102. else if((last_end != end) && (subs[0] == -1))
  103. {
  104. N =-1;
  105. result.first = last_end;
  106. result.second = end;
  107. result.matched = (last_end != end);
  108. return true;
  109. }
  110. return false;
  111. }
  112. private:
  113. regex_token_iterator_implementation& operator=(const regex_token_iterator_implementation&);
  114. };
  115. template <class BidirectionalIterator,
  116. class charT = typename std::iterator_traits<BidirectionalIterator>::value_type,
  117. class traits = regex_traits<charT> >
  118. class regex_token_iterator
  119. {
  120. private:
  121. typedef regex_token_iterator_implementation<BidirectionalIterator, charT, traits> impl;
  122. typedef std::shared_ptr<impl> pimpl;
  123. public:
  124. typedef basic_regex<charT, traits> regex_type;
  125. typedef sub_match<BidirectionalIterator> value_type;
  126. typedef typename std::iterator_traits<BidirectionalIterator>::difference_type
  127. difference_type;
  128. typedef const value_type* pointer;
  129. typedef const value_type& reference;
  130. typedef std::forward_iterator_tag iterator_category;
  131. regex_token_iterator(){}
  132. regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b, const regex_type& re,
  133. int submatch = 0, match_flag_type m = match_default)
  134. : pdata(new impl(&re, b, submatch, m))
  135. {
  136. if(!pdata->init(a))
  137. pdata.reset();
  138. }
  139. regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b, const regex_type& re,
  140. const std::vector<int>& submatches, match_flag_type m = match_default)
  141. : pdata(new impl(&re, b, submatches, m))
  142. {
  143. if(!pdata->init(a))
  144. pdata.reset();
  145. }
  146. template <std::size_t N>
  147. regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b, const regex_type& re,
  148. const int (&submatches)[N], match_flag_type m = match_default)
  149. : pdata(new impl(&re, b, submatches, m))
  150. {
  151. if(!pdata->init(a))
  152. pdata.reset();
  153. }
  154. regex_token_iterator(const regex_token_iterator& that)
  155. : pdata(that.pdata) {}
  156. regex_token_iterator& operator=(const regex_token_iterator& that)
  157. {
  158. pdata = that.pdata;
  159. return *this;
  160. }
  161. bool operator==(const regex_token_iterator& that)const
  162. {
  163. if((pdata.get() == 0) || (that.pdata.get() == 0))
  164. return pdata.get() == that.pdata.get();
  165. return pdata->compare(*(that.pdata.get()));
  166. }
  167. bool operator!=(const regex_token_iterator& that)const
  168. { return !(*this == that); }
  169. const value_type& operator*()const
  170. { return pdata->get(); }
  171. const value_type* operator->()const
  172. { return &(pdata->get()); }
  173. regex_token_iterator& operator++()
  174. {
  175. cow();
  176. if(0 == pdata->next())
  177. {
  178. pdata.reset();
  179. }
  180. return *this;
  181. }
  182. regex_token_iterator operator++(int)
  183. {
  184. regex_token_iterator result(*this);
  185. ++(*this);
  186. return result;
  187. }
  188. private:
  189. pimpl pdata;
  190. void cow()
  191. {
  192. // copy-on-write
  193. if(pdata.get() && (pdata.use_count() > 1))
  194. {
  195. pdata.reset(new impl(*(pdata.get())));
  196. }
  197. }
  198. };
  199. typedef regex_token_iterator<const char*> cregex_token_iterator;
  200. typedef regex_token_iterator<std::string::const_iterator> sregex_token_iterator;
  201. #ifndef BOOST_NO_WREGEX
  202. typedef regex_token_iterator<const wchar_t*> wcregex_token_iterator;
  203. typedef regex_token_iterator<std::wstring::const_iterator> wsregex_token_iterator;
  204. #endif
  205. template <class charT, class traits>
  206. inline regex_token_iterator<const charT*, charT, traits> make_regex_token_iterator(const charT* p, const basic_regex<charT, traits>& e, int submatch = 0, regex_constants::match_flag_type m = regex_constants::match_default)
  207. {
  208. return regex_token_iterator<const charT*, charT, traits>(p, p+traits::length(p), e, submatch, m);
  209. }
  210. template <class charT, class traits, class ST, class SA>
  211. inline regex_token_iterator<typename std::basic_string<charT, ST, SA>::const_iterator, charT, traits> make_regex_token_iterator(const std::basic_string<charT, ST, SA>& p, const basic_regex<charT, traits>& e, int submatch = 0, regex_constants::match_flag_type m = regex_constants::match_default)
  212. {
  213. return regex_token_iterator<typename std::basic_string<charT, ST, SA>::const_iterator, charT, traits>(p.begin(), p.end(), e, submatch, m);
  214. }
  215. template <class charT, class traits, std::size_t N>
  216. inline regex_token_iterator<const charT*, charT, traits> make_regex_token_iterator(const charT* p, const basic_regex<charT, traits>& e, const int (&submatch)[N], regex_constants::match_flag_type m = regex_constants::match_default)
  217. {
  218. return regex_token_iterator<const charT*, charT, traits>(p, p+traits::length(p), e, submatch, m);
  219. }
  220. template <class charT, class traits, class ST, class SA, std::size_t N>
  221. inline regex_token_iterator<typename std::basic_string<charT, ST, SA>::const_iterator, charT, traits> make_regex_token_iterator(const std::basic_string<charT, ST, SA>& p, const basic_regex<charT, traits>& e, const int (&submatch)[N], regex_constants::match_flag_type m = regex_constants::match_default)
  222. {
  223. return regex_token_iterator<typename std::basic_string<charT, ST, SA>::const_iterator, charT, traits>(p.begin(), p.end(), e, submatch, m);
  224. }
  225. template <class charT, class traits>
  226. inline regex_token_iterator<const charT*, charT, traits> make_regex_token_iterator(const charT* p, const basic_regex<charT, traits>& e, const std::vector<int>& submatch, regex_constants::match_flag_type m = regex_constants::match_default)
  227. {
  228. return regex_token_iterator<const charT*, charT, traits>(p, p+traits::length(p), e, submatch, m);
  229. }
  230. template <class charT, class traits, class ST, class SA>
  231. inline regex_token_iterator<typename std::basic_string<charT, ST, SA>::const_iterator, charT, traits> make_regex_token_iterator(const std::basic_string<charT, ST, SA>& p, const basic_regex<charT, traits>& e, const std::vector<int>& submatch, regex_constants::match_flag_type m = regex_constants::match_default)
  232. {
  233. return regex_token_iterator<typename std::basic_string<charT, ST, SA>::const_iterator, charT, traits>(p.begin(), p.end(), e, submatch, m);
  234. }
  235. } // namespace boost
  236. #endif // BOOST_REGEX_V5_REGEX_TOKEN_ITERATOR_HPP