Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:51:27

0001 /*
0002  *
0003  * Copyright (c) 2003
0004  * John Maddock
0005  *
0006  * Use, modification and distribution are subject to the 
0007  * Boost Software License, Version 1.0. (See accompanying file 
0008  * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0009  *
0010  */
0011 
0012  /*
0013   *   LOCATION:    see http://www.boost.org for most recent version.
0014   *   FILE         u32regex_token_iterator.hpp
0015   *   VERSION      see <boost/version.hpp>
0016   *   DESCRIPTION: Provides u32regex_token_iterator implementation.
0017   */
0018 
0019 #ifndef BOOST_REGEX_V4_U32REGEX_TOKEN_ITERATOR_HPP
0020 #define BOOST_REGEX_V4_U32REGEX_TOKEN_ITERATOR_HPP
0021 
0022 #if (BOOST_WORKAROUND(BOOST_BORLANDC, >= 0x560) && BOOST_WORKAROUND(BOOST_BORLANDC, BOOST_TESTED_AT(0x570)))\
0023       || BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003))
0024 //
0025 // Borland C++ Builder 6, and Visual C++ 6,
0026 // can't cope with the array template constructor
0027 // so we have a template member that will accept any type as 
0028 // argument, and then assert that is really is an array:
0029 //
0030 #include <boost/static_assert.hpp>
0031 #include <boost/type_traits/is_array.hpp>
0032 #endif
0033 
0034 namespace boost{
0035 
0036 #ifdef BOOST_HAS_ABI_HEADERS
0037 #  include BOOST_ABI_PREFIX
0038 #endif
0039 #ifdef BOOST_MSVC
0040 #  pragma warning(push)
0041 #  pragma warning(disable:4700)
0042 #endif
0043 
0044 template <class BidirectionalIterator>
0045 class u32regex_token_iterator_implementation 
0046 {
0047    typedef u32regex                              regex_type;
0048    typedef sub_match<BidirectionalIterator>      value_type;
0049 
0050    match_results<BidirectionalIterator> what;   // current match
0051    BidirectionalIterator                end;    // end of search area
0052    BidirectionalIterator                base;   // start of search area
0053    const regex_type                     re;     // the expression
0054    match_flag_type                      flags;  // match flags
0055    value_type                           result; // the current string result
0056    int                                  N;      // the current sub-expression being enumerated
0057    std::vector<int>                     subs;   // the sub-expressions to enumerate
0058 
0059 public:
0060    u32regex_token_iterator_implementation(const regex_type* p, BidirectionalIterator last, int sub, match_flag_type f)
0061       : end(last), re(*p), flags(f){ subs.push_back(sub); }
0062    u32regex_token_iterator_implementation(const regex_type* p, BidirectionalIterator last, const std::vector<int>& v, match_flag_type f)
0063       : end(last), re(*p), flags(f), subs(v){}
0064 #if (BOOST_WORKAROUND(__BORLANDC__, >= 0x560) && BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x570)))\
0065       || BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003)) \
0066       || BOOST_WORKAROUND(__HP_aCC, < 60700)
0067    template <class T>
0068    u32regex_token_iterator_implementation(const regex_type* p, BidirectionalIterator last, const T& submatches, match_flag_type f)
0069       : end(last), re(*p), flags(f)
0070    {
0071       // assert that T really is an array:
0072       BOOST_STATIC_ASSERT(::boost::is_array<T>::value);
0073       const std::size_t array_size = sizeof(T) / sizeof(submatches[0]);
0074       for(std::size_t i = 0; i < array_size; ++i)
0075       {
0076          subs.push_back(submatches[i]);
0077       }
0078    }
0079 #else
0080    template <std::size_t CN>
0081    u32regex_token_iterator_implementation(const regex_type* p, BidirectionalIterator last, const int (&submatches)[CN], match_flag_type f)
0082       : end(last), re(*p), flags(f)
0083    {
0084       for(std::size_t i = 0; i < CN; ++i)
0085       {
0086          subs.push_back(submatches[i]);
0087       }
0088    }
0089 #endif
0090 
0091    bool init(BidirectionalIterator first)
0092    {
0093       base = first;
0094       N = 0;
0095       if(u32regex_search(first, end, what, re, flags, base) == true)
0096       {
0097          N = 0;
0098          result = ((subs[N] == -1) ? what.prefix() : what[(int)subs[N]]);
0099          return true;
0100       }
0101       else if((subs[N] == -1) && (first != end))
0102       {
0103          result.first = first;
0104          result.second = end;
0105          result.matched = (first != end);
0106          N = -1;
0107          return true;
0108       }
0109       return false;
0110    }
0111    bool compare(const u32regex_token_iterator_implementation& that)
0112    {
0113       if(this == &that) return true;
0114       return (&re.get_data() == &that.re.get_data()) 
0115          && (end == that.end) 
0116          && (flags == that.flags) 
0117          && (N == that.N) 
0118          && (what[0].first == that.what[0].first) 
0119          && (what[0].second == that.what[0].second);
0120    }
0121    const value_type& get()
0122    { return result; }
0123    bool next()
0124    {
0125       if(N == -1)
0126          return false;
0127       if(N+1 < (int)subs.size())
0128       {
0129          ++N;
0130          result =((subs[N] == -1) ? what.prefix() : what[subs[N]]);
0131          return true;
0132       }
0133       //if(what.prefix().first != what[0].second)
0134       //   flags |= match_prev_avail | regex_constants::match_not_bob;
0135       BidirectionalIterator last_end(what[0].second);
0136       if(u32regex_search(last_end, end, what, re, ((what[0].first == what[0].second) ? flags | regex_constants::match_not_initial_null : flags), base))
0137       {
0138          N =0;
0139          result =((subs[N] == -1) ? what.prefix() : what[subs[N]]);
0140          return true;
0141       }
0142       else if((last_end != end) && (subs[0] == -1))
0143       {
0144          N =-1;
0145          result.first = last_end;
0146          result.second = end;
0147          result.matched = (last_end != end);
0148          return true;
0149       }
0150       return false;
0151    }
0152 private:
0153    u32regex_token_iterator_implementation& operator=(const u32regex_token_iterator_implementation&);
0154 };
0155 
0156 template <class BidirectionalIterator>
0157 class u32regex_token_iterator 
0158 {
0159 private:
0160    typedef u32regex_token_iterator_implementation<BidirectionalIterator> impl;
0161    typedef shared_ptr<impl> pimpl;
0162 public:
0163    typedef          u32regex                                                regex_type;
0164    typedef          sub_match<BidirectionalIterator>                        value_type;
0165    typedef typename BOOST_REGEX_DETAIL_NS::regex_iterator_traits<BidirectionalIterator>::difference_type 
0166                                                                             difference_type;
0167    typedef          const value_type*                                       pointer;
0168    typedef          const value_type&                                       reference; 
0169    typedef          std::forward_iterator_tag                               iterator_category;
0170    
0171    u32regex_token_iterator(){}
0172    u32regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b, const regex_type& re, 
0173                         int submatch = 0, match_flag_type m = match_default)
0174                         : pdata(new impl(&re, b, submatch, m))
0175    {
0176       if(!pdata->init(a))
0177          pdata.reset();
0178    }
0179    u32regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b, const regex_type& re, 
0180                         const std::vector<int>& submatches, match_flag_type m = match_default)
0181                         : pdata(new impl(&re, b, submatches, m))
0182    {
0183       if(!pdata->init(a))
0184          pdata.reset();
0185    }
0186 #if (BOOST_WORKAROUND(__BORLANDC__, >= 0x560) && BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x570)))\
0187       || BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003)) \
0188       || BOOST_WORKAROUND(__HP_aCC, < 60700)
0189    template <class T>
0190    u32regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b, const regex_type& re,
0191                         const T& submatches, match_flag_type m = match_default)
0192                         : pdata(new impl(&re, b, submatches, m))
0193    {
0194       if(!pdata->init(a))
0195          pdata.reset();
0196    }
0197 #else
0198    template <std::size_t N>
0199    u32regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b, const regex_type& re,
0200                         const int (&submatches)[N], match_flag_type m = match_default)
0201                         : pdata(new impl(&re, b, submatches, m))
0202    {
0203       if(!pdata->init(a))
0204          pdata.reset();
0205    }
0206 #endif
0207    u32regex_token_iterator(const u32regex_token_iterator& that)
0208       : pdata(that.pdata) {}
0209    u32regex_token_iterator& operator=(const u32regex_token_iterator& that)
0210    {
0211       pdata = that.pdata;
0212       return *this;
0213    }
0214    bool operator==(const u32regex_token_iterator& that)const
0215    { 
0216       if((pdata.get() == 0) || (that.pdata.get() == 0))
0217          return pdata.get() == that.pdata.get();
0218       return pdata->compare(*(that.pdata.get())); 
0219    }
0220    bool operator!=(const u32regex_token_iterator& that)const
0221    { return !(*this == that); }
0222    const value_type& operator*()const
0223    { return pdata->get(); }
0224    const value_type* operator->()const
0225    { return &(pdata->get()); }
0226    u32regex_token_iterator& operator++()
0227    {
0228       cow();
0229       if(0 == pdata->next())
0230       {
0231          pdata.reset();
0232       }
0233       return *this;
0234    }
0235    u32regex_token_iterator operator++(int)
0236    {
0237       u32regex_token_iterator result(*this);
0238       ++(*this);
0239       return result;
0240    }
0241 private:
0242 
0243    pimpl pdata;
0244 
0245    void cow()
0246    {
0247       // copy-on-write
0248       if(pdata.get() && !pdata.unique())
0249       {
0250          pdata.reset(new impl(*(pdata.get())));
0251       }
0252    }
0253 };
0254 
0255 typedef u32regex_token_iterator<const char*> utf8regex_token_iterator;
0256 typedef u32regex_token_iterator<const UChar*> utf16regex_token_iterator;
0257 typedef u32regex_token_iterator<const UChar32*> utf32regex_token_iterator;
0258 
0259 // construction from an integral sub_match state_id:
0260 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)
0261 {
0262    return u32regex_token_iterator<const char*>(p, p+std::strlen(p), e, submatch, m);
0263 }
0264 #ifndef BOOST_NO_WREGEX
0265 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)
0266 {
0267    return u32regex_token_iterator<const wchar_t*>(p, p+std::wcslen(p), e, submatch, m);
0268 }
0269 #endif
0270 #if !defined(BOOST_REGEX_UCHAR_IS_WCHAR_T)
0271 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)
0272 {
0273    return u32regex_token_iterator<const UChar*>(p, p+u_strlen(p), e, submatch, m);
0274 }
0275 #endif
0276 template <class charT, class Traits, class Alloc>
0277 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)
0278 {
0279    typedef typename std::basic_string<charT, Traits, Alloc>::const_iterator iter_type;
0280    return u32regex_token_iterator<iter_type>(p.begin(), p.end(), e, submatch, m);
0281 }
0282 inline u32regex_token_iterator<const UChar*> 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)
0283 {
0284    return u32regex_token_iterator<const UChar*>(s.getBuffer(), s.getBuffer() + s.length(), e, submatch, m);
0285 }
0286 
0287 // construction from a reference to an array:
0288 template <std::size_t N>
0289 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)
0290 {
0291    return u32regex_token_iterator<const char*>(p, p+std::strlen(p), e, submatch, m);
0292 }
0293 #ifndef BOOST_NO_WREGEX
0294 template <std::size_t N>
0295 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)
0296 {
0297    return u32regex_token_iterator<const wchar_t*>(p, p+std::wcslen(p), e, submatch, m);
0298 }
0299 #endif
0300 #if !defined(BOOST_REGEX_UCHAR_IS_WCHAR_T)
0301 template <std::size_t N>
0302 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)
0303 {
0304    return u32regex_token_iterator<const UChar*>(p, p+u_strlen(p), e, submatch, m);
0305 }
0306 #endif
0307 template <class charT, class Traits, class Alloc, std::size_t N>
0308 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)
0309 {
0310    typedef typename std::basic_string<charT, Traits, Alloc>::const_iterator iter_type;
0311    return u32regex_token_iterator<iter_type>(p.begin(), p.end(), e, submatch, m);
0312 }
0313 template <std::size_t N>
0314 inline u32regex_token_iterator<const UChar*> 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)
0315 {
0316    return u32regex_token_iterator<const UChar*>(s.getBuffer(), s.getBuffer() + s.length(), e, submatch, m);
0317 }
0318 
0319 // construction from a vector of sub_match state_id's:
0320 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)
0321 {
0322    return u32regex_token_iterator<const char*>(p, p+std::strlen(p), e, submatch, m);
0323 }
0324 #ifndef BOOST_NO_WREGEX
0325 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)
0326 {
0327    return u32regex_token_iterator<const wchar_t*>(p, p+std::wcslen(p), e, submatch, m);
0328 }
0329 #endif
0330 #if !defined(U_WCHAR_IS_UTF16) && (U_SIZEOF_WCHAR_T != 2)
0331 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)
0332 {
0333    return u32regex_token_iterator<const UChar*>(p, p+u_strlen(p), e, submatch, m);
0334 }
0335 #endif
0336 template <class charT, class Traits, class Alloc>
0337 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)
0338 {
0339    typedef typename std::basic_string<charT, Traits, Alloc>::const_iterator iter_type;
0340    return u32regex_token_iterator<iter_type>(p.begin(), p.end(), e, submatch, m);
0341 }
0342 inline u32regex_token_iterator<const UChar*> make_u32regex_token_iterator(const U_NAMESPACE_QUALIFIER UnicodeString& s, const u32regex& e, const std::vector<int>& submatch, regex_constants::match_flag_type m = regex_constants::match_default)
0343 {
0344    return u32regex_token_iterator<const UChar*>(s.getBuffer(), s.getBuffer() + s.length(), e, submatch, m);
0345 }
0346 
0347 #ifdef BOOST_MSVC
0348 #  pragma warning(pop)
0349 #endif
0350 #ifdef BOOST_HAS_ABI_HEADERS
0351 #  include BOOST_ABI_SUFFIX
0352 #endif
0353 
0354 } // namespace boost
0355 
0356 #endif // BOOST_REGEX_V4_REGEX_TOKEN_ITERATOR_HPP
0357 
0358 
0359 
0360