u32regex_token_iterator.hpp 15 KB

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