regex_token_iterator.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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_V4_REGEX_TOKEN_ITERATOR_HPP
  18. #define BOOST_REGEX_V4_REGEX_TOKEN_ITERATOR_HPP
  19. #include <boost/shared_ptr.hpp>
  20. #include <boost/detail/workaround.hpp>
  21. #if (BOOST_WORKAROUND(__BORLANDC__, >= 0x560) && BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x570)))\
  22. || BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003))
  23. //
  24. // Borland C++ Builder 6, and Visual C++ 6,
  25. // can't cope with the array template constructor
  26. // so we have a template member that will accept any type as
  27. // argument, and then assert that is really is an array:
  28. //
  29. #include <boost/static_assert.hpp>
  30. #include <boost/type_traits/is_array.hpp>
  31. #endif
  32. namespace boost{
  33. #ifdef BOOST_MSVC
  34. #pragma warning(push)
  35. #pragma warning(disable: 4103)
  36. #endif
  37. #ifdef BOOST_HAS_ABI_HEADERS
  38. # include BOOST_ABI_PREFIX
  39. #endif
  40. #ifdef BOOST_MSVC
  41. #pragma warning(pop)
  42. #pragma warning(push)
  43. #pragma warning(disable:4700)
  44. #endif
  45. template <class BidirectionalIterator,
  46. class charT,
  47. class traits>
  48. class regex_token_iterator_implementation
  49. {
  50. typedef basic_regex<charT, traits> regex_type;
  51. typedef sub_match<BidirectionalIterator> value_type;
  52. match_results<BidirectionalIterator> what; // current match
  53. BidirectionalIterator base; // start of search area
  54. BidirectionalIterator end; // end of search area
  55. const regex_type re; // the expression
  56. match_flag_type flags; // match flags
  57. value_type result; // the current string result
  58. int N; // the current sub-expression being enumerated
  59. std::vector<int> subs; // the sub-expressions to enumerate
  60. public:
  61. regex_token_iterator_implementation(const regex_type* p, BidirectionalIterator last, int sub, match_flag_type f)
  62. : end(last), re(*p), flags(f){ subs.push_back(sub); }
  63. regex_token_iterator_implementation(const regex_type* p, BidirectionalIterator last, const std::vector<int>& v, match_flag_type f)
  64. : end(last), re(*p), flags(f), subs(v){}
  65. #if !BOOST_WORKAROUND(__HP_aCC, < 60700)
  66. #if (BOOST_WORKAROUND(__BORLANDC__, >= 0x560) && BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x570)))\
  67. || BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003)) \
  68. || BOOST_WORKAROUND(__HP_aCC, < 60700)
  69. template <class T>
  70. regex_token_iterator_implementation(const regex_type* p, BidirectionalIterator last, const T& submatches, match_flag_type f)
  71. : end(last), re(*p), flags(f)
  72. {
  73. // assert that T really is an array:
  74. BOOST_STATIC_ASSERT(::boost::is_array<T>::value);
  75. const std::size_t array_size = sizeof(T) / sizeof(submatches[0]);
  76. for(std::size_t i = 0; i < array_size; ++i)
  77. {
  78. subs.push_back(submatches[i]);
  79. }
  80. }
  81. #else
  82. template <std::size_t CN>
  83. regex_token_iterator_implementation(const regex_type* p, BidirectionalIterator last, const int (&submatches)[CN], match_flag_type f)
  84. : end(last), re(*p), flags(f)
  85. {
  86. for(std::size_t i = 0; i < CN; ++i)
  87. {
  88. subs.push_back(submatches[i]);
  89. }
  90. }
  91. #endif
  92. #endif
  93. bool init(BidirectionalIterator first)
  94. {
  95. N = 0;
  96. base = first;
  97. if(regex_search(first, end, what, re, flags, base) == true)
  98. {
  99. N = 0;
  100. result = ((subs[N] == -1) ? what.prefix() : what[(int)subs[N]]);
  101. return true;
  102. }
  103. else if((subs[N] == -1) && (first != end))
  104. {
  105. result.first = first;
  106. result.second = end;
  107. result.matched = (first != end);
  108. N = -1;
  109. return true;
  110. }
  111. return false;
  112. }
  113. bool compare(const regex_token_iterator_implementation& that)
  114. {
  115. if(this == &that) return true;
  116. return (&re.get_data() == &that.re.get_data())
  117. && (end == that.end)
  118. && (flags == that.flags)
  119. && (N == that.N)
  120. && (what[0].first == that.what[0].first)
  121. && (what[0].second == that.what[0].second);
  122. }
  123. const value_type& get()
  124. { return result; }
  125. bool next()
  126. {
  127. if(N == -1)
  128. return false;
  129. if(N+1 < (int)subs.size())
  130. {
  131. ++N;
  132. result =((subs[N] == -1) ? what.prefix() : what[subs[N]]);
  133. return true;
  134. }
  135. //if(what.prefix().first != what[0].second)
  136. // flags |= /*match_prev_avail |*/ regex_constants::match_not_bob;
  137. BidirectionalIterator last_end(what[0].second);
  138. if(regex_search(last_end, end, what, re, ((what[0].first == what[0].second) ? flags | regex_constants::match_not_initial_null : flags), base))
  139. {
  140. N =0;
  141. result =((subs[N] == -1) ? what.prefix() : what[subs[N]]);
  142. return true;
  143. }
  144. else if((last_end != end) && (subs[0] == -1))
  145. {
  146. N =-1;
  147. result.first = last_end;
  148. result.second = end;
  149. result.matched = (last_end != end);
  150. return true;
  151. }
  152. return false;
  153. }
  154. private:
  155. regex_token_iterator_implementation& operator=(const regex_token_iterator_implementation&);
  156. };
  157. template <class BidirectionalIterator,
  158. class charT = BOOST_DEDUCED_TYPENAME BOOST_REGEX_DETAIL_NS::regex_iterator_traits<BidirectionalIterator>::value_type,
  159. class traits = regex_traits<charT> >
  160. class regex_token_iterator
  161. {
  162. private:
  163. typedef regex_token_iterator_implementation<BidirectionalIterator, charT, traits> impl;
  164. typedef shared_ptr<impl> pimpl;
  165. public:
  166. typedef basic_regex<charT, traits> regex_type;
  167. typedef sub_match<BidirectionalIterator> value_type;
  168. typedef typename BOOST_REGEX_DETAIL_NS::regex_iterator_traits<BidirectionalIterator>::difference_type
  169. difference_type;
  170. typedef const value_type* pointer;
  171. typedef const value_type& reference;
  172. typedef std::forward_iterator_tag iterator_category;
  173. regex_token_iterator(){}
  174. regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b, const regex_type& re,
  175. int submatch = 0, match_flag_type m = match_default)
  176. : pdata(new impl(&re, b, submatch, m))
  177. {
  178. if(!pdata->init(a))
  179. pdata.reset();
  180. }
  181. regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b, const regex_type& re,
  182. const std::vector<int>& submatches, match_flag_type m = match_default)
  183. : pdata(new impl(&re, b, submatches, m))
  184. {
  185. if(!pdata->init(a))
  186. pdata.reset();
  187. }
  188. #if !BOOST_WORKAROUND(__HP_aCC, < 60700)
  189. #if (BOOST_WORKAROUND(__BORLANDC__, >= 0x560) && BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x570)))\
  190. || BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003)) \
  191. || BOOST_WORKAROUND(__HP_aCC, < 60700)
  192. template <class T>
  193. regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b, const regex_type& re,
  194. const T& submatches, match_flag_type m = match_default)
  195. : pdata(new impl(&re, b, submatches, m))
  196. {
  197. if(!pdata->init(a))
  198. pdata.reset();
  199. }
  200. #else
  201. template <std::size_t N>
  202. regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b, const regex_type& re,
  203. const int (&submatches)[N], match_flag_type m = match_default)
  204. : pdata(new impl(&re, b, submatches, m))
  205. {
  206. if(!pdata->init(a))
  207. pdata.reset();
  208. }
  209. #endif
  210. #endif
  211. regex_token_iterator(const regex_token_iterator& that)
  212. : pdata(that.pdata) {}
  213. regex_token_iterator& operator=(const regex_token_iterator& that)
  214. {
  215. pdata = that.pdata;
  216. return *this;
  217. }
  218. bool operator==(const regex_token_iterator& that)const
  219. {
  220. if((pdata.get() == 0) || (that.pdata.get() == 0))
  221. return pdata.get() == that.pdata.get();
  222. return pdata->compare(*(that.pdata.get()));
  223. }
  224. bool operator!=(const regex_token_iterator& that)const
  225. { return !(*this == that); }
  226. const value_type& operator*()const
  227. { return pdata->get(); }
  228. const value_type* operator->()const
  229. { return &(pdata->get()); }
  230. regex_token_iterator& operator++()
  231. {
  232. cow();
  233. if(0 == pdata->next())
  234. {
  235. pdata.reset();
  236. }
  237. return *this;
  238. }
  239. regex_token_iterator operator++(int)
  240. {
  241. regex_token_iterator result(*this);
  242. ++(*this);
  243. return result;
  244. }
  245. private:
  246. pimpl pdata;
  247. void cow()
  248. {
  249. // copy-on-write
  250. if(pdata.get() && !pdata.unique())
  251. {
  252. pdata.reset(new impl(*(pdata.get())));
  253. }
  254. }
  255. };
  256. typedef regex_token_iterator<const char*> cregex_token_iterator;
  257. typedef regex_token_iterator<std::string::const_iterator> sregex_token_iterator;
  258. #ifndef BOOST_NO_WREGEX
  259. typedef regex_token_iterator<const wchar_t*> wcregex_token_iterator;
  260. typedef regex_token_iterator<std::wstring::const_iterator> wsregex_token_iterator;
  261. #endif
  262. template <class charT, class traits>
  263. 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)
  264. {
  265. return regex_token_iterator<const charT*, charT, traits>(p, p+traits::length(p), e, submatch, m);
  266. }
  267. template <class charT, class traits, class ST, class SA>
  268. 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)
  269. {
  270. return regex_token_iterator<typename std::basic_string<charT, ST, SA>::const_iterator, charT, traits>(p.begin(), p.end(), e, submatch, m);
  271. }
  272. template <class charT, class traits, std::size_t N>
  273. 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)
  274. {
  275. return regex_token_iterator<const charT*, charT, traits>(p, p+traits::length(p), e, submatch, m);
  276. }
  277. template <class charT, class traits, class ST, class SA, std::size_t N>
  278. 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)
  279. {
  280. return regex_token_iterator<typename std::basic_string<charT, ST, SA>::const_iterator, charT, traits>(p.begin(), p.end(), e, submatch, m);
  281. }
  282. template <class charT, class traits>
  283. 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)
  284. {
  285. return regex_token_iterator<const charT*, charT, traits>(p, p+traits::length(p), e, submatch, m);
  286. }
  287. template <class charT, class traits, class ST, class SA>
  288. 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)
  289. {
  290. return regex_token_iterator<typename std::basic_string<charT, ST, SA>::const_iterator, charT, traits>(p.begin(), p.end(), e, submatch, m);
  291. }
  292. #ifdef BOOST_MSVC
  293. #pragma warning(pop)
  294. #pragma warning(push)
  295. #pragma warning(disable: 4103)
  296. #endif
  297. #ifdef BOOST_HAS_ABI_HEADERS
  298. # include BOOST_ABI_SUFFIX
  299. #endif
  300. #ifdef BOOST_MSVC
  301. #pragma warning(pop)
  302. #endif
  303. } // namespace boost
  304. #endif // BOOST_REGEX_V4_REGEX_TOKEN_ITERATOR_HPP