icu.hpp
来自「support vector clustering for vc++」· HPP 代码 · 共 1,018 行 · 第 1/3 页
HPP
1,018 行
/*
*
* Copyright (c) 2004
* John Maddock
*
* Use, modification and distribution are subject to the
* Boost Software License, Version 1.0. (See accompanying file
* LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*
*/
/*
* LOCATION: see http://www.boost.org for most recent version.
* FILE icu.hpp
* VERSION see <boost/version.hpp>
* DESCRIPTION: Unicode regular expressions on top of the ICU Library.
*/
#ifndef BOOST_REGEX_ICU_HPP
#define BOOST_REGEX_ICU_HPP
#include <unicode/utypes.h>
#include <unicode/uchar.h>
#include <unicode/coll.h>
#include <boost/regex.hpp>
#include <boost/regex/pending/unicode_iterator.hpp>
#include <boost/mpl/int_fwd.hpp>
#include <bitset>
namespace boost{
namespace re_detail{
//
// Implementation details:
//
class BOOST_REGEX_DECL icu_regex_traits_implementation
{
typedef UChar32 char_type;
typedef std::size_t size_type;
typedef std::vector<char_type> string_type;
typedef U_NAMESPACE_QUALIFIER Locale locale_type;
typedef boost::uint_least32_t char_class_type;
public:
icu_regex_traits_implementation(const U_NAMESPACE_QUALIFIER Locale& l)
: m_locale(l)
{
UErrorCode success = U_ZERO_ERROR;
m_collator.reset(U_NAMESPACE_QUALIFIER Collator::createInstance(l, success));
if(U_SUCCESS(success) == 0)
init_error();
m_collator->setStrength(U_NAMESPACE_QUALIFIER Collator::IDENTICAL);
success = U_ZERO_ERROR;
m_primary_collator.reset(U_NAMESPACE_QUALIFIER Collator::createInstance(l, success));
if(U_SUCCESS(success) == 0)
init_error();
m_primary_collator->setStrength(U_NAMESPACE_QUALIFIER Collator::PRIMARY);
}
U_NAMESPACE_QUALIFIER Locale getloc()const
{
return m_locale;
}
string_type do_transform(const char_type* p1, const char_type* p2, const U_NAMESPACE_QUALIFIER Collator* pcoll) const;
string_type transform(const char_type* p1, const char_type* p2) const
{
return do_transform(p1, p2, m_collator.get());
}
string_type transform_primary(const char_type* p1, const char_type* p2) const
{
return do_transform(p1, p2, m_primary_collator.get());
}
private:
void init_error()
{
std::runtime_error e("Could not initialize ICU resources");
boost::throw_exception(e);
}
U_NAMESPACE_QUALIFIER Locale m_locale; // The ICU locale that we're using
boost::scoped_ptr< U_NAMESPACE_QUALIFIER Collator> m_collator; // The full collation object
boost::scoped_ptr< U_NAMESPACE_QUALIFIER Collator> m_primary_collator; // The primary collation object
};
inline boost::shared_ptr<icu_regex_traits_implementation> get_icu_regex_traits_implementation(const U_NAMESPACE_QUALIFIER Locale& loc)
{
return boost::shared_ptr<icu_regex_traits_implementation>(new icu_regex_traits_implementation(loc));
}
}
class BOOST_REGEX_DECL icu_regex_traits
{
public:
typedef UChar32 char_type;
typedef std::size_t size_type;
typedef std::vector<char_type> string_type;
typedef U_NAMESPACE_QUALIFIER Locale locale_type;
#ifdef BOOST_NO_INT64_T
typedef std::bitset<64> char_class_type;
#else
typedef boost::uint64_t char_class_type;
#endif
struct boost_extensions_tag{};
icu_regex_traits()
: m_pimpl(re_detail::get_icu_regex_traits_implementation(U_NAMESPACE_QUALIFIER Locale()))
{
}
static size_type length(const char_type* p);
::boost::regex_constants::syntax_type syntax_type(char_type c)const
{
return ((c < 0x7f) && (c > 0)) ? re_detail::get_default_syntax_type(static_cast<char>(c)) : regex_constants::syntax_char;
}
::boost::regex_constants::escape_syntax_type escape_syntax_type(char_type c) const
{
return ((c < 0x7f) && (c > 0)) ? re_detail::get_default_escape_syntax_type(static_cast<char>(c)) : regex_constants::syntax_char;
}
char_type translate(char_type c) const
{
return c;
}
char_type translate_nocase(char_type c) const
{
return ::u_tolower(c);
}
char_type translate(char_type c, bool icase) const
{
return icase ? translate_nocase(c) : translate(c);
}
char_type tolower(char_type c) const
{
return ::u_tolower(c);
}
char_type toupper(char_type c) const
{
return ::u_toupper(c);
}
string_type transform(const char_type* p1, const char_type* p2) const
{
return m_pimpl->transform(p1, p2);
}
string_type transform_primary(const char_type* p1, const char_type* p2) const
{
return m_pimpl->transform_primary(p1, p2);
}
char_class_type lookup_classname(const char_type* p1, const char_type* p2) const;
string_type lookup_collatename(const char_type* p1, const char_type* p2) const;
bool isctype(char_type c, char_class_type f) const;
int toi(const char_type*& p1, const char_type* p2, int radix)const
{
return re_detail::global_toi(p1, p2, radix, *this);
}
int value(char_type c, int radix)const
{
return u_digit(c, static_cast< ::int8_t>(radix));
}
locale_type imbue(locale_type l)
{
locale_type result(m_pimpl->getloc());
m_pimpl = re_detail::get_icu_regex_traits_implementation(l);
return result;
}
locale_type getloc()const
{
return locale_type();
}
std::string error_string(::boost::regex_constants::error_type n) const
{
return re_detail::get_default_error_string(n);
}
private:
icu_regex_traits(const icu_regex_traits&);
icu_regex_traits& operator=(const icu_regex_traits&);
//
// define the bitmasks offsets we need for additional character properties:
//
enum{
offset_blank = U_CHAR_CATEGORY_COUNT,
offset_space = U_CHAR_CATEGORY_COUNT+1,
offset_xdigit = U_CHAR_CATEGORY_COUNT+2,
offset_underscore = U_CHAR_CATEGORY_COUNT+3,
offset_unicode = U_CHAR_CATEGORY_COUNT+4,
offset_any = U_CHAR_CATEGORY_COUNT+5,
offset_ascii = U_CHAR_CATEGORY_COUNT+6
};
//
// and now the masks:
//
static const char_class_type mask_blank;
static const char_class_type mask_space;
static const char_class_type mask_xdigit;
static const char_class_type mask_underscore;
static const char_class_type mask_unicode;
static const char_class_type mask_any;
static const char_class_type mask_ascii;
static char_class_type lookup_icu_mask(const ::UChar32* p1, const ::UChar32* p2);
boost::shared_ptr< ::boost::re_detail::icu_regex_traits_implementation> m_pimpl;
};
} // namespace boost
//
// template instances:
//
#define BOOST_REGEX_CHAR_T UChar32
#undef BOOST_REGEX_TRAITS_T
#define BOOST_REGEX_TRAITS_T , icu_regex_traits
#define BOOST_REGEX_ICU_INSTANCES
#ifdef BOOST_REGEX_ICU_INSTANTIATE
# define BOOST_REGEX_INSTANTIATE
#endif
#include <boost/regex/v4/instances.hpp>
#undef BOOST_REGEX_CHAR_T
#undef BOOST_REGEX_TRAITS_T
#undef BOOST_REGEX_ICU_INSTANCES
#ifdef BOOST_REGEX_INSTANTIATE
# undef BOOST_REGEX_INSTANTIATE
#endif
namespace boost{
// types:
typedef basic_regex< ::UChar32, icu_regex_traits> u32regex;
typedef match_results<const ::UChar32*> u32match;
typedef match_results<const ::UChar*> u16match;
//
// Construction of 32-bit regex types from UTF-8 and UTF-16 primitives:
//
namespace re_detail{
#if !defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(__IBMCPP__)
template <class InputIterator>
inline u32regex do_make_u32regex(InputIterator i,
InputIterator j,
boost::regex_constants::syntax_option_type opt,
const boost::mpl::int_<1>*)
{
typedef boost::u8_to_u32_iterator<InputIterator, UChar32> conv_type;
return u32regex(conv_type(i), conv_type(j), opt);
}
template <class InputIterator>
inline u32regex do_make_u32regex(InputIterator i,
InputIterator j,
boost::regex_constants::syntax_option_type opt,
const boost::mpl::int_<2>*)
{
typedef boost::u16_to_u32_iterator<InputIterator, UChar32> conv_type;
return u32regex(conv_type(i), conv_type(j), opt);
}
template <class InputIterator>
inline u32regex do_make_u32regex(InputIterator i,
InputIterator j,
boost::regex_constants::syntax_option_type opt,
const boost::mpl::int_<4>*)
{
return u32regex(i, j, opt);
}
#else
template <class InputIterator>
inline u32regex do_make_u32regex(InputIterator i,
InputIterator j,
boost::regex_constants::syntax_option_type opt,
const boost::mpl::int_<1>*)
{
typedef boost::u8_to_u32_iterator<InputIterator, UChar32> conv_type;
typedef std::vector<UChar32> vector_type;
vector_type v;
conv_type a(i), b(j);
while(a != b)
{
v.push_back(*a);
++a;
}
if(v.size())
return u32regex(&*v.begin(), v.size(), opt);
return u32regex(static_cast<UChar32 const*>(0), static_cast<u32regex::size_type>(0), opt);
}
template <class InputIterator>
inline u32regex do_make_u32regex(InputIterator i,
InputIterator j,
boost::regex_constants::syntax_option_type opt,
const boost::mpl::int_<2>*)
{
typedef boost::u16_to_u32_iterator<InputIterator, UChar32> conv_type;
typedef std::vector<UChar32> vector_type;
vector_type v;
conv_type a(i), b(j);
while(a != b)
{
v.push_back(*a);
++a;
}
if(v.size())
return u32regex(&*v.begin(), v.size(), opt);
return u32regex(static_cast<UChar32 const*>(0), static_cast<u32regex::size_type>(0), opt);
}
template <class InputIterator>
inline u32regex do_make_u32regex(InputIterator i,
InputIterator j,
boost::regex_constants::syntax_option_type opt,
const boost::mpl::int_<4>*)
{
typedef std::vector<UCHAR32> vector_type;
vector_type v;
while(i != j)
{
v.push_back((UCHAR32)(*i));
++a;
}
if(v.size())
return u32regex(&*v.begin(), v.size(), opt);
return u32regex(static_cast<UChar32 const*>(0), static_cast<u32regex::size_type>(0), opt);
}
#endif
}
//
// Construction from an iterator pair:
//
template <class InputIterator>
inline u32regex make_u32regex(InputIterator i,
InputIterator j,
boost::regex_constants::syntax_option_type opt)
{
return re_detail::do_make_u32regex(i, j, opt, static_cast<boost::mpl::int_<sizeof(*i)> const*>(0));
}
//
// construction from UTF-8 nul-terminated strings:
//
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?