00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef __UTF8_H__
00024 #define __UTF8_H__
00025
00026 #include <string>
00027 #include <iterator>
00028 #include <stdexcept>
00029 #include <cwchar>
00030
00031 namespace libtorrent {
00032 namespace detail {
00033
00034 template<typename InputIterator>
00035 wchar_t decode_utf8_mb(InputIterator &iter, InputIterator last)
00036 {
00037 if (iter == last) throw std::runtime_error("incomplete UTF-8 sequence");
00038 if (((*iter) & 0xc0) != 0x80) throw std::runtime_error("invalid UTF-8 sequence");
00039
00040 return (wchar_t)((*iter++) & 0x3f);
00041 }
00042
00043 template<typename InputIterator>
00044 wchar_t decode_utf8(InputIterator &iter, InputIterator last)
00045 {
00046 wchar_t ret;
00047
00048 if (((*iter) & 0x80) == 0)
00049 {
00050 ret = *iter++;
00051 }
00052 else if (((*iter) & 0xe0) == 0xc0)
00053 {
00054 wchar_t byte1 = (*iter++) & 0x1f;
00055 wchar_t byte2 = decode_utf8_mb(iter, last);
00056 ret = (byte1 << 6) | byte2;
00057 }
00058 else if (((*iter) & 0xf0) == 0xe0)
00059 {
00060 wchar_t byte1 = (*iter++) & 0x0f;
00061 wchar_t byte2 = decode_utf8_mb(iter, last);
00062 wchar_t byte3 = decode_utf8_mb(iter, last);
00063 ret = (byte1 << 12) | (byte2 << 6) | byte3;
00064 }
00065
00066 else throw std::runtime_error("UTF-8 not convertable to UTF-16");
00067
00068 return ret;
00069 }
00070
00071 template<typename InputIterator, typename OutputIterator>
00072 OutputIterator utf8_wchar(InputIterator first, InputIterator last, OutputIterator dest)
00073 {
00074 for(; first!=last; ++dest)
00075 *dest = decode_utf8(first, last);
00076 return dest;
00077 }
00078
00079 template<typename InputIterator, typename OutputIterator>
00080 void encode_wchar(InputIterator iter, OutputIterator &dest)
00081 {
00082 if(*iter <= 0x007F)
00083 {
00084 *dest=(char)*iter;
00085 ++dest;
00086 }
00087 else if(*iter <= 0x07FF)
00088 {
00089 *dest = (char)(
00090 0xC0 |
00091 ((*iter & 0x07C0) >> 6)
00092 );
00093 ++dest;
00094
00095 *dest = (char)(
00096 0x80 |
00097 (*iter & 0x003F)
00098 );
00099 ++dest;
00100 }
00101 else if(*iter <= 0xFFFF)
00102 {
00103 *dest = (char)(
00104 0xE0 |
00105 ((*iter & 0xF000) >> 12)
00106 );
00107 ++dest;
00108
00109 *dest = (char)(
00110 0x80 |
00111 ((*iter & 0x0FC0) >> 6)
00112 );
00113 ++dest;
00114
00115 *dest = (char)(
00116 0x80 |
00117 (*iter & 0x003F)
00118 );
00119 ++dest;
00120 }
00121 }
00122
00123 template<typename InputIterator, typename OutputIterator>
00124 OutputIterator wchar_utf8(InputIterator first, InputIterator last, OutputIterator dest)
00125 {
00126 for(; first!=last; ++first)
00127 encode_wchar(first, dest);
00128 return dest;
00129 }
00130
00131 }
00132
00133 inline void utf8_wchar(const std::string &utf8, std::wstring &wide)
00134 {
00135 wide.clear();
00136 detail::utf8_wchar(utf8.begin(), utf8.end(), std::back_inserter(wide));
00137 }
00138
00139 inline std::wstring utf8_wchar(const std::string &str)
00140 {
00141 std::wstring ret;
00142 utf8_wchar(str, ret);
00143 return ret;
00144 }
00145
00146 inline void wchar_utf8(const std::wstring &wide, std::string &utf8)
00147 {
00148 utf8.clear();
00149 detail::wchar_utf8(wide.begin(), wide.end(), std::back_inserter(utf8));
00150 }
00151
00152 inline std::string wchar_utf8(const std::wstring &str)
00153 {
00154 std::string ret;
00155 wchar_utf8(str, ret);
00156 return ret;
00157 }
00158
00159 }
00160
00161 #endif