regex_token_iterator.hpp 13 KB

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