]> git.lyx.org Git - lyx.git/blob - boost/boost/regex/icu.hpp
boost: update to 1.42.0
[lyx.git] / boost / boost / regex / icu.hpp
1 /*
2  *
3  * Copyright (c) 2004
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         icu.hpp
15   *   VERSION      see <boost/version.hpp>
16   *   DESCRIPTION: Unicode regular expressions on top of the ICU Library.
17   */
18
19 #ifndef BOOST_REGEX_ICU_HPP
20 #define BOOST_REGEX_ICU_HPP
21
22 #include <unicode/utypes.h>
23 #include <unicode/uchar.h>
24 #include <unicode/coll.h>
25 #include <boost/regex.hpp>
26 #include <boost/regex/pending/unicode_iterator.hpp>
27 #include <boost/mpl/int_fwd.hpp>
28 #include <bitset>
29
30
31 namespace boost{
32
33 namespace re_detail{
34
35 // 
36 // Implementation details:
37 //
38 class BOOST_REGEX_DECL icu_regex_traits_implementation
39 {
40    typedef UChar32                      char_type;
41    typedef std::size_t                  size_type;
42    typedef std::vector<char_type>       string_type;
43    typedef U_NAMESPACE_QUALIFIER Locale locale_type;
44    typedef boost::uint_least32_t        char_class_type;
45 public:
46    icu_regex_traits_implementation(const U_NAMESPACE_QUALIFIER Locale& l)
47       : m_locale(l)
48    {
49       UErrorCode success = U_ZERO_ERROR;
50       m_collator.reset(U_NAMESPACE_QUALIFIER Collator::createInstance(l, success));
51       if(U_SUCCESS(success) == 0)
52          init_error();
53       m_collator->setStrength(U_NAMESPACE_QUALIFIER Collator::IDENTICAL);
54       success = U_ZERO_ERROR;
55       m_primary_collator.reset(U_NAMESPACE_QUALIFIER Collator::createInstance(l, success));
56       if(U_SUCCESS(success) == 0)
57          init_error();
58       m_primary_collator->setStrength(U_NAMESPACE_QUALIFIER Collator::PRIMARY);
59    }
60    U_NAMESPACE_QUALIFIER Locale getloc()const
61    {
62       return m_locale;
63    }
64    string_type do_transform(const char_type* p1, const char_type* p2, const U_NAMESPACE_QUALIFIER Collator* pcoll) const;
65    string_type transform(const char_type* p1, const char_type* p2) const
66    {
67       return do_transform(p1, p2, m_collator.get());
68    }
69    string_type transform_primary(const char_type* p1, const char_type* p2) const
70    {
71       return do_transform(p1, p2, m_primary_collator.get());
72    }
73 private:
74    void init_error()
75    {
76       std::runtime_error e("Could not initialize ICU resources");
77       boost::throw_exception(e);
78    }
79    U_NAMESPACE_QUALIFIER Locale m_locale;                                  // The ICU locale that we're using
80    boost::scoped_ptr< U_NAMESPACE_QUALIFIER Collator> m_collator;          // The full collation object
81    boost::scoped_ptr< U_NAMESPACE_QUALIFIER Collator> m_primary_collator;  // The primary collation object
82 };
83
84 inline boost::shared_ptr<icu_regex_traits_implementation> get_icu_regex_traits_implementation(const U_NAMESPACE_QUALIFIER Locale& loc)
85 {
86    return boost::shared_ptr<icu_regex_traits_implementation>(new icu_regex_traits_implementation(loc));
87 }
88
89 }
90
91 class BOOST_REGEX_DECL icu_regex_traits
92 {
93 public:
94    typedef UChar32                      char_type;
95    typedef std::size_t                  size_type;
96    typedef std::vector<char_type>       string_type;
97    typedef U_NAMESPACE_QUALIFIER Locale locale_type;
98 #ifdef BOOST_NO_INT64_T
99    typedef std::bitset<64>              char_class_type;
100 #else
101    typedef boost::uint64_t              char_class_type;
102 #endif
103
104    struct boost_extensions_tag{};
105
106    icu_regex_traits()
107       : m_pimpl(re_detail::get_icu_regex_traits_implementation(U_NAMESPACE_QUALIFIER Locale()))
108    {
109    }
110    static size_type length(const char_type* p);
111
112    ::boost::regex_constants::syntax_type syntax_type(char_type c)const
113    {
114       return ((c < 0x7f) && (c > 0)) ? re_detail::get_default_syntax_type(static_cast<char>(c)) : regex_constants::syntax_char;
115    }
116    ::boost::regex_constants::escape_syntax_type escape_syntax_type(char_type c) const
117    {
118       return ((c < 0x7f) && (c > 0)) ? re_detail::get_default_escape_syntax_type(static_cast<char>(c)) : regex_constants::syntax_char;
119    }
120    char_type translate(char_type c) const
121    {
122       return c;
123    }
124    char_type translate_nocase(char_type c) const
125    {
126       return ::u_tolower(c);
127    }
128    char_type translate(char_type c, bool icase) const
129    {
130       return icase ? translate_nocase(c) : translate(c);
131    }
132    char_type tolower(char_type c) const
133    {
134       return ::u_tolower(c);
135    }
136    char_type toupper(char_type c) const
137    {
138       return ::u_toupper(c);
139    }
140    string_type transform(const char_type* p1, const char_type* p2) const
141    {
142       return m_pimpl->transform(p1, p2);
143    }
144    string_type transform_primary(const char_type* p1, const char_type* p2) const
145    {
146       return m_pimpl->transform_primary(p1, p2);
147    }
148    char_class_type lookup_classname(const char_type* p1, const char_type* p2) const;
149    string_type lookup_collatename(const char_type* p1, const char_type* p2) const;
150    bool isctype(char_type c, char_class_type f) const;
151    int toi(const char_type*& p1, const char_type* p2, int radix)const
152    {
153       return re_detail::global_toi(p1, p2, radix, *this);
154    }
155    int value(char_type c, int radix)const
156    {
157       return u_digit(c, static_cast< ::int8_t>(radix));
158    }
159    locale_type imbue(locale_type l)
160    {
161       locale_type result(m_pimpl->getloc());
162       m_pimpl = re_detail::get_icu_regex_traits_implementation(l);
163       return result;
164    }
165    locale_type getloc()const
166    {
167       return locale_type();
168    }
169    std::string error_string(::boost::regex_constants::error_type n) const
170    {
171       return re_detail::get_default_error_string(n);
172    }
173 private:
174    icu_regex_traits(const icu_regex_traits&);
175    icu_regex_traits& operator=(const icu_regex_traits&);
176
177    //
178    // define the bitmasks offsets we need for additional character properties:
179    //
180    enum{
181       offset_blank = U_CHAR_CATEGORY_COUNT,
182       offset_space = U_CHAR_CATEGORY_COUNT+1,
183       offset_xdigit = U_CHAR_CATEGORY_COUNT+2,
184       offset_underscore = U_CHAR_CATEGORY_COUNT+3,
185       offset_unicode = U_CHAR_CATEGORY_COUNT+4,
186       offset_any = U_CHAR_CATEGORY_COUNT+5,
187       offset_ascii = U_CHAR_CATEGORY_COUNT+6,
188       offset_horizontal = U_CHAR_CATEGORY_COUNT+7,
189       offset_vertical = U_CHAR_CATEGORY_COUNT+8
190    };
191
192    //
193    // and now the masks:
194    //
195    static const char_class_type mask_blank;
196    static const char_class_type mask_space;
197    static const char_class_type mask_xdigit;
198    static const char_class_type mask_underscore;
199    static const char_class_type mask_unicode;
200    static const char_class_type mask_any;
201    static const char_class_type mask_ascii;
202    static const char_class_type mask_horizontal;
203    static const char_class_type mask_vertical;
204
205    static char_class_type lookup_icu_mask(const ::UChar32* p1, const ::UChar32* p2);
206
207    boost::shared_ptr< ::boost::re_detail::icu_regex_traits_implementation> m_pimpl;
208 };
209
210 } // namespace boost
211
212 //
213 // template instances:
214 //
215 #define BOOST_REGEX_CHAR_T UChar32
216 #undef BOOST_REGEX_TRAITS_T
217 #define BOOST_REGEX_TRAITS_T , icu_regex_traits
218 #define BOOST_REGEX_ICU_INSTANCES
219 #ifdef BOOST_REGEX_ICU_INSTANTIATE
220 #  define BOOST_REGEX_INSTANTIATE
221 #endif
222 #include <boost/regex/v4/instances.hpp>
223 #undef BOOST_REGEX_CHAR_T
224 #undef BOOST_REGEX_TRAITS_T
225 #undef BOOST_REGEX_ICU_INSTANCES
226 #ifdef BOOST_REGEX_INSTANTIATE
227 #  undef BOOST_REGEX_INSTANTIATE
228 #endif
229
230 namespace boost{
231
232 // types:
233 typedef basic_regex< ::UChar32, icu_regex_traits> u32regex;
234 typedef match_results<const ::UChar32*> u32match;
235 typedef match_results<const ::UChar*> u16match;
236
237 //
238 // Construction of 32-bit regex types from UTF-8 and UTF-16 primitives:
239 //
240 namespace re_detail{
241
242 #if !defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(__IBMCPP__)
243 template <class InputIterator>
244 inline u32regex do_make_u32regex(InputIterator i, 
245                               InputIterator j, 
246                               boost::regex_constants::syntax_option_type opt, 
247                               const boost::mpl::int_<1>*)
248 {
249    typedef boost::u8_to_u32_iterator<InputIterator, UChar32> conv_type;
250    return u32regex(conv_type(i), conv_type(j), opt);
251 }
252
253 template <class InputIterator>
254 inline u32regex do_make_u32regex(InputIterator i, 
255                               InputIterator j, 
256                               boost::regex_constants::syntax_option_type opt, 
257                               const boost::mpl::int_<2>*)
258 {
259    typedef boost::u16_to_u32_iterator<InputIterator, UChar32> conv_type;
260    return u32regex(conv_type(i), conv_type(j), opt);
261 }
262
263 template <class InputIterator>
264 inline u32regex do_make_u32regex(InputIterator i, 
265                               InputIterator j, 
266                               boost::regex_constants::syntax_option_type opt, 
267                               const boost::mpl::int_<4>*)
268 {
269    return u32regex(i, j, opt);
270 }
271 #else
272 template <class InputIterator>
273 inline u32regex do_make_u32regex(InputIterator i, 
274                               InputIterator j, 
275                               boost::regex_constants::syntax_option_type opt, 
276                               const boost::mpl::int_<1>*)
277 {
278    typedef boost::u8_to_u32_iterator<InputIterator, UChar32> conv_type;
279    typedef std::vector<UChar32> vector_type;
280    vector_type v;
281    conv_type a(i), b(j);
282    while(a != b)
283    {
284       v.push_back(*a);
285       ++a;
286    }
287    if(v.size())
288       return u32regex(&*v.begin(), v.size(), opt);
289    return u32regex(static_cast<UChar32 const*>(0), static_cast<u32regex::size_type>(0), opt);
290 }
291
292 template <class InputIterator>
293 inline u32regex do_make_u32regex(InputIterator i, 
294                               InputIterator j, 
295                               boost::regex_constants::syntax_option_type opt, 
296                               const boost::mpl::int_<2>*)
297 {
298    typedef boost::u16_to_u32_iterator<InputIterator, UChar32> conv_type;
299    typedef std::vector<UChar32> vector_type;
300    vector_type v;
301    conv_type a(i), b(j);
302    while(a != b)
303    {
304       v.push_back(*a);
305       ++a;
306    }
307    if(v.size())
308       return u32regex(&*v.begin(), v.size(), opt);
309    return u32regex(static_cast<UChar32 const*>(0), static_cast<u32regex::size_type>(0), opt);
310 }
311
312 template <class InputIterator>
313 inline u32regex do_make_u32regex(InputIterator i, 
314                               InputIterator j, 
315                               boost::regex_constants::syntax_option_type opt, 
316                               const boost::mpl::int_<4>*)
317 {
318    typedef std::vector<UChar32> vector_type;
319    vector_type v;
320    while(i != j)
321    {
322       v.push_back((UChar32)(*i));
323       ++i;
324    }
325    if(v.size())
326       return u32regex(&*v.begin(), v.size(), opt);
327    return u32regex(static_cast<UChar32 const*>(0), static_cast<u32regex::size_type>(0), opt);
328 }
329 #endif
330 }
331
332 //
333 // Construction from an iterator pair:
334 //
335 template <class InputIterator>
336 inline u32regex make_u32regex(InputIterator i, 
337                               InputIterator j, 
338                               boost::regex_constants::syntax_option_type opt)
339 {
340    return re_detail::do_make_u32regex(i, j, opt, static_cast<boost::mpl::int_<sizeof(*i)> const*>(0));
341 }
342 //
343 // construction from UTF-8 nul-terminated strings:
344 //
345 inline u32regex make_u32regex(const char* p, boost::regex_constants::syntax_option_type opt = boost::regex_constants::perl)
346 {
347    return re_detail::do_make_u32regex(p, p + std::strlen(p), opt, static_cast<boost::mpl::int_<1> const*>(0));
348 }
349 inline u32regex make_u32regex(const unsigned char* p, boost::regex_constants::syntax_option_type opt = boost::regex_constants::perl)
350 {
351    return re_detail::do_make_u32regex(p, p + std::strlen(reinterpret_cast<const char*>(p)), opt, static_cast<boost::mpl::int_<1> const*>(0));
352 }
353 //
354 // construction from UTF-16 nul-terminated strings:
355 //
356 #ifndef BOOST_NO_WREGEX
357 inline u32regex make_u32regex(const wchar_t* p, boost::regex_constants::syntax_option_type opt = boost::regex_constants::perl)
358 {
359    return re_detail::do_make_u32regex(p, p + std::wcslen(p), opt, static_cast<boost::mpl::int_<sizeof(wchar_t)> const*>(0));
360 }
361 #endif
362 #if !defined(U_WCHAR_IS_UTF16) && (U_SIZEOF_WCHAR_T != 2)
363 inline u32regex make_u32regex(const UChar* p, boost::regex_constants::syntax_option_type opt = boost::regex_constants::perl)
364 {
365    return re_detail::do_make_u32regex(p, p + u_strlen(p), opt, static_cast<boost::mpl::int_<2> const*>(0));
366 }
367 #endif
368 //
369 // construction from basic_string class-template:
370 //
371 template<class C, class T, class A>
372 inline u32regex make_u32regex(const std::basic_string<C, T, A>& s, boost::regex_constants::syntax_option_type opt = boost::regex_constants::perl)
373 {
374    return re_detail::do_make_u32regex(s.begin(), s.end(), opt, static_cast<boost::mpl::int_<sizeof(C)> const*>(0));
375 }
376 //
377 // Construction from ICU string type:
378 //
379 inline u32regex make_u32regex(const U_NAMESPACE_QUALIFIER UnicodeString& s, boost::regex_constants::syntax_option_type opt = boost::regex_constants::perl)
380 {
381    return re_detail::do_make_u32regex(s.getBuffer(), s.getBuffer() + s.length(), opt, static_cast<boost::mpl::int_<2> const*>(0));
382 }
383
384 //
385 // regex_match overloads that widen the character type as appropriate:
386 //
387 namespace re_detail{
388 template<class MR1, class MR2>
389 void copy_results(MR1& out, MR2 const& in)
390 {
391    // copy results from an adapted MR2 match_results:
392    out.set_size(in.size(), in.prefix().first.base(), in.suffix().second.base());
393    out.set_base(in.base().base());
394    for(int i = 0; i < (int)in.size(); ++i)
395    {
396       if(in[i].matched)
397       {
398          out.set_first(in[i].first.base(), i);
399          out.set_second(in[i].second.base(), i);
400       }
401    }
402 }
403
404 template <class BidiIterator, class Allocator>
405 inline bool do_regex_match(BidiIterator first, BidiIterator last, 
406                  match_results<BidiIterator, Allocator>& m, 
407                  const u32regex& e, 
408                  match_flag_type flags,
409                  boost::mpl::int_<4> const*)
410 {
411    return ::boost::regex_match(first, last, m, e, flags);
412 }
413 template <class BidiIterator, class Allocator>
414 bool do_regex_match(BidiIterator first, BidiIterator last, 
415                  match_results<BidiIterator, Allocator>& m, 
416                  const u32regex& e, 
417                  match_flag_type flags,
418                  boost::mpl::int_<2> const*)
419 {
420    typedef u16_to_u32_iterator<BidiIterator, UChar32> conv_type;
421    typedef match_results<conv_type>                   match_type;
422    typedef typename match_type::allocator_type        alloc_type;
423    match_type what;
424    bool result = ::boost::regex_match(conv_type(first), conv_type(last), what, e, flags);
425    // copy results across to m:
426    if(result) copy_results(m, what);
427    return result;
428 }
429 template <class BidiIterator, class Allocator>
430 bool do_regex_match(BidiIterator first, BidiIterator last, 
431                  match_results<BidiIterator, Allocator>& m, 
432                  const u32regex& e, 
433                  match_flag_type flags,
434                  boost::mpl::int_<1> const*)
435 {
436    typedef u8_to_u32_iterator<BidiIterator, UChar32>  conv_type;
437    typedef match_results<conv_type>                   match_type;
438    typedef typename match_type::allocator_type        alloc_type;
439    match_type what;
440    bool result = ::boost::regex_match(conv_type(first), conv_type(last), what, e, flags);
441    // copy results across to m:
442    if(result) copy_results(m, what);
443    return result;
444 }
445 } // namespace re_detail
446
447 template <class BidiIterator, class Allocator>
448 inline bool u32regex_match(BidiIterator first, BidiIterator last, 
449                  match_results<BidiIterator, Allocator>& m, 
450                  const u32regex& e, 
451                  match_flag_type flags = match_default)
452 {
453    return re_detail::do_regex_match(first, last, m, e, flags, static_cast<mpl::int_<sizeof(*first)> const*>(0));
454 }
455 inline bool u32regex_match(const UChar* p, 
456                  match_results<const UChar*>& m, 
457                  const u32regex& e, 
458                  match_flag_type flags = match_default)
459 {
460    return re_detail::do_regex_match(p, p+u_strlen(p), m, e, flags, static_cast<mpl::int_<2> const*>(0));
461 }
462 #if !defined(U_WCHAR_IS_UTF16) && (U_SIZEOF_WCHAR_T != 2) && !defined(BOOST_NO_WREGEX)
463 inline bool u32regex_match(const wchar_t* p, 
464                  match_results<const wchar_t*>& m, 
465                  const u32regex& e, 
466                  match_flag_type flags = match_default)
467 {
468    return re_detail::do_regex_match(p, p+std::wcslen(p), m, e, flags, static_cast<mpl::int_<sizeof(wchar_t)> const*>(0));
469 }
470 #endif
471 inline bool u32regex_match(const char* p, 
472                  match_results<const char*>& m, 
473                  const u32regex& e, 
474                  match_flag_type flags = match_default)
475 {
476    return re_detail::do_regex_match(p, p+std::strlen(p), m, e, flags, static_cast<mpl::int_<1> const*>(0));
477 }
478 inline bool u32regex_match(const unsigned char* p, 
479                  match_results<const unsigned char*>& m, 
480                  const u32regex& e, 
481                  match_flag_type flags = match_default)
482 {
483    return re_detail::do_regex_match(p, p+std::strlen((const char*)p), m, e, flags, static_cast<mpl::int_<1> const*>(0));
484 }
485 inline bool u32regex_match(const std::string& s, 
486                         match_results<std::string::const_iterator>& m, 
487                         const u32regex& e, 
488                         match_flag_type flags = match_default)
489 {
490    return re_detail::do_regex_match(s.begin(), s.end(), m, e, flags, static_cast<mpl::int_<1> const*>(0));
491 }
492 #ifndef BOOST_NO_STD_WSTRING
493 inline bool u32regex_match(const std::wstring& s, 
494                         match_results<std::wstring::const_iterator>& m, 
495                         const u32regex& e, 
496                         match_flag_type flags = match_default)
497 {
498    return re_detail::do_regex_match(s.begin(), s.end(), m, e, flags, static_cast<mpl::int_<sizeof(wchar_t)> const*>(0));
499 }
500 #endif
501 inline bool u32regex_match(const U_NAMESPACE_QUALIFIER UnicodeString& s, 
502                         match_results<const UChar*>& m, 
503                         const u32regex& e, 
504                         match_flag_type flags = match_default)
505 {
506    return re_detail::do_regex_match(s.getBuffer(), s.getBuffer() + s.length(), m, e, flags, static_cast<mpl::int_<sizeof(wchar_t)> const*>(0));
507 }
508 //
509 // regex_match overloads that do not return what matched:
510 //
511 template <class BidiIterator>
512 inline bool u32regex_match(BidiIterator first, BidiIterator last, 
513                  const u32regex& e, 
514                  match_flag_type flags = match_default)
515 {
516    match_results<BidiIterator> m;
517    return re_detail::do_regex_match(first, last, m, e, flags, static_cast<mpl::int_<sizeof(*first)> const*>(0));
518 }
519 inline bool u32regex_match(const UChar* p, 
520                  const u32regex& e, 
521                  match_flag_type flags = match_default)
522 {
523    match_results<const UChar*> m;
524    return re_detail::do_regex_match(p, p+u_strlen(p), m, e, flags, static_cast<mpl::int_<2> const*>(0));
525 }
526 #if !defined(U_WCHAR_IS_UTF16) && (U_SIZEOF_WCHAR_T != 2) && !defined(BOOST_NO_WREGEX)
527 inline bool u32regex_match(const wchar_t* p, 
528                  const u32regex& e, 
529                  match_flag_type flags = match_default)
530 {
531    match_results<const wchar_t*> m;
532    return re_detail::do_regex_match(p, p+std::wcslen(p), m, e, flags, static_cast<mpl::int_<sizeof(wchar_t)> const*>(0));
533 }
534 #endif
535 inline bool u32regex_match(const char* p, 
536                  const u32regex& e, 
537                  match_flag_type flags = match_default)
538 {
539    match_results<const char*> m;
540    return re_detail::do_regex_match(p, p+std::strlen(p), m, e, flags, static_cast<mpl::int_<1> const*>(0));
541 }
542 inline bool u32regex_match(const unsigned char* p, 
543                  const u32regex& e, 
544                  match_flag_type flags = match_default)
545 {
546    match_results<const unsigned char*> m;
547    return re_detail::do_regex_match(p, p+std::strlen((const char*)p), m, e, flags, static_cast<mpl::int_<1> const*>(0));
548 }
549 inline bool u32regex_match(const std::string& s, 
550                         const u32regex& e, 
551                         match_flag_type flags = match_default)
552 {
553    match_results<std::string::const_iterator> m;
554    return re_detail::do_regex_match(s.begin(), s.end(), m, e, flags, static_cast<mpl::int_<1> const*>(0));
555 }
556 #ifndef BOOST_NO_STD_WSTRING
557 inline bool u32regex_match(const std::wstring& s, 
558                         const u32regex& e, 
559                         match_flag_type flags = match_default)
560 {
561    match_results<std::wstring::const_iterator> m;
562    return re_detail::do_regex_match(s.begin(), s.end(), m, e, flags, static_cast<mpl::int_<sizeof(wchar_t)> const*>(0));
563 }
564 #endif
565 inline bool u32regex_match(const U_NAMESPACE_QUALIFIER UnicodeString& s, 
566                         const u32regex& e, 
567                         match_flag_type flags = match_default)
568 {
569    match_results<const UChar*> m;
570    return re_detail::do_regex_match(s.getBuffer(), s.getBuffer() + s.length(), m, e, flags, static_cast<mpl::int_<sizeof(wchar_t)> const*>(0));
571 }
572
573 //
574 // regex_search overloads that widen the character type as appropriate:
575 //
576 namespace re_detail{
577 template <class BidiIterator, class Allocator>
578 inline bool do_regex_search(BidiIterator first, BidiIterator last, 
579                  match_results<BidiIterator, Allocator>& m, 
580                  const u32regex& e, 
581                  match_flag_type flags,
582                  BidiIterator base,
583                  boost::mpl::int_<4> const*)
584 {
585    return ::boost::regex_search(first, last, m, e, flags, base);
586 }
587 template <class BidiIterator, class Allocator>
588 bool do_regex_search(BidiIterator first, BidiIterator last, 
589                  match_results<BidiIterator, Allocator>& m, 
590                  const u32regex& e, 
591                  match_flag_type flags,
592                  BidiIterator base,
593                  boost::mpl::int_<2> const*)
594 {
595    typedef u16_to_u32_iterator<BidiIterator, UChar32> conv_type;
596    typedef match_results<conv_type>                   match_type;
597    typedef typename match_type::allocator_type        alloc_type;
598    match_type what;
599    bool result = ::boost::regex_search(conv_type(first), conv_type(last), what, e, flags, conv_type(base));
600    // copy results across to m:
601    if(result) copy_results(m, what);
602    return result;
603 }
604 template <class BidiIterator, class Allocator>
605 bool do_regex_search(BidiIterator first, BidiIterator last, 
606                  match_results<BidiIterator, Allocator>& m, 
607                  const u32regex& e, 
608                  match_flag_type flags,
609                  BidiIterator base,
610                  boost::mpl::int_<1> const*)
611 {
612    typedef u8_to_u32_iterator<BidiIterator, UChar32>  conv_type;
613    typedef match_results<conv_type>                   match_type;
614    typedef typename match_type::allocator_type        alloc_type;
615    match_type what;
616    bool result = ::boost::regex_search(conv_type(first), conv_type(last), what, e, flags, conv_type(base));
617    // copy results across to m:
618    if(result) copy_results(m, what);
619    return result;
620 }
621 }
622
623 template <class BidiIterator, class Allocator>
624 inline bool u32regex_search(BidiIterator first, BidiIterator last, 
625                  match_results<BidiIterator, Allocator>& m, 
626                  const u32regex& e, 
627                  match_flag_type flags = match_default)
628 {
629    return re_detail::do_regex_search(first, last, m, e, flags, first, static_cast<mpl::int_<sizeof(*first)> const*>(0));
630 }
631 template <class BidiIterator, class Allocator>
632 inline bool u32regex_search(BidiIterator first, BidiIterator last, 
633                  match_results<BidiIterator, Allocator>& m, 
634                  const u32regex& e, 
635                  match_flag_type flags,
636                  BidiIterator base)
637 {
638    return re_detail::do_regex_search(first, last, m, e, flags, base, static_cast<mpl::int_<sizeof(*first)> const*>(0));
639 }
640 inline bool u32regex_search(const UChar* p, 
641                  match_results<const UChar*>& m, 
642                  const u32regex& e, 
643                  match_flag_type flags = match_default)
644 {
645    return re_detail::do_regex_search(p, p+u_strlen(p), m, e, flags, p, static_cast<mpl::int_<2> const*>(0));
646 }
647 #if !defined(U_WCHAR_IS_UTF16) && (U_SIZEOF_WCHAR_T != 2) && !defined(BOOST_NO_WREGEX)
648 inline bool u32regex_search(const wchar_t* p, 
649                  match_results<const wchar_t*>& m, 
650                  const u32regex& e, 
651                  match_flag_type flags = match_default)
652 {
653    return re_detail::do_regex_search(p, p+std::wcslen(p), m, e, flags, p, static_cast<mpl::int_<sizeof(wchar_t)> const*>(0));
654 }
655 #endif
656 inline bool u32regex_search(const char* p, 
657                  match_results<const char*>& m, 
658                  const u32regex& e, 
659                  match_flag_type flags = match_default)
660 {
661    return re_detail::do_regex_search(p, p+std::strlen(p), m, e, flags, p, static_cast<mpl::int_<1> const*>(0));
662 }
663 inline bool u32regex_search(const unsigned char* p, 
664                  match_results<const unsigned char*>& m, 
665                  const u32regex& e, 
666                  match_flag_type flags = match_default)
667 {
668    return re_detail::do_regex_search(p, p+std::strlen((const char*)p), m, e, flags, p, static_cast<mpl::int_<1> const*>(0));
669 }
670 inline bool u32regex_search(const std::string& s, 
671                         match_results<std::string::const_iterator>& m, 
672                         const u32regex& e, 
673                         match_flag_type flags = match_default)
674 {
675    return re_detail::do_regex_search(s.begin(), s.end(), m, e, flags, s.begin(), static_cast<mpl::int_<1> const*>(0));
676 }
677 #ifndef BOOST_NO_STD_WSTRING
678 inline bool u32regex_search(const std::wstring& s, 
679                         match_results<std::wstring::const_iterator>& m, 
680                         const u32regex& e, 
681                         match_flag_type flags = match_default)
682 {
683    return re_detail::do_regex_search(s.begin(), s.end(), m, e, flags, s.begin(), static_cast<mpl::int_<sizeof(wchar_t)> const*>(0));
684 }
685 #endif
686 inline bool u32regex_search(const U_NAMESPACE_QUALIFIER UnicodeString& s, 
687                         match_results<const UChar*>& m, 
688                         const u32regex& e, 
689                         match_flag_type flags = match_default)
690 {
691    return re_detail::do_regex_search(s.getBuffer(), s.getBuffer() + s.length(), m, e, flags, s.getBuffer(), static_cast<mpl::int_<sizeof(wchar_t)> const*>(0));
692 }
693 template <class BidiIterator>
694 inline bool u32regex_search(BidiIterator first, BidiIterator last, 
695                  const u32regex& e, 
696                  match_flag_type flags = match_default)
697 {
698    match_results<BidiIterator> m;
699    return re_detail::do_regex_search(first, last, m, e, flags, first, static_cast<mpl::int_<sizeof(*first)> const*>(0));
700 }
701 inline bool u32regex_search(const UChar* p, 
702                  const u32regex& e, 
703                  match_flag_type flags = match_default)
704 {
705    match_results<const UChar*> m;
706    return re_detail::do_regex_search(p, p+u_strlen(p), m, e, flags, p, static_cast<mpl::int_<2> const*>(0));
707 }
708 #if !defined(U_WCHAR_IS_UTF16) && (U_SIZEOF_WCHAR_T != 2) && !defined(BOOST_NO_WREGEX)
709 inline bool u32regex_search(const wchar_t* p, 
710                  const u32regex& e, 
711                  match_flag_type flags = match_default)
712 {
713    match_results<const wchar_t*> m;
714    return re_detail::do_regex_search(p, p+std::wcslen(p), m, e, flags, p, static_cast<mpl::int_<sizeof(wchar_t)> const*>(0));
715 }
716 #endif
717 inline bool u32regex_search(const char* p, 
718                  const u32regex& e, 
719                  match_flag_type flags = match_default)
720 {
721    match_results<const char*> m;
722    return re_detail::do_regex_search(p, p+std::strlen(p), m, e, flags, p, static_cast<mpl::int_<1> const*>(0));
723 }
724 inline bool u32regex_search(const unsigned char* p, 
725                  const u32regex& e, 
726                  match_flag_type flags = match_default)
727 {
728    match_results<const unsigned char*> m;
729    return re_detail::do_regex_search(p, p+std::strlen((const char*)p), m, e, flags, p, static_cast<mpl::int_<1> const*>(0));
730 }
731 inline bool u32regex_search(const std::string& s, 
732                         const u32regex& e, 
733                         match_flag_type flags = match_default)
734 {
735    match_results<std::string::const_iterator> m;
736    return re_detail::do_regex_search(s.begin(), s.end(), m, e, flags, s.begin(), static_cast<mpl::int_<1> const*>(0));
737 }
738 #ifndef BOOST_NO_STD_WSTRING
739 inline bool u32regex_search(const std::wstring& s, 
740                         const u32regex& e, 
741                         match_flag_type flags = match_default)
742 {
743    match_results<std::wstring::const_iterator> m;
744    return re_detail::do_regex_search(s.begin(), s.end(), m, e, flags, s.begin(), static_cast<mpl::int_<sizeof(wchar_t)> const*>(0));
745 }
746 #endif
747 inline bool u32regex_search(const U_NAMESPACE_QUALIFIER UnicodeString& s, 
748                         const u32regex& e, 
749                         match_flag_type flags = match_default)
750 {
751    match_results<const UChar*> m;
752    return re_detail::do_regex_search(s.getBuffer(), s.getBuffer() + s.length(), m, e, flags, s.getBuffer(), static_cast<mpl::int_<sizeof(wchar_t)> const*>(0));
753 }
754
755 //
756 // overloads for regex_replace with utf-8 and utf-16 data types:
757 //
758 namespace re_detail{
759 template <class I>
760 inline std::pair< boost::u8_to_u32_iterator<I>, boost::u8_to_u32_iterator<I> >
761    make_utf32_seq(I i, I j, mpl::int_<1> const*)
762 {
763    return std::pair< boost::u8_to_u32_iterator<I>, boost::u8_to_u32_iterator<I> >(boost::u8_to_u32_iterator<I>(i), boost::u8_to_u32_iterator<I>(j));
764 }
765 template <class I>
766 inline std::pair< boost::u16_to_u32_iterator<I>, boost::u16_to_u32_iterator<I> >
767    make_utf32_seq(I i, I j, mpl::int_<2> const*)
768 {
769    return std::pair< boost::u16_to_u32_iterator<I>, boost::u16_to_u32_iterator<I> >(boost::u16_to_u32_iterator<I>(i), boost::u16_to_u32_iterator<I>(j));
770 }
771 template <class I>
772 inline std::pair< I, I >
773    make_utf32_seq(I i, I j, mpl::int_<4> const*)
774 {
775    return std::pair< I, I >(i, j);
776 }
777 template <class charT>
778 inline std::pair< boost::u8_to_u32_iterator<const charT*>, boost::u8_to_u32_iterator<const charT*> >
779    make_utf32_seq(const charT* p, mpl::int_<1> const*)
780 {
781    return std::pair< boost::u8_to_u32_iterator<const charT*>, boost::u8_to_u32_iterator<const charT*> >(boost::u8_to_u32_iterator<const charT*>(p), boost::u8_to_u32_iterator<const charT*>(p+std::strlen((const char*)p)));
782 }
783 template <class charT>
784 inline std::pair< boost::u16_to_u32_iterator<const charT*>, boost::u16_to_u32_iterator<const charT*> >
785    make_utf32_seq(const charT* p, mpl::int_<2> const*)
786 {
787    return std::pair< boost::u16_to_u32_iterator<const charT*>, boost::u16_to_u32_iterator<const charT*> >(boost::u16_to_u32_iterator<const charT*>(p), boost::u16_to_u32_iterator<const charT*>(p+u_strlen((const UChar*)p)));
788 }
789 template <class charT>
790 inline std::pair< const charT*, const charT* >
791    make_utf32_seq(const charT* p, mpl::int_<4> const*)
792 {
793    return std::pair< const charT*, const charT* >(p, p+icu_regex_traits::length((UChar32 const*)p));
794 }
795 template <class OutputIterator>
796 inline OutputIterator make_utf32_out(OutputIterator o, mpl::int_<4> const*)
797 {
798    return o;
799 }
800 template <class OutputIterator>
801 inline utf16_output_iterator<OutputIterator> make_utf32_out(OutputIterator o, mpl::int_<2> const*)
802 {
803    return o;
804 }
805 template <class OutputIterator>
806 inline utf8_output_iterator<OutputIterator> make_utf32_out(OutputIterator o, mpl::int_<1> const*)
807 {
808    return o;
809 }
810
811 template <class OutputIterator, class I1, class I2>
812 OutputIterator do_regex_replace(OutputIterator out,
813                                  std::pair<I1, I1> const& in,
814                                  const u32regex& e, 
815                                  const std::pair<I2, I2>& fmt, 
816                                  match_flag_type flags
817                                  )
818 {
819    // unfortunately we have to copy the format string in order to pass in onward:
820    std::vector<UChar32> f;
821 #ifndef BOOST_NO_TEMPLATED_ITERATOR_CONSTRUCTORS
822    f.assign(fmt.first, fmt.second);
823 #else
824    f.clear();
825    I2 pos = fmt.first;
826    while(pos != fmt.second)
827       f.push_back(*pos++);
828 #endif
829    
830    regex_iterator<I1, UChar32, icu_regex_traits> i(in.first, in.second, e, flags);
831    regex_iterator<I1, UChar32, icu_regex_traits> j;
832    if(i == j)
833    {
834       if(!(flags & regex_constants::format_no_copy))
835          out = re_detail::copy(in.first, in.second, out);
836    }
837    else
838    {
839       I1 last_m = in.first;
840       while(i != j)
841       {
842          if(!(flags & regex_constants::format_no_copy))
843             out = re_detail::copy(i->prefix().first, i->prefix().second, out); 
844          if(f.size())
845             out = ::boost::re_detail::regex_format_imp(out, *i, &*f.begin(), &*f.begin() + f.size(), flags, e.get_traits());
846          else
847             out = ::boost::re_detail::regex_format_imp(out, *i, static_cast<UChar32 const*>(0), static_cast<UChar32 const*>(0), flags, e.get_traits());
848          last_m = (*i)[0].second;
849          if(flags & regex_constants::format_first_only)
850             break;
851          ++i;
852       }
853       if(!(flags & regex_constants::format_no_copy))
854          out = re_detail::copy(last_m, in.second, out);
855    }
856    return out;
857 }
858 template <class BaseIterator>
859 inline const BaseIterator& extract_output_base(const BaseIterator& b)
860 {
861    return b;
862 }
863 template <class BaseIterator>
864 inline BaseIterator extract_output_base(const utf8_output_iterator<BaseIterator>& b)
865 {
866    return b.base();
867 }
868 template <class BaseIterator>
869 inline BaseIterator extract_output_base(const utf16_output_iterator<BaseIterator>& b)
870 {
871    return b.base();
872 }
873 }  // re_detail
874
875 template <class OutputIterator, class BidirectionalIterator, class charT>
876 inline OutputIterator u32regex_replace(OutputIterator out,
877                          BidirectionalIterator first,
878                          BidirectionalIterator last,
879                          const u32regex& e, 
880                          const charT* fmt, 
881                          match_flag_type flags = match_default)
882 {
883    return re_detail::extract_output_base
884 #if BOOST_WORKAROUND(BOOST_MSVC, <= 1300)
885    <OutputIterator>
886 #endif
887     (
888       re_detail::do_regex_replace(
889          re_detail::make_utf32_out(out, static_cast<mpl::int_<sizeof(*first)> const*>(0)),
890          re_detail::make_utf32_seq(first, last, static_cast<mpl::int_<sizeof(*first)> const*>(0)),
891          e,
892          re_detail::make_utf32_seq(fmt, static_cast<mpl::int_<sizeof(*fmt)> const*>(0)),
893          flags)
894       );
895 }
896
897 template <class OutputIterator, class Iterator, class charT>
898 inline OutputIterator u32regex_replace(OutputIterator out,
899                          Iterator first,
900                          Iterator last,
901                          const u32regex& e, 
902                          const std::basic_string<charT>& fmt,
903                          match_flag_type flags = match_default)
904 {
905    return re_detail::extract_output_base
906 #if BOOST_WORKAROUND(BOOST_MSVC, <= 1300)
907    <OutputIterator>
908 #endif
909     (
910       re_detail::do_regex_replace(
911          re_detail::make_utf32_out(out, static_cast<mpl::int_<sizeof(*first)> const*>(0)),
912          re_detail::make_utf32_seq(first, last, static_cast<mpl::int_<sizeof(*first)> const*>(0)),
913          e,
914          re_detail::make_utf32_seq(fmt.begin(), fmt.end(), static_cast<mpl::int_<sizeof(charT)> const*>(0)),
915          flags)
916       );
917 }
918
919 template <class OutputIterator, class Iterator>
920 inline OutputIterator u32regex_replace(OutputIterator out,
921                          Iterator first,
922                          Iterator last,
923                          const u32regex& e, 
924                          const U_NAMESPACE_QUALIFIER UnicodeString& fmt,
925                          match_flag_type flags = match_default)
926 {
927    return re_detail::extract_output_base
928 #if BOOST_WORKAROUND(BOOST_MSVC, <= 1300)
929    <OutputIterator>
930 #endif
931    (
932       re_detail::do_regex_replace(
933          re_detail::make_utf32_out(out, static_cast<mpl::int_<sizeof(*first)> const*>(0)),
934          re_detail::make_utf32_seq(first, last, static_cast<mpl::int_<sizeof(*first)> const*>(0)),
935          e,
936          re_detail::make_utf32_seq(fmt.getBuffer(), fmt.getBuffer() + fmt.length(), static_cast<mpl::int_<2> const*>(0)),
937          flags)
938       );
939 }
940
941 template <class charT>
942 std::basic_string<charT> u32regex_replace(const std::basic_string<charT>& s,
943                          const u32regex& e, 
944                          const charT* fmt,
945                          match_flag_type flags = match_default)
946 {
947    std::basic_string<charT> result;
948    re_detail::string_out_iterator<std::basic_string<charT> > i(result);
949    u32regex_replace(i, s.begin(), s.end(), e, fmt, flags);
950    return result;
951 }
952
953 template <class charT>
954 std::basic_string<charT> u32regex_replace(const std::basic_string<charT>& s,
955                          const u32regex& e, 
956                          const std::basic_string<charT>& fmt,
957                          match_flag_type flags = match_default)
958 {
959    std::basic_string<charT> result;
960    re_detail::string_out_iterator<std::basic_string<charT> > i(result);
961    u32regex_replace(i, s.begin(), s.end(), e, fmt.c_str(), flags);
962    return result;
963 }
964
965 namespace re_detail{
966
967 class unicode_string_out_iterator
968 {
969    U_NAMESPACE_QUALIFIER UnicodeString* out;
970 public:
971    unicode_string_out_iterator(U_NAMESPACE_QUALIFIER UnicodeString& s) : out(&s) {}
972    unicode_string_out_iterator& operator++() { return *this; }
973    unicode_string_out_iterator& operator++(int) { return *this; }
974    unicode_string_out_iterator& operator*() { return *this; }
975    unicode_string_out_iterator& operator=(UChar v) 
976    { 
977       *out += v; 
978       return *this; 
979    }
980    typedef std::ptrdiff_t difference_type;
981    typedef UChar value_type;
982    typedef value_type* pointer;
983    typedef value_type& reference;
984    typedef std::output_iterator_tag iterator_category;
985 };
986
987 }
988
989 inline U_NAMESPACE_QUALIFIER UnicodeString u32regex_replace(const U_NAMESPACE_QUALIFIER UnicodeString& s,
990                          const u32regex& e, 
991                          const UChar* fmt,
992                          match_flag_type flags = match_default)
993 {
994    U_NAMESPACE_QUALIFIER UnicodeString result;
995    re_detail::unicode_string_out_iterator i(result);
996    u32regex_replace(i, s.getBuffer(), s.getBuffer()+s.length(), e, fmt, flags);
997    return result;
998 }
999
1000 inline U_NAMESPACE_QUALIFIER UnicodeString u32regex_replace(const U_NAMESPACE_QUALIFIER UnicodeString& s,
1001                          const u32regex& e, 
1002                          const U_NAMESPACE_QUALIFIER UnicodeString& fmt,
1003                          match_flag_type flags = match_default)
1004 {
1005    U_NAMESPACE_QUALIFIER UnicodeString result;
1006    re_detail::unicode_string_out_iterator i(result);
1007    re_detail::do_regex_replace(
1008          re_detail::make_utf32_out(i, static_cast<mpl::int_<2> const*>(0)),
1009          re_detail::make_utf32_seq(s.getBuffer(), s.getBuffer()+s.length(), static_cast<mpl::int_<2> const*>(0)),
1010          e,
1011          re_detail::make_utf32_seq(fmt.getBuffer(), fmt.getBuffer() + fmt.length(), static_cast<mpl::int_<2> const*>(0)),
1012          flags);
1013    return result;
1014 }
1015
1016 } // namespace boost.
1017
1018 #include <boost/regex/v4/u32regex_iterator.hpp>
1019 #include <boost/regex/v4/u32regex_token_iterator.hpp>
1020
1021 #endif