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