@@ -146,7 +146,43 @@ std::wstring widen(const std::string &s)
146
146
147
147
/* ******************************************************************\
148
148
149
- Function:
149
+ Function: utf32_to_utf8
150
+
151
+ Inputs:
152
+
153
+ Outputs:
154
+
155
+ Purpose:
156
+
157
+ \*******************************************************************/
158
+
159
+ void utf32_to_utf8 (unsigned int c, std::string &result)
160
+ {
161
+ if (c<=0x7f )
162
+ result+=char (c);
163
+ else if (c<=0x7ff )
164
+ {
165
+ result+=char ((c >> 6 ) | 0xc0 );
166
+ result+=char ((c & 0x3f ) | 0x80 );
167
+ }
168
+ else if (c<=0xffff )
169
+ {
170
+ result+=char ((c >> 12 ) | 0xe0 );
171
+ result+=char (((c >> 6 ) & 0x3f ) | 0x80 );
172
+ result+=char ((c & 0x3f ) | 0x80 );
173
+ }
174
+ else
175
+ {
176
+ result+=char ((c >> 18 ) | 0xf0 );
177
+ result+=char (((c >> 12 ) & 0x3f )| 0x80 );
178
+ result+=char (((c >> 6 ) & 0x3f ) | 0x80 );
179
+ result+=char ((c & 0x3f ) | 0x80 );
180
+ }
181
+ }
182
+
183
+ /* ******************************************************************\
184
+
185
+ Function: utf32_to_utf8
150
186
151
187
Inputs:
152
188
@@ -162,34 +198,32 @@ std::string utf32_to_utf8(const std::basic_string<unsigned int> &s)
162
198
163
199
result.reserve (s.size ()); // at least that long
164
200
165
- for (std::basic_string<unsigned int >::const_iterator
166
- it=s.begin ();
167
- it!=s.end ();
168
- it++)
169
- {
170
- unsigned int c=*it;
201
+ for (const auto it : s)
202
+ utf32_to_utf8 (it, result);
203
+
204
+ return result;
205
+ }
206
+
207
+ /* ******************************************************************\
208
+
209
+ Function: utf16_to_utf8
210
+
211
+ Inputs:
212
+
213
+ Outputs:
214
+
215
+ Purpose:
216
+
217
+ \*******************************************************************/
218
+
219
+ std::string utf16_to_utf8 (const std::basic_string<unsigned short int > &s)
220
+ {
221
+ std::string result;
171
222
172
- if (c<=0x7f )
173
- result+=char (c);
174
- else if (c<=0x7ff )
175
- {
176
- result+=char ((c >> 6 ) | 0xc0 );
177
- result+=char ((c & 0x3f ) | 0x80 );
178
- }
179
- else if (c<=0xffff )
180
- {
181
- result+=char ((c >> 12 ) | 0xe0 );
182
- result+=char (((c >> 6 ) & 0x3f ) | 0x80 );
183
- result+=char ((c & 0x3f ) | 0x80 );
184
- }
185
- else
186
- {
187
- result+=char ((c >> 18 ) | 0xf0 );
188
- result+=char (((c >> 12 ) & 0x3f )| 0x80 );
189
- result+=char (((c >> 6 ) & 0x3f ) | 0x80 );
190
- result+=char ((c & 0x3f ) | 0x80 );
191
- }
192
- }
223
+ result.reserve (s.size ()); // at least that long
224
+
225
+ for (const auto it : s)
226
+ utf32_to_utf8 (it, result);
193
227
194
228
return result;
195
229
}
0 commit comments