CppCMS
booster/locale/encoding_utf.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_ENCODING_UTF_H_INCLUDED
00009 #define BOOSTER_LOCALE_ENCODING_UTF_H_INCLUDED
00010 
00011 #include <booster/locale/utf.h>
00012 #include <booster/locale/encoding_errors.h>
00013 #include <iterator>
00014 #ifdef BOOSTER_MSVC
00015 #  pragma warning(push)
00016 #  pragma warning(disable : 4275 4251 4231 4660)
00017 #endif
00018 
00019 
00020 
00021 namespace booster {
00022     namespace locale {
00023         namespace conv {
00028 
00032             template<typename CharOut,typename CharIn>
00033             std::basic_string<CharOut>
00034             utf_to_utf(CharIn const *begin,CharIn const *end,method_type how = default_method)
00035             {
00036                 std::basic_string<CharOut> result;
00037                 result.reserve(end-begin);
00038                 typedef std::back_insert_iterator<std::basic_string<CharOut> > inserter_type;
00039                 inserter_type inserter(result);
00040                 utf::code_point c;
00041                 while(begin!=end) {
00042                     c=utf::utf_traits<CharIn>::template decode<CharIn const *>(begin,end);
00043                     if(c==utf::illegal || c==utf::incomplete) {
00044                         if(how==stop)
00045                             throw conversion_error();
00046                     }
00047                     else {
00048                         utf::utf_traits<CharOut>::template encode<inserter_type>(c,inserter);
00049                     }
00050                 }
00051                 return result;
00052             }
00053 
00057             template<typename CharOut,typename CharIn>
00058             std::basic_string<CharOut>
00059             utf_to_utf(CharIn const *str,method_type how = default_method)
00060             {
00061                 CharIn const *end = str;
00062                 while(*end)
00063                     end++;
00064                 return utf_to_utf<CharOut,CharIn>(str,end,how);
00065             }
00066 
00067 
00071             template<typename CharOut,typename CharIn>
00072             std::basic_string<CharOut>
00073             utf_to_utf(std::basic_string<CharIn> const &str,method_type how = default_method)
00074             {
00075                 return utf_to_utf<CharOut,CharIn>(str.c_str(),str.c_str()+str.size(),how);
00076             }
00077 
00078 
00080 
00081         } // conv
00082 
00083     } // locale
00084 } // boost
00085 
00086 #ifdef BOOSTER_MSVC
00087 #pragma warning(pop)
00088 #endif
00089 
00090 #endif
00091 
00092 // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
00093