u32regex_token_iterator.hpp 15 KB

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