1 // Scintilla source code edit control
2 /** @file UniConversion.cxx
3 ** Functions to handle UTF-8 and UTF-16 strings.
5 // Copyright 1998-2001 by Neil Hodgson <neilh@scintilla.org>
6 // The License.txt file describes the conditions under which this software may be distributed.
12 #include "UniConversion.h"
15 using namespace Scintilla
;
22 enum { SURROGATE_TRAIL_FIRST
= 0xDC00 };
23 enum { SURROGATE_TRAIL_LAST
= 0xDFFF };
24 enum { SUPPLEMENTAL_PLANE_FIRST
= 0x10000 };
26 unsigned int UTF8Length(const wchar_t *uptr
, unsigned int tlen
) {
28 for (unsigned int i
= 0; i
< tlen
&& uptr
[i
];) {
29 unsigned int uch
= uptr
[i
];
32 } else if (uch
< 0x800) {
34 } else if ((uch
>= SURROGATE_LEAD_FIRST
) &&
35 (uch
<= SURROGATE_TRAIL_LAST
)) {
46 void UTF8FromUTF16(const wchar_t *uptr
, unsigned int tlen
, char *putf
, unsigned int len
) {
48 for (unsigned int i
= 0; i
< tlen
&& uptr
[i
];) {
49 unsigned int uch
= uptr
[i
];
51 putf
[k
++] = static_cast<char>(uch
);
52 } else if (uch
< 0x800) {
53 putf
[k
++] = static_cast<char>(0xC0 | (uch
>> 6));
54 putf
[k
++] = static_cast<char>(0x80 | (uch
& 0x3f));
55 } else if ((uch
>= SURROGATE_LEAD_FIRST
) &&
56 (uch
<= SURROGATE_TRAIL_LAST
)) {
57 // Half a surrogate pair
59 unsigned int xch
= 0x10000 + ((uch
& 0x3ff) << 10) + (uptr
[i
] & 0x3ff);
60 putf
[k
++] = static_cast<char>(0xF0 | (xch
>> 18));
61 putf
[k
++] = static_cast<char>(0x80 | ((xch
>> 12) & 0x3f));
62 putf
[k
++] = static_cast<char>(0x80 | ((xch
>> 6) & 0x3f));
63 putf
[k
++] = static_cast<char>(0x80 | (xch
& 0x3f));
65 putf
[k
++] = static_cast<char>(0xE0 | (uch
>> 12));
66 putf
[k
++] = static_cast<char>(0x80 | ((uch
>> 6) & 0x3f));
67 putf
[k
++] = static_cast<char>(0x80 | (uch
& 0x3f));
75 unsigned int UTF8CharLength(unsigned char ch
) {
78 } else if (ch
< 0x80 + 0x40 + 0x20) {
80 } else if (ch
< 0x80 + 0x40 + 0x20 + 0x10) {
87 size_t UTF16Length(const char *s
, size_t len
) {
90 for (size_t i
= 0; i
<len
;) {
91 unsigned char ch
= static_cast<unsigned char>(s
[i
]);
94 } else if (ch
< 0x80 + 0x40 + 0x20) {
96 } else if (ch
< 0x80 + 0x40 + 0x20 + 0x10) {
108 size_t UTF16FromUTF8(const char *s
, size_t len
, wchar_t *tbuf
, size_t tlen
) {
110 const unsigned char *us
= reinterpret_cast<const unsigned char *>(s
);
112 while ((i
<len
) && (ui
<tlen
)) {
113 unsigned char ch
= us
[i
++];
116 } else if (ch
< 0x80 + 0x40 + 0x20) {
117 tbuf
[ui
] = static_cast<wchar_t>((ch
& 0x1F) << 6);
119 tbuf
[ui
] = static_cast<wchar_t>(tbuf
[ui
] + (ch
& 0x7F));
120 } else if (ch
< 0x80 + 0x40 + 0x20 + 0x10) {
121 tbuf
[ui
] = static_cast<wchar_t>((ch
& 0xF) << 12);
123 tbuf
[ui
] = static_cast<wchar_t>(tbuf
[ui
] + ((ch
& 0x7F) << 6));
125 tbuf
[ui
] = static_cast<wchar_t>(tbuf
[ui
] + (ch
& 0x7F));
127 // Outside the BMP so need two surrogates
128 int val
= (ch
& 0x7) << 18;
130 val
+= (ch
& 0x3F) << 12;
132 val
+= (ch
& 0x3F) << 6;
135 tbuf
[ui
] = static_cast<wchar_t>(((val
- 0x10000) >> 10) + SURROGATE_LEAD_FIRST
);
137 tbuf
[ui
] = static_cast<wchar_t>((val
& 0x3ff) + SURROGATE_TRAIL_FIRST
);
144 unsigned int UTF32FromUTF8(const char *s
, unsigned int len
, unsigned int *tbuf
, unsigned int tlen
) {
146 const unsigned char *us
= reinterpret_cast<const unsigned char *>(s
);
148 while ((i
<len
) && (ui
<tlen
)) {
149 unsigned char ch
= us
[i
++];
150 unsigned int value
= 0;
153 } else if (((len
-i
) >= 1) && (ch
< 0x80 + 0x40 + 0x20)) {
154 value
= (ch
& 0x1F) << 6;
157 } else if (((len
-i
) >= 2) && (ch
< 0x80 + 0x40 + 0x20 + 0x10)) {
158 value
= (ch
& 0xF) << 12;
160 value
+= (ch
& 0x7F) << 6;
163 } else if ((len
-i
) >= 3) {
164 value
= (ch
& 0x7) << 18;
166 value
+= (ch
& 0x3F) << 12;
168 value
+= (ch
& 0x3F) << 6;
178 unsigned int UTF16FromUTF32Character(unsigned int val
, wchar_t *tbuf
) {
179 if (val
< SUPPLEMENTAL_PLANE_FIRST
) {
180 tbuf
[0] = static_cast<wchar_t>(val
);
183 tbuf
[0] = static_cast<wchar_t>(((val
- SUPPLEMENTAL_PLANE_FIRST
) >> 10) + SURROGATE_LEAD_FIRST
);
184 tbuf
[1] = static_cast<wchar_t>((val
& 0x3ff) + SURROGATE_TRAIL_FIRST
);
189 int UTF8BytesOfLead
[256];
190 static bool initialisedBytesOfLead
= false;
192 static int BytesFromLead(int leadByte
) {
193 if (leadByte
< 0xC2) {
194 // Single byte or invalid
196 } else if (leadByte
< 0xE0) {
198 } else if (leadByte
< 0xF0) {
200 } else if (leadByte
< 0xF5) {
203 // Characters longer than 4 bytes not possible in current UTF-8
208 void UTF8BytesOfLeadInitialise() {
209 if (!initialisedBytesOfLead
) {
210 for (int i
=0; i
<256; i
++) {
211 UTF8BytesOfLead
[i
] = BytesFromLead(i
);
213 initialisedBytesOfLead
= true;
217 // Return both the width of the first character in the string and a status
218 // saying whether it is valid or invalid.
219 // Most invalid sequences return a width of 1 so are treated as isolated bytes but
220 // the non-characters *FFFE, *FFFF and FDD0 .. FDEF return 3 or 4 as they can be
221 // reasonably treated as code points in some circumstances. They will, however,
222 // not have associated glyphs.
223 int UTF8Classify(const unsigned char *us
, int len
) {
224 // For the rules: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
228 } else if (*us
> 0xf4) {
229 // Characters longer than 4 bytes not possible in current UTF-8
230 return UTF8MaskInvalid
| 1;
231 } else if (*us
>= 0xf0) {
234 return UTF8MaskInvalid
| 1;
235 if (UTF8IsTrailByte(us
[1]) && UTF8IsTrailByte(us
[2]) && UTF8IsTrailByte(us
[3])) {
236 if (((us
[1] & 0xf) == 0xf) && (us
[2] == 0xbf) && ((us
[3] == 0xbe) || (us
[3] == 0xbf))) {
237 // *FFFE or *FFFF non-character
238 return UTF8MaskInvalid
| 4;
241 // Check if encoding a value beyond the last Unicode character 10FFFF
243 return UTF8MaskInvalid
| 1;
244 } else if (us
[1] == 0x8f) {
246 return UTF8MaskInvalid
| 1;
247 } else if (us
[2] == 0xbf) {
249 return UTF8MaskInvalid
| 1;
253 } else if ((*us
== 0xf0) && ((us
[1] & 0xf0) == 0x80)) {
255 return UTF8MaskInvalid
| 1;
259 return UTF8MaskInvalid
| 1;
261 } else if (*us
>= 0xe0) {
264 return UTF8MaskInvalid
| 1;
265 if (UTF8IsTrailByte(us
[1]) && UTF8IsTrailByte(us
[2])) {
266 if ((*us
== 0xe0) && ((us
[1] & 0xe0) == 0x80)) {
268 return UTF8MaskInvalid
| 1;
270 if ((*us
== 0xed) && ((us
[1] & 0xe0) == 0xa0)) {
272 return UTF8MaskInvalid
| 1;
274 if ((*us
== 0xef) && (us
[1] == 0xbf) && (us
[2] == 0xbe)) {
275 // U+FFFE non-character - 3 bytes long
276 return UTF8MaskInvalid
| 3;
278 if ((*us
== 0xef) && (us
[1] == 0xbf) && (us
[2] == 0xbf)) {
279 // U+FFFF non-character - 3 bytes long
280 return UTF8MaskInvalid
| 3;
282 if ((*us
== 0xef) && (us
[1] == 0xb7) && (((us
[2] & 0xf0) == 0x90) || ((us
[2] & 0xf0) == 0xa0))) {
284 return UTF8MaskInvalid
| 3;
288 return UTF8MaskInvalid
| 1;
290 } else if (*us
>= 0xc2) {
293 return UTF8MaskInvalid
| 1;
294 if (UTF8IsTrailByte(us
[1])) {
297 return UTF8MaskInvalid
| 1;
300 // 0xc0 .. 0xc1 is overlong encoding
301 // 0x80 .. 0xbf is trail byte
302 return UTF8MaskInvalid
| 1;
306 int UTF8DrawBytes(const unsigned char *us
, int len
) {
307 int utf8StatusNext
= UTF8Classify(us
, len
);
308 return (utf8StatusNext
& UTF8MaskInvalid
) ? 1 : (utf8StatusNext
& UTF8MaskWidth
);