forked from yuryfdr/pbtk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert.cxx
More file actions
122 lines (104 loc) · 3.07 KB
/
convert.cxx
File metadata and controls
122 lines (104 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* Copyright (C) 2009 AI */
/* Copyright (C) 2011 Yury P. Fedorchenko (yuryfdr at users.sf.net) */
/*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "convert.h"
/* UTF-8
*
* Bits Pattern
* ---- -------
* 7 0xxxxxxx
* 11 110xxxxx 10xxxxxx
* 16 1110xxxx 10xxxxxx 10xxxxxx
* 21 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
* 26 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
* 32 111111xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
*/
enum {
Low6Bits = 0x3F, /* 00111111 */
High2Bits = 0xC0, /* 11000000 */
ByteMask = 0x00BF, /* 10111111 */
ContinueBits = 0x80 /* 10xxxxxx */
};
void to_utf8(const char *from, char *to, const unsigned short *encoding)
{
unsigned int i, j;
for (i = 0, j = 0; i < strlen(from); i++) {
unsigned short ch = (unsigned char) from[i];
if (ch >= 0200) {
ch = encoding[ch - 0200];
}
if (ch < ContinueBits) {
to[j++] = ch;
} else if (ch < 0x800) {
to[j++] = (ch >> 6 | High2Bits);
to[j++] = ((ch & Low6Bits) | ContinueBits);
} else {
to[j++] = (ch >> 12 | 0xe0);
to[j++] = (((ch >> 6) & Low6Bits) | ContinueBits);
to[j++] = ((ch & Low6Bits) | ContinueBits);
}
}
to[j] = '\0';
}
void to_utf8(const char *from, std::string * to, const unsigned short *encoding)
{
if (from == 0) {
to->clear();
return;
}
char *buf = new char[strlen((const char *) from) * 3 + 1];
to_utf8(from, buf, encoding);
to->assign(buf);
delete[]buf;
}
std::string to_utf8(const char *from, const unsigned short *encoding)
{
std::string to;
if (from == 0) {
to.clear();
return to;
}
char *buf = new char[strlen(from) * 3 + 1];
to_utf8(from, buf, encoding);
to.assign(buf);
delete[]buf;
return to;
}
std::string utf8_to(const char *from, const unsigned short *encoding)
{
std::string to;
if (from == 0)
return to;
int len = strlen(from);
for (int i = 0; i < len; i++) {
char ch(0x20);
if ((unsigned char) from[i] < ContinueBits)
ch = (unsigned char) from[i];
else if (i < len - 1) {
wchar_t wch;
wch = (((unsigned char) from[i] & Low6Bits) << 6) | ((unsigned char) from[i + 1] & Low6Bits);
for (int c = 127; c >= 0; --c)
if (encoding[c] == wch) {
ch = (char) (c + ContinueBits);
i++;
break;
}
}
to.append(1, ch);
}
return to;
}