@@ -142,11 +142,12 @@ namespace {
142142 }
143143
144144 size_t offset = result.size ();
145- result.resize (offset + (num * 2 )); // This fills the buffer with '\0'
145+ result.reserve (offset + (num * 2 )); // Allocate buffer conservatively
146146
147- auto * dst = result.data () + offset;
148147 if (isAscii) {
149148 // Widen ASCII characters from char into char16_t
149+ result.resize (offset + (num * 2 )); // This fills the buffer with '\0'
150+ auto * dst = result.data () + offset;
150151 const auto * asciiSrc = reinterpret_cast <const char *>(data);
151152 for (size_t i = 0 ; i < num; i++, dst += 2 ) {
152153 *dst = asciiSrc[i];
@@ -155,13 +156,16 @@ namespace {
155156 return ;
156157 }
157158
158- const auto * utf16Src = reinterpret_cast <const char16_t *>(data);
159159 if constexpr (kCanDirectCopyUtf16 ) {
160160 // Fast&direct copy path
161- std::memcpy (dst, utf16Src, num * 2 );
161+ const auto * byteSrc = reinterpret_cast <const uint8_t *>(data);
162+ result.insert (result.end (), byteSrc, byteSrc + num * 2 );
162163 return ;
163164 }
164165 // Slow path for unexpected endianness/char16_t size
166+ result.resize (offset + (num * 2 ));
167+ const auto * utf16Src = reinterpret_cast <const char16_t *>(data);
168+ auto * dst = result.data () + offset;
165169 for (size_t i = 0 ; i < num; i++) {
166170 const uint16_t codeUnit = static_cast <uint16_t >(utf16Src[i]);
167171 dst[i * 2 + 0 ] = static_cast <uint8_t >(codeUnit & 0xFFu );
0 commit comments