sl@0: /* sl@0: * sl@0: * Copyright (c) 2004 sl@0: * John Maddock sl@0: * sl@0: * Use, modification and distribution are subject to the sl@0: * Boost Software License, Version 1.0. (See accompanying file sl@0: * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) sl@0: * sl@0: */ sl@0: sl@0: /* sl@0: * LOCATION: see http://www.boost.org for most recent version. sl@0: * FILE icu.hpp sl@0: * VERSION see sl@0: * DESCRIPTION: Unicode regular expressions on top of the ICU Library. sl@0: */ sl@0: sl@0: #ifndef BOOST_REGEX_ICU_HPP sl@0: #define BOOST_REGEX_ICU_HPP sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: sl@0: namespace boost{ sl@0: sl@0: namespace re_detail{ sl@0: sl@0: // sl@0: // Implementation details: sl@0: // sl@0: class BOOST_REGEX_DECL icu_regex_traits_implementation sl@0: { sl@0: typedef UChar32 char_type; sl@0: typedef std::size_t size_type; sl@0: typedef std::vector string_type; sl@0: typedef U_NAMESPACE_QUALIFIER Locale locale_type; sl@0: typedef boost::uint_least32_t char_class_type; sl@0: public: sl@0: icu_regex_traits_implementation(const U_NAMESPACE_QUALIFIER Locale& l) sl@0: : m_locale(l) sl@0: { sl@0: UErrorCode success = U_ZERO_ERROR; sl@0: m_collator.reset(U_NAMESPACE_QUALIFIER Collator::createInstance(l, success)); sl@0: if(U_SUCCESS(success) == 0) sl@0: init_error(); sl@0: m_collator->setStrength(U_NAMESPACE_QUALIFIER Collator::IDENTICAL); sl@0: success = U_ZERO_ERROR; sl@0: m_primary_collator.reset(U_NAMESPACE_QUALIFIER Collator::createInstance(l, success)); sl@0: if(U_SUCCESS(success) == 0) sl@0: init_error(); sl@0: m_primary_collator->setStrength(U_NAMESPACE_QUALIFIER Collator::PRIMARY); sl@0: } sl@0: U_NAMESPACE_QUALIFIER Locale getloc()const sl@0: { sl@0: return m_locale; sl@0: } sl@0: string_type do_transform(const char_type* p1, const char_type* p2, const U_NAMESPACE_QUALIFIER Collator* pcoll) const; sl@0: string_type transform(const char_type* p1, const char_type* p2) const sl@0: { sl@0: return do_transform(p1, p2, m_collator.get()); sl@0: } sl@0: string_type transform_primary(const char_type* p1, const char_type* p2) const sl@0: { sl@0: return do_transform(p1, p2, m_primary_collator.get()); sl@0: } sl@0: private: sl@0: void init_error() sl@0: { sl@0: std::runtime_error e("Could not initialize ICU resources"); sl@0: boost::throw_exception(e); sl@0: } sl@0: U_NAMESPACE_QUALIFIER Locale m_locale; // The ICU locale that we're using sl@0: boost::scoped_ptr< U_NAMESPACE_QUALIFIER Collator> m_collator; // The full collation object sl@0: boost::scoped_ptr< U_NAMESPACE_QUALIFIER Collator> m_primary_collator; // The primary collation object sl@0: }; sl@0: sl@0: inline boost::shared_ptr get_icu_regex_traits_implementation(const U_NAMESPACE_QUALIFIER Locale& loc) sl@0: { sl@0: return boost::shared_ptr(new icu_regex_traits_implementation(loc)); sl@0: } sl@0: sl@0: } sl@0: sl@0: class BOOST_REGEX_DECL icu_regex_traits sl@0: { sl@0: public: sl@0: typedef UChar32 char_type; sl@0: typedef std::size_t size_type; sl@0: typedef std::vector string_type; sl@0: typedef U_NAMESPACE_QUALIFIER Locale locale_type; sl@0: #ifdef BOOST_NO_INT64_T sl@0: typedef std::bitset<64> char_class_type; sl@0: #else sl@0: typedef boost::uint64_t char_class_type; sl@0: #endif sl@0: sl@0: struct boost_extensions_tag{}; sl@0: sl@0: icu_regex_traits() sl@0: : m_pimpl(re_detail::get_icu_regex_traits_implementation(U_NAMESPACE_QUALIFIER Locale())) sl@0: { sl@0: } sl@0: static size_type length(const char_type* p); sl@0: sl@0: ::boost::regex_constants::syntax_type syntax_type(char_type c)const sl@0: { sl@0: return ((c < 0x7f) && (c > 0)) ? re_detail::get_default_syntax_type(static_cast(c)) : regex_constants::syntax_char; sl@0: } sl@0: ::boost::regex_constants::escape_syntax_type escape_syntax_type(char_type c) const sl@0: { sl@0: return ((c < 0x7f) && (c > 0)) ? re_detail::get_default_escape_syntax_type(static_cast(c)) : regex_constants::syntax_char; sl@0: } sl@0: char_type translate(char_type c) const sl@0: { sl@0: return c; sl@0: } sl@0: char_type translate_nocase(char_type c) const sl@0: { sl@0: return ::u_tolower(c); sl@0: } sl@0: char_type translate(char_type c, bool icase) const sl@0: { sl@0: return icase ? translate_nocase(c) : translate(c); sl@0: } sl@0: char_type tolower(char_type c) const sl@0: { sl@0: return ::u_tolower(c); sl@0: } sl@0: char_type toupper(char_type c) const sl@0: { sl@0: return ::u_toupper(c); sl@0: } sl@0: string_type transform(const char_type* p1, const char_type* p2) const sl@0: { sl@0: return m_pimpl->transform(p1, p2); sl@0: } sl@0: string_type transform_primary(const char_type* p1, const char_type* p2) const sl@0: { sl@0: return m_pimpl->transform_primary(p1, p2); sl@0: } sl@0: char_class_type lookup_classname(const char_type* p1, const char_type* p2) const; sl@0: string_type lookup_collatename(const char_type* p1, const char_type* p2) const; sl@0: bool isctype(char_type c, char_class_type f) const; sl@0: int toi(const char_type*& p1, const char_type* p2, int radix)const sl@0: { sl@0: return re_detail::global_toi(p1, p2, radix, *this); sl@0: } sl@0: int value(char_type c, int radix)const sl@0: { sl@0: return u_digit(c, static_cast< ::int8_t>(radix)); sl@0: } sl@0: locale_type imbue(locale_type l) sl@0: { sl@0: locale_type result(m_pimpl->getloc()); sl@0: m_pimpl = re_detail::get_icu_regex_traits_implementation(l); sl@0: return result; sl@0: } sl@0: locale_type getloc()const sl@0: { sl@0: return locale_type(); sl@0: } sl@0: std::string error_string(::boost::regex_constants::error_type n) const sl@0: { sl@0: return re_detail::get_default_error_string(n); sl@0: } sl@0: private: sl@0: icu_regex_traits(const icu_regex_traits&); sl@0: icu_regex_traits& operator=(const icu_regex_traits&); sl@0: sl@0: // sl@0: // define the bitmasks offsets we need for additional character properties: sl@0: // sl@0: enum{ sl@0: offset_blank = U_CHAR_CATEGORY_COUNT, sl@0: offset_space = U_CHAR_CATEGORY_COUNT+1, sl@0: offset_xdigit = U_CHAR_CATEGORY_COUNT+2, sl@0: offset_underscore = U_CHAR_CATEGORY_COUNT+3, sl@0: offset_unicode = U_CHAR_CATEGORY_COUNT+4, sl@0: offset_any = U_CHAR_CATEGORY_COUNT+5, sl@0: offset_ascii = U_CHAR_CATEGORY_COUNT+6 sl@0: }; sl@0: sl@0: // sl@0: // and now the masks: sl@0: // sl@0: static const char_class_type mask_blank; sl@0: static const char_class_type mask_space; sl@0: static const char_class_type mask_xdigit; sl@0: static const char_class_type mask_underscore; sl@0: static const char_class_type mask_unicode; sl@0: static const char_class_type mask_any; sl@0: static const char_class_type mask_ascii; sl@0: sl@0: static char_class_type lookup_icu_mask(const ::UChar32* p1, const ::UChar32* p2); sl@0: sl@0: boost::shared_ptr< ::boost::re_detail::icu_regex_traits_implementation> m_pimpl; sl@0: }; sl@0: sl@0: } // namespace boost sl@0: sl@0: // sl@0: // template instances: sl@0: // sl@0: #define BOOST_REGEX_CHAR_T UChar32 sl@0: #undef BOOST_REGEX_TRAITS_T sl@0: #define BOOST_REGEX_TRAITS_T , icu_regex_traits sl@0: #define BOOST_REGEX_ICU_INSTANCES sl@0: #ifdef BOOST_REGEX_ICU_INSTANTIATE sl@0: # define BOOST_REGEX_INSTANTIATE sl@0: #endif sl@0: #include sl@0: #undef BOOST_REGEX_CHAR_T sl@0: #undef BOOST_REGEX_TRAITS_T sl@0: #undef BOOST_REGEX_ICU_INSTANCES sl@0: #ifdef BOOST_REGEX_INSTANTIATE sl@0: # undef BOOST_REGEX_INSTANTIATE sl@0: #endif sl@0: sl@0: namespace boost{ sl@0: sl@0: // types: sl@0: typedef basic_regex< ::UChar32, icu_regex_traits> u32regex; sl@0: typedef match_results u32match; sl@0: typedef match_results u16match; sl@0: sl@0: // sl@0: // Construction of 32-bit regex types from UTF-8 and UTF-16 primitives: sl@0: // sl@0: namespace re_detail{ sl@0: sl@0: #if !defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(__IBMCPP__) sl@0: template sl@0: inline u32regex do_make_u32regex(InputIterator i, sl@0: InputIterator j, sl@0: boost::regex_constants::syntax_option_type opt, sl@0: const boost::mpl::int_<1>*) sl@0: { sl@0: typedef boost::u8_to_u32_iterator conv_type; sl@0: return u32regex(conv_type(i), conv_type(j), opt); sl@0: } sl@0: sl@0: template sl@0: inline u32regex do_make_u32regex(InputIterator i, sl@0: InputIterator j, sl@0: boost::regex_constants::syntax_option_type opt, sl@0: const boost::mpl::int_<2>*) sl@0: { sl@0: typedef boost::u16_to_u32_iterator conv_type; sl@0: return u32regex(conv_type(i), conv_type(j), opt); sl@0: } sl@0: sl@0: template sl@0: inline u32regex do_make_u32regex(InputIterator i, sl@0: InputIterator j, sl@0: boost::regex_constants::syntax_option_type opt, sl@0: const boost::mpl::int_<4>*) sl@0: { sl@0: return u32regex(i, j, opt); sl@0: } sl@0: #else sl@0: template sl@0: inline u32regex do_make_u32regex(InputIterator i, sl@0: InputIterator j, sl@0: boost::regex_constants::syntax_option_type opt, sl@0: const boost::mpl::int_<1>*) sl@0: { sl@0: typedef boost::u8_to_u32_iterator conv_type; sl@0: typedef std::vector vector_type; sl@0: vector_type v; sl@0: conv_type a(i), b(j); sl@0: while(a != b) sl@0: { sl@0: v.push_back(*a); sl@0: ++a; sl@0: } sl@0: if(v.size()) sl@0: return u32regex(&*v.begin(), v.size(), opt); sl@0: return u32regex(static_cast(0), static_cast(0), opt); sl@0: } sl@0: sl@0: template sl@0: inline u32regex do_make_u32regex(InputIterator i, sl@0: InputIterator j, sl@0: boost::regex_constants::syntax_option_type opt, sl@0: const boost::mpl::int_<2>*) sl@0: { sl@0: typedef boost::u16_to_u32_iterator conv_type; sl@0: typedef std::vector vector_type; sl@0: vector_type v; sl@0: conv_type a(i), b(j); sl@0: while(a != b) sl@0: { sl@0: v.push_back(*a); sl@0: ++a; sl@0: } sl@0: if(v.size()) sl@0: return u32regex(&*v.begin(), v.size(), opt); sl@0: return u32regex(static_cast(0), static_cast(0), opt); sl@0: } sl@0: sl@0: template sl@0: inline u32regex do_make_u32regex(InputIterator i, sl@0: InputIterator j, sl@0: boost::regex_constants::syntax_option_type opt, sl@0: const boost::mpl::int_<4>*) sl@0: { sl@0: typedef std::vector vector_type; sl@0: vector_type v; sl@0: while(i != j) sl@0: { sl@0: v.push_back((UCHAR32)(*i)); sl@0: ++a; sl@0: } sl@0: if(v.size()) sl@0: return u32regex(&*v.begin(), v.size(), opt); sl@0: return u32regex(static_cast(0), static_cast(0), opt); sl@0: } sl@0: #endif sl@0: } sl@0: sl@0: // sl@0: // Construction from an iterator pair: sl@0: // sl@0: template sl@0: inline u32regex make_u32regex(InputIterator i, sl@0: InputIterator j, sl@0: boost::regex_constants::syntax_option_type opt) sl@0: { sl@0: return re_detail::do_make_u32regex(i, j, opt, static_cast const*>(0)); sl@0: } sl@0: // sl@0: // construction from UTF-8 nul-terminated strings: sl@0: // sl@0: inline u32regex make_u32regex(const char* p, boost::regex_constants::syntax_option_type opt = boost::regex_constants::perl) sl@0: { sl@0: return re_detail::do_make_u32regex(p, p + std::strlen(p), opt, static_cast const*>(0)); sl@0: } sl@0: inline u32regex make_u32regex(const unsigned char* p, boost::regex_constants::syntax_option_type opt = boost::regex_constants::perl) sl@0: { sl@0: return re_detail::do_make_u32regex(p, p + std::strlen(reinterpret_cast(p)), opt, static_cast const*>(0)); sl@0: } sl@0: // sl@0: // construction from UTF-16 nul-terminated strings: sl@0: // sl@0: #ifndef BOOST_NO_WREGEX sl@0: inline u32regex make_u32regex(const wchar_t* p, boost::regex_constants::syntax_option_type opt = boost::regex_constants::perl) sl@0: { sl@0: return re_detail::do_make_u32regex(p, p + std::wcslen(p), opt, static_cast const*>(0)); sl@0: } sl@0: #endif sl@0: #ifndef U_WCHAR_IS_UTF16 sl@0: inline u32regex make_u32regex(const UChar* p, boost::regex_constants::syntax_option_type opt = boost::regex_constants::perl) sl@0: { sl@0: return re_detail::do_make_u32regex(p, p + u_strlen(p), opt, static_cast const*>(0)); sl@0: } sl@0: #endif sl@0: // sl@0: // construction from basic_string class-template: sl@0: // sl@0: template sl@0: inline u32regex make_u32regex(const std::basic_string& s, boost::regex_constants::syntax_option_type opt = boost::regex_constants::perl) sl@0: { sl@0: return re_detail::do_make_u32regex(s.begin(), s.end(), opt, static_cast const*>(0)); sl@0: } sl@0: // sl@0: // Construction from ICU string type: sl@0: // sl@0: inline u32regex make_u32regex(const UnicodeString& s, boost::regex_constants::syntax_option_type opt = boost::regex_constants::perl) sl@0: { sl@0: return re_detail::do_make_u32regex(s.getBuffer(), s.getBuffer() + s.length(), opt, static_cast const*>(0)); sl@0: } sl@0: sl@0: // sl@0: // regex_match overloads that widen the character type as appropriate: sl@0: // sl@0: namespace re_detail{ sl@0: template sl@0: void copy_results(MR1& out, MR2 const& in) sl@0: { sl@0: // copy results from an adapted MR2 match_results: sl@0: out.set_size(in.size(), in.prefix().first.base(), in.suffix().second.base()); sl@0: out.set_base(in.base().base()); sl@0: for(int i = 0; i < (int)in.size(); ++i) sl@0: { sl@0: if(in[i].matched) sl@0: { sl@0: out.set_first(in[i].first.base(), i); sl@0: out.set_second(in[i].second.base(), i); sl@0: } sl@0: } sl@0: } sl@0: sl@0: template sl@0: inline bool do_regex_match(BidiIterator first, BidiIterator last, sl@0: match_results& m, sl@0: const u32regex& e, sl@0: match_flag_type flags, sl@0: boost::mpl::int_<4> const*) sl@0: { sl@0: return ::boost::regex_match(first, last, m, e, flags); sl@0: } sl@0: template sl@0: bool do_regex_match(BidiIterator first, BidiIterator last, sl@0: match_results& m, sl@0: const u32regex& e, sl@0: match_flag_type flags, sl@0: boost::mpl::int_<2> const*) sl@0: { sl@0: typedef u16_to_u32_iterator conv_type; sl@0: typedef match_results match_type; sl@0: typedef typename match_type::allocator_type alloc_type; sl@0: match_type what; sl@0: bool result = ::boost::regex_match(conv_type(first), conv_type(last), what, e, flags); sl@0: // copy results across to m: sl@0: if(result) copy_results(m, what); sl@0: return result; sl@0: } sl@0: template sl@0: bool do_regex_match(BidiIterator first, BidiIterator last, sl@0: match_results& m, sl@0: const u32regex& e, sl@0: match_flag_type flags, sl@0: boost::mpl::int_<1> const*) sl@0: { sl@0: typedef u8_to_u32_iterator conv_type; sl@0: typedef match_results match_type; sl@0: typedef typename match_type::allocator_type alloc_type; sl@0: match_type what; sl@0: bool result = ::boost::regex_match(conv_type(first), conv_type(last), what, e, flags); sl@0: // copy results across to m: sl@0: if(result) copy_results(m, what); sl@0: return result; sl@0: } sl@0: } // namespace re_detail sl@0: sl@0: template sl@0: inline bool u32regex_match(BidiIterator first, BidiIterator last, sl@0: match_results& m, sl@0: const u32regex& e, sl@0: match_flag_type flags = match_default) sl@0: { sl@0: return re_detail::do_regex_match(first, last, m, e, flags, static_cast const*>(0)); sl@0: } sl@0: inline bool u32regex_match(const UChar* p, sl@0: match_results& m, sl@0: const u32regex& e, sl@0: match_flag_type flags = match_default) sl@0: { sl@0: return re_detail::do_regex_match(p, p+u_strlen(p), m, e, flags, static_cast const*>(0)); sl@0: } sl@0: #if !defined(U_WCHAR_IS_UTF16) && !defined(BOOST_NO_WREGEX) sl@0: inline bool u32regex_match(const wchar_t* p, sl@0: match_results& m, sl@0: const u32regex& e, sl@0: match_flag_type flags = match_default) sl@0: { sl@0: return re_detail::do_regex_match(p, p+std::wcslen(p), m, e, flags, static_cast const*>(0)); sl@0: } sl@0: #endif sl@0: inline bool u32regex_match(const char* p, sl@0: match_results& m, sl@0: const u32regex& e, sl@0: match_flag_type flags = match_default) sl@0: { sl@0: return re_detail::do_regex_match(p, p+std::strlen(p), m, e, flags, static_cast const*>(0)); sl@0: } sl@0: inline bool u32regex_match(const unsigned char* p, sl@0: match_results& m, sl@0: const u32regex& e, sl@0: match_flag_type flags = match_default) sl@0: { sl@0: return re_detail::do_regex_match(p, p+std::strlen((const char*)p), m, e, flags, static_cast const*>(0)); sl@0: } sl@0: inline bool u32regex_match(const std::string& s, sl@0: match_results& m, sl@0: const u32regex& e, sl@0: match_flag_type flags = match_default) sl@0: { sl@0: return re_detail::do_regex_match(s.begin(), s.end(), m, e, flags, static_cast const*>(0)); sl@0: } sl@0: #ifndef BOOST_NO_STD_WSTRING sl@0: inline bool u32regex_match(const std::wstring& s, sl@0: match_results& m, sl@0: const u32regex& e, sl@0: match_flag_type flags = match_default) sl@0: { sl@0: return re_detail::do_regex_match(s.begin(), s.end(), m, e, flags, static_cast const*>(0)); sl@0: } sl@0: #endif sl@0: inline bool u32regex_match(const UnicodeString& s, sl@0: match_results& m, sl@0: const u32regex& e, sl@0: match_flag_type flags = match_default) sl@0: { sl@0: return re_detail::do_regex_match(s.getBuffer(), s.getBuffer() + s.length(), m, e, flags, static_cast const*>(0)); sl@0: } sl@0: // sl@0: // regex_match overloads that do not return what matched: sl@0: // sl@0: template sl@0: inline bool u32regex_match(BidiIterator first, BidiIterator last, sl@0: const u32regex& e, sl@0: match_flag_type flags = match_default) sl@0: { sl@0: match_results m; sl@0: return re_detail::do_regex_match(first, last, m, e, flags, static_cast const*>(0)); sl@0: } sl@0: inline bool u32regex_match(const UChar* p, sl@0: const u32regex& e, sl@0: match_flag_type flags = match_default) sl@0: { sl@0: match_results m; sl@0: return re_detail::do_regex_match(p, p+u_strlen(p), m, e, flags, static_cast const*>(0)); sl@0: } sl@0: #if !defined(U_WCHAR_IS_UTF16) && !defined(BOOST_NO_WREGEX) sl@0: inline bool u32regex_match(const wchar_t* p, sl@0: const u32regex& e, sl@0: match_flag_type flags = match_default) sl@0: { sl@0: match_results m; sl@0: return re_detail::do_regex_match(p, p+std::wcslen(p), m, e, flags, static_cast const*>(0)); sl@0: } sl@0: #endif sl@0: inline bool u32regex_match(const char* p, sl@0: const u32regex& e, sl@0: match_flag_type flags = match_default) sl@0: { sl@0: match_results m; sl@0: return re_detail::do_regex_match(p, p+std::strlen(p), m, e, flags, static_cast const*>(0)); sl@0: } sl@0: inline bool u32regex_match(const unsigned char* p, sl@0: const u32regex& e, sl@0: match_flag_type flags = match_default) sl@0: { sl@0: match_results m; sl@0: return re_detail::do_regex_match(p, p+std::strlen((const char*)p), m, e, flags, static_cast const*>(0)); sl@0: } sl@0: inline bool u32regex_match(const std::string& s, sl@0: const u32regex& e, sl@0: match_flag_type flags = match_default) sl@0: { sl@0: match_results m; sl@0: return re_detail::do_regex_match(s.begin(), s.end(), m, e, flags, static_cast const*>(0)); sl@0: } sl@0: #ifndef BOOST_NO_STD_WSTRING sl@0: inline bool u32regex_match(const std::wstring& s, sl@0: const u32regex& e, sl@0: match_flag_type flags = match_default) sl@0: { sl@0: match_results m; sl@0: return re_detail::do_regex_match(s.begin(), s.end(), m, e, flags, static_cast const*>(0)); sl@0: } sl@0: #endif sl@0: inline bool u32regex_match(const UnicodeString& s, sl@0: const u32regex& e, sl@0: match_flag_type flags = match_default) sl@0: { sl@0: match_results m; sl@0: return re_detail::do_regex_match(s.getBuffer(), s.getBuffer() + s.length(), m, e, flags, static_cast const*>(0)); sl@0: } sl@0: sl@0: // sl@0: // regex_search overloads that widen the character type as appropriate: sl@0: // sl@0: namespace re_detail{ sl@0: template sl@0: inline bool do_regex_search(BidiIterator first, BidiIterator last, sl@0: match_results& m, sl@0: const u32regex& e, sl@0: match_flag_type flags, sl@0: BidiIterator base, sl@0: boost::mpl::int_<4> const*) sl@0: { sl@0: return ::boost::regex_search(first, last, m, e, flags, base); sl@0: } sl@0: template sl@0: bool do_regex_search(BidiIterator first, BidiIterator last, sl@0: match_results& m, sl@0: const u32regex& e, sl@0: match_flag_type flags, sl@0: BidiIterator base, sl@0: boost::mpl::int_<2> const*) sl@0: { sl@0: typedef u16_to_u32_iterator conv_type; sl@0: typedef match_results match_type; sl@0: typedef typename match_type::allocator_type alloc_type; sl@0: match_type what; sl@0: bool result = ::boost::regex_search(conv_type(first), conv_type(last), what, e, flags, conv_type(base)); sl@0: // copy results across to m: sl@0: if(result) copy_results(m, what); sl@0: return result; sl@0: } sl@0: template sl@0: bool do_regex_search(BidiIterator first, BidiIterator last, sl@0: match_results& m, sl@0: const u32regex& e, sl@0: match_flag_type flags, sl@0: BidiIterator base, sl@0: boost::mpl::int_<1> const*) sl@0: { sl@0: typedef u8_to_u32_iterator conv_type; sl@0: typedef match_results match_type; sl@0: typedef typename match_type::allocator_type alloc_type; sl@0: match_type what; sl@0: bool result = ::boost::regex_search(conv_type(first), conv_type(last), what, e, flags, conv_type(base)); sl@0: // copy results across to m: sl@0: if(result) copy_results(m, what); sl@0: return result; sl@0: } sl@0: } sl@0: sl@0: template sl@0: inline bool u32regex_search(BidiIterator first, BidiIterator last, sl@0: match_results& m, sl@0: const u32regex& e, sl@0: match_flag_type flags = match_default) sl@0: { sl@0: return re_detail::do_regex_search(first, last, m, e, flags, first, static_cast const*>(0)); sl@0: } sl@0: template sl@0: inline bool u32regex_search(BidiIterator first, BidiIterator last, sl@0: match_results& m, sl@0: const u32regex& e, sl@0: match_flag_type flags, sl@0: BidiIterator base) sl@0: { sl@0: return re_detail::do_regex_search(first, last, m, e, flags, base, static_cast const*>(0)); sl@0: } sl@0: inline bool u32regex_search(const UChar* p, sl@0: match_results& m, sl@0: const u32regex& e, sl@0: match_flag_type flags = match_default) sl@0: { sl@0: return re_detail::do_regex_search(p, p+u_strlen(p), m, e, flags, p, static_cast const*>(0)); sl@0: } sl@0: #if !defined(U_WCHAR_IS_UTF16) && !defined(BOOST_NO_WREGEX) sl@0: inline bool u32regex_search(const wchar_t* p, sl@0: match_results& m, sl@0: const u32regex& e, sl@0: match_flag_type flags = match_default) sl@0: { sl@0: return re_detail::do_regex_search(p, p+std::wcslen(p), m, e, flags, p, static_cast const*>(0)); sl@0: } sl@0: #endif sl@0: inline bool u32regex_search(const char* p, sl@0: match_results& m, sl@0: const u32regex& e, sl@0: match_flag_type flags = match_default) sl@0: { sl@0: return re_detail::do_regex_search(p, p+std::strlen(p), m, e, flags, p, static_cast const*>(0)); sl@0: } sl@0: inline bool u32regex_search(const unsigned char* p, sl@0: match_results& m, sl@0: const u32regex& e, sl@0: match_flag_type flags = match_default) sl@0: { sl@0: return re_detail::do_regex_search(p, p+std::strlen((const char*)p), m, e, flags, p, static_cast const*>(0)); sl@0: } sl@0: inline bool u32regex_search(const std::string& s, sl@0: match_results& m, sl@0: const u32regex& e, sl@0: match_flag_type flags = match_default) sl@0: { sl@0: return re_detail::do_regex_search(s.begin(), s.end(), m, e, flags, s.begin(), static_cast const*>(0)); sl@0: } sl@0: #ifndef BOOST_NO_STD_WSTRING sl@0: inline bool u32regex_search(const std::wstring& s, sl@0: match_results& m, sl@0: const u32regex& e, sl@0: match_flag_type flags = match_default) sl@0: { sl@0: return re_detail::do_regex_search(s.begin(), s.end(), m, e, flags, s.begin(), static_cast const*>(0)); sl@0: } sl@0: #endif sl@0: inline bool u32regex_search(const UnicodeString& s, sl@0: match_results& m, sl@0: const u32regex& e, sl@0: match_flag_type flags = match_default) sl@0: { sl@0: return re_detail::do_regex_search(s.getBuffer(), s.getBuffer() + s.length(), m, e, flags, s.getBuffer(), static_cast const*>(0)); sl@0: } sl@0: template sl@0: inline bool u32regex_search(BidiIterator first, BidiIterator last, sl@0: const u32regex& e, sl@0: match_flag_type flags = match_default) sl@0: { sl@0: match_results m; sl@0: return re_detail::do_regex_search(first, last, m, e, flags, first, static_cast const*>(0)); sl@0: } sl@0: inline bool u32regex_search(const UChar* p, sl@0: const u32regex& e, sl@0: match_flag_type flags = match_default) sl@0: { sl@0: match_results m; sl@0: return re_detail::do_regex_search(p, p+u_strlen(p), m, e, flags, p, static_cast const*>(0)); sl@0: } sl@0: #if !defined(U_WCHAR_IS_UTF16) && !defined(BOOST_NO_WREGEX) sl@0: inline bool u32regex_search(const wchar_t* p, sl@0: const u32regex& e, sl@0: match_flag_type flags = match_default) sl@0: { sl@0: match_results m; sl@0: return re_detail::do_regex_search(p, p+std::wcslen(p), m, e, flags, p, static_cast const*>(0)); sl@0: } sl@0: #endif sl@0: inline bool u32regex_search(const char* p, sl@0: const u32regex& e, sl@0: match_flag_type flags = match_default) sl@0: { sl@0: match_results m; sl@0: return re_detail::do_regex_search(p, p+std::strlen(p), m, e, flags, p, static_cast const*>(0)); sl@0: } sl@0: inline bool u32regex_search(const unsigned char* p, sl@0: const u32regex& e, sl@0: match_flag_type flags = match_default) sl@0: { sl@0: match_results m; sl@0: return re_detail::do_regex_search(p, p+std::strlen((const char*)p), m, e, flags, p, static_cast const*>(0)); sl@0: } sl@0: inline bool u32regex_search(const std::string& s, sl@0: const u32regex& e, sl@0: match_flag_type flags = match_default) sl@0: { sl@0: match_results m; sl@0: return re_detail::do_regex_search(s.begin(), s.end(), m, e, flags, s.begin(), static_cast const*>(0)); sl@0: } sl@0: #ifndef BOOST_NO_STD_WSTRING sl@0: inline bool u32regex_search(const std::wstring& s, sl@0: const u32regex& e, sl@0: match_flag_type flags = match_default) sl@0: { sl@0: match_results m; sl@0: return re_detail::do_regex_search(s.begin(), s.end(), m, e, flags, s.begin(), static_cast const*>(0)); sl@0: } sl@0: #endif sl@0: inline bool u32regex_search(const UnicodeString& s, sl@0: const u32regex& e, sl@0: match_flag_type flags = match_default) sl@0: { sl@0: match_results m; sl@0: return re_detail::do_regex_search(s.getBuffer(), s.getBuffer() + s.length(), m, e, flags, s.getBuffer(), static_cast const*>(0)); sl@0: } sl@0: sl@0: // sl@0: // overloads for regex_replace with utf-8 and utf-16 data types: sl@0: // sl@0: namespace re_detail{ sl@0: template sl@0: inline std::pair< boost::u8_to_u32_iterator, boost::u8_to_u32_iterator > sl@0: make_utf32_seq(I i, I j, mpl::int_<1> const*) sl@0: { sl@0: return std::pair< boost::u8_to_u32_iterator, boost::u8_to_u32_iterator >(boost::u8_to_u32_iterator(i), boost::u8_to_u32_iterator(j)); sl@0: } sl@0: template sl@0: inline std::pair< boost::u16_to_u32_iterator, boost::u16_to_u32_iterator > sl@0: make_utf32_seq(I i, I j, mpl::int_<2> const*) sl@0: { sl@0: return std::pair< boost::u16_to_u32_iterator, boost::u16_to_u32_iterator >(boost::u16_to_u32_iterator(i), boost::u16_to_u32_iterator(j)); sl@0: } sl@0: template sl@0: inline std::pair< I, I > sl@0: make_utf32_seq(I i, I j, mpl::int_<4> const*) sl@0: { sl@0: return std::pair< I, I >(i, j); sl@0: } sl@0: template sl@0: inline std::pair< boost::u8_to_u32_iterator, boost::u8_to_u32_iterator > sl@0: make_utf32_seq(const charT* p, mpl::int_<1> const*) sl@0: { sl@0: return std::pair< boost::u8_to_u32_iterator, boost::u8_to_u32_iterator >(boost::u8_to_u32_iterator(p), boost::u8_to_u32_iterator(p+std::strlen((const char*)p))); sl@0: } sl@0: template sl@0: inline std::pair< boost::u16_to_u32_iterator, boost::u16_to_u32_iterator > sl@0: make_utf32_seq(const charT* p, mpl::int_<2> const*) sl@0: { sl@0: return std::pair< boost::u16_to_u32_iterator, boost::u16_to_u32_iterator >(boost::u16_to_u32_iterator(p), boost::u16_to_u32_iterator(p+u_strlen((const UChar*)p))); sl@0: } sl@0: template sl@0: inline std::pair< const charT*, const charT* > sl@0: make_utf32_seq(const charT* p, mpl::int_<4> const*) sl@0: { sl@0: return std::pair< const charT*, const charT* >(p, p+icu_regex_traits::length((UChar32 const*)p)); sl@0: } sl@0: template sl@0: inline OutputIterator make_utf32_out(OutputIterator o, mpl::int_<4> const*) sl@0: { sl@0: return o; sl@0: } sl@0: template sl@0: inline utf16_output_iterator make_utf32_out(OutputIterator o, mpl::int_<2> const*) sl@0: { sl@0: return o; sl@0: } sl@0: template sl@0: inline utf8_output_iterator make_utf32_out(OutputIterator o, mpl::int_<1> const*) sl@0: { sl@0: return o; sl@0: } sl@0: sl@0: template sl@0: OutputIterator do_regex_replace(OutputIterator out, sl@0: std::pair const& in, sl@0: const u32regex& e, sl@0: const std::pair& fmt, sl@0: match_flag_type flags sl@0: ) sl@0: { sl@0: // unfortunately we have to copy the format string in order to pass in onward: sl@0: std::vector f; sl@0: #ifndef BOOST_NO_TEMPLATED_ITERATOR_CONSTRUCTORS sl@0: f.assign(fmt.first, fmt.second); sl@0: #else sl@0: f.clear(); sl@0: I2 pos = fmt.first; sl@0: while(pos != fmt.second) sl@0: f.push_back(*pos++); sl@0: #endif sl@0: sl@0: regex_iterator i(in.first, in.second, e, flags); sl@0: regex_iterator j; sl@0: if(i == j) sl@0: { sl@0: if(!(flags & regex_constants::format_no_copy)) sl@0: out = re_detail::copy(in.first, in.second, out); sl@0: } sl@0: else sl@0: { sl@0: I1 last_m = in.first; sl@0: while(i != j) sl@0: { sl@0: if(!(flags & regex_constants::format_no_copy)) sl@0: out = re_detail::copy(i->prefix().first, i->prefix().second, out); sl@0: if(f.size()) sl@0: out = ::boost::re_detail::regex_format_imp(out, *i, &*f.begin(), &*f.begin() + f.size(), flags, e.get_traits()); sl@0: else sl@0: out = ::boost::re_detail::regex_format_imp(out, *i, static_cast(0), static_cast(0), flags, e.get_traits()); sl@0: last_m = (*i)[0].second; sl@0: if(flags & regex_constants::format_first_only) sl@0: break; sl@0: ++i; sl@0: } sl@0: if(!(flags & regex_constants::format_no_copy)) sl@0: out = re_detail::copy(last_m, in.second, out); sl@0: } sl@0: return out; sl@0: } sl@0: template sl@0: inline const BaseIterator& extract_output_base(const BaseIterator& b) sl@0: { sl@0: return b; sl@0: } sl@0: template sl@0: inline BaseIterator extract_output_base(const utf8_output_iterator& b) sl@0: { sl@0: return b.base(); sl@0: } sl@0: template sl@0: inline BaseIterator extract_output_base(const utf16_output_iterator& b) sl@0: { sl@0: return b.base(); sl@0: } sl@0: } // re_detail sl@0: sl@0: template sl@0: inline OutputIterator u32regex_replace(OutputIterator out, sl@0: BidirectionalIterator first, sl@0: BidirectionalIterator last, sl@0: const u32regex& e, sl@0: const charT* fmt, sl@0: match_flag_type flags = match_default) sl@0: { sl@0: return re_detail::extract_output_base sl@0: #if BOOST_WORKAROUND(BOOST_MSVC, <= 1300) sl@0: sl@0: #endif sl@0: ( sl@0: re_detail::do_regex_replace( sl@0: re_detail::make_utf32_out(out, static_cast const*>(0)), sl@0: re_detail::make_utf32_seq(first, last, static_cast const*>(0)), sl@0: e, sl@0: re_detail::make_utf32_seq(fmt, static_cast const*>(0)), sl@0: flags) sl@0: ); sl@0: } sl@0: sl@0: template sl@0: inline OutputIterator u32regex_replace(OutputIterator out, sl@0: Iterator first, sl@0: Iterator last, sl@0: const u32regex& e, sl@0: const std::basic_string& fmt, sl@0: match_flag_type flags = match_default) sl@0: { sl@0: return re_detail::extract_output_base sl@0: #if BOOST_WORKAROUND(BOOST_MSVC, <= 1300) sl@0: sl@0: #endif sl@0: ( sl@0: re_detail::do_regex_replace( sl@0: re_detail::make_utf32_out(out, static_cast const*>(0)), sl@0: re_detail::make_utf32_seq(first, last, static_cast const*>(0)), sl@0: e, sl@0: re_detail::make_utf32_seq(fmt.begin(), fmt.end(), static_cast const*>(0)), sl@0: flags) sl@0: ); sl@0: } sl@0: sl@0: template sl@0: inline OutputIterator u32regex_replace(OutputIterator out, sl@0: Iterator first, sl@0: Iterator last, sl@0: const u32regex& e, sl@0: const UnicodeString& fmt, sl@0: match_flag_type flags = match_default) sl@0: { sl@0: return re_detail::extract_output_base sl@0: #if BOOST_WORKAROUND(BOOST_MSVC, <= 1300) sl@0: sl@0: #endif sl@0: ( sl@0: re_detail::do_regex_replace( sl@0: re_detail::make_utf32_out(out, static_cast const*>(0)), sl@0: re_detail::make_utf32_seq(first, last, static_cast const*>(0)), sl@0: e, sl@0: re_detail::make_utf32_seq(fmt.getBuffer(), fmt.getBuffer() + fmt.length(), static_cast const*>(0)), sl@0: flags) sl@0: ); sl@0: } sl@0: sl@0: template sl@0: std::basic_string u32regex_replace(const std::basic_string& s, sl@0: const u32regex& e, sl@0: const charT* fmt, sl@0: match_flag_type flags = match_default) sl@0: { sl@0: std::basic_string result; sl@0: re_detail::string_out_iterator > i(result); sl@0: u32regex_replace(i, s.begin(), s.end(), e, fmt, flags); sl@0: return result; sl@0: } sl@0: sl@0: template sl@0: std::basic_string u32regex_replace(const std::basic_string& s, sl@0: const u32regex& e, sl@0: const std::basic_string& fmt, sl@0: match_flag_type flags = match_default) sl@0: { sl@0: std::basic_string result; sl@0: re_detail::string_out_iterator > i(result); sl@0: u32regex_replace(i, s.begin(), s.end(), e, fmt.c_str(), flags); sl@0: return result; sl@0: } sl@0: sl@0: namespace re_detail{ sl@0: sl@0: class unicode_string_out_iterator sl@0: { sl@0: UnicodeString* out; sl@0: public: sl@0: unicode_string_out_iterator(UnicodeString& s) : out(&s) {} sl@0: unicode_string_out_iterator& operator++() { return *this; } sl@0: unicode_string_out_iterator& operator++(int) { return *this; } sl@0: unicode_string_out_iterator& operator*() { return *this; } sl@0: unicode_string_out_iterator& operator=(UChar v) sl@0: { sl@0: *out += v; sl@0: return *this; sl@0: } sl@0: typedef std::ptrdiff_t difference_type; sl@0: typedef UChar value_type; sl@0: typedef value_type* pointer; sl@0: typedef value_type& reference; sl@0: typedef std::output_iterator_tag iterator_category; sl@0: }; sl@0: sl@0: } sl@0: sl@0: inline UnicodeString u32regex_replace(const UnicodeString& s, sl@0: const u32regex& e, sl@0: const UChar* fmt, sl@0: match_flag_type flags = match_default) sl@0: { sl@0: UnicodeString result; sl@0: re_detail::unicode_string_out_iterator i(result); sl@0: u32regex_replace(i, s.getBuffer(), s.getBuffer()+s.length(), e, fmt, flags); sl@0: return result; sl@0: } sl@0: sl@0: inline UnicodeString u32regex_replace(const UnicodeString& s, sl@0: const u32regex& e, sl@0: const UnicodeString& fmt, sl@0: match_flag_type flags = match_default) sl@0: { sl@0: UnicodeString result; sl@0: re_detail::unicode_string_out_iterator i(result); sl@0: re_detail::do_regex_replace( sl@0: re_detail::make_utf32_out(i, static_cast const*>(0)), sl@0: re_detail::make_utf32_seq(s.getBuffer(), s.getBuffer()+s.length(), static_cast const*>(0)), sl@0: e, sl@0: re_detail::make_utf32_seq(fmt.getBuffer(), fmt.getBuffer() + fmt.length(), static_cast const*>(0)), sl@0: flags); sl@0: return result; sl@0: } sl@0: sl@0: } // namespace boost. sl@0: sl@0: #include sl@0: #include sl@0: sl@0: #endif