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_token_iterator.hpp Chris@16: * VERSION see Chris@16: * DESCRIPTION: Provides u32regex_token_iterator implementation. Chris@16: */ Chris@16: Chris@16: #ifndef BOOST_REGEX_V4_U32REGEX_TOKEN_ITERATOR_HPP Chris@16: #define BOOST_REGEX_V4_U32REGEX_TOKEN_ITERATOR_HPP Chris@16: Chris@16: #if (BOOST_WORKAROUND(__BORLANDC__, >= 0x560) && BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x570)))\ Chris@16: || BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003)) Chris@16: // Chris@16: // Borland C++ Builder 6, and Visual C++ 6, Chris@16: // can't cope with the array template constructor Chris@16: // so we have a template member that will accept any type as Chris@16: // argument, and then assert that is really is an array: Chris@16: // Chris@16: #include Chris@16: #include Chris@16: #endif 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@101: #ifdef BOOST_MSVC Chris@16: # pragma warning(push) Chris@16: # pragma warning(disable:4700) Chris@16: #endif Chris@16: Chris@16: template Chris@16: class u32regex_token_iterator_implementation Chris@16: { Chris@16: typedef u32regex regex_type; Chris@16: typedef sub_match value_type; Chris@16: Chris@16: match_results what; // current match Chris@16: BidirectionalIterator end; // end of search area Chris@16: BidirectionalIterator base; // start of search area Chris@16: const regex_type re; // the expression Chris@16: match_flag_type flags; // match flags Chris@16: value_type result; // the current string result Chris@16: int N; // the current sub-expression being enumerated Chris@16: std::vector subs; // the sub-expressions to enumerate Chris@16: Chris@16: public: Chris@16: u32regex_token_iterator_implementation(const regex_type* p, BidirectionalIterator last, int sub, match_flag_type f) Chris@16: : end(last), re(*p), flags(f){ subs.push_back(sub); } Chris@16: u32regex_token_iterator_implementation(const regex_type* p, BidirectionalIterator last, const std::vector& v, match_flag_type f) Chris@16: : end(last), re(*p), flags(f), subs(v){} Chris@101: #if (BOOST_WORKAROUND(__BORLANDC__, >= 0x560) && BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x570)))\ Chris@16: || BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003)) \ Chris@16: || BOOST_WORKAROUND(__HP_aCC, < 60700) Chris@16: template Chris@16: u32regex_token_iterator_implementation(const regex_type* p, BidirectionalIterator last, const T& submatches, match_flag_type f) Chris@16: : end(last), re(*p), flags(f) Chris@16: { Chris@16: // assert that T really is an array: Chris@16: BOOST_STATIC_ASSERT(::boost::is_array::value); Chris@16: const std::size_t array_size = sizeof(T) / sizeof(submatches[0]); Chris@16: for(std::size_t i = 0; i < array_size; ++i) Chris@16: { Chris@16: subs.push_back(submatches[i]); Chris@16: } Chris@16: } Chris@16: #else Chris@16: template Chris@16: u32regex_token_iterator_implementation(const regex_type* p, BidirectionalIterator last, const int (&submatches)[CN], match_flag_type f) Chris@16: : end(last), re(*p), flags(f) Chris@16: { Chris@16: for(std::size_t i = 0; i < CN; ++i) Chris@16: { Chris@16: subs.push_back(submatches[i]); Chris@16: } Chris@16: } Chris@16: #endif Chris@16: Chris@16: bool init(BidirectionalIterator first) Chris@16: { Chris@16: base = first; Chris@16: N = 0; Chris@16: if(u32regex_search(first, end, what, re, flags, base) == true) Chris@16: { Chris@16: N = 0; Chris@16: result = ((subs[N] == -1) ? what.prefix() : what[(int)subs[N]]); Chris@16: return true; Chris@16: } Chris@16: else if((subs[N] == -1) && (first != end)) Chris@16: { Chris@16: result.first = first; Chris@16: result.second = end; Chris@16: result.matched = (first != end); Chris@16: N = -1; Chris@16: return true; Chris@16: } Chris@16: return false; Chris@16: } Chris@16: bool compare(const u32regex_token_iterator_implementation& that) Chris@16: { Chris@16: if(this == &that) return true; Chris@16: return (&re.get_data() == &that.re.get_data()) Chris@16: && (end == that.end) Chris@16: && (flags == that.flags) Chris@16: && (N == that.N) Chris@16: && (what[0].first == that.what[0].first) Chris@16: && (what[0].second == that.what[0].second); Chris@16: } Chris@16: const value_type& get() Chris@16: { return result; } Chris@16: bool next() Chris@16: { Chris@16: if(N == -1) Chris@16: return false; Chris@16: if(N+1 < (int)subs.size()) Chris@16: { Chris@16: ++N; Chris@16: result =((subs[N] == -1) ? what.prefix() : what[subs[N]]); Chris@16: return true; Chris@16: } Chris@16: //if(what.prefix().first != what[0].second) Chris@16: // flags |= match_prev_avail | regex_constants::match_not_bob; Chris@16: BidirectionalIterator last_end(what[0].second); Chris@16: if(u32regex_search(last_end, end, what, re, ((what[0].first == what[0].second) ? flags | regex_constants::match_not_initial_null : flags), base)) Chris@16: { Chris@16: N =0; Chris@16: result =((subs[N] == -1) ? what.prefix() : what[subs[N]]); Chris@16: return true; Chris@16: } Chris@16: else if((last_end != end) && (subs[0] == -1)) Chris@16: { Chris@16: N =-1; Chris@16: result.first = last_end; Chris@16: result.second = end; Chris@16: result.matched = (last_end != end); Chris@16: return true; Chris@16: } Chris@16: return false; Chris@16: } Chris@16: private: Chris@16: u32regex_token_iterator_implementation& operator=(const u32regex_token_iterator_implementation&); Chris@16: }; Chris@16: Chris@16: template Chris@16: class u32regex_token_iterator Chris@16: #ifndef BOOST_NO_STD_ITERATOR Chris@16: : public std::iterator< Chris@16: std::forward_iterator_tag, Chris@16: sub_match, Chris@16: typename re_detail::regex_iterator_traits::difference_type, Chris@16: const sub_match*, Chris@16: const sub_match& > Chris@16: #endif Chris@16: { Chris@16: private: Chris@16: typedef u32regex_token_iterator_implementation impl; Chris@16: typedef shared_ptr pimpl; Chris@16: public: Chris@16: typedef u32regex regex_type; Chris@16: typedef sub_match 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_token_iterator(){} Chris@16: u32regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b, const regex_type& re, Chris@16: int submatch = 0, match_flag_type m = match_default) Chris@16: : pdata(new impl(&re, b, submatch, m)) Chris@16: { Chris@16: if(!pdata->init(a)) Chris@16: pdata.reset(); Chris@16: } Chris@16: u32regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b, const regex_type& re, Chris@16: const std::vector& submatches, match_flag_type m = match_default) Chris@16: : pdata(new impl(&re, b, submatches, m)) Chris@16: { Chris@16: if(!pdata->init(a)) Chris@16: pdata.reset(); Chris@16: } Chris@101: #if (BOOST_WORKAROUND(__BORLANDC__, >= 0x560) && BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x570)))\ Chris@16: || BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003)) \ Chris@16: || BOOST_WORKAROUND(__HP_aCC, < 60700) Chris@16: template Chris@16: u32regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b, const regex_type& re, Chris@16: const T& submatches, match_flag_type m = match_default) Chris@16: : pdata(new impl(&re, b, submatches, m)) Chris@16: { Chris@16: if(!pdata->init(a)) Chris@16: pdata.reset(); Chris@16: } Chris@16: #else Chris@16: template Chris@16: u32regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b, const regex_type& re, Chris@16: const int (&submatches)[N], match_flag_type m = match_default) Chris@16: : pdata(new impl(&re, b, submatches, m)) Chris@16: { Chris@16: if(!pdata->init(a)) Chris@16: pdata.reset(); Chris@16: } Chris@16: #endif Chris@16: u32regex_token_iterator(const u32regex_token_iterator& that) Chris@16: : pdata(that.pdata) {} Chris@16: u32regex_token_iterator& operator=(const u32regex_token_iterator& that) Chris@16: { Chris@16: pdata = that.pdata; Chris@16: return *this; Chris@16: } Chris@16: bool operator==(const u32regex_token_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_token_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_token_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_token_iterator operator++(int) Chris@16: { Chris@16: u32regex_token_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_token_iterator utf8regex_token_iterator; Chris@16: typedef u32regex_token_iterator utf16regex_token_iterator; Chris@16: typedef u32regex_token_iterator utf32regex_token_iterator; Chris@16: Chris@16: // construction from an integral sub_match state_id: Chris@16: inline u32regex_token_iterator make_u32regex_token_iterator(const char* p, const u32regex& e, int submatch = 0, regex_constants::match_flag_type m = regex_constants::match_default) Chris@16: { Chris@16: return u32regex_token_iterator(p, p+std::strlen(p), e, submatch, m); Chris@16: } Chris@16: #ifndef BOOST_NO_WREGEX Chris@16: inline u32regex_token_iterator make_u32regex_token_iterator(const wchar_t* p, const u32regex& e, int submatch = 0, regex_constants::match_flag_type m = regex_constants::match_default) Chris@16: { Chris@16: return u32regex_token_iterator(p, p+std::wcslen(p), e, submatch, m); Chris@16: } Chris@16: #endif Chris@16: #if !defined(U_WCHAR_IS_UTF16) && (U_SIZEOF_WCHAR_T != 2) Chris@16: inline u32regex_token_iterator make_u32regex_token_iterator(const UChar* p, const u32regex& e, int submatch = 0, regex_constants::match_flag_type m = regex_constants::match_default) Chris@16: { Chris@16: return u32regex_token_iterator(p, p+u_strlen(p), e, submatch, m); Chris@16: } Chris@16: #endif Chris@16: template Chris@16: inline u32regex_token_iterator::const_iterator> make_u32regex_token_iterator(const std::basic_string& p, const u32regex& e, int submatch = 0, 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_token_iterator(p.begin(), p.end(), e, submatch, m); Chris@16: } Chris@16: inline u32regex_token_iterator 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) Chris@16: { Chris@16: return u32regex_token_iterator(s.getBuffer(), s.getBuffer() + s.length(), e, submatch, m); Chris@16: } Chris@16: Chris@16: // construction from a reference to an array: Chris@16: template Chris@16: inline u32regex_token_iterator make_u32regex_token_iterator(const char* p, const u32regex& e, const int (&submatch)[N], regex_constants::match_flag_type m = regex_constants::match_default) Chris@16: { Chris@16: return u32regex_token_iterator(p, p+std::strlen(p), e, submatch, m); Chris@16: } Chris@16: #ifndef BOOST_NO_WREGEX Chris@16: template Chris@16: inline u32regex_token_iterator 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) Chris@16: { Chris@16: return u32regex_token_iterator(p, p+std::wcslen(p), e, submatch, m); Chris@16: } Chris@16: #endif Chris@16: #if !defined(U_WCHAR_IS_UTF16) && (U_SIZEOF_WCHAR_T != 2) Chris@16: template Chris@16: inline u32regex_token_iterator make_u32regex_token_iterator(const UChar* p, const u32regex& e, const int (&submatch)[N], regex_constants::match_flag_type m = regex_constants::match_default) Chris@16: { Chris@16: return u32regex_token_iterator(p, p+u_strlen(p), e, submatch, m); Chris@16: } Chris@16: #endif Chris@16: template Chris@16: inline u32regex_token_iterator::const_iterator> make_u32regex_token_iterator(const std::basic_string& p, const u32regex& e, const int (&submatch)[N], 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_token_iterator(p.begin(), p.end(), e, submatch, m); Chris@16: } Chris@16: template Chris@16: inline u32regex_token_iterator 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) Chris@16: { Chris@16: return u32regex_token_iterator(s.getBuffer(), s.getBuffer() + s.length(), e, submatch, m); Chris@16: } Chris@16: Chris@16: // construction from a vector of sub_match state_id's: Chris@16: inline u32regex_token_iterator make_u32regex_token_iterator(const char* p, const u32regex& e, const std::vector& submatch, regex_constants::match_flag_type m = regex_constants::match_default) Chris@16: { Chris@16: return u32regex_token_iterator(p, p+std::strlen(p), e, submatch, m); Chris@16: } Chris@16: #ifndef BOOST_NO_WREGEX Chris@16: inline u32regex_token_iterator make_u32regex_token_iterator(const wchar_t* p, const u32regex& e, const std::vector& submatch, regex_constants::match_flag_type m = regex_constants::match_default) Chris@16: { Chris@16: return u32regex_token_iterator(p, p+std::wcslen(p), e, submatch, m); Chris@16: } Chris@16: #endif Chris@16: #if !defined(U_WCHAR_IS_UTF16) && (U_SIZEOF_WCHAR_T != 2) Chris@16: inline u32regex_token_iterator make_u32regex_token_iterator(const UChar* p, const u32regex& e, const std::vector& submatch, regex_constants::match_flag_type m = regex_constants::match_default) Chris@16: { Chris@16: return u32regex_token_iterator(p, p+u_strlen(p), e, submatch, m); Chris@16: } Chris@16: #endif Chris@16: template Chris@16: inline u32regex_token_iterator::const_iterator> make_u32regex_token_iterator(const std::basic_string& p, const u32regex& e, const std::vector& submatch, 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_token_iterator(p.begin(), p.end(), e, submatch, m); Chris@16: } Chris@16: inline u32regex_token_iterator make_u32regex_token_iterator(const U_NAMESPACE_QUALIFIER UnicodeString& s, const u32regex& e, const std::vector& submatch, regex_constants::match_flag_type m = regex_constants::match_default) Chris@16: { Chris@16: return u32regex_token_iterator(s.getBuffer(), s.getBuffer() + s.length(), e, submatch, m); Chris@16: } Chris@16: Chris@101: #ifdef BOOST_MSVC Chris@16: # pragma warning(pop) Chris@16: #endif 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_TOKEN_ITERATOR_HPP Chris@16: Chris@16: Chris@16: Chris@16: