]> git.saurik.com Git - wxWidgets.git/blob - contrib/src/stc/scintilla/src/UniConversion.cxx
MSVC fixes for wxEditableListBox (still misteriously gives assertion failure when...
[wxWidgets.git] / 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.
4
5 #include <stdlib.h>
6
7 #include "UniConversion.h"
8
9 unsigned int UTF8Length(const wchar_t *uptr, unsigned int tlen) {
10 unsigned int len = 0;
11 for (unsigned int i = 0; i < tlen && uptr[i]; i++) {
12 unsigned int uch = uptr[i];
13 if (uch < 0x80)
14 len++;
15 else if (uch < 0x800)
16 len+=2;
17 else
18 len +=3;
19 }
20 return len;
21 }
22
23 void UTF8FromUCS2(const wchar_t *uptr, unsigned int tlen, char *putf, unsigned int len) {
24 int k = 0;
25 for (unsigned int i = 0; i < tlen && uptr[i]; i++) {
26 unsigned int uch = uptr[i];
27 if (uch < 0x80) {
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));
32 } else {
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));
36 }
37 }
38 putf[len] = '\0';
39 }
40
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)))
46 ulen++;
47 }
48 return ulen;
49 }
50
51 unsigned int UCS2FromUTF8(const char *s, unsigned int len, wchar_t *tbuf, unsigned int tlen) {
52 #ifdef USE_API
53 return ::MultiByteToWideChar(CP_UTF8, 0, s, len, tbuf, tlen);
54 #else
55 unsigned int ui=0;
56 const unsigned char *us = reinterpret_cast<const unsigned char *>(s);
57 unsigned int i=0;
58 while ((i<len) && (ui<tlen)) {
59 unsigned char ch = us[i++];
60 if (ch < 0x80) {
61 tbuf[ui] = ch;
62 } else if (ch < 0x80 + 0x40 + 0x20) {
63 tbuf[ui] = static_cast<wchar_t>((ch & 0x1F) << 6);
64 ch = us[i++];
65 tbuf[ui] = static_cast<wchar_t>(tbuf[ui] + (ch & 0x7F));
66 } else {
67 tbuf[ui] = static_cast<wchar_t>((ch & 0xF) << 12);
68 ch = us[i++];
69 tbuf[ui] = static_cast<wchar_t>(tbuf[ui] + ((ch & 0x7F) << 6));
70 ch = us[i++];
71 tbuf[ui] = static_cast<wchar_t>(tbuf[ui] + (ch & 0x7F));
72 }
73 ui++;
74 }
75 return ui;
76 #endif
77 }