Chris@16: // Chris@16: // Copyright (c) 2009-2011 Artyom Beilis (Tonkikh) Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. (See Chris@16: // accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: #ifndef BOOST_LOCALE_CONVERTER_HPP_INCLUDED Chris@16: #define BOOST_LOCALE_CONVERTER_HPP_INCLUDED Chris@16: Chris@16: #include Chris@16: #ifdef BOOST_MSVC Chris@16: # pragma warning(push) Chris@16: # pragma warning(disable : 4275 4251 4231 4660) Chris@16: #endif Chris@16: #include Chris@16: Chris@16: Chris@16: namespace boost { Chris@16: namespace locale { Chris@16: Chris@16: /// Chris@16: /// \defgroup convert Text Conversions Chris@16: /// Chris@16: /// This module provides various function for string manipulation like Unicode normalization, case conversion etc. Chris@16: /// @{ Chris@16: /// Chris@16: Chris@16: Chris@16: /// Chris@16: /// \brief This class provides base flags for text manipulation. It is used as base for converter facet. Chris@16: /// Chris@16: class converter_base { Chris@16: public: Chris@16: /// Chris@16: /// The flag used for facet - the type of operation to perform Chris@16: /// Chris@16: typedef enum { Chris@16: normalization, ///< Apply Unicode normalization on the text Chris@16: upper_case, ///< Convert text to upper case Chris@16: lower_case, ///< Convert text to lower case Chris@16: case_folding, ///< Fold case in the text Chris@16: title_case ///< Convert text to title case Chris@16: } conversion_type; Chris@16: }; Chris@16: Chris@16: template Chris@16: class converter; Chris@16: Chris@16: #ifdef BOOST_LOCALE_DOXYGEN Chris@16: /// Chris@16: /// \brief The facet that implements text manipulation Chris@16: /// Chris@16: /// It is used to performs text conversion operations defined by \ref conversion_type. It is specialized Chris@16: /// for four types of characters \c char, \c wchar_t, \c char16_t, \c char32_t Chris@16: /// Chris@16: template Chris@16: class BOOST_LOCALE_DECL converter: public converter_base, public std::locale::facet { Chris@16: public: Chris@16: /// Locale identification Chris@16: static std::locale::id id; Chris@16: Chris@16: /// Standard constructor Chris@16: converter(size_t refs = 0) : std::locale::facet(refs) Chris@16: { Chris@16: } Chris@16: /// Chris@16: /// Convert text in range [\a begin, \a end) according to conversion method \a how. Parameter Chris@16: /// \a flags is used for specification of normalization method like nfd, nfc etc. Chris@16: /// Chris@16: virtual std::basic_string convert(conversion_type how,Char const *begin,Char const *end,int flags = 0) const = 0; Chris@16: #if defined (__SUNPRO_CC) && defined (_RWSTD_VER) Chris@16: std::locale::id& __get_id (void) const { return id; } Chris@16: #endif Chris@16: }; Chris@16: #else Chris@16: Chris@16: template<> Chris@16: class BOOST_LOCALE_DECL converter : public converter_base, public std::locale::facet { Chris@16: public: Chris@16: static std::locale::id id; Chris@16: Chris@16: converter(size_t refs = 0) : std::locale::facet(refs) Chris@16: { Chris@16: } Chris@16: virtual std::string convert(conversion_type how,char const *begin,char const *end,int flags = 0) const = 0; Chris@16: #if defined (__SUNPRO_CC) && defined (_RWSTD_VER) Chris@16: std::locale::id& __get_id (void) const { return id; } Chris@16: #endif Chris@16: }; Chris@16: Chris@16: template<> Chris@16: class BOOST_LOCALE_DECL converter : public converter_base, public std::locale::facet { Chris@16: public: Chris@16: static std::locale::id id; Chris@16: converter(size_t refs = 0) : std::locale::facet(refs) Chris@16: { Chris@16: } Chris@16: virtual std::wstring convert(conversion_type how,wchar_t const *begin,wchar_t const *end,int flags = 0) const = 0; Chris@16: #if defined (__SUNPRO_CC) && defined (_RWSTD_VER) Chris@16: std::locale::id& __get_id (void) const { return id; } Chris@16: #endif Chris@16: }; Chris@16: Chris@16: #ifdef BOOST_HAS_CHAR16_T Chris@16: template<> Chris@16: class BOOST_LOCALE_DECL converter : public converter_base, public std::locale::facet { Chris@16: public: Chris@16: static std::locale::id id; Chris@16: converter(size_t refs = 0) : std::locale::facet(refs) Chris@16: { Chris@16: } Chris@16: virtual std::u16string convert(conversion_type how,char16_t const *begin,char16_t const *end,int flags = 0) const = 0; Chris@16: #if defined (__SUNPRO_CC) && defined (_RWSTD_VER) Chris@16: std::locale::id& __get_id (void) const { return id; } Chris@16: #endif Chris@16: }; Chris@16: #endif Chris@16: Chris@16: #ifdef BOOST_HAS_CHAR32_T Chris@16: template<> Chris@16: class BOOST_LOCALE_DECL converter : public converter_base, public std::locale::facet { Chris@16: public: Chris@16: static std::locale::id id; Chris@16: converter(size_t refs = 0) : std::locale::facet(refs) Chris@16: { Chris@16: } Chris@16: virtual std::u32string convert(conversion_type how,char32_t const *begin,char32_t const *end,int flags = 0) const = 0; Chris@16: #if defined (__SUNPRO_CC) && defined (_RWSTD_VER) Chris@16: std::locale::id& __get_id (void) const { return id; } Chris@16: #endif Chris@16: }; Chris@16: #endif Chris@16: Chris@16: #endif Chris@16: Chris@16: /// Chris@16: /// The type that defined normalization form Chris@16: /// Chris@16: Chris@16: typedef enum { Chris@16: norm_nfd, ///< Canonical decomposition Chris@16: norm_nfc, ///< Canonical decomposition followed by canonical composition Chris@16: norm_nfkd, ///< Compatibility decomposition Chris@16: norm_nfkc, ///< Compatibility decomposition followed by canonical composition. Chris@16: norm_default = norm_nfc, ///< Default normalization - canonical decomposition followed by canonical composition Chris@16: } norm_type; Chris@16: Chris@16: /// Chris@16: /// Normalize Unicode string \a str according to \ref norm_type "normalization form" \a n Chris@16: /// Chris@16: /// Note: This function receives only Unicode strings, i.e.: UTF-8, UTF-16 or UTF-32. It does not take Chris@16: /// in account the locale encoding, because Unicode decomposition and composition are meaningless outside Chris@16: /// of a Unicode character set. Chris@16: /// Chris@16: /// \note throws std::bad_cast if loc does not have \ref converter facet installed Chris@16: /// Chris@16: template Chris@16: std::basic_string normalize(std::basic_string const &str,norm_type n=norm_default,std::locale const &loc=std::locale()) Chris@16: { Chris@16: return std::use_facet >(loc).convert(converter_base::normalization,str.data(),str.data() + str.size(),n); Chris@16: } Chris@16: Chris@16: /// Chris@16: /// Normalize NUL terminated Unicode string \a str according to \ref norm_type "normalization form" \a n Chris@16: /// Chris@16: /// Note: This function receives only Unicode strings, i.e.: UTF-8, UTF-16 or UTF-32. It does not take Chris@16: /// in account the locale encoding, because Unicode decomposition and composition are meaningless outside Chris@16: /// of a Unicode character set. Chris@16: /// Chris@16: /// \note throws std::bad_cast if loc does not have \ref converter facet installed Chris@16: /// Chris@16: template Chris@16: std::basic_string normalize(CharType const *str,norm_type n=norm_default,std::locale const &loc=std::locale()) Chris@16: { Chris@16: CharType const *end=str; Chris@16: while(*end) Chris@16: end++; Chris@16: return std::use_facet >(loc).convert(converter_base::normalization,str,end,n); Chris@16: } Chris@16: Chris@16: /// Chris@16: /// Normalize Unicode string in range [begin,end) according to \ref norm_type "normalization form" \a n Chris@16: /// Chris@16: /// Note: This function receives only Unicode strings, i.e.: UTF-8, UTF-16 or UTF-32. It does not take Chris@16: /// in account the locale encoding, because Unicode decomposition and composition are meaningless outside Chris@16: /// of a Unicode character set. Chris@16: /// Chris@16: /// \note throws std::bad_cast if loc does not have \ref converter facet installed Chris@16: /// Chris@16: template Chris@16: std::basic_string normalize( CharType const *begin, Chris@16: CharType const *end, Chris@16: norm_type n=norm_default, Chris@16: std::locale const &loc=std::locale()) Chris@16: { Chris@16: return std::use_facet >(loc).convert(converter_base::normalization,begin,end,n); Chris@16: } Chris@16: Chris@16: /////////////////////////////////////////////////// Chris@16: Chris@16: /// Chris@16: /// Convert a string \a str to upper case according to locale \a loc Chris@16: /// Chris@16: /// \note throws std::bad_cast if loc does not have \ref converter facet installed Chris@16: /// Chris@16: Chris@16: template Chris@16: std::basic_string to_upper(std::basic_string const &str,std::locale const &loc=std::locale()) Chris@16: { Chris@16: return std::use_facet >(loc).convert(converter_base::upper_case,str.data(),str.data()+str.size()); Chris@16: } Chris@16: Chris@16: /// Chris@16: /// Convert a NUL terminated string \a str to upper case according to locale \a loc Chris@16: /// Chris@16: /// \note throws std::bad_cast if loc does not have \ref converter facet installed Chris@16: /// Chris@16: template Chris@16: std::basic_string to_upper(CharType const *str,std::locale const &loc=std::locale()) Chris@16: { Chris@16: CharType const *end=str; Chris@16: while(*end) Chris@16: end++; Chris@16: return std::use_facet >(loc).convert(converter_base::upper_case,str,end); Chris@16: } Chris@16: Chris@16: /// Chris@16: /// Convert a string in range [begin,end) to upper case according to locale \a loc Chris@16: /// Chris@16: /// \note throws std::bad_cast if loc does not have \ref converter facet installed Chris@16: /// Chris@16: template Chris@16: std::basic_string to_upper(CharType const *begin,CharType const *end,std::locale const &loc=std::locale()) Chris@16: { Chris@16: return std::use_facet >(loc).convert(converter_base::upper_case,begin,end); Chris@16: } Chris@16: Chris@16: /////////////////////////////////////////////////// Chris@16: Chris@16: /// Chris@16: /// Convert a string \a str to lower case according to locale \a loc Chris@16: /// Chris@16: /// \note throws std::bad_cast if loc does not have \ref converter facet installed Chris@16: /// Chris@16: Chris@16: template Chris@16: std::basic_string to_lower(std::basic_string const &str,std::locale const &loc=std::locale()) Chris@16: { Chris@16: return std::use_facet >(loc).convert(converter_base::lower_case,str.data(),str.data()+str.size()); Chris@16: } Chris@16: Chris@16: /// Chris@16: /// Convert a NUL terminated string \a str to lower case according to locale \a loc Chris@16: /// Chris@16: /// \note throws std::bad_cast if loc does not have \ref converter facet installed Chris@16: /// Chris@16: template Chris@16: std::basic_string to_lower(CharType const *str,std::locale const &loc=std::locale()) Chris@16: { Chris@16: CharType const *end=str; Chris@16: while(*end) Chris@16: end++; Chris@16: return std::use_facet >(loc).convert(converter_base::lower_case,str,end); Chris@16: } Chris@16: Chris@16: /// Chris@16: /// Convert a string in range [begin,end) to lower case according to locale \a loc Chris@16: /// Chris@16: /// \note throws std::bad_cast if loc does not have \ref converter facet installed Chris@16: /// Chris@16: template Chris@16: std::basic_string to_lower(CharType const *begin,CharType const *end,std::locale const &loc=std::locale()) Chris@16: { Chris@16: return std::use_facet >(loc).convert(converter_base::lower_case,begin,end); Chris@16: } Chris@16: /////////////////////////////////////////////////// Chris@16: Chris@16: /// Chris@16: /// Convert a string \a str to title case according to locale \a loc Chris@16: /// Chris@16: /// \note throws std::bad_cast if loc does not have \ref converter facet installed Chris@16: /// Chris@16: Chris@16: template Chris@16: std::basic_string to_title(std::basic_string const &str,std::locale const &loc=std::locale()) Chris@16: { Chris@16: return std::use_facet >(loc).convert(converter_base::title_case,str.data(),str.data()+str.size()); Chris@16: } Chris@16: Chris@16: /// Chris@16: /// Convert a NUL terminated string \a str to title case according to locale \a loc Chris@16: /// Chris@16: /// \note throws std::bad_cast if loc does not have \ref converter facet installed Chris@16: /// Chris@16: template Chris@16: std::basic_string to_title(CharType const *str,std::locale const &loc=std::locale()) Chris@16: { Chris@16: CharType const *end=str; Chris@16: while(*end) Chris@16: end++; Chris@16: return std::use_facet >(loc).convert(converter_base::title_case,str,end); Chris@16: } Chris@16: Chris@16: /// Chris@16: /// Convert a string in range [begin,end) to title case according to locale \a loc Chris@16: /// Chris@16: /// \note throws std::bad_cast if loc does not have \ref converter facet installed Chris@16: /// Chris@16: template Chris@16: std::basic_string to_title(CharType const *begin,CharType const *end,std::locale const &loc=std::locale()) Chris@16: { Chris@16: return std::use_facet >(loc).convert(converter_base::title_case,begin,end); Chris@16: } Chris@16: Chris@16: /////////////////////////////////////////////////// Chris@16: Chris@16: /// Chris@16: /// Fold case of a string \a str according to locale \a loc Chris@16: /// Chris@16: /// \note throws std::bad_cast if loc does not have \ref converter facet installed Chris@16: /// Chris@16: Chris@16: template Chris@16: std::basic_string fold_case(std::basic_string const &str,std::locale const &loc=std::locale()) Chris@16: { Chris@16: return std::use_facet >(loc).convert(converter_base::case_folding,str.data(),str.data()+str.size()); Chris@16: } Chris@16: Chris@16: /// Chris@16: /// Fold case of a NUL terminated string \a str according to locale \a loc Chris@16: /// Chris@16: /// \note throws std::bad_cast if loc does not have \ref converter facet installed Chris@16: /// Chris@16: template Chris@16: std::basic_string fold_case(CharType const *str,std::locale const &loc=std::locale()) Chris@16: { Chris@16: CharType const *end=str; Chris@16: while(*end) Chris@16: end++; Chris@16: return std::use_facet >(loc).convert(converter_base::case_folding,str,end); Chris@16: } Chris@16: Chris@16: /// Chris@16: /// Fold case of a string in range [begin,end) according to locale \a loc Chris@16: /// Chris@16: /// \note throws std::bad_cast if loc does not have \ref converter facet installed Chris@16: /// Chris@16: template Chris@16: std::basic_string fold_case(CharType const *begin,CharType const *end,std::locale const &loc=std::locale()) Chris@16: { Chris@16: return std::use_facet >(loc).convert(converter_base::case_folding,begin,end); Chris@16: } Chris@16: Chris@16: /// Chris@16: ///@} Chris@16: /// Chris@16: } // locale Chris@16: Chris@16: } // boost Chris@16: Chris@16: #ifdef BOOST_MSVC Chris@16: #pragma warning(pop) Chris@16: #endif Chris@16: Chris@16: Chris@16: #endif Chris@16: Chris@16: /// Chris@16: /// \example conversions.cpp Chris@16: /// Chris@16: /// Example of using various text conversion functions. Chris@16: /// Chris@16: /// \example wconversions.cpp Chris@16: /// Chris@16: /// Example of using various text conversion functions with wide strings. Chris@16: /// Chris@16: Chris@16: // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 Chris@16: