]>
git.saurik.com Git - wxWidgets.git/blob - contrib/src/stc/scintilla/src/UniConversion.cxx
1 // UniConversion.h - functions to handle UFT-8 and UCS-2 strings
2 // Copyright 1998-2000 by Neil Hodgson <neilh@scintilla.org>
3 // The License.txt file describes the conditions under which this software may be distributed.
7 #include "UniConversion.h"
9 unsigned int UTF8Length(const wchar_t *uptr
, unsigned int tlen
) {
11 for (unsigned int i
= 0; i
< tlen
&& uptr
[i
]; i
++) {
12 unsigned int uch
= uptr
[i
];
23 void UTF8FromUCS2(const wchar_t *uptr
, unsigned int tlen
, char *putf
, unsigned int len
) {
25 for (unsigned int i
= 0; i
< tlen
&& uptr
[i
]; i
++) {
26 unsigned int uch
= uptr
[i
];
28 putf
[k
++] = static_cast<char>(uch
);
29 } else if (uch
< 0x800) {
30 putf
[k
++] = static_cast<char>(0xC0 | (uch
>> 6));
31 putf
[k
++] = static_cast<char>(0x80 | (uch
& 0x3f));
33 putf
[k
++] = static_cast<char>(0xE0 | (uch
>> 12));
34 putf
[k
++] = static_cast<char>(0x80 | ((uch
>> 6) & 0x3f));
35 putf
[k
++] = static_cast<char>(0x80 | (uch
& 0x3f));
41 unsigned int UCS2Length(const char *s
, unsigned int len
) {
42 unsigned int ulen
= 0;
43 for (unsigned int i
=0;i
<len
;i
++) {
44 unsigned char ch
= static_cast<unsigned char>(s
[i
]);
45 if ((ch
< 0x80) || (ch
> (0x80 + 0x40)))
51 unsigned int UCS2FromUTF8(const char *s
, unsigned int len
, wchar_t *tbuf
, unsigned int tlen
) {
53 return ::MultiByteToWideChar(CP_UTF8
, 0, s
, len
, tbuf
, tlen
);
56 const unsigned char *us
= reinterpret_cast<const unsigned char *>(s
);
58 while ((i
<len
) && (ui
<tlen
)) {
59 unsigned char ch
= us
[i
++];
62 } else if (ch
< 0x80 + 0x40 + 0x20) {
63 tbuf
[ui
] = static_cast<wchar_t>((ch
& 0x1F) << 6);
65 tbuf
[ui
] = static_cast<wchar_t>(tbuf
[ui
] + (ch
& 0x7F));
67 tbuf
[ui
] = static_cast<wchar_t>((ch
& 0xF) << 12);
69 tbuf
[ui
] = static_cast<wchar_t>(tbuf
[ui
] + ((ch
& 0x7F) << 6));
71 tbuf
[ui
] = static_cast<wchar_t>(tbuf
[ui
] + (ch
& 0x7F));