regex_token_iterator.hpp 13 KB

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