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.
10 #include "UniConversion.h"
13 using namespace Scintilla
;
20 enum { SURROGATE_LEAD_FIRST
= 0xD800 };
21 enum { SURROGATE_TRAIL_FIRST
= 0xDC00 };
22 enum { SURROGATE_TRAIL_LAST
= 0xDFFF };
24 unsigned int UTF8Length(const wchar_t *uptr
, unsigned int tlen
) {
26 for (unsigned int i
= 0; i
< tlen
&& uptr
[i
];) {
27 unsigned int uch
= uptr
[i
];
30 } else if (uch
< 0x800) {
32 } else if ((uch
>= SURROGATE_LEAD_FIRST
) &&
33 (uch
<= SURROGATE_TRAIL_LAST
)) {
44 void UTF8FromUTF16(const wchar_t *uptr
, unsigned int tlen
, char *putf
, unsigned int len
) {
46 for (unsigned int i
= 0; i
< tlen
&& uptr
[i
];) {
47 unsigned int uch
= uptr
[i
];
49 putf
[k
++] = static_cast<char>(uch
);
50 } else if (uch
< 0x800) {
51 putf
[k
++] = static_cast<char>(0xC0 | (uch
>> 6));
52 putf
[k
++] = static_cast<char>(0x80 | (uch
& 0x3f));
53 } else if ((uch
>= SURROGATE_LEAD_FIRST
) &&
54 (uch
<= SURROGATE_TRAIL_LAST
)) {
55 // Half a surrogate pair
57 unsigned int xch
= 0x10000 + ((uch
& 0x3ff) << 10) + (uptr
[i
] & 0x3ff);
58 putf
[k
++] = static_cast<char>(0xF0 | (xch
>> 18));
59 putf
[k
++] = static_cast<char>(0x80 | ((xch
>> 12) & 0x3f));
60 putf
[k
++] = static_cast<char>(0x80 | ((xch
>> 6) & 0x3f));
61 putf
[k
++] = static_cast<char>(0x80 | (xch
& 0x3f));
63 putf
[k
++] = static_cast<char>(0xE0 | (uch
>> 12));
64 putf
[k
++] = static_cast<char>(0x80 | ((uch
>> 6) & 0x3f));
65 putf
[k
++] = static_cast<char>(0x80 | (uch
& 0x3f));
72 unsigned int UTF8CharLength(unsigned char ch
) {
75 } else if (ch
< 0x80 + 0x40 + 0x20) {
77 } else if (ch
< 0x80 + 0x40 + 0x20 + 0x10) {
84 unsigned int UTF16Length(const char *s
, unsigned int len
) {
85 unsigned int ulen
= 0;
87 for (unsigned int i
=0; i
<len
;) {
88 unsigned char ch
= static_cast<unsigned char>(s
[i
]);
91 } else if (ch
< 0x80 + 0x40 + 0x20) {
93 } else if (ch
< 0x80 + 0x40 + 0x20 + 0x10) {
105 unsigned int UTF16FromUTF8(const char *s
, unsigned int len
, wchar_t *tbuf
, unsigned int tlen
) {
107 const unsigned char *us
= reinterpret_cast<const unsigned char *>(s
);
109 while ((i
<len
) && (ui
<tlen
)) {
110 unsigned char ch
= us
[i
++];
113 } else if (ch
< 0x80 + 0x40 + 0x20) {
114 tbuf
[ui
] = static_cast<wchar_t>((ch
& 0x1F) << 6);
116 tbuf
[ui
] = static_cast<wchar_t>(tbuf
[ui
] + (ch
& 0x7F));
117 } else if (ch
< 0x80 + 0x40 + 0x20 + 0x10) {
118 tbuf
[ui
] = static_cast<wchar_t>((ch
& 0xF) << 12);
120 tbuf
[ui
] = static_cast<wchar_t>(tbuf
[ui
] + ((ch
& 0x7F) << 6));
122 tbuf
[ui
] = static_cast<wchar_t>(tbuf
[ui
] + (ch
& 0x7F));
124 // Outside the BMP so need two surrogates
125 int val
= (ch
& 0x7) << 18;
127 val
+= (ch
& 0x3F) << 12;
129 val
+= (ch
& 0x3F) << 6;
132 tbuf
[ui
] = static_cast<wchar_t>(((val
- 0x10000) >> 10) + SURROGATE_LEAD_FIRST
);
134 tbuf
[ui
] = static_cast<wchar_t>((val
& 0x3ff) + SURROGATE_TRAIL_FIRST
);
141 int UTF8BytesOfLead
[256];
142 static bool initialisedBytesOfLead
= false;
144 static int BytesFromLead(int leadByte
) {
145 if (leadByte
< 0xC2) {
146 // Single byte or invalid
148 } else if (leadByte
< 0xE0) {
150 } else if (leadByte
< 0xF0) {
152 } else if (leadByte
< 0xF5) {
155 // Characters longer than 4 bytes not possible in current UTF-8
160 void UTF8BytesOfLeadInitialise() {
161 if (!initialisedBytesOfLead
) {
162 for (int i
=0; i
<256; i
++) {
163 UTF8BytesOfLead
[i
] = BytesFromLead(i
);
165 initialisedBytesOfLead
= true;
169 // Return both the width of the first character in the string and a status
170 // saying whether it is valid or invalid.
171 // Most invalid sequences return a width of 1 so are treated as isolated bytes but
172 // the non-characters *FFFE, *FFFF and FDD0 .. FDEF return 3 or 4 as they can be
173 // reasonably treated as code points in some circumstances. They will, however,
174 // not have associated glyphs.
175 int UTF8Classify(const unsigned char *us
, int len
) {
176 // For the rules: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
180 } else if (*us
> 0xf4) {
181 // Characters longer than 4 bytes not possible in current UTF-8
182 return UTF8MaskInvalid
| 1;
183 } else if (*us
>= 0xf0) {
186 return UTF8MaskInvalid
| 1;
187 if (UTF8IsTrailByte(us
[1]) && UTF8IsTrailByte(us
[2]) && UTF8IsTrailByte(us
[3])) {
188 if (((us
[1] & 0xf) == 0xf) && (us
[2] == 0xbf) && ((us
[3] == 0xbe) || (us
[3] == 0xbf))) {
189 // *FFFE or *FFFF non-character
190 return UTF8MaskInvalid
| 4;
193 // Check if encoding a value beyond the last Unicode character 10FFFF
195 return UTF8MaskInvalid
| 1;
196 } else if (us
[1] == 0x8f) {
198 return UTF8MaskInvalid
| 1;
199 } else if (us
[2] == 0xbf) {
201 return UTF8MaskInvalid
| 1;
205 } else if ((*us
== 0xf0) && ((us
[1] & 0xf0) == 0x80)) {
207 return UTF8MaskInvalid
| 1;
211 return UTF8MaskInvalid
| 1;
213 } else if (*us
>= 0xe0) {
216 return UTF8MaskInvalid
| 1;
217 if (UTF8IsTrailByte(us
[1]) && UTF8IsTrailByte(us
[2])) {
218 if ((*us
== 0xe0) && ((us
[1] & 0xe0) == 0x80)) {
220 return UTF8MaskInvalid
| 1;
222 if ((*us
== 0xed) && ((us
[1] & 0xe0) == 0xa0)) {
224 return UTF8MaskInvalid
| 1;
226 if ((*us
== 0xef) && (us
[1] == 0xbf) && (us
[2] == 0xbe)) {
227 // U+FFFE non-character - 3 bytes long
228 return UTF8MaskInvalid
| 3;
230 if ((*us
== 0xef) && (us
[1] == 0xbf) && (us
[2] == 0xbf)) {
231 // U+FFFF non-character - 3 bytes long
232 return UTF8MaskInvalid
| 3;
234 if ((*us
== 0xef) && (us
[1] == 0xb7) && (((us
[2] & 0xf0) == 0x90) || ((us
[2] & 0xf0) == 0xa0))) {
236 return UTF8MaskInvalid
| 3;
240 return UTF8MaskInvalid
| 1;
242 } else if (*us
>= 0xc2) {
245 return UTF8MaskInvalid
| 1;
246 if (UTF8IsTrailByte(us
[1])) {
249 return UTF8MaskInvalid
| 1;
252 // 0xc0 .. 0xc1 is overlong encoding
253 // 0x80 .. 0xbf is trail byte
254 return UTF8MaskInvalid
| 1;
258 int UTF8DrawBytes(const unsigned char *us
, int len
) {
259 int utf8StatusNext
= UTF8Classify(us
, len
);
260 return (utf8StatusNext
& UTF8MaskInvalid
) ? 1 : (utf8StatusNext
& UTF8MaskWidth
);