CppCMS
booster/locale/conversion.h
00001 //
00002 //  Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
00003 //
00004 //  Distributed under the Boost Software License, Version 1.0. (See
00005 //  accompanying file LICENSE_1_0.txt or copy at
00006 //  http://www.boost.org/LICENSE_1_0.txt)
00007 //
00008 #ifndef BOOSTER_LOCALE_CONVERTER_H_INCLUDED
00009 #define BOOSTER_LOCALE_CONVERTER_H_INCLUDED
00010 
00011 #include <booster/config.h>
00012 #ifdef BOOSTER_MSVC
00013 #  pragma warning(push)
00014 #  pragma warning(disable : 4275 4251 4231 4660)
00015 #endif
00016 #include <locale>
00017 
00018 
00019 namespace booster {
00020     namespace locale {
00021         
00028 
00029         
00033         class converter_base {
00034         public:
00038             typedef enum {
00039                 normalization,  
00040                 upper_case,     
00041                 lower_case,     
00042                 case_folding,   
00043                 title_case      
00044             } conversion_type;
00045         };
00046 
00047         template<typename CharType>
00048         class converter;
00049 
00050         #ifdef BOOSTER_LOCALE_DOXYGEN
00051 
00052 
00053 
00054 
00055 
00056 
00057         template<typename Char>
00058         class BOOSTER_API converter: public converter_base, public std::locale::facet {
00059         public:
00061             static std::locale::id id;
00062 
00064             converter(size_t refs = 0) : std::locale::facet(refs)
00065             {
00066             }
00071             virtual std::basic_string<Char> convert(conversion_type how,Char const *begin,Char const *end,int flags = 0) const = 0;
00072 #if defined (__SUNPRO_CC) && defined (_RWSTD_VER)
00073             std::locale::id& __get_id (void) const { return id; }
00074 #endif
00075         };
00076         #else
00077 
00078         template<>
00079         class BOOSTER_API converter<char> : public converter_base, public std::locale::facet {
00080         public:
00081             static std::locale::id id;
00082 
00083             converter(size_t refs = 0) : std::locale::facet(refs)
00084             {
00085             }
00086             virtual std::string convert(conversion_type how,char const *begin,char const *end,int flags = 0) const = 0;
00087 #if defined (__SUNPRO_CC) && defined (_RWSTD_VER)
00088             std::locale::id& __get_id (void) const { return id; }
00089 #endif
00090         };
00091 
00092         template<>
00093         class BOOSTER_API converter<wchar_t> : public converter_base, public std::locale::facet {
00094         public:
00095             static std::locale::id id;
00096             converter(size_t refs = 0) : std::locale::facet(refs)
00097             {
00098             }
00099              virtual std::wstring convert(conversion_type how,wchar_t const *begin,wchar_t const *end,int flags = 0) const = 0;
00100 #if defined (__SUNPRO_CC) && defined (_RWSTD_VER)
00101             std::locale::id& __get_id (void) const { return id; }
00102 #endif
00103         };
00104 
00105         #ifdef BOOSTER_HAS_CHAR16_T
00106         template<>
00107         class BOOSTER_API converter<char16_t> : public converter_base, public std::locale::facet {
00108         public:
00109             static std::locale::id id;
00110             converter(size_t refs = 0) : std::locale::facet(refs)
00111             {
00112             }
00113             virtual std::u16string convert(conversion_type how,char16_t const *begin,char16_t const *end,int flags = 0) const = 0; 
00114 #if defined (__SUNPRO_CC) && defined (_RWSTD_VER)
00115             std::locale::id& __get_id (void) const { return id; }
00116 #endif
00117         };
00118         #endif
00119 
00120         #ifdef BOOSTER_HAS_CHAR32_T
00121         template<>
00122         class BOOSTER_API converter<char32_t> : public converter_base, public std::locale::facet {
00123         public:
00124             static std::locale::id id;
00125             converter(size_t refs = 0) : std::locale::facet(refs)
00126             {
00127             }
00128             virtual std::u32string convert(conversion_type how,char32_t const *begin,char32_t const *end,int flags = 0) const = 0;
00129 #if defined (__SUNPRO_CC) && defined (_RWSTD_VER)
00130             std::locale::id& __get_id (void) const { return id; }
00131 #endif
00132         };
00133         #endif
00134 
00135         #endif
00136 
00140 
00141         typedef enum {
00142             norm_nfd,   
00143             norm_nfc,   
00144             norm_nfkd,  
00145             norm_nfkc,  
00146             norm_default = norm_nfc, 
00147         } norm_type;
00148        
00158         template<typename CharType>
00159         std::basic_string<CharType> normalize(std::basic_string<CharType> const &str,norm_type n=norm_default,std::locale const &loc=std::locale())
00160         {
00161             return std::use_facet<converter<CharType> >(loc).convert(converter_base::normalization,str.data(),str.data() + str.size(),n);
00162         }
00163 
00173         template<typename CharType>
00174         std::basic_string<CharType> normalize(CharType const *str,norm_type n=norm_default,std::locale const &loc=std::locale())
00175         {
00176             CharType const *end=str;
00177             while(*end)
00178                 end++;
00179             return std::use_facet<converter<CharType> >(loc).convert(converter_base::normalization,str,end,n);
00180         }
00181         
00191         template<typename CharType>
00192         std::basic_string<CharType> normalize(  CharType const *begin,
00193                                                 CharType const *end,
00194                                                 norm_type n=norm_default,
00195                                                 std::locale const &loc=std::locale())
00196         {
00197             return std::use_facet<converter<CharType> >(loc).convert(converter_base::normalization,begin,end,n);
00198         }
00199 
00201         
00207 
00208         template<typename CharType>
00209         std::basic_string<CharType> to_upper(std::basic_string<CharType> const &str,std::locale const &loc=std::locale())
00210         {
00211             return std::use_facet<converter<CharType> >(loc).convert(converter_base::upper_case,str.data(),str.data()+str.size());
00212         }
00213         
00219         template<typename CharType>
00220         std::basic_string<CharType> to_upper(CharType const *str,std::locale const &loc=std::locale())
00221         {
00222             CharType const *end=str;
00223             while(*end)
00224                 end++;
00225             return std::use_facet<converter<CharType> >(loc).convert(converter_base::upper_case,str,end);
00226         }
00227         
00233         template<typename CharType>
00234         std::basic_string<CharType> to_upper(CharType const *begin,CharType const *end,std::locale const &loc=std::locale())
00235         {
00236             return std::use_facet<converter<CharType> >(loc).convert(converter_base::upper_case,begin,end);
00237         }
00238 
00240         
00246 
00247         template<typename CharType>
00248         std::basic_string<CharType> to_lower(std::basic_string<CharType> const &str,std::locale const &loc=std::locale())
00249         {
00250             return std::use_facet<converter<CharType> >(loc).convert(converter_base::lower_case,str.data(),str.data()+str.size());
00251         }
00252         
00258         template<typename CharType>
00259         std::basic_string<CharType> to_lower(CharType const *str,std::locale const &loc=std::locale())
00260         {
00261             CharType const *end=str;
00262             while(*end)
00263                 end++;
00264             return std::use_facet<converter<CharType> >(loc).convert(converter_base::lower_case,str,end);
00265         }
00266         
00272         template<typename CharType>
00273         std::basic_string<CharType> to_lower(CharType const *begin,CharType const *end,std::locale const &loc=std::locale())
00274         {
00275             return std::use_facet<converter<CharType> >(loc).convert(converter_base::lower_case,begin,end);
00276         }
00278         
00284 
00285         template<typename CharType>
00286         std::basic_string<CharType> to_title(std::basic_string<CharType> const &str,std::locale const &loc=std::locale())
00287         {
00288             return std::use_facet<converter<CharType> >(loc).convert(converter_base::title_case,str.data(),str.data()+str.size());
00289         }
00290         
00296         template<typename CharType>
00297         std::basic_string<CharType> to_title(CharType const *str,std::locale const &loc=std::locale())
00298         {
00299             CharType const *end=str;
00300             while(*end)
00301                 end++;
00302             return std::use_facet<converter<CharType> >(loc).convert(converter_base::title_case,str,end);
00303         }
00304         
00310         template<typename CharType>
00311         std::basic_string<CharType> to_title(CharType const *begin,CharType const *end,std::locale const &loc=std::locale())
00312         {
00313             return std::use_facet<converter<CharType> >(loc).convert(converter_base::title_case,begin,end);
00314         }
00315 
00317         
00323 
00324         template<typename CharType>
00325         std::basic_string<CharType> fold_case(std::basic_string<CharType> const &str,std::locale const &loc=std::locale())
00326         {
00327             return std::use_facet<converter<CharType> >(loc).convert(converter_base::case_folding,str.data(),str.data()+str.size());
00328         }
00329         
00335         template<typename CharType>
00336         std::basic_string<CharType> fold_case(CharType const *str,std::locale const &loc=std::locale())
00337         {
00338             CharType const *end=str;
00339             while(*end)
00340                 end++;
00341             return std::use_facet<converter<CharType> >(loc).convert(converter_base::case_folding,str,end);
00342         }
00343         
00349         template<typename CharType>
00350         std::basic_string<CharType> fold_case(CharType const *begin,CharType const *end,std::locale const &loc=std::locale())
00351         {
00352             return std::use_facet<converter<CharType> >(loc).convert(converter_base::case_folding,begin,end);
00353         }
00354 
00358     } // locale
00359 
00360 } // boost
00361 
00362 #ifdef BOOSTER_MSVC
00363 #pragma warning(pop)
00364 #endif
00365 
00366 
00367 #endif
00368 
00378 
00379 // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
00380