u32regex_token_iterator.hpp 13 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 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. #include <boost/regex/config.hpp>
  20. #ifndef BOOST_REGEX_STANDALONE
  21. #include <boost/config.hpp>
  22. #if defined(BOOST_HAS_PRAGMA_ONCE)
  23. #pragma once
  24. #include <boost/regex/v5/icu.hpp>
  25. #endif
  26. #endif
  27. namespace boost{
  28. #ifdef BOOST_REGEX_MSVC
  29. # pragma warning(push)
  30. # pragma warning(disable:4700)
  31. #endif
  32. template <class BidirectionalIterator>
  33. class u32regex_token_iterator_implementation
  34. {
  35. typedef u32regex regex_type;
  36. typedef sub_match<BidirectionalIterator> value_type;
  37. match_results<BidirectionalIterator> what; // current match
  38. BidirectionalIterator end; // end of search area
  39. BidirectionalIterator base; // start of search area
  40. const regex_type re; // the expression
  41. match_flag_type flags; // match flags
  42. value_type result; // the current string result
  43. int N; // the current sub-expression being enumerated
  44. std::vector<int> subs; // the sub-expressions to enumerate
  45. public:
  46. u32regex_token_iterator_implementation(const regex_type* p, BidirectionalIterator last, int sub, match_flag_type f)
  47. : end(last), re(*p), flags(f){ subs.push_back(sub); }
  48. u32regex_token_iterator_implementation(const regex_type* p, BidirectionalIterator last, const std::vector<int>& v, match_flag_type f)
  49. : end(last), re(*p), flags(f), subs(v){}
  50. template <std::size_t CN>
  51. u32regex_token_iterator_implementation(const regex_type* p, BidirectionalIterator last, const int (&submatches)[CN], match_flag_type f)
  52. : end(last), re(*p), flags(f)
  53. {
  54. for(std::size_t i = 0; i < CN; ++i)
  55. {
  56. subs.push_back(submatches[i]);
  57. }
  58. }
  59. bool init(BidirectionalIterator first)
  60. {
  61. base = first;
  62. N = 0;
  63. if(u32regex_search(first, end, what, re, flags, base) == true)
  64. {
  65. N = 0;
  66. result = ((subs[N] == -1) ? what.prefix() : what[(int)subs[N]]);
  67. return true;
  68. }
  69. else if((subs[N] == -1) && (first != end))
  70. {
  71. result.first = first;
  72. result.second = end;
  73. result.matched = (first != end);
  74. N = -1;
  75. return true;
  76. }
  77. return false;
  78. }
  79. bool compare(const u32regex_token_iterator_implementation& that)
  80. {
  81. if(this == &that) return true;
  82. return (&re.get_data() == &that.re.get_data())
  83. && (end == that.end)
  84. && (flags == that.flags)
  85. && (N == that.N)
  86. && (what[0].first == that.what[0].first)
  87. && (what[0].second == that.what[0].second);
  88. }
  89. const value_type& get()
  90. { return result; }
  91. bool next()
  92. {
  93. if(N == -1)
  94. return false;
  95. if(N+1 < (int)subs.size())
  96. {
  97. ++N;
  98. result =((subs[N] == -1) ? what.prefix() : what[subs[N]]);
  99. return true;
  100. }
  101. //if(what.prefix().first != what[0].second)
  102. // flags |= match_prev_avail | regex_constants::match_not_bob;
  103. BidirectionalIterator last_end(what[0].second);
  104. if(u32regex_search(last_end, end, what, re, ((what[0].first == what[0].second) ? flags | regex_constants::match_not_initial_null : flags), base))
  105. {
  106. N =0;
  107. result =((subs[N] == -1) ? what.prefix() : what[subs[N]]);
  108. return true;
  109. }
  110. else if((last_end != end) && (subs[0] == -1))
  111. {
  112. N =-1;
  113. result.first = last_end;
  114. result.second = end;
  115. result.matched = (last_end != end);
  116. return true;
  117. }
  118. return false;
  119. }
  120. private:
  121. u32regex_token_iterator_implementation& operator=(const u32regex_token_iterator_implementation&);
  122. };
  123. template <class BidirectionalIterator>
  124. class u32regex_token_iterator
  125. {
  126. private:
  127. typedef u32regex_token_iterator_implementation<BidirectionalIterator> impl;
  128. typedef std::shared_ptr<impl> pimpl;
  129. public:
  130. typedef u32regex regex_type;
  131. typedef sub_match<BidirectionalIterator> value_type;
  132. typedef typename std::iterator_traits<BidirectionalIterator>::difference_type
  133. difference_type;
  134. typedef const value_type* pointer;
  135. typedef const value_type& reference;
  136. typedef std::forward_iterator_tag iterator_category;
  137. u32regex_token_iterator(){}
  138. u32regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b, const regex_type& re,
  139. int submatch = 0, match_flag_type m = match_default)
  140. : pdata(new impl(&re, b, submatch, m))
  141. {
  142. if(!pdata->init(a))
  143. pdata.reset();
  144. }
  145. u32regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b, const regex_type& re,
  146. const std::vector<int>& submatches, 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. template <std::size_t N>
  153. u32regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b, const regex_type& re,
  154. const int (&submatches)[N], match_flag_type m = match_default)
  155. : pdata(new impl(&re, b, submatches, m))
  156. {
  157. if(!pdata->init(a))
  158. pdata.reset();
  159. }
  160. u32regex_token_iterator(const u32regex_token_iterator& that)
  161. : pdata(that.pdata) {}
  162. u32regex_token_iterator& operator=(const u32regex_token_iterator& that)
  163. {
  164. pdata = that.pdata;
  165. return *this;
  166. }
  167. bool operator==(const u32regex_token_iterator& that)const
  168. {
  169. if((pdata.get() == 0) || (that.pdata.get() == 0))
  170. return pdata.get() == that.pdata.get();
  171. return pdata->compare(*(that.pdata.get()));
  172. }
  173. bool operator!=(const u32regex_token_iterator& that)const
  174. { return !(*this == that); }
  175. const value_type& operator*()const
  176. { return pdata->get(); }
  177. const value_type* operator->()const
  178. { return &(pdata->get()); }
  179. u32regex_token_iterator& operator++()
  180. {
  181. cow();
  182. if(0 == pdata->next())
  183. {
  184. pdata.reset();
  185. }
  186. return *this;
  187. }
  188. u32regex_token_iterator operator++(int)
  189. {
  190. u32regex_token_iterator result(*this);
  191. ++(*this);
  192. return result;
  193. }
  194. private:
  195. pimpl pdata;
  196. void cow()
  197. {
  198. // copy-on-write
  199. if(pdata.get() && (pdata.use_count() > 1))
  200. {
  201. pdata.reset(new impl(*(pdata.get())));
  202. }
  203. }
  204. };
  205. typedef u32regex_token_iterator<const char*> utf8regex_token_iterator;
  206. typedef u32regex_token_iterator<const UChar*> utf16regex_token_iterator;
  207. typedef u32regex_token_iterator<const UChar32*> utf32regex_token_iterator;
  208. // construction from an integral sub_match state_id:
  209. 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)
  210. {
  211. return u32regex_token_iterator<const char*>(p, p+std::strlen(p), e, submatch, m);
  212. }
  213. #ifndef BOOST_NO_WREGEX
  214. 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)
  215. {
  216. return u32regex_token_iterator<const wchar_t*>(p, p+std::wcslen(p), e, submatch, m);
  217. }
  218. #endif
  219. #if !defined(BOOST_REGEX_UCHAR_IS_WCHAR_T)
  220. 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)
  221. {
  222. return u32regex_token_iterator<const UChar*>(p, p+u_strlen(p), e, submatch, m);
  223. }
  224. #endif
  225. template <class charT, class Traits, class Alloc>
  226. 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)
  227. {
  228. typedef typename std::basic_string<charT, Traits, Alloc>::const_iterator iter_type;
  229. return u32regex_token_iterator<iter_type>(p.begin(), p.end(), e, submatch, m);
  230. }
  231. 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)
  232. {
  233. return u32regex_token_iterator<const UChar*>(s.getBuffer(), s.getBuffer() + s.length(), e, submatch, m);
  234. }
  235. // construction from a reference to an array:
  236. template <std::size_t N>
  237. 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)
  238. {
  239. return u32regex_token_iterator<const char*>(p, p+std::strlen(p), e, submatch, m);
  240. }
  241. #ifndef BOOST_NO_WREGEX
  242. template <std::size_t N>
  243. 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)
  244. {
  245. return u32regex_token_iterator<const wchar_t*>(p, p+std::wcslen(p), e, submatch, m);
  246. }
  247. #endif
  248. #if !defined(BOOST_REGEX_UCHAR_IS_WCHAR_T)
  249. template <std::size_t N>
  250. 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)
  251. {
  252. return u32regex_token_iterator<const UChar*>(p, p+u_strlen(p), e, submatch, m);
  253. }
  254. #endif
  255. template <class charT, class Traits, class Alloc, std::size_t N>
  256. 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)
  257. {
  258. typedef typename std::basic_string<charT, Traits, Alloc>::const_iterator iter_type;
  259. return u32regex_token_iterator<iter_type>(p.begin(), p.end(), e, submatch, m);
  260. }
  261. template <std::size_t N>
  262. 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)
  263. {
  264. return u32regex_token_iterator<const UChar*>(s.getBuffer(), s.getBuffer() + s.length(), e, submatch, m);
  265. }
  266. // construction from a vector of sub_match state_id's:
  267. 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)
  268. {
  269. return u32regex_token_iterator<const char*>(p, p+std::strlen(p), e, submatch, m);
  270. }
  271. #ifndef BOOST_NO_WREGEX
  272. 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)
  273. {
  274. return u32regex_token_iterator<const wchar_t*>(p, p+std::wcslen(p), e, submatch, m);
  275. }
  276. #endif
  277. #if !defined(U_WCHAR_IS_UTF16) && (U_SIZEOF_WCHAR_T != 2)
  278. 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)
  279. {
  280. return u32regex_token_iterator<const UChar*>(p, p+u_strlen(p), e, submatch, m);
  281. }
  282. #endif
  283. template <class charT, class Traits, class Alloc>
  284. 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)
  285. {
  286. typedef typename std::basic_string<charT, Traits, Alloc>::const_iterator iter_type;
  287. return u32regex_token_iterator<iter_type>(p.begin(), p.end(), e, submatch, m);
  288. }
  289. 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)
  290. {
  291. return u32regex_token_iterator<const UChar*>(s.getBuffer(), s.getBuffer() + s.length(), e, submatch, m);
  292. }
  293. #ifdef BOOST_REGEX_MSVC
  294. # pragma warning(pop)
  295. #endif
  296. } // namespace boost
  297. #endif // BOOST_REGEX_V5_REGEX_TOKEN_ITERATOR_HPP