added some precautionary checks in bdecoder
[libtorrent.git] / include / libtorrent / utf8.hpp
blob157a7fdba51dc41c1c745d89d0b6f4d70da14e0b
1 /*
2 Copyright (C) 2004-2005 Cory Nelson
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely, subject to the following restrictions:
12 1. The origin of this software must not be misrepresented; you must not
13 claim that you wrote the original software. If you use this software
14 in a product, an acknowledgment in the product documentation would be
15 appreciated but is not required.
16 2. Altered source versions must be plainly marked as such, and must not be
17 misrepresented as being the original software.
18 3. This notice may not be removed or altered from any source distribution.
21 // namespaces added by Arvid Norberg
23 #ifndef __UTF8_H__
24 #define __UTF8_H__
26 #include <string>
27 #include <iterator>
28 #include <stdexcept>
29 #include <cwchar>
31 namespace libtorrent {
32 namespace detail {
34 template<typename InputIterator>
35 wchar_t decode_utf8_mb(InputIterator &iter, InputIterator last)
37 if (iter == last) throw std::runtime_error("incomplete UTF-8 sequence");
38 if (((*iter) & 0xc0) != 0x80) throw std::runtime_error("invalid UTF-8 sequence");
40 return (wchar_t)((*iter++) & 0x3f);
43 template<typename InputIterator>
44 wchar_t decode_utf8(InputIterator &iter, InputIterator last)
46 wchar_t ret;
48 if (((*iter) & 0x80) == 0) // one byte
50 ret = *iter++;
52 else if (((*iter) & 0xe0) == 0xc0) // two bytes
54 wchar_t byte1 = (*iter++) & 0x1f;
55 wchar_t byte2 = decode_utf8_mb(iter, last);
56 ret = (byte1 << 6) | byte2;
58 else if (((*iter) & 0xf0) == 0xe0) // three bytes
60 wchar_t byte1 = (*iter++) & 0x0f;
61 wchar_t byte2 = decode_utf8_mb(iter, last);
62 wchar_t byte3 = decode_utf8_mb(iter, last);
63 ret = (byte1 << 12) | (byte2 << 6) | byte3;
65 // TODO: support surrogate pairs
66 else throw std::runtime_error("UTF-8 not convertable to UTF-16");
68 return ret;
71 template<typename InputIterator, typename OutputIterator>
72 OutputIterator utf8_wchar(InputIterator first, InputIterator last, OutputIterator dest)
74 for(; first!=last; ++dest)
75 *dest = decode_utf8(first, last);
76 return dest;
79 template<typename InputIterator, typename OutputIterator>
80 void encode_wchar(InputIterator iter, OutputIterator &dest)
82 if(*iter <= 0x007F)
84 *dest=(char)*iter;
85 ++dest;
87 else if(*iter <= 0x07FF)
89 *dest = (char)(
90 0xC0 |
91 ((*iter & 0x07C0) >> 6)
93 ++dest;
95 *dest = (char)(
96 0x80 |
97 (*iter & 0x003F)
99 ++dest;
101 else if(*iter <= 0xFFFF)
103 *dest = (char)(
104 0xE0 |
105 ((*iter & 0xF000) >> 12)
107 ++dest;
109 *dest = (char)(
110 0x80 |
111 ((*iter & 0x0FC0) >> 6)
113 ++dest;
115 *dest = (char)(
116 0x80 |
117 (*iter & 0x003F)
119 ++dest;
123 template<typename InputIterator, typename OutputIterator>
124 OutputIterator wchar_utf8(InputIterator first, InputIterator last, OutputIterator dest)
126 for(; first!=last; ++first)
127 encode_wchar(first, dest);
128 return dest;
133 inline void utf8_wchar(const std::string &utf8, std::wstring &wide)
135 wide.clear();
136 detail::utf8_wchar(utf8.begin(), utf8.end(), std::back_inserter(wide));
139 inline std::wstring utf8_wchar(const std::string &str)
141 std::wstring ret;
142 utf8_wchar(str, ret);
143 return ret;
146 inline void wchar_utf8(const std::wstring &wide, std::string &utf8)
148 utf8.clear();
149 detail::wchar_utf8(wide.begin(), wide.end(), std::back_inserter(utf8));
152 inline std::string wchar_utf8(const std::wstring &str)
154 std::string ret;
155 wchar_utf8(str, ret);
156 return ret;
161 #endif