Jeff Thompson | 86b6d64 | 2013-10-17 15:01:56 -0700 | [diff] [blame^] | 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_iterator.hpp |
| 15 | * VERSION see <ndnboost/version.hpp> |
| 16 | * DESCRIPTION: Provides u32regex_iterator implementation. |
| 17 | */ |
| 18 | |
| 19 | #ifndef NDNBOOST_REGEX_V4_U32REGEX_ITERATOR_HPP |
| 20 | #define NDNBOOST_REGEX_V4_U32REGEX_ITERATOR_HPP |
| 21 | |
| 22 | namespace ndnboost{ |
| 23 | |
| 24 | #ifdef NDNBOOST_HAS_ABI_HEADERS |
| 25 | # include NDNBOOST_ABI_PREFIX |
| 26 | #endif |
| 27 | |
| 28 | template <class BidirectionalIterator> |
| 29 | class u32regex_iterator_implementation |
| 30 | { |
| 31 | typedef u32regex regex_type; |
| 32 | |
| 33 | match_results<BidirectionalIterator> what; // current match |
| 34 | BidirectionalIterator base; // start of sequence |
| 35 | BidirectionalIterator end; // end of sequence |
| 36 | const regex_type re; // the expression |
| 37 | match_flag_type flags; // flags for matching |
| 38 | |
| 39 | public: |
| 40 | u32regex_iterator_implementation(const regex_type* p, BidirectionalIterator last, match_flag_type f) |
| 41 | : base(), end(last), re(*p), flags(f){} |
| 42 | bool init(BidirectionalIterator first) |
| 43 | { |
| 44 | base = first; |
| 45 | return u32regex_search(first, end, what, re, flags, base); |
| 46 | } |
| 47 | bool compare(const u32regex_iterator_implementation& that) |
| 48 | { |
| 49 | if(this == &that) return true; |
| 50 | 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); |
| 51 | } |
| 52 | const match_results<BidirectionalIterator>& get() |
| 53 | { return what; } |
| 54 | bool next() |
| 55 | { |
| 56 | //if(what.prefix().first != what[0].second) |
| 57 | // flags |= match_prev_avail; |
| 58 | BidirectionalIterator next_start = what[0].second; |
| 59 | match_flag_type f(flags); |
| 60 | if(!what.length()) |
| 61 | f |= regex_constants::match_not_initial_null; |
| 62 | //if(base != next_start) |
| 63 | // f |= regex_constants::match_not_bob; |
| 64 | bool result = u32regex_search(next_start, end, what, re, f, base); |
| 65 | if(result) |
| 66 | what.set_base(base); |
| 67 | return result; |
| 68 | } |
| 69 | private: |
| 70 | u32regex_iterator_implementation& operator=(const u32regex_iterator_implementation&); |
| 71 | }; |
| 72 | |
| 73 | template <class BidirectionalIterator> |
| 74 | class u32regex_iterator |
| 75 | #ifndef NDNBOOST_NO_STD_ITERATOR |
| 76 | : public std::iterator< |
| 77 | std::forward_iterator_tag, |
| 78 | match_results<BidirectionalIterator>, |
| 79 | typename re_detail::regex_iterator_traits<BidirectionalIterator>::difference_type, |
| 80 | const match_results<BidirectionalIterator>*, |
| 81 | const match_results<BidirectionalIterator>& > |
| 82 | #endif |
| 83 | { |
| 84 | private: |
| 85 | typedef u32regex_iterator_implementation<BidirectionalIterator> impl; |
| 86 | typedef shared_ptr<impl> pimpl; |
| 87 | public: |
| 88 | typedef u32regex regex_type; |
| 89 | typedef match_results<BidirectionalIterator> value_type; |
| 90 | typedef typename re_detail::regex_iterator_traits<BidirectionalIterator>::difference_type |
| 91 | difference_type; |
| 92 | typedef const value_type* pointer; |
| 93 | typedef const value_type& reference; |
| 94 | typedef std::forward_iterator_tag iterator_category; |
| 95 | |
| 96 | u32regex_iterator(){} |
| 97 | u32regex_iterator(BidirectionalIterator a, BidirectionalIterator b, |
| 98 | const regex_type& re, |
| 99 | match_flag_type m = match_default) |
| 100 | : pdata(new impl(&re, b, m)) |
| 101 | { |
| 102 | if(!pdata->init(a)) |
| 103 | { |
| 104 | pdata.reset(); |
| 105 | } |
| 106 | } |
| 107 | u32regex_iterator(const u32regex_iterator& that) |
| 108 | : pdata(that.pdata) {} |
| 109 | u32regex_iterator& operator=(const u32regex_iterator& that) |
| 110 | { |
| 111 | pdata = that.pdata; |
| 112 | return *this; |
| 113 | } |
| 114 | bool operator==(const u32regex_iterator& that)const |
| 115 | { |
| 116 | if((pdata.get() == 0) || (that.pdata.get() == 0)) |
| 117 | return pdata.get() == that.pdata.get(); |
| 118 | return pdata->compare(*(that.pdata.get())); |
| 119 | } |
| 120 | bool operator!=(const u32regex_iterator& that)const |
| 121 | { return !(*this == that); } |
| 122 | const value_type& operator*()const |
| 123 | { return pdata->get(); } |
| 124 | const value_type* operator->()const |
| 125 | { return &(pdata->get()); } |
| 126 | u32regex_iterator& operator++() |
| 127 | { |
| 128 | cow(); |
| 129 | if(0 == pdata->next()) |
| 130 | { |
| 131 | pdata.reset(); |
| 132 | } |
| 133 | return *this; |
| 134 | } |
| 135 | u32regex_iterator operator++(int) |
| 136 | { |
| 137 | u32regex_iterator result(*this); |
| 138 | ++(*this); |
| 139 | return result; |
| 140 | } |
| 141 | private: |
| 142 | |
| 143 | pimpl pdata; |
| 144 | |
| 145 | void cow() |
| 146 | { |
| 147 | // copy-on-write |
| 148 | if(pdata.get() && !pdata.unique()) |
| 149 | { |
| 150 | pdata.reset(new impl(*(pdata.get()))); |
| 151 | } |
| 152 | } |
| 153 | }; |
| 154 | |
| 155 | typedef u32regex_iterator<const char*> utf8regex_iterator; |
| 156 | typedef u32regex_iterator<const UChar*> utf16regex_iterator; |
| 157 | typedef u32regex_iterator<const UChar32*> utf32regex_iterator; |
| 158 | |
| 159 | inline u32regex_iterator<const char*> make_u32regex_iterator(const char* p, const u32regex& e, regex_constants::match_flag_type m = regex_constants::match_default) |
| 160 | { |
| 161 | return u32regex_iterator<const char*>(p, p+std::strlen(p), e, m); |
| 162 | } |
| 163 | #ifndef NDNBOOST_NO_WREGEX |
| 164 | inline u32regex_iterator<const wchar_t*> make_u32regex_iterator(const wchar_t* p, const u32regex& e, regex_constants::match_flag_type m = regex_constants::match_default) |
| 165 | { |
| 166 | return u32regex_iterator<const wchar_t*>(p, p+std::wcslen(p), e, m); |
| 167 | } |
| 168 | #endif |
| 169 | #if !defined(U_WCHAR_IS_UTF16) && (U_SIZEOF_WCHAR_T != 2) |
| 170 | inline u32regex_iterator<const UChar*> make_u32regex_iterator(const UChar* p, const u32regex& e, regex_constants::match_flag_type m = regex_constants::match_default) |
| 171 | { |
| 172 | return u32regex_iterator<const UChar*>(p, p+u_strlen(p), e, m); |
| 173 | } |
| 174 | #endif |
| 175 | template <class charT, class Traits, class Alloc> |
| 176 | inline u32regex_iterator<typename std::basic_string<charT, Traits, Alloc>::const_iterator> make_u32regex_iterator(const std::basic_string<charT, Traits, Alloc>& p, const u32regex& e, regex_constants::match_flag_type m = regex_constants::match_default) |
| 177 | { |
| 178 | typedef typename std::basic_string<charT, Traits, Alloc>::const_iterator iter_type; |
| 179 | return u32regex_iterator<iter_type>(p.begin(), p.end(), e, m); |
| 180 | } |
| 181 | inline u32regex_iterator<const UChar*> make_u32regex_iterator(const U_NAMESPACE_QUALIFIER UnicodeString& s, const u32regex& e, regex_constants::match_flag_type m = regex_constants::match_default) |
| 182 | { |
| 183 | return u32regex_iterator<const UChar*>(s.getBuffer(), s.getBuffer() + s.length(), e, m); |
| 184 | } |
| 185 | |
| 186 | #ifdef NDNBOOST_HAS_ABI_HEADERS |
| 187 | # include NDNBOOST_ABI_SUFFIX |
| 188 | #endif |
| 189 | |
| 190 | } // namespace ndnboost |
| 191 | |
| 192 | #endif // NDNBOOST_REGEX_V4_REGEX_ITERATOR_HPP |
| 193 | |