File tree Expand file tree Collapse file tree
packages/react-native-quick-crypto/cpp/utils Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -145,22 +145,26 @@ namespace {
145145 }
146146
147147 size_t offset = result.size ();
148- result.resize (offset + (num * 2 ));
148+ result.resize (offset + (num * 2 )); // This fills the buffer with '\0'
149149
150150 auto * dst = result.data () + offset;
151151 if (isAscii) {
152+ // Widen ASCII characters from char into char16_t
152153 const auto * asciiSrc = reinterpret_cast <const char *>(data);
153154 for (size_t i = 0 ; i < num; i++, dst += 2 ) {
154155 *dst = asciiSrc[i];
156+ // *(dst + 1) = '\0' is unnecessary because the buffer is zero initialized
155157 }
156158 return ;
157159 }
158160
159161 const auto * utf16Src = reinterpret_cast <const char16_t *>(data);
160162 if constexpr (std::endian::native == std::endian::little && sizeof (char16_t ) == 2 ) {
163+ // Fast/Direct copy path for expected endianness and char16_t size
161164 std::memcpy (dst, utf16Src, num * 2 );
162165 return ;
163166 }
167+ // Slow path for unexpected endianness/char16_t size
164168 for (size_t i = 0 ; i < num; i++) {
165169 const uint16_t codeUnit = static_cast <uint16_t >(utf16Src[i]);
166170 dst[i * 2 + 0 ] = static_cast <uint8_t >(codeUnit & 0xFFu );
You can’t perform that action at this time.
0 commit comments