File indexing completed on 2025-01-18 09:51:26
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019 #ifndef BOOST_REGEX_V4_REGEX_TOKEN_ITERATOR_HPP
0020 #define BOOST_REGEX_V4_REGEX_TOKEN_ITERATOR_HPP
0021
0022 #include <boost/shared_ptr.hpp>
0023 #include <boost/detail/workaround.hpp>
0024 #if (BOOST_WORKAROUND(BOOST_BORLANDC, >= 0x560) && BOOST_WORKAROUND(BOOST_BORLANDC, BOOST_TESTED_AT(0x570)))\
0025 || BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003))
0026
0027
0028
0029
0030
0031
0032 #include <boost/static_assert.hpp>
0033 #include <boost/type_traits/is_array.hpp>
0034 #endif
0035
0036 namespace boost{
0037
0038 #ifdef BOOST_MSVC
0039 #pragma warning(push)
0040 #pragma warning(disable: 4103)
0041 #endif
0042 #ifdef BOOST_HAS_ABI_HEADERS
0043 # include BOOST_ABI_PREFIX
0044 #endif
0045 #ifdef BOOST_MSVC
0046 #pragma warning(pop)
0047 #pragma warning(push)
0048 #pragma warning(disable:4700)
0049 #endif
0050
0051 template <class BidirectionalIterator,
0052 class charT,
0053 class traits>
0054 class regex_token_iterator_implementation
0055 {
0056 typedef basic_regex<charT, traits> regex_type;
0057 typedef sub_match<BidirectionalIterator> value_type;
0058
0059 match_results<BidirectionalIterator> what;
0060 BidirectionalIterator base;
0061 BidirectionalIterator end;
0062 const regex_type re;
0063 match_flag_type flags;
0064 value_type result;
0065 int N;
0066 std::vector<int> subs;
0067
0068 public:
0069 regex_token_iterator_implementation(const regex_token_iterator_implementation& other)
0070 : what(other.what), base(other.base), end(other.end), re(other.re), flags(other.flags), result(other.result), N(other.N), subs(other.subs) {}
0071 regex_token_iterator_implementation(const regex_type* p, BidirectionalIterator last, int sub, match_flag_type f)
0072 : end(last), re(*p), flags(f), N(0){ subs.push_back(sub); }
0073 regex_token_iterator_implementation(const regex_type* p, BidirectionalIterator last, const std::vector<int>& v, match_flag_type f)
0074 : end(last), re(*p), flags(f), N(0), subs(v){}
0075 #if !BOOST_WORKAROUND(__HP_aCC, < 60700)
0076 #if (BOOST_WORKAROUND(BOOST_BORLANDC, >= 0x560) && BOOST_WORKAROUND(BOOST_BORLANDC, BOOST_TESTED_AT(0x570)))\
0077 || BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003)) \
0078 || BOOST_WORKAROUND(__HP_aCC, < 60700)
0079 template <class T>
0080 regex_token_iterator_implementation(const regex_type* p, BidirectionalIterator last, const T& submatches, match_flag_type f)
0081 : end(last), re(*p), flags(f), N(0)
0082 {
0083
0084 BOOST_STATIC_ASSERT(::boost::is_array<T>::value);
0085 const std::size_t array_size = sizeof(T) / sizeof(submatches[0]);
0086 for(std::size_t i = 0; i < array_size; ++i)
0087 {
0088 subs.push_back(submatches[i]);
0089 }
0090 }
0091 #else
0092 template <std::size_t CN>
0093 regex_token_iterator_implementation(const regex_type* p, BidirectionalIterator last, const int (&submatches)[CN], match_flag_type f)
0094 : end(last), re(*p), flags(f), N(0)
0095 {
0096 for(std::size_t i = 0; i < CN; ++i)
0097 {
0098 subs.push_back(submatches[i]);
0099 }
0100 }
0101 #endif
0102 #endif
0103 bool init(BidirectionalIterator first)
0104 {
0105 N = 0;
0106 base = first;
0107 if(regex_search(first, end, what, re, flags, base) == true)
0108 {
0109 N = 0;
0110 result = ((subs[N] == -1) ? what.prefix() : what[(int)subs[N]]);
0111 return true;
0112 }
0113 else if((subs[N] == -1) && (first != end))
0114 {
0115 result.first = first;
0116 result.second = end;
0117 result.matched = (first != end);
0118 N = -1;
0119 return true;
0120 }
0121 return false;
0122 }
0123 bool compare(const regex_token_iterator_implementation& that)
0124 {
0125 if(this == &that) return true;
0126 return (&re.get_data() == &that.re.get_data())
0127 && (end == that.end)
0128 && (flags == that.flags)
0129 && (N == that.N)
0130 && (what[0].first == that.what[0].first)
0131 && (what[0].second == that.what[0].second);
0132 }
0133 const value_type& get()
0134 { return result; }
0135 bool next()
0136 {
0137 if(N == -1)
0138 return false;
0139 if(N+1 < (int)subs.size())
0140 {
0141 ++N;
0142 result =((subs[N] == -1) ? what.prefix() : what[subs[N]]);
0143 return true;
0144 }
0145
0146
0147 BidirectionalIterator last_end(what[0].second);
0148 if(regex_search(last_end, end, what, re, ((what[0].first == what[0].second) ? flags | regex_constants::match_not_initial_null : flags), base))
0149 {
0150 N =0;
0151 result =((subs[N] == -1) ? what.prefix() : what[subs[N]]);
0152 return true;
0153 }
0154 else if((last_end != end) && (subs[0] == -1))
0155 {
0156 N =-1;
0157 result.first = last_end;
0158 result.second = end;
0159 result.matched = (last_end != end);
0160 return true;
0161 }
0162 return false;
0163 }
0164 private:
0165 regex_token_iterator_implementation& operator=(const regex_token_iterator_implementation&);
0166 };
0167
0168 template <class BidirectionalIterator,
0169 class charT = BOOST_DEDUCED_TYPENAME BOOST_REGEX_DETAIL_NS::regex_iterator_traits<BidirectionalIterator>::value_type,
0170 class traits = regex_traits<charT> >
0171 class regex_token_iterator
0172 {
0173 private:
0174 typedef regex_token_iterator_implementation<BidirectionalIterator, charT, traits> impl;
0175 typedef shared_ptr<impl> pimpl;
0176 public:
0177 typedef basic_regex<charT, traits> regex_type;
0178 typedef sub_match<BidirectionalIterator> value_type;
0179 typedef typename BOOST_REGEX_DETAIL_NS::regex_iterator_traits<BidirectionalIterator>::difference_type
0180 difference_type;
0181 typedef const value_type* pointer;
0182 typedef const value_type& reference;
0183 typedef std::forward_iterator_tag iterator_category;
0184
0185 regex_token_iterator(){}
0186 regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b, const regex_type& re,
0187 int submatch = 0, match_flag_type m = match_default)
0188 : pdata(new impl(&re, b, submatch, m))
0189 {
0190 if(!pdata->init(a))
0191 pdata.reset();
0192 }
0193 regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b, const regex_type& re,
0194 const std::vector<int>& submatches, match_flag_type m = match_default)
0195 : pdata(new impl(&re, b, submatches, m))
0196 {
0197 if(!pdata->init(a))
0198 pdata.reset();
0199 }
0200 #if !BOOST_WORKAROUND(__HP_aCC, < 60700)
0201 #if (BOOST_WORKAROUND(BOOST_BORLANDC, >= 0x560) && BOOST_WORKAROUND(BOOST_BORLANDC, BOOST_TESTED_AT(0x570)))\
0202 || BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003)) \
0203 || BOOST_WORKAROUND(__HP_aCC, < 60700)
0204 template <class T>
0205 regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b, const regex_type& re,
0206 const T& submatches, match_flag_type m = match_default)
0207 : pdata(new impl(&re, b, submatches, m))
0208 {
0209 if(!pdata->init(a))
0210 pdata.reset();
0211 }
0212 #else
0213 template <std::size_t N>
0214 regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b, const regex_type& re,
0215 const int (&submatches)[N], match_flag_type m = match_default)
0216 : pdata(new impl(&re, b, submatches, m))
0217 {
0218 if(!pdata->init(a))
0219 pdata.reset();
0220 }
0221 #endif
0222 #endif
0223 regex_token_iterator(const regex_token_iterator& that)
0224 : pdata(that.pdata) {}
0225 regex_token_iterator& operator=(const regex_token_iterator& that)
0226 {
0227 pdata = that.pdata;
0228 return *this;
0229 }
0230 bool operator==(const regex_token_iterator& that)const
0231 {
0232 if((pdata.get() == 0) || (that.pdata.get() == 0))
0233 return pdata.get() == that.pdata.get();
0234 return pdata->compare(*(that.pdata.get()));
0235 }
0236 bool operator!=(const regex_token_iterator& that)const
0237 { return !(*this == that); }
0238 const value_type& operator*()const
0239 { return pdata->get(); }
0240 const value_type* operator->()const
0241 { return &(pdata->get()); }
0242 regex_token_iterator& operator++()
0243 {
0244 cow();
0245 if(0 == pdata->next())
0246 {
0247 pdata.reset();
0248 }
0249 return *this;
0250 }
0251 regex_token_iterator operator++(int)
0252 {
0253 regex_token_iterator result(*this);
0254 ++(*this);
0255 return result;
0256 }
0257 private:
0258
0259 pimpl pdata;
0260
0261 void cow()
0262 {
0263
0264 if(pdata.get() && !pdata.unique())
0265 {
0266 pdata.reset(new impl(*(pdata.get())));
0267 }
0268 }
0269 };
0270
0271 typedef regex_token_iterator<const char*> cregex_token_iterator;
0272 typedef regex_token_iterator<std::string::const_iterator> sregex_token_iterator;
0273 #ifndef BOOST_NO_WREGEX
0274 typedef regex_token_iterator<const wchar_t*> wcregex_token_iterator;
0275 typedef regex_token_iterator<std::wstring::const_iterator> wsregex_token_iterator;
0276 #endif
0277
0278 template <class charT, class traits>
0279 inline regex_token_iterator<const charT*, charT, traits> make_regex_token_iterator(const charT* p, const basic_regex<charT, traits>& e, int submatch = 0, regex_constants::match_flag_type m = regex_constants::match_default)
0280 {
0281 return regex_token_iterator<const charT*, charT, traits>(p, p+traits::length(p), e, submatch, m);
0282 }
0283 template <class charT, class traits, class ST, class SA>
0284 inline regex_token_iterator<typename std::basic_string<charT, ST, SA>::const_iterator, charT, traits> make_regex_token_iterator(const std::basic_string<charT, ST, SA>& p, const basic_regex<charT, traits>& e, int submatch = 0, regex_constants::match_flag_type m = regex_constants::match_default)
0285 {
0286 return regex_token_iterator<typename std::basic_string<charT, ST, SA>::const_iterator, charT, traits>(p.begin(), p.end(), e, submatch, m);
0287 }
0288 template <class charT, class traits, std::size_t N>
0289 inline regex_token_iterator<const charT*, charT, traits> make_regex_token_iterator(const charT* p, const basic_regex<charT, traits>& e, const int (&submatch)[N], regex_constants::match_flag_type m = regex_constants::match_default)
0290 {
0291 return regex_token_iterator<const charT*, charT, traits>(p, p+traits::length(p), e, submatch, m);
0292 }
0293 template <class charT, class traits, class ST, class SA, std::size_t N>
0294 inline regex_token_iterator<typename std::basic_string<charT, ST, SA>::const_iterator, charT, traits> make_regex_token_iterator(const std::basic_string<charT, ST, SA>& p, const basic_regex<charT, traits>& e, const int (&submatch)[N], regex_constants::match_flag_type m = regex_constants::match_default)
0295 {
0296 return regex_token_iterator<typename std::basic_string<charT, ST, SA>::const_iterator, charT, traits>(p.begin(), p.end(), e, submatch, m);
0297 }
0298 template <class charT, class traits>
0299 inline regex_token_iterator<const charT*, charT, traits> make_regex_token_iterator(const charT* p, const basic_regex<charT, traits>& e, const std::vector<int>& submatch, regex_constants::match_flag_type m = regex_constants::match_default)
0300 {
0301 return regex_token_iterator<const charT*, charT, traits>(p, p+traits::length(p), e, submatch, m);
0302 }
0303 template <class charT, class traits, class ST, class SA>
0304 inline regex_token_iterator<typename std::basic_string<charT, ST, SA>::const_iterator, charT, traits> make_regex_token_iterator(const std::basic_string<charT, ST, SA>& p, const basic_regex<charT, traits>& e, const std::vector<int>& submatch, regex_constants::match_flag_type m = regex_constants::match_default)
0305 {
0306 return regex_token_iterator<typename std::basic_string<charT, ST, SA>::const_iterator, charT, traits>(p.begin(), p.end(), e, submatch, m);
0307 }
0308
0309 #ifdef BOOST_MSVC
0310 #pragma warning(pop)
0311 #pragma warning(push)
0312 #pragma warning(disable: 4103)
0313 #endif
0314 #ifdef BOOST_HAS_ABI_HEADERS
0315 # include BOOST_ABI_SUFFIX
0316 #endif
0317 #ifdef BOOST_MSVC
0318 #pragma warning(pop)
0319 #endif
0320
0321 }
0322
0323 #endif
0324
0325
0326
0327