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