Chris@16: /* Chris@16: * Chris@16: * Copyright (c) 2003 Chris@16: * John Maddock Chris@16: * Chris@16: * Use, modification and distribution are subject to the Chris@16: * Boost Software License, Version 1.0. (See accompanying file Chris@16: * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: * Chris@16: */ Chris@16: Chris@16: /* Chris@16: * LOCATION: see http://www.boost.org for most recent version. Chris@16: * FILE u32regex_iterator.hpp Chris@16: * VERSION see Chris@16: * DESCRIPTION: Provides u32regex_iterator implementation. Chris@16: */ Chris@16: Chris@16: #ifndef BOOST_REGEX_V4_U32REGEX_ITERATOR_HPP Chris@16: #define BOOST_REGEX_V4_U32REGEX_ITERATOR_HPP Chris@16: Chris@16: namespace boost{ Chris@16: Chris@16: #ifdef BOOST_HAS_ABI_HEADERS Chris@16: # include BOOST_ABI_PREFIX Chris@16: #endif Chris@16: Chris@16: template Chris@16: class u32regex_iterator_implementation Chris@16: { Chris@16: typedef u32regex regex_type; Chris@16: Chris@16: match_results what; // current match Chris@16: BidirectionalIterator base; // start of sequence Chris@16: BidirectionalIterator end; // end of sequence Chris@16: const regex_type re; // the expression Chris@16: match_flag_type flags; // flags for matching Chris@16: Chris@16: public: Chris@16: u32regex_iterator_implementation(const regex_type* p, BidirectionalIterator last, match_flag_type f) Chris@16: : base(), end(last), re(*p), flags(f){} Chris@16: bool init(BidirectionalIterator first) Chris@16: { Chris@16: base = first; Chris@16: return u32regex_search(first, end, what, re, flags, base); Chris@16: } Chris@16: bool compare(const u32regex_iterator_implementation& that) Chris@16: { Chris@16: if(this == &that) return true; Chris@16: return (&re.get_data() == &that.re.get_data()) && (end == that.end) && (flags == that.flags) && (what[0].first == that.what[0].first) && (what[0].second == that.what[0].second); Chris@16: } Chris@16: const match_results& get() Chris@16: { return what; } Chris@16: bool next() Chris@16: { Chris@16: //if(what.prefix().first != what[0].second) Chris@16: // flags |= match_prev_avail; Chris@16: BidirectionalIterator next_start = what[0].second; Chris@16: match_flag_type f(flags); Chris@16: if(!what.length()) Chris@16: f |= regex_constants::match_not_initial_null; Chris@16: //if(base != next_start) Chris@16: // f |= regex_constants::match_not_bob; Chris@16: bool result = u32regex_search(next_start, end, what, re, f, base); Chris@16: if(result) Chris@16: what.set_base(base); Chris@16: return result; Chris@16: } Chris@16: private: Chris@16: u32regex_iterator_implementation& operator=(const u32regex_iterator_implementation&); Chris@16: }; Chris@16: Chris@16: template Chris@16: class u32regex_iterator Chris@16: #ifndef BOOST_NO_STD_ITERATOR Chris@16: : public std::iterator< Chris@16: std::forward_iterator_tag, Chris@16: match_results, Chris@16: typename re_detail::regex_iterator_traits::difference_type, Chris@16: const match_results*, Chris@16: const match_results& > Chris@16: #endif Chris@16: { Chris@16: private: Chris@16: typedef u32regex_iterator_implementation impl; Chris@16: typedef shared_ptr pimpl; Chris@16: public: Chris@16: typedef u32regex regex_type; Chris@16: typedef match_results value_type; Chris@16: typedef typename re_detail::regex_iterator_traits::difference_type Chris@16: difference_type; Chris@16: typedef const value_type* pointer; Chris@16: typedef const value_type& reference; Chris@16: typedef std::forward_iterator_tag iterator_category; Chris@16: Chris@16: u32regex_iterator(){} Chris@16: u32regex_iterator(BidirectionalIterator a, BidirectionalIterator b, Chris@16: const regex_type& re, Chris@16: match_flag_type m = match_default) Chris@16: : pdata(new impl(&re, b, m)) Chris@16: { Chris@16: if(!pdata->init(a)) Chris@16: { Chris@16: pdata.reset(); Chris@16: } Chris@16: } Chris@16: u32regex_iterator(const u32regex_iterator& that) Chris@16: : pdata(that.pdata) {} Chris@16: u32regex_iterator& operator=(const u32regex_iterator& that) Chris@16: { Chris@16: pdata = that.pdata; Chris@16: return *this; Chris@16: } Chris@16: bool operator==(const u32regex_iterator& that)const Chris@16: { Chris@16: if((pdata.get() == 0) || (that.pdata.get() == 0)) Chris@16: return pdata.get() == that.pdata.get(); Chris@16: return pdata->compare(*(that.pdata.get())); Chris@16: } Chris@16: bool operator!=(const u32regex_iterator& that)const Chris@16: { return !(*this == that); } Chris@16: const value_type& operator*()const Chris@16: { return pdata->get(); } Chris@16: const value_type* operator->()const Chris@16: { return &(pdata->get()); } Chris@16: u32regex_iterator& operator++() Chris@16: { Chris@16: cow(); Chris@16: if(0 == pdata->next()) Chris@16: { Chris@16: pdata.reset(); Chris@16: } Chris@16: return *this; Chris@16: } Chris@16: u32regex_iterator operator++(int) Chris@16: { Chris@16: u32regex_iterator result(*this); Chris@16: ++(*this); Chris@16: return result; Chris@16: } Chris@16: private: Chris@16: Chris@16: pimpl pdata; Chris@16: Chris@16: void cow() Chris@16: { Chris@16: // copy-on-write Chris@16: if(pdata.get() && !pdata.unique()) Chris@16: { Chris@16: pdata.reset(new impl(*(pdata.get()))); Chris@16: } Chris@16: } Chris@16: }; Chris@16: Chris@16: typedef u32regex_iterator utf8regex_iterator; Chris@16: typedef u32regex_iterator utf16regex_iterator; Chris@16: typedef u32regex_iterator utf32regex_iterator; Chris@16: Chris@16: inline u32regex_iterator make_u32regex_iterator(const char* p, const u32regex& e, regex_constants::match_flag_type m = regex_constants::match_default) Chris@16: { Chris@16: return u32regex_iterator(p, p+std::strlen(p), e, m); Chris@16: } Chris@16: #ifndef BOOST_NO_WREGEX Chris@16: inline u32regex_iterator make_u32regex_iterator(const wchar_t* p, const u32regex& e, regex_constants::match_flag_type m = regex_constants::match_default) Chris@16: { Chris@16: return u32regex_iterator(p, p+std::wcslen(p), e, m); Chris@16: } Chris@16: #endif Chris@16: #if !defined(U_WCHAR_IS_UTF16) && (U_SIZEOF_WCHAR_T != 2) Chris@16: inline u32regex_iterator make_u32regex_iterator(const UChar* p, const u32regex& e, regex_constants::match_flag_type m = regex_constants::match_default) Chris@16: { Chris@16: return u32regex_iterator(p, p+u_strlen(p), e, m); Chris@16: } Chris@16: #endif Chris@16: template Chris@16: inline u32regex_iterator::const_iterator> make_u32regex_iterator(const std::basic_string& p, const u32regex& e, regex_constants::match_flag_type m = regex_constants::match_default) Chris@16: { Chris@16: typedef typename std::basic_string::const_iterator iter_type; Chris@16: return u32regex_iterator(p.begin(), p.end(), e, m); Chris@16: } Chris@16: inline u32regex_iterator make_u32regex_iterator(const U_NAMESPACE_QUALIFIER UnicodeString& s, const u32regex& e, regex_constants::match_flag_type m = regex_constants::match_default) Chris@16: { Chris@16: return u32regex_iterator(s.getBuffer(), s.getBuffer() + s.length(), e, m); Chris@16: } Chris@16: Chris@16: #ifdef BOOST_HAS_ABI_HEADERS Chris@16: # include BOOST_ABI_SUFFIX Chris@16: #endif Chris@16: Chris@16: } // namespace boost Chris@16: Chris@16: #endif // BOOST_REGEX_V4_REGEX_ITERATOR_HPP Chris@16: