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
31 namespace libtorrent
{
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
)
48 if (((*iter
) & 0x80) == 0) // one byte
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");
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
);
79 template<typename InputIterator
, typename OutputIterator
>
80 void encode_wchar(InputIterator iter
, OutputIterator
&dest
)
87 else if(*iter
<= 0x07FF)
91 ((*iter
& 0x07C0) >> 6)
101 else if(*iter
<= 0xFFFF)
105 ((*iter
& 0xF000) >> 12)
111 ((*iter
& 0x0FC0) >> 6)
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
);
133 inline void utf8_wchar(const std::string
&utf8
, std::wstring
&wide
)
136 detail::utf8_wchar(utf8
.begin(), utf8
.end(), std::back_inserter(wide
));
139 inline std::wstring
utf8_wchar(const std::string
&str
)
142 utf8_wchar(str
, ret
);
146 inline void wchar_utf8(const std::wstring
&wide
, std::string
&utf8
)
149 detail::wchar_utf8(wide
.begin(), wide
.end(), std::back_inserter(utf8
));
152 inline std::string
wchar_utf8(const std::wstring
&str
)
155 wchar_utf8(str
, ret
);