]>
git.saurik.com Git - wxWidgets.git/blob - contrib/src/stc/scintilla/src/UniConversion.cxx
15cad6382969fb8070ac312b40547de47f3949bd
1 // Scintilla source code edit control
2 /** @file UniConversion.cxx
3 ** Functions to handle UFT-8 and UCS-2 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"
12 unsigned int UTF8Length(const wchar_t *uptr
, unsigned int tlen
) {
14 for (unsigned int i
= 0; i
< tlen
&& uptr
[i
]; i
++) {
15 unsigned int uch
= uptr
[i
];
26 void UTF8FromUCS2(const wchar_t *uptr
, unsigned int tlen
, char *putf
, unsigned int len
) {
28 for (unsigned int i
= 0; i
< tlen
&& uptr
[i
]; i
++) {
29 unsigned int uch
= uptr
[i
];
31 putf
[k
++] = static_cast<char>(uch
);
32 } else if (uch
< 0x800) {
33 putf
[k
++] = static_cast<char>(0xC0 | (uch
>> 6));
34 putf
[k
++] = static_cast<char>(0x80 | (uch
& 0x3f));
36 putf
[k
++] = static_cast<char>(0xE0 | (uch
>> 12));
37 putf
[k
++] = static_cast<char>(0x80 | ((uch
>> 6) & 0x3f));
38 putf
[k
++] = static_cast<char>(0x80 | (uch
& 0x3f));
44 unsigned int UCS2Length(const char *s
, unsigned int len
) {
45 unsigned int ulen
= 0;
46 for (unsigned int i
=0;i
<len
;i
++) {
47 unsigned char ch
= static_cast<unsigned char>(s
[i
]);
48 if ((ch
< 0x80) || (ch
> (0x80 + 0x40)))
54 unsigned int UCS2FromUTF8(const char *s
, unsigned int len
, wchar_t *tbuf
, unsigned int 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));