Add an UI to enable/disable specific overlay handlers.
[TortoiseGit.git] / ext / scintilla / src / UniConversion.cxx
blobf9ffb4dad0a6c879a6f31f578704028fff11851d
1 // Scintilla source code edit control
2 /** @file UniConversion.cxx
3 ** Functions to handle UFT-8 and UCS-2 strings.
4 **/
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.
8 #include <stdlib.h>
10 #include "UniConversion.h"
12 enum { SURROGATE_LEAD_FIRST = 0xD800 };
13 enum { SURROGATE_TRAIL_FIRST = 0xDC00 };
14 enum { SURROGATE_TRAIL_LAST = 0xDFFF };
16 unsigned int UTF8Length(const wchar_t *uptr, unsigned int tlen) {
17 unsigned int len = 0;
18 for (unsigned int i = 0; i < tlen && uptr[i];) {
19 unsigned int uch = uptr[i];
20 if (uch < 0x80) {
21 len++;
22 } else if (uch < 0x800) {
23 len += 2;
24 } else if ((uch >= SURROGATE_LEAD_FIRST) &&
25 (uch <= SURROGATE_TRAIL_LAST)) {
26 len += 4;
27 i++;
28 } else {
29 len += 3;
31 i++;
33 return len;
36 void UTF8FromUTF16(const wchar_t *uptr, unsigned int tlen, char *putf, unsigned int len) {
37 int k = 0;
38 for (unsigned int i = 0; i < tlen && uptr[i];) {
39 unsigned int uch = uptr[i];
40 if (uch < 0x80) {
41 putf[k++] = static_cast<char>(uch);
42 } else if (uch < 0x800) {
43 putf[k++] = static_cast<char>(0xC0 | (uch >> 6));
44 putf[k++] = static_cast<char>(0x80 | (uch & 0x3f));
45 } else if ((uch >= SURROGATE_LEAD_FIRST) &&
46 (uch <= SURROGATE_TRAIL_LAST)) {
47 // Half a surrogate pair
48 i++;
49 unsigned int xch = 0x10000 + ((uch & 0x3ff) << 10) + (uptr[i] & 0x3ff);
50 putf[k++] = static_cast<char>(0xF0 | (xch >> 18));
51 putf[k++] = static_cast<char>(0x80 | (xch >> 12) & 0x3f);
52 putf[k++] = static_cast<char>(0x80 | ((xch >> 6) & 0x3f));
53 putf[k++] = static_cast<char>(0x80 | (xch & 0x3f));
54 } else {
55 putf[k++] = static_cast<char>(0xE0 | (uch >> 12));
56 putf[k++] = static_cast<char>(0x80 | ((uch >> 6) & 0x3f));
57 putf[k++] = static_cast<char>(0x80 | (uch & 0x3f));
59 i++;
61 putf[len] = '\0';
64 unsigned int UTF16Length(const char *s, unsigned int len) {
65 unsigned int ulen = 0;
66 unsigned int charLen;
67 for (unsigned int i=0;i<len;) {
68 unsigned char ch = static_cast<unsigned char>(s[i]);
69 if (ch < 0x80) {
70 charLen = 1;
71 } else if (ch < 0x80 + 0x40 + 0x20) {
72 charLen = 2;
73 } else if (ch < 0x80 + 0x40 + 0x20 + 0x10) {
74 charLen = 3;
75 } else {
76 charLen = 4;
77 ulen++;
79 i += charLen;
80 ulen++;
82 return ulen;
85 unsigned int UTF16FromUTF8(const char *s, unsigned int len, wchar_t *tbuf, unsigned int tlen) {
86 unsigned int ui=0;
87 const unsigned char *us = reinterpret_cast<const unsigned char *>(s);
88 unsigned int i=0;
89 while ((i<len) && (ui<tlen)) {
90 unsigned char ch = us[i++];
91 if (ch < 0x80) {
92 tbuf[ui] = ch;
93 } else if (ch < 0x80 + 0x40 + 0x20) {
94 tbuf[ui] = static_cast<wchar_t>((ch & 0x1F) << 6);
95 ch = us[i++];
96 tbuf[ui] = static_cast<wchar_t>(tbuf[ui] + (ch & 0x7F));
97 } else if (ch < 0x80 + 0x40 + 0x20 + 0x10) {
98 tbuf[ui] = static_cast<wchar_t>((ch & 0xF) << 12);
99 ch = us[i++];
100 tbuf[ui] = static_cast<wchar_t>(tbuf[ui] + ((ch & 0x7F) << 6));
101 ch = us[i++];
102 tbuf[ui] = static_cast<wchar_t>(tbuf[ui] + (ch & 0x7F));
103 } else {
104 // Outside the BMP so need two surrogates
105 int val = (ch & 0x7) << 18;
106 ch = us[i++];
107 val += (ch & 0x3F) << 12;
108 ch = us[i++];
109 val += (ch & 0x3F) << 6;
110 ch = us[i++];
111 val += (ch & 0x3F);
112 tbuf[ui] = static_cast<wchar_t>(((val - 0x10000) >> 10) + SURROGATE_LEAD_FIRST);
113 ui++;
114 tbuf[ui] = static_cast<wchar_t>((val & 0x3ff) + SURROGATE_TRAIL_FIRST);
116 ui++;
118 return ui;