Chris@16
|
1 /*
|
Chris@16
|
2 *
|
Chris@16
|
3 * Copyright (c) 2003
|
Chris@16
|
4 * John Maddock
|
Chris@16
|
5 *
|
Chris@16
|
6 * Use, modification and distribution are subject to the
|
Chris@16
|
7 * Boost Software License, Version 1.0. (See accompanying file
|
Chris@16
|
8 * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
9 *
|
Chris@16
|
10 */
|
Chris@16
|
11
|
Chris@16
|
12 /*
|
Chris@16
|
13 * LOCATION: see http://www.boost.org for most recent version.
|
Chris@16
|
14 * FILE u32regex_iterator.hpp
|
Chris@16
|
15 * VERSION see <boost/version.hpp>
|
Chris@16
|
16 * DESCRIPTION: Provides u32regex_iterator implementation.
|
Chris@16
|
17 */
|
Chris@16
|
18
|
Chris@16
|
19 #ifndef BOOST_REGEX_V4_U32REGEX_ITERATOR_HPP
|
Chris@16
|
20 #define BOOST_REGEX_V4_U32REGEX_ITERATOR_HPP
|
Chris@16
|
21
|
Chris@16
|
22 namespace boost{
|
Chris@16
|
23
|
Chris@16
|
24 #ifdef BOOST_HAS_ABI_HEADERS
|
Chris@16
|
25 # include BOOST_ABI_PREFIX
|
Chris@16
|
26 #endif
|
Chris@16
|
27
|
Chris@16
|
28 template <class BidirectionalIterator>
|
Chris@16
|
29 class u32regex_iterator_implementation
|
Chris@16
|
30 {
|
Chris@16
|
31 typedef u32regex regex_type;
|
Chris@16
|
32
|
Chris@16
|
33 match_results<BidirectionalIterator> what; // current match
|
Chris@16
|
34 BidirectionalIterator base; // start of sequence
|
Chris@16
|
35 BidirectionalIterator end; // end of sequence
|
Chris@16
|
36 const regex_type re; // the expression
|
Chris@16
|
37 match_flag_type flags; // flags for matching
|
Chris@16
|
38
|
Chris@16
|
39 public:
|
Chris@16
|
40 u32regex_iterator_implementation(const regex_type* p, BidirectionalIterator last, match_flag_type f)
|
Chris@16
|
41 : base(), end(last), re(*p), flags(f){}
|
Chris@16
|
42 bool init(BidirectionalIterator first)
|
Chris@16
|
43 {
|
Chris@16
|
44 base = first;
|
Chris@16
|
45 return u32regex_search(first, end, what, re, flags, base);
|
Chris@16
|
46 }
|
Chris@16
|
47 bool compare(const u32regex_iterator_implementation& that)
|
Chris@16
|
48 {
|
Chris@16
|
49 if(this == &that) return true;
|
Chris@16
|
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);
|
Chris@16
|
51 }
|
Chris@16
|
52 const match_results<BidirectionalIterator>& get()
|
Chris@16
|
53 { return what; }
|
Chris@16
|
54 bool next()
|
Chris@16
|
55 {
|
Chris@16
|
56 //if(what.prefix().first != what[0].second)
|
Chris@16
|
57 // flags |= match_prev_avail;
|
Chris@16
|
58 BidirectionalIterator next_start = what[0].second;
|
Chris@16
|
59 match_flag_type f(flags);
|
Chris@16
|
60 if(!what.length())
|
Chris@16
|
61 f |= regex_constants::match_not_initial_null;
|
Chris@16
|
62 //if(base != next_start)
|
Chris@16
|
63 // f |= regex_constants::match_not_bob;
|
Chris@16
|
64 bool result = u32regex_search(next_start, end, what, re, f, base);
|
Chris@16
|
65 if(result)
|
Chris@16
|
66 what.set_base(base);
|
Chris@16
|
67 return result;
|
Chris@16
|
68 }
|
Chris@16
|
69 private:
|
Chris@16
|
70 u32regex_iterator_implementation& operator=(const u32regex_iterator_implementation&);
|
Chris@16
|
71 };
|
Chris@16
|
72
|
Chris@16
|
73 template <class BidirectionalIterator>
|
Chris@16
|
74 class u32regex_iterator
|
Chris@16
|
75 #ifndef BOOST_NO_STD_ITERATOR
|
Chris@16
|
76 : public std::iterator<
|
Chris@16
|
77 std::forward_iterator_tag,
|
Chris@16
|
78 match_results<BidirectionalIterator>,
|
Chris@16
|
79 typename re_detail::regex_iterator_traits<BidirectionalIterator>::difference_type,
|
Chris@16
|
80 const match_results<BidirectionalIterator>*,
|
Chris@16
|
81 const match_results<BidirectionalIterator>& >
|
Chris@16
|
82 #endif
|
Chris@16
|
83 {
|
Chris@16
|
84 private:
|
Chris@16
|
85 typedef u32regex_iterator_implementation<BidirectionalIterator> impl;
|
Chris@16
|
86 typedef shared_ptr<impl> pimpl;
|
Chris@16
|
87 public:
|
Chris@16
|
88 typedef u32regex regex_type;
|
Chris@16
|
89 typedef match_results<BidirectionalIterator> value_type;
|
Chris@16
|
90 typedef typename re_detail::regex_iterator_traits<BidirectionalIterator>::difference_type
|
Chris@16
|
91 difference_type;
|
Chris@16
|
92 typedef const value_type* pointer;
|
Chris@16
|
93 typedef const value_type& reference;
|
Chris@16
|
94 typedef std::forward_iterator_tag iterator_category;
|
Chris@16
|
95
|
Chris@16
|
96 u32regex_iterator(){}
|
Chris@16
|
97 u32regex_iterator(BidirectionalIterator a, BidirectionalIterator b,
|
Chris@16
|
98 const regex_type& re,
|
Chris@16
|
99 match_flag_type m = match_default)
|
Chris@16
|
100 : pdata(new impl(&re, b, m))
|
Chris@16
|
101 {
|
Chris@16
|
102 if(!pdata->init(a))
|
Chris@16
|
103 {
|
Chris@16
|
104 pdata.reset();
|
Chris@16
|
105 }
|
Chris@16
|
106 }
|
Chris@16
|
107 u32regex_iterator(const u32regex_iterator& that)
|
Chris@16
|
108 : pdata(that.pdata) {}
|
Chris@16
|
109 u32regex_iterator& operator=(const u32regex_iterator& that)
|
Chris@16
|
110 {
|
Chris@16
|
111 pdata = that.pdata;
|
Chris@16
|
112 return *this;
|
Chris@16
|
113 }
|
Chris@16
|
114 bool operator==(const u32regex_iterator& that)const
|
Chris@16
|
115 {
|
Chris@16
|
116 if((pdata.get() == 0) || (that.pdata.get() == 0))
|
Chris@16
|
117 return pdata.get() == that.pdata.get();
|
Chris@16
|
118 return pdata->compare(*(that.pdata.get()));
|
Chris@16
|
119 }
|
Chris@16
|
120 bool operator!=(const u32regex_iterator& that)const
|
Chris@16
|
121 { return !(*this == that); }
|
Chris@16
|
122 const value_type& operator*()const
|
Chris@16
|
123 { return pdata->get(); }
|
Chris@16
|
124 const value_type* operator->()const
|
Chris@16
|
125 { return &(pdata->get()); }
|
Chris@16
|
126 u32regex_iterator& operator++()
|
Chris@16
|
127 {
|
Chris@16
|
128 cow();
|
Chris@16
|
129 if(0 == pdata->next())
|
Chris@16
|
130 {
|
Chris@16
|
131 pdata.reset();
|
Chris@16
|
132 }
|
Chris@16
|
133 return *this;
|
Chris@16
|
134 }
|
Chris@16
|
135 u32regex_iterator operator++(int)
|
Chris@16
|
136 {
|
Chris@16
|
137 u32regex_iterator result(*this);
|
Chris@16
|
138 ++(*this);
|
Chris@16
|
139 return result;
|
Chris@16
|
140 }
|
Chris@16
|
141 private:
|
Chris@16
|
142
|
Chris@16
|
143 pimpl pdata;
|
Chris@16
|
144
|
Chris@16
|
145 void cow()
|
Chris@16
|
146 {
|
Chris@16
|
147 // copy-on-write
|
Chris@16
|
148 if(pdata.get() && !pdata.unique())
|
Chris@16
|
149 {
|
Chris@16
|
150 pdata.reset(new impl(*(pdata.get())));
|
Chris@16
|
151 }
|
Chris@16
|
152 }
|
Chris@16
|
153 };
|
Chris@16
|
154
|
Chris@16
|
155 typedef u32regex_iterator<const char*> utf8regex_iterator;
|
Chris@16
|
156 typedef u32regex_iterator<const UChar*> utf16regex_iterator;
|
Chris@16
|
157 typedef u32regex_iterator<const UChar32*> utf32regex_iterator;
|
Chris@16
|
158
|
Chris@16
|
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)
|
Chris@16
|
160 {
|
Chris@16
|
161 return u32regex_iterator<const char*>(p, p+std::strlen(p), e, m);
|
Chris@16
|
162 }
|
Chris@16
|
163 #ifndef BOOST_NO_WREGEX
|
Chris@16
|
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)
|
Chris@16
|
165 {
|
Chris@16
|
166 return u32regex_iterator<const wchar_t*>(p, p+std::wcslen(p), e, m);
|
Chris@16
|
167 }
|
Chris@16
|
168 #endif
|
Chris@16
|
169 #if !defined(U_WCHAR_IS_UTF16) && (U_SIZEOF_WCHAR_T != 2)
|
Chris@16
|
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)
|
Chris@16
|
171 {
|
Chris@16
|
172 return u32regex_iterator<const UChar*>(p, p+u_strlen(p), e, m);
|
Chris@16
|
173 }
|
Chris@16
|
174 #endif
|
Chris@16
|
175 template <class charT, class Traits, class Alloc>
|
Chris@16
|
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)
|
Chris@16
|
177 {
|
Chris@16
|
178 typedef typename std::basic_string<charT, Traits, Alloc>::const_iterator iter_type;
|
Chris@16
|
179 return u32regex_iterator<iter_type>(p.begin(), p.end(), e, m);
|
Chris@16
|
180 }
|
Chris@16
|
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)
|
Chris@16
|
182 {
|
Chris@16
|
183 return u32regex_iterator<const UChar*>(s.getBuffer(), s.getBuffer() + s.length(), e, m);
|
Chris@16
|
184 }
|
Chris@16
|
185
|
Chris@16
|
186 #ifdef BOOST_HAS_ABI_HEADERS
|
Chris@16
|
187 # include BOOST_ABI_SUFFIX
|
Chris@16
|
188 #endif
|
Chris@16
|
189
|
Chris@16
|
190 } // namespace boost
|
Chris@16
|
191
|
Chris@16
|
192 #endif // BOOST_REGEX_V4_REGEX_ITERATOR_HPP
|
Chris@16
|
193
|