~ubuntu-branches/ubuntu/maverick/libtorrent-rasterbar/maverick

« back to all changes in this revision

Viewing changes to include/libtorrent/utf8.hpp

  • Committer: Bazaar Package Importer
  • Author(s): Cristian Greco
  • Date: 2008-07-02 10:46:21 UTC
  • Revision ID: james.westby@ubuntu.com-20080702104621-jzx3pfke9lkcxfxn
Tags: upstream-0.13.1
ImportĀ upstreamĀ versionĀ 0.13.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
        Copyright (C) 2004-2005 Cory Nelson
 
3
 
 
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.
 
7
 
 
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:
 
11
 
 
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.
 
19
*/
 
20
 
 
21
// namespaces added by Arvid Norberg
 
22
 
 
23
#ifndef __UTF8_H__
 
24
#define __UTF8_H__
 
25
 
 
26
#include <string>
 
27
#include <iterator>
 
28
#include <stdexcept>
 
29
#include <cwchar>
 
30
 
 
31
namespace libtorrent {
 
32
namespace detail {
 
33
 
 
34
template<typename InputIterator>
 
35
wchar_t decode_utf8_mb(InputIterator &iter, InputIterator last)
 
36
{
 
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");
 
39
 
 
40
        return (wchar_t)((*iter++) & 0x3f);
 
41
}
 
42
 
 
43
template<typename InputIterator>
 
44
wchar_t decode_utf8(InputIterator &iter, InputIterator last)
 
45
{
 
46
        wchar_t ret;
 
47
 
 
48
        if (((*iter) & 0x80) == 0) // one byte
 
49
        {
 
50
                ret = *iter++;
 
51
        }
 
52
        else if (((*iter) & 0xe0) == 0xc0) // two bytes
 
53
        {
 
54
                wchar_t byte1 = (*iter++) & 0x1f;
 
55
                wchar_t byte2 = decode_utf8_mb(iter, last);
 
56
                ret = (byte1 << 6) | byte2;
 
57
        }
 
58
        else if (((*iter) & 0xf0) == 0xe0) // three bytes
 
59
        {
 
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;
 
64
        }
 
65
        // TODO: support surrogate pairs
 
66
        else throw std::runtime_error("UTF-8 not convertable to UTF-16");
 
67
 
 
68
        return ret;
 
69
}
 
70
 
 
71
template<typename InputIterator, typename OutputIterator>
 
72
OutputIterator utf8_wchar(InputIterator first, InputIterator last, OutputIterator dest)
 
73
{
 
74
        for(; first!=last; ++dest)
 
75
                *dest = decode_utf8(first, last);
 
76
        return dest;
 
77
}
 
78
 
 
79
template<typename InputIterator, typename OutputIterator>
 
80
void encode_wchar(InputIterator iter, OutputIterator &dest)
 
81
{
 
82
        if(*iter <= 0x007F)
 
83
        {
 
84
                *dest=(char)*iter;
 
85
                ++dest;
 
86
        }
 
87
        else if(*iter <= 0x07FF)
 
88
        {
 
89
                *dest = (char)(
 
90
                        0xC0 |
 
91
                        ((*iter & 0x07C0) >> 6)
 
92
                );
 
93
                ++dest;
 
94
 
 
95
                *dest = (char)(
 
96
                        0x80 |
 
97
                        (*iter & 0x003F)
 
98
                );
 
99
                ++dest;
 
100
        }
 
101
        else if(*iter <= 0xFFFF)
 
102
        {
 
103
                *dest = (char)(
 
104
                        0xE0 |
 
105
                        ((*iter & 0xF000) >> 12)
 
106
                );
 
107
                ++dest;
 
108
 
 
109
                *dest = (char)(
 
110
                        0x80 |
 
111
                        ((*iter & 0x0FC0) >> 6)
 
112
                );
 
113
                ++dest;
 
114
 
 
115
                *dest = (char)(
 
116
                        0x80 |
 
117
                        (*iter & 0x003F)
 
118
                );
 
119
                ++dest;
 
120
        }
 
121
}
 
122
 
 
123
template<typename InputIterator, typename OutputIterator>
 
124
OutputIterator wchar_utf8(InputIterator first, InputIterator last, OutputIterator dest)
 
125
{
 
126
        for(; first!=last; ++first)
 
127
                encode_wchar(first, dest);
 
128
        return dest;
 
129
}
 
130
 
 
131
}
 
132
 
 
133
inline void utf8_wchar(const std::string &utf8, std::wstring &wide)
 
134
{
 
135
        wide.clear();
 
136
        detail::utf8_wchar(utf8.begin(), utf8.end(), std::back_inserter(wide));
 
137
}
 
138
 
 
139
inline std::wstring utf8_wchar(const std::string &str)
 
140
{
 
141
        std::wstring ret;
 
142
        utf8_wchar(str, ret);
 
143
        return ret;
 
144
}
 
145
 
 
146
inline void wchar_utf8(const std::wstring &wide, std::string &utf8)
 
147
{
 
148
        utf8.clear();
 
149
        detail::wchar_utf8(wide.begin(), wide.end(), std::back_inserter(utf8));
 
150
}
 
151
 
 
152
inline std::string wchar_utf8(const std::wstring &str)
 
153
{
 
154
        std::string ret;
 
155
        wchar_utf8(str, ret);
 
156
        return ret;
 
157
}
 
158
 
 
159
}
 
160
 
 
161
#endif