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