| 1380 | |
| 1381 | template<typename opt_swap> |
| 1382 | PUGI__FN bool convert_buffer_utf16(char_t*& out_buffer, size_t& out_length, const void* contents, size_t size, opt_swap) |
| 1383 | { |
| 1384 | const uint16_t* data = static_cast<const uint16_t*>(contents); |
| 1385 | size_t length = size / sizeof(uint16_t); |
| 1386 | |
| 1387 | // first pass: get length in wchar_t units |
| 1388 | out_length = utf_decoder<wchar_counter, opt_swap>::decode_utf16_block(data, length, 0); |
| 1389 | |
| 1390 | // allocate buffer of suitable length |
| 1391 | out_buffer = static_cast<char_t*>(xml_memory::allocate((out_length > 0 ? out_length : 1) * sizeof(char_t))); |
| 1392 | if (!out_buffer) |
| 1393 | return false; |
| 1394 | |
| 1395 | // second pass: convert utf16 input to wchar_t |
| 1396 | wchar_writer::value_type out_begin = reinterpret_cast<wchar_writer::value_type>(out_buffer); |
| 1397 | wchar_writer::value_type out_end = utf_decoder<wchar_writer, opt_swap>::decode_utf16_block(data, length, out_begin); |
| 1398 | |
| 1399 | assert(out_end == out_begin + out_length); |
| 1400 | (void)!out_end; |
| 1401 | |
| 1402 | return true; |
| 1403 | } |
| 1404 | |
| 1405 | template<typename opt_swap> |
| 1406 | PUGI__FN bool convert_buffer_utf32(char_t*& out_buffer, size_t& out_length, const void* contents, size_t size, opt_swap) |