forked from programmerjake/preyproject-lock-screen
-
Notifications
You must be signed in to change notification settings - Fork 4
/
string_cast.h
187 lines (176 loc) · 5.99 KB
/
string_cast.h
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/* Copyright (c) 2015 Jacob Lifshay, Fork Ltd.
* This file is part of Prey.
*
* Prey 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 3 of the License, or
* (at your option) any later version.
*
* Prey 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 Prey. If not, see <http://www.gnu.org/licenses/>.
*/
// Original license header
/*
* Copyright (C) 2012-2015 Jacob R. Lifshay
* This file is part of Voxels.
*
* Voxels 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 of the License, or
* (at your option) any later version.
*
* Voxels 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 Voxels; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*/
#ifndef STRING_CAST_H_INCLUDED
#define STRING_CAST_H_INCLUDED
#include <cwchar>
#include <string>
#include <cstdint>
#include "wchar_bits.h"
template <typename T>
T string_cast(std::string);
template <typename T>
T string_cast(std::wstring);
template <>
inline std::string string_cast<std::string>(std::string str)
{
return str;
}
template <>
inline std::wstring string_cast<std::wstring>(std::wstring str)
{
return str;
}
template <>
inline std::string string_cast<std::string>(std::wstring wstr)
{
std::string retval;
for(size_t i = 0; i < wstr.size(); i++)
{
unsigned value = static_cast<unsigned>(static_cast<UNSIGNED_WCHAR>(wstr[i]));
#if WCHAR_BITS == 16
if(value >= 0xD800U && value <= 0xDBFFU && i + 1 < wstr.size())
{
unsigned nextValue = static_cast<unsigned>(static_cast<UNSIGNED_WCHAR>(wstr[i + 1]));
if(nextValue >= 0xDC00U && nextValue <= 0xDFFFU)
{
i++;
value -= 0xD800U;
nextValue -= 0xDC00U;
value <<= 10;
value += nextValue;
}
}
#endif
value &= 0x1FFFFFU;
if(value <= 0x7FU)
{
retval += (char)value;
}
else if(value <= 0x7FFU)
{
retval += (char)((value >> 6) | 0xC0U);
retval += (char)((value & 0x3FU) | 0x80U);
}
else if(value <= 0xFFFFU)
{
retval += (char)((value >> 12) | 0xE0U);
retval += (char)(((value >> 6) & 0x3FU) | 0x80U);
retval += (char)((value & 0x3FU) | 0x80U);
}
else
{
retval += (char)((value >> 18) | 0xF0U);
retval += (char)(((value >> 12) & 0x3FU) | 0x80U);
retval += (char)(((value >> 6) & 0x3FU) | 0x80U);
retval += (char)((value & 0x3FU) | 0x80U);
}
}
return retval;
}
template <>
inline std::wstring string_cast<std::wstring>(std::string str)
{
std::wstring retval;
for(size_t i = 0; i < str.size(); i++)
{
unsigned value = static_cast<unsigned char>(str[i]);
if((value & 0xF0) == 0xF0) // 4 or more byte
{
value &= 0x7;
if(i + 1 < str.size() && (static_cast<unsigned char>(str[i + 1]) & 0xC0) == 0x80)
{
i++;
value <<= 6;
value |= 0x3F & static_cast<unsigned char>(str[i]);
if(i + 1 < str.size() && (static_cast<unsigned char>(str[i + 1]) & 0xC0) == 0x80)
{
i++;
value <<= 6;
value |= 0x3F & static_cast<unsigned char>(str[i]);
if(i + 1 < str.size() && (static_cast<unsigned char>(str[i + 1]) & 0xC0) == 0x80)
{
i++;
value <<= 6;
value |= 0x3F & static_cast<unsigned char>(str[i]);
}
}
}
}
else if((value & 0xF0) == 0xE0) // 3 byte
{
value &= 0xF;
if(i + 1 < str.size() && (static_cast<unsigned char>(str[i + 1]) & 0xC0) == 0x80)
{
i++;
value <<= 6;
value |= 0x3F & static_cast<unsigned char>(str[i]);
if(i + 1 < str.size() && (static_cast<unsigned char>(str[i + 1]) & 0xC0) == 0x80)
{
i++;
value <<= 6;
value |= 0x3F & static_cast<unsigned char>(str[i]);
}
}
}
else if((value & 0xE0) == 0xC0) // 2 byte
{
value &= 0x1F;
if(i + 1 < str.size() && (static_cast<unsigned char>(str[i + 1]) & 0xC0) == 0x80)
{
i++;
value <<= 6;
value |= 0x3F & static_cast<unsigned char>(str[i]);
}
}
#if WCHAR_BITS == 16
if(value >= 0x10000U)
{
value -= 0x10000U;
value &= 0xFFFFFU;
retval += static_cast<wchar_t>((value >> 10) + 0xD800U);
retval += static_cast<wchar_t>((value & 0x3FF) + 0xDC00U);
}
else
#endif
{
retval += static_cast<wchar_t>(value);
}
}
return retval;
}
#endif // STRING_CAST_H_INCLUDED