-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.cc
143 lines (136 loc) · 4.35 KB
/
util.cc
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/* util.cc
* by Trinity Quirk <[email protected]>
* last updated 29 Nov 2019, 15:29:33 tquirk
*
* CuddlyGL OpenGL widget toolkit
* Copyright (C) 2019 Trinity Annabelle Quirk
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*
* This file contains some string conversion functions.
*
* Things to do
*
*/
#include "util.h"
/* We need to be able to convert from UTF-8 representation to
* actual Unicode code points and back. All network traffic
* should be in UTF-8, and we'll of course need to display things
* in whatever native font the user needs.
*
* Ref: http://www.cprogramming.com/tutorial/unicode.html
* Ref: https://www.cl.cam.ac.uk/~mgk25/unicode.html
*/
std::u32string ui::utf8tou32str(const std::string& str)
{
std::string::const_iterator i = str.begin();
std::u32string newstr;
uint32_t ch;
while (i != str.end())
{
if ((*i & 0xfe) == 0xfc)
{
ch = (*i & 0x01) << 30;
ch |= (*(++i) & 0x3f) << 24;
ch |= (*(++i) & 0x3f) << 18;
ch |= (*(++i) & 0x3f) << 12;
ch |= (*(++i) & 0x3f) << 6;
ch |= (*(++i) & 0x3f);
}
else if ((*i & 0xfc) == 0xf8)
{
ch = (*i & 0x03) << 24;
ch |= (*(++i) & 0x3f) << 18;
ch |= (*(++i) & 0x3f) << 12;
ch |= (*(++i) & 0x3f) << 6;
ch |= (*(++i) & 0x3f);
}
else if ((*i & 0xf8) == 0xf0)
{
ch = (*i & 0x07) << 18;
ch |= (*(++i) & 0x3f) << 12;
ch |= (*(++i) & 0x3f) << 6;
ch |= (*(++i) & 0x3f);
}
else if ((*i & 0xf0) == 0xe0)
{
ch = (*i & 0x0f) << 12;
ch |= (*(++i) & 0x3f) << 6;
ch |= (*(++i) & 0x3f);
}
else if ((*i & 0xe0) == 0xc0)
{
ch = (*i & 0x1f) << 6;
ch |= (*(++i) & 0x3f);
}
else if ((*i & 0x80) == 0x00)
ch = *i;
else
ch = 0xfffd;
newstr.push_back(ch);
++i;
}
return newstr;
}
std::string ui::u32strtoutf8(const std::u32string& str)
{
std::u32string::const_iterator i = str.begin();
std::string newstr;
while (i != str.end())
{
if (*i & 0x7c000000)
{
newstr.push_back(0xfc | ((*i & 0x40000000) >> 30));
newstr.push_back(0x80 | ((*i & 0x3f000000) >> 24));
newstr.push_back(0x80 | ((*i & 0xfc0000) >> 18));
newstr.push_back(0x80 | ((*i & 0x3f000) >> 12));
newstr.push_back(0x80 | ((*i & 0xfc0) >> 6));
newstr.push_back(0x80 | (*i & 0x3f));
}
else if (*i & 0x3e00000)
{
newstr.push_back(0xf8 | ((*i & 0x3000000) >> 24));
newstr.push_back(0x80 | ((*i & 0xfc0000) >> 18));
newstr.push_back(0x80 | ((*i & 0x3f000) >> 12));
newstr.push_back(0x80 | ((*i & 0xfc0) >> 6));
newstr.push_back(0x80 | (*i & 0x3f));
}
else if (*i & 0x1f0000)
{
newstr.push_back(0xf0 | ((*i & 0x1c0000) >> 18));
newstr.push_back(0x80 | ((*i & 0x3f000) >> 12));
newstr.push_back(0x80 | ((*i & 0xfc0) >> 6));
newstr.push_back(0x80 | (*i & 0x3f));
}
else if (*i & 0xf800)
{
newstr.push_back(0xe0 | ((*i & 0x1f000) >> 12));
newstr.push_back(0x80 | ((*i & 0xfc0) >> 6));
newstr.push_back(0x80 | (*i & 0x3f));
}
else if (*i & 0x780)
{
newstr.push_back(0xc0 | ((*i & 0x7c0) >> 6));
newstr.push_back(0x80 | (*i & 0x3f));
}
else
{
newstr.push_back(*i & 0x7f);
}
++i;
}
return newstr;
}