~ubuntu-branches/debian/experimental/geany/experimental

« back to all changes in this revision

Viewing changes to scintilla/UniConversion.cxx

  • Committer: Bazaar Package Importer
  • Author(s): Damián Viano
  • Date: 2008-05-02 11:37:45 UTC
  • mfrom: (1.2.1 upstream) (3.1.6 hardy)
  • Revision ID: james.westby@ubuntu.com-20080502113745-xzp4g6dmovrpoj17
Tags: 0.14-1
New upstream release (Closes: #478126)

Show diffs side-by-side

added added

removed removed

Lines of Context:
9
9
 
10
10
#include "UniConversion.h"
11
11
 
 
12
enum { SURROGATE_LEAD_FIRST = 0xD800 };
 
13
enum { SURROGATE_TRAIL_FIRST = 0xDC00 };
 
14
enum { SURROGATE_TRAIL_LAST = 0xDFFF };
 
15
 
12
16
unsigned int UTF8Length(const wchar_t *uptr, unsigned int tlen) {
13
17
        unsigned int len = 0;
14
 
        for (unsigned int i = 0; i < tlen && uptr[i]; i++) {
 
18
        for (unsigned int i = 0; i < tlen && uptr[i];) {
15
19
                unsigned int uch = uptr[i];
16
 
                if (uch < 0x80)
 
20
                if (uch < 0x80) {
17
21
                        len++;
18
 
                else if (uch < 0x800)
 
22
                } else if (uch < 0x800) {
19
23
                        len += 2;
20
 
                else
21
 
                        len +=3;
 
24
                } else if ((uch >= SURROGATE_LEAD_FIRST) &&
 
25
                        (uch <= SURROGATE_TRAIL_LAST)) {
 
26
                        len += 4;
 
27
                        i++;
 
28
                } else {
 
29
                        len += 3;
 
30
                }
 
31
                i++;
22
32
        }
23
33
        return len;
24
34
}
25
35
 
26
 
void UTF8FromUCS2(const wchar_t *uptr, unsigned int tlen, char *putf, unsigned int len) {
 
36
void UTF8FromUTF16(const wchar_t *uptr, unsigned int tlen, char *putf, unsigned int len) {
27
37
        int k = 0;
28
 
        for (unsigned int i = 0; i < tlen && uptr[i]; i++) {
 
38
        for (unsigned int i = 0; i < tlen && uptr[i];) {
29
39
                unsigned int uch = uptr[i];
30
40
                if (uch < 0x80) {
31
41
                        putf[k++] = static_cast<char>(uch);
32
42
                } else if (uch < 0x800) {
33
43
                        putf[k++] = static_cast<char>(0xC0 | (uch >> 6));
34
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));
35
54
                } else {
36
55
                        putf[k++] = static_cast<char>(0xE0 | (uch >> 12));
37
56
                        putf[k++] = static_cast<char>(0x80 | ((uch >> 6) & 0x3f));
38
57
                        putf[k++] = static_cast<char>(0x80 | (uch & 0x3f));
39
58
                }
 
59
                i++;
40
60
        }
41
61
        putf[len] = '\0';
42
62
}
43
63
 
44
 
unsigned int UCS2Length(const char *s, unsigned int len) {
 
64
unsigned int UTF16Length(const char *s, unsigned int len) {
45
65
        unsigned int ulen = 0;
46
 
        for (unsigned int i=0;i<len;i++) {
 
66
        unsigned int charLen;
 
67
        for (unsigned int i=0;i<len;) {
47
68
                unsigned char ch = static_cast<unsigned char>(s[i]);
48
 
                if ((ch < 0x80) || (ch > (0x80 + 0x40)))
 
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;
49
77
                        ulen++;
 
78
                }
 
79
                i += charLen;
 
80
                ulen++;
50
81
        }
51
82
        return ulen;
52
83
}
53
84
 
54
 
unsigned int UCS2FromUTF8(const char *s, unsigned int len, wchar_t *tbuf, unsigned int tlen) {
 
85
unsigned int UTF16FromUTF8(const char *s, unsigned int len, wchar_t *tbuf, unsigned int tlen) {
55
86
        unsigned int ui=0;
56
87
        const unsigned char *us = reinterpret_cast<const unsigned char *>(s);
57
88
        unsigned int i=0;
63
94
                        tbuf[ui] = static_cast<wchar_t>((ch & 0x1F) << 6);
64
95
                        ch = us[i++];
65
96
                        tbuf[ui] = static_cast<wchar_t>(tbuf[ui] + (ch & 0x7F));
66
 
                } else {
 
97
                } else if (ch < 0x80 + 0x40 + 0x20 + 0x10) {
67
98
                        tbuf[ui] = static_cast<wchar_t>((ch & 0xF) << 12);
68
99
                        ch = us[i++];
69
100
                        tbuf[ui] = static_cast<wchar_t>(tbuf[ui] + ((ch & 0x7F) << 6));
70
101
                        ch = us[i++];
71
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);
72
115
                }
73
116
                ui++;
74
117
        }