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