Debian Codegraph @

Frameworks/OpenVanilla/Headers/OVUTF8Helper.h

raw symbols SCIP
1 //
2 // OVUTF8Helper.h
3 //
4 // Copyright (c) 2004-2008 The OpenVanilla Project (http://openvanilla.org)
5 // All rights reserved.
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 //
11 // 1. Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // 2. Redistributions in binary form must reproduce the above copyright
14 // notice, this list of conditions and the following disclaimer in the
15 // documentation and/or other materials provided with the distribution.
16 // 3. Neither the name of OpenVanilla nor the names of its contributors
17 // may be used to endorse or promote products derived from this software
18 // without specific prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 // POSSIBILITY OF SUCH DAMAGE.
31
32 #ifndef OVUTF8Helper_h
33 #define OVUTF8Helper_h
34
35 #include <cstddef>
36 #include <string>
37 #include <vector>
38
39 #include <iostream>
40
41 namespace OpenVanilla {
42 using namespace std;
43
44 class OVUTF8Helper {
45 public:
46 static const vector<string> SplitStringByCodePoint(const string& utf8String)
47 {
48 size_t index, length = utf8String.length();
49 vector<string> result;
50
51 for (index = 0; index < length; index++) {
52 unsigned char c = utf8String[index];
53
54 if (!(c & 0x80)) {
55 result.push_back(utf8String.substr(index, 1));
56 }
57 else if ((c & (0x80 | 0x40)) && !(c & 0x20)) {
58 result.push_back(utf8String.substr(index, 2));
59 index += 1;
60 }
61 else if ((c & (0x80 | 0x40 | 0x20)) && !(c & 0x10)) {
62 result.push_back(utf8String.substr(index, 3));
63 index += 2;
64 }
65 else if ((c & (0x80 | 0x40 | 0x20 | 0x10)) && !(c & 0x8)) {
66 result.push_back(utf8String.substr(index, 4));
67 index += 3;
68 }
69 }
70
71 return result;
72 }
73
74 static const string CombineCodePoints(const vector<string>& codePoints)
75 {
76 string result;
77 for (vector<string>::const_iterator iter = codePoints.begin(); iter != codePoints.end() ; ++iter)
78 result += *iter;
79
80 return result;
81 }
82
83 static const wstring ConvertStringToUTF16WideString(const string& utf8String)
84 {
85 wstring wideString;
86 vector<string> charVector = OVUTF8Helper::SplitStringByCodePoint(utf8String);
87
88 size_t length = charVector.size();
89 for (size_t index = 0; index < length; index++)
90 {
91 unsigned int codePoint = OVUTF8Helper::CodePointFromSingleUTF8String(charVector[index]);
92
93 if (codePoint < 0x10000) {
94 wideString += (wchar_t)codePoint;
95 }
96 else {
97 const unsigned int leadOffset = 0xD800 - (0x10000 >> 10);
98 unsigned int lead = leadOffset + (codePoint >> 10);
99 unsigned int trail = 0xdc00 + (codePoint & 0x3ff);
100 wideString += (wchar_t)lead;
101 wideString += (wchar_t)trail;
102 }
103 }
104
105 return wideString;
106 }
107
108 static const string StringFromUTF16WideString(const wstring& utf16WideString)
109 {
110 string utf8String;
111 size_t length = utf16WideString.length();
112
113 unsigned int u16;
114 for (size_t index = 0; index < length; index++) {
115 u16 = utf16WideString[index];
116
117 if (u16 >= 0xd800 && u16 <= 0xdbff) {
118 if (index + 1 < length) {
119 unsigned int trailing = utf16WideString[index + 1];
120 if (trailing >= 0xdc00 && trailing <= 0xdfff) {
121 index++;
122
123 const unsigned int surrogateOffset = 0x10000 - (0xD800 << 10) - 0xDC00;
124 unsigned int codePoint = (u16 << 10) + trailing + surrogateOffset;
125 utf8String += OVUTF8Helper::SingleUTF8StringFromCodePoint(codePoint);
126 }
127 }
128 }
129 else if (!(u16 >= 0xdc00 && u16 <= 0xdfff))
130 {
131 utf8String += OVUTF8Helper::SingleUTF8StringFromCodePoint(u16);
132 }
133 }
134
135 return utf8String;
136 }
137
138 static const string SingleUTF8StringFromCodePoint(unsigned int codePoint)
139 {
140 string utf8String;
141
142 if (codePoint < 0x80) {
143 utf8String += (unsigned char)codePoint;
144 }
145 else if (codePoint < 0x800) {
146 utf8String += (unsigned char)((codePoint >> 6) | 0xc0);
147 utf8String += (unsigned char)((codePoint & 0x3f) | 0x80);
148 }
149 else if (codePoint < 0x10000) {
150 utf8String += (unsigned char)((codePoint >> 12) | 0xe0);
151 utf8String += (unsigned char)((codePoint >> 6) & 0x3f | 0x80);
152 utf8String += (unsigned char)((codePoint & 0x3f) | 0x80);
153 }
154 else {
155 utf8String += (unsigned char)((codePoint >> 18) | 0xf0);
156 utf8String += (unsigned char)((codePoint >> 12) | 0x80);
157 utf8String += (unsigned char)((codePoint >> 6) & 0x3f | 0x80);
158 utf8String += (unsigned char)((codePoint & 0x3f) | 0x80);
159 }
160
161 return utf8String;
162 }
163
164 static unsigned int CodePointFromSingleUTF8String(const string& utf8String)
165 {
166 switch(utf8String.length()) {
167 case 1:
168 return (unsigned int)utf8String[0];
169 case 2:
170 return (((unsigned char)utf8String[0] << 6) & 0x7ff) + ((unsigned char)utf8String[1] & 0x3f);
171 case 3:
172 return (((unsigned char)utf8String[0] << 12) & 0xffff) + (((unsigned char)utf8String[1] << 6) & 0xfff) + ((unsigned char)utf8String[2] & 0x3f);
173 case 4:
174 return (((unsigned char)utf8String[0] << 18) & 0x1fffff) + (((unsigned char)utf8String[1] << 12) & 0x3ffff) + (((unsigned char)utf8String[2] << 6) & 0xfff) + ((unsigned char)utf8String[3] & 0x3f);
175 }
176
177 return 0;
178 }
179 };
180
181 class OVUTF8 {
182 public:
183 static const string FromUTF16(const wstring& utf16String)
184 {
185 return OVUTF8Helper::StringFromUTF16WideString(utf16String);
186 }
187 };
188
189 class OVUTF16 {
190 public:
191 static const wstring FromUTF8(const string& utf8String)
192 {
193 return OVUTF8Helper::ConvertStringToUTF16WideString(utf8String);
194 }
195 };
196 };
197
198 #endif