u32regex_token_iterator.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 u32regex_token_iterator.hpp
  14. * VERSION see <boost/version.hpp>
  15. * DESCRIPTION: Provides u32regex_token_iterator implementation.
  16. */
  17. #ifndef BOOST_REGEX_V5_U32REGEX_TOKEN_ITERATOR_HPP
  18. #define BOOST_REGEX_V5_U32REGEX_TOKEN_ITERATOR_HPP
  19. namespace boost{
  20. #ifdef BOOST_REGEX_MSVC
  21. # pragma warning(push)
  22. # pragma warning(disable:4700)
  23. #endif
  24. template <class BidirectionalIterator>
  25. class u32regex_token_iterator_implementation
  26. {
  27. typedef u32regex regex_type;
  28. typedef sub_match<BidirectionalIterator> value_type;
  29. match_results<BidirectionalIterator> what; // current match
  30. BidirectionalIterator end; // end of search area
  31. BidirectionalIterator base; // start of search area
  32. const regex_type re; // the expression
  33. match_flag_type flags; // match flags
  34. value_type result; // the current string result
  35. int N; // the current sub-expression being enumerated
  36. std::vector<int> subs; // the sub-expressions to enumerate
  37. public:
  38. u32regex_token_iterator_implementation(const regex_type* p, BidirectionalIterator last, int sub, match_flag_type f)
  39. : end(last), re(*p), flags(f){ subs.push_back(sub); }
  40. u32regex_token_iterator_implementation(const regex_type* p, BidirectionalIterator last, const std::vector<int>& v, match_flag_type f)
  41. : end(last), re(*p), flags(f), subs(v){}
  42. template <std::size_t CN>
  43. u32regex_token_iterator_implementation(const regex_type* p, BidirectionalIterator last, const int (&submatches)[CN], match_flag_type f)
  44. : end(last), re(*p), flags(f)
  45. {
  46. for(std::size_t i = 0; i < CN; ++i)
  47. {
  48. subs.push_back(submatches[i]);
  49. }
  50. }
  51. bool init(BidirectionalIterator first)
  52. {
  53. base = first;
  54. N = 0;
  55. if(u32regex_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 u32regex_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(u32regex_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. u32regex_token_iterator_implementation& operator=(const u32regex_token_iterator_implementation&);
  114. };
  115. template <class BidirectionalIterator>
  116. class u32regex_token_iterator
  117. {
  118. private:
  119. typedef u32regex_token_iterator_implementation<BidirectionalIterator> impl;
  120. typedef std::shared_ptr<impl> pimpl;
  121. public:
  122. typedef u32regex regex_type;
  123. typedef sub_match<BidirectionalIterator> value_type;
  124. typedef typename std::iterator_traits<BidirectionalIterator>::difference_type
  125. difference_type;
  126. typedef const value_type* pointer;
  127. typedef const value_type& reference;
  128. typedef std::forward_iterator_tag iterator_category;
  129. u32regex_token_iterator(){}
  130. u32regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b, const regex_type& re,
  131. int submatch = 0, match_flag_type m = match_default)
  132. : pdata(new impl(&re, b, submatch, m))
  133. {
  134. if(!pdata->init(a))
  135. pdata.reset();
  136. }
  137. u32regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b, const regex_type& re,
  138. const std::vector<int>& submatches, match_flag_type m = match_default)
  139. : pdata(new impl(&re, b, submatches, m))
  140. {
  141. if(!pdata->init(a))
  142. pdata.reset();
  143. }
  144. template <std::size_t N>
  145. u32regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b, const regex_type& re,
  146. const int (&submatches)[N], match_flag_type m = match_default)
  147. : pdata(new impl(&re, b, submatches, m))
  148. {
  149. if(!pdata->init(a))
  150. pdata.reset();
  151. }
  152. u32regex_token_iterator(const u32regex_token_iterator& that)
  153. : pdata(that.pdata) {}
  154. u32regex_token_iterator& operator=(const u32regex_token_iterator& that)
  155. {
  156. pdata = that.pdata;
  157. return *this;
  158. }
  159. bool operator==(const u32regex_token_iterator& that)const
  160. {
  161. if((pdata.get() == 0) || (that.pdata.get() == 0))
  162. return pdata.get() == that.pdata.get();
  163. return pdata->compare(*(that.pdata.get()));
  164. }
  165. bool operator!=(const u32regex_token_iterator& that)const
  166. { return !(*this == that); }
  167. const value_type& operator*()const
  168. { return pdata->get(); }
  169. const value_type* operator->()const
  170. { return &(pdata->get()); }
  171. u32regex_token_iterator& operator++()
  172. {
  173. cow();
  174. if(0 == pdata->next())
  175. {
  176. pdata.reset();
  177. }
  178. return *this;
  179. }
  180. u32regex_token_iterator operator++(int)
  181. {
  182. u32regex_token_iterator result(*this);
  183. ++(*this);
  184. return result;
  185. }
  186. private:
  187. pimpl pdata;
  188. void cow()
  189. {
  190. // copy-on-write
  191. if(pdata.get() && (pdata.use_count() > 1))
  192. {
  193. pdata.reset(new impl(*(pdata.get())));
  194. }
  195. }
  196. };
  197. typedef u32regex_token_iterator<const char*> utf8regex_token_iterator;
  198. typedef u32regex_token_iterator<const UChar*> utf16regex_token_iterator;
  199. typedef u32regex_token_iterator<const UChar32*> utf32regex_token_iterator;
  200. // construction from an integral sub_match state_id:
  201. inline u32regex_token_iterator<const char*> make_u32regex_token_iterator(const char* p, const u32regex& e, int submatch = 0, regex_constants::match_flag_type m = regex_constants::match_default)
  202. {
  203. return u32regex_token_iterator<const char*>(p, p+std::strlen(p), e, submatch, m);
  204. }
  205. #ifndef BOOST_NO_WREGEX
  206. inline u32regex_token_iterator<const wchar_t*> make_u32regex_token_iterator(const wchar_t* p, const u32regex& e, int submatch = 0, regex_constants::match_flag_type m = regex_constants::match_default)
  207. {
  208. return u32regex_token_iterator<const wchar_t*>(p, p+std::wcslen(p), e, submatch, m);
  209. }
  210. #endif
  211. #if !defined(BOOST_REGEX_UCHAR_IS_WCHAR_T)
  212. inline u32regex_token_iterator<const UChar*> make_u32regex_token_iterator(const UChar* p, const u32regex& e, int submatch = 0, regex_constants::match_flag_type m = regex_constants::match_default)
  213. {
  214. return u32regex_token_iterator<const UChar*>(p, p+u_strlen(p), e, submatch, m);
  215. }
  216. #endif
  217. template <class charT, class Traits, class Alloc>
  218. inline u32regex_token_iterator<typename std::basic_string<charT, Traits, Alloc>::const_iterator> make_u32regex_token_iterator(const std::basic_string<charT, Traits, Alloc>& p, const u32regex& e, int submatch = 0, regex_constants::match_flag_type m = regex_constants::match_default)
  219. {
  220. typedef typename std::basic_string<charT, Traits, Alloc>::const_iterator iter_type;
  221. return u32regex_token_iterator<iter_type>(p.begin(), p.end(), e, submatch, m);
  222. }
  223. inline u32regex_token_iterator<const UChar*> make_u32regex_token_iterator(const U_NAMESPACE_QUALIFIER UnicodeString& s, const u32regex& e, int submatch = 0, regex_constants::match_flag_type m = regex_constants::match_default)
  224. {
  225. return u32regex_token_iterator<const UChar*>(s.getBuffer(), s.getBuffer() + s.length(), e, submatch, m);
  226. }
  227. // construction from a reference to an array:
  228. template <std::size_t N>
  229. inline u32regex_token_iterator<const char*> make_u32regex_token_iterator(const char* p, const u32regex& e, const int (&submatch)[N], regex_constants::match_flag_type m = regex_constants::match_default)
  230. {
  231. return u32regex_token_iterator<const char*>(p, p+std::strlen(p), e, submatch, m);
  232. }
  233. #ifndef BOOST_NO_WREGEX
  234. template <std::size_t N>
  235. inline u32regex_token_iterator<const wchar_t*> make_u32regex_token_iterator(const wchar_t* p, const u32regex& e, const int (&submatch)[N], regex_constants::match_flag_type m = regex_constants::match_default)
  236. {
  237. return u32regex_token_iterator<const wchar_t*>(p, p+std::wcslen(p), e, submatch, m);
  238. }
  239. #endif
  240. #if !defined(BOOST_REGEX_UCHAR_IS_WCHAR_T)
  241. template <std::size_t N>
  242. inline u32regex_token_iterator<const UChar*> make_u32regex_token_iterator(const UChar* p, const u32regex& e, const int (&submatch)[N], regex_constants::match_flag_type m = regex_constants::match_default)
  243. {
  244. return u32regex_token_iterator<const UChar*>(p, p+u_strlen(p), e, submatch, m);
  245. }
  246. #endif
  247. template <class charT, class Traits, class Alloc, std::size_t N>
  248. inline u32regex_token_iterator<typename std::basic_string<charT, Traits, Alloc>::const_iterator> make_u32regex_token_iterator(const std::basic_string<charT, Traits, Alloc>& p, const u32regex& e, const int (&submatch)[N], regex_constants::match_flag_type m = regex_constants::match_default)
  249. {
  250. typedef typename std::basic_string<charT, Traits, Alloc>::const_iterator iter_type;
  251. return u32regex_token_iterator<iter_type>(p.begin(), p.end(), e, submatch, m);
  252. }
  253. template <std::size_t N>
  254. inline u32regex_token_iterator<const UChar*> make_u32regex_token_iterator(const U_NAMESPACE_QUALIFIER UnicodeString& s, const u32regex& e, const int (&submatch)[N], regex_constants::match_flag_type m = regex_constants::match_default)
  255. {
  256. return u32regex_token_iterator<const UChar*>(s.getBuffer(), s.getBuffer() + s.length(), e, submatch, m);
  257. }
  258. // construction from a vector of sub_match state_id's:
  259. inline u32regex_token_iterator<const char*> make_u32regex_token_iterator(const char* p, const u32regex& e, const std::vector<int>& submatch, regex_constants::match_flag_type m = regex_constants::match_default)
  260. {
  261. return u32regex_token_iterator<const char*>(p, p+std::strlen(p), e, submatch, m);
  262. }
  263. #ifndef BOOST_NO_WREGEX
  264. inline u32regex_token_iterator<const wchar_t*> make_u32regex_token_iterator(const wchar_t* p, const u32regex& e, const std::vector<int>& submatch, regex_constants::match_flag_type m = regex_constants::match_default)
  265. {
  266. return u32regex_token_iterator<const wchar_t*>(p, p+std::wcslen(p), e, submatch, m);
  267. }
  268. #endif
  269. #if !defined(U_WCHAR_IS_UTF16) && (U_SIZEOF_WCHAR_T != 2)
  270. inline u32regex_token_iterator<const UChar*> make_u32regex_token_iterator(const UChar* p, const u32regex& e, const std::vector<int>& submatch, regex_constants::match_flag_type m = regex_constants::match_default)
  271. {
  272. return u32regex_token_iterator<const UChar*>(p, p+u_strlen(p), e, submatch, m);
  273. }
  274. #endif
  275. template <class charT, class Traits, class Alloc>
  276. inline u32regex_token_iterator<typename std::basic_string<charT, Traits, Alloc>::const_iterator> make_u32regex_token_iterator(const std::basic_string<charT, Traits, Alloc>& p, const u32regex& e, const std::vector<int>& submatch, regex_constants::match_flag_type m = regex_constants::match_default)
  277. {
  278. typedef typename std::basic_string<charT, Traits, Alloc>::const_iterator iter_type;
  279. return u32regex_token_iterator<iter_type>(p.begin(), p.end(), e, submatch, m);
  280. }
  281. inline u32regex_token_iterator<const UChar*> make_u32regex_token_iterator(const U_NAMESPACE_QUALIFIER UnicodeString& s, const u32regex& e, const std::vector<int>& submatch, regex_constants::match_flag_type m = regex_constants::match_default)
  282. {
  283. return u32regex_token_iterator<const UChar*>(s.getBuffer(), s.getBuffer() + s.length(), e, submatch, m);
  284. }
  285. #ifdef BOOST_REGEX_MSVC
  286. # pragma warning(pop)
  287. #endif
  288. } // namespace boost
  289. #endif // BOOST_REGEX_V5_REGEX_TOKEN_ITERATOR_HPP