MCPcopy Create free account
hub / github.com/NativeScript/android / utf32_to_utf8

Function utf32_to_utf8

test-app/runtime/src/main/cpp/ada/ada.cpp:267–320  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

265}
266
267size_t utf32_to_utf8(const char32_t* buf, size_t len, char* utf8_output) {
268 const uint32_t* data = reinterpret_cast<const uint32_t*>(buf);
269 size_t pos = 0;
270 const char* start{utf8_output};
271 while (pos < len) {
272 // try to convert the next block of 2 ASCII characters
273 if (pos + 2 <= len) { // if it is safe to read 8 more
274 // bytes, check that they are ascii
275 uint64_t v;
276 std::memcpy(&v, data + pos, sizeof(uint64_t));
277 if ((v & 0xFFFFFF80FFFFFF80) == 0) {
278 *utf8_output++ = char(buf[pos]);
279 *utf8_output++ = char(buf[pos + 1]);
280 pos += 2;
281 continue;
282 }
283 }
284 uint32_t word = data[pos];
285 if ((word & 0xFFFFFF80) == 0) {
286 // will generate one UTF-8 bytes
287 *utf8_output++ = char(word);
288 pos++;
289 } else if ((word & 0xFFFFF800) == 0) {
290 // will generate two UTF-8 bytes
291 // we have 0b110XXXXX 0b10XXXXXX
292 *utf8_output++ = char((word >> 6) | 0b11000000);
293 *utf8_output++ = char((word & 0b111111) | 0b10000000);
294 pos++;
295 } else if ((word & 0xFFFF0000) == 0) {
296 // will generate three UTF-8 bytes
297 // we have 0b1110XXXX 0b10XXXXXX 0b10XXXXXX
298 if (word >= 0xD800 && word <= 0xDFFF) {
299 return 0;
300 }
301 *utf8_output++ = char((word >> 12) | 0b11100000);
302 *utf8_output++ = char(((word >> 6) & 0b111111) | 0b10000000);
303 *utf8_output++ = char((word & 0b111111) | 0b10000000);
304 pos++;
305 } else {
306 // will generate four UTF-8 bytes
307 // we have 0b11110XXX 0b10XXXXXX 0b10XXXXXX
308 // 0b10XXXXXX
309 if (word > 0x10FFFF) {
310 return 0;
311 }
312 *utf8_output++ = char((word >> 18) | 0b11110000);
313 *utf8_output++ = char(((word >> 12) & 0b111111) | 0b10000000);
314 *utf8_output++ = char(((word >> 6) & 0b111111) | 0b10000000);
315 *utf8_output++ = char((word & 0b111111) | 0b10000000);
316 pos++;
317 }
318 }
319 return utf8_output - start;
320}
321} // namespace ada::idna
322/* end file src/unicode_transcoding.cpp */
323/* begin file src/mapping.cpp */

Callers 1

to_unicodeFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected