regex_token_iterator.hpp 11 KB

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