Skip to content

Commit d109f1c

Browse files
ronagtargos
authored andcommitted
buffer: use simdutf convert_latin1_to_utf8_safe
simdutf 5.5 includes convert_latin1_to_utf8_safe PR-URL: #54798 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Daniel Lemire <daniel@lemire.me>
1 parent 90a87ca commit d109f1c

File tree

1 file changed

+1
-53
lines changed

1 file changed

+1
-53
lines changed

src/node_buffer.cc

+1-53
Original file line numberDiff line numberDiff line change
@@ -1440,58 +1440,6 @@ void CopyArrayBuffer(const FunctionCallbackInfo<Value>& args) {
14401440
memcpy(dest, src, bytes_to_copy);
14411441
}
14421442

1443-
size_t convert_latin1_to_utf8_s(const char* src,
1444-
size_t src_len,
1445-
char* dst,
1446-
size_t dst_len) noexcept {
1447-
size_t src_pos = 0;
1448-
size_t dst_pos = 0;
1449-
1450-
const auto safe_len = std::min(src_len, dst_len >> 1);
1451-
if (safe_len > 16) {
1452-
// convert_latin1_to_utf8 will never write more than input length * 2.
1453-
dst_pos += simdutf::convert_latin1_to_utf8(src, safe_len, dst);
1454-
src_pos += safe_len;
1455-
}
1456-
1457-
// Based on:
1458-
// https://github.com/simdutf/simdutf/blob/master/src/scalar/latin1_to_utf8/latin1_to_utf8.h
1459-
// with an upper limit on the number of bytes to write.
1460-
1461-
const auto src_ptr = reinterpret_cast<const uint8_t*>(src);
1462-
const auto dst_ptr = reinterpret_cast<uint8_t*>(dst);
1463-
1464-
size_t skip_pos = src_pos;
1465-
while (src_pos < src_len && dst_pos < dst_len) {
1466-
if (skip_pos <= src_pos && src_pos + 16 <= src_len &&
1467-
dst_pos + 16 <= dst_len) {
1468-
uint64_t v1;
1469-
memcpy(&v1, src_ptr + src_pos + 0, 8);
1470-
uint64_t v2;
1471-
memcpy(&v2, src_ptr + src_pos + 8, 8);
1472-
if (((v1 | v2) & UINT64_C(0x8080808080808080)) == 0) {
1473-
memcpy(dst_ptr + dst_pos, src_ptr + src_pos, 16);
1474-
dst_pos += 16;
1475-
src_pos += 16;
1476-
} else {
1477-
skip_pos = src_pos + 16;
1478-
}
1479-
} else {
1480-
const auto byte = src_ptr[src_pos++];
1481-
if ((byte & 0x80) == 0) {
1482-
dst_ptr[dst_pos++] = byte;
1483-
} else if (dst_pos + 2 <= dst_len) {
1484-
dst_ptr[dst_pos++] = (byte >> 6) | 0b11000000;
1485-
dst_ptr[dst_pos++] = (byte & 0b111111) | 0b10000000;
1486-
} else {
1487-
break;
1488-
}
1489-
}
1490-
}
1491-
1492-
return dst_pos;
1493-
}
1494-
14951443
template <encoding encoding>
14961444
uint32_t WriteOneByteString(const char* src,
14971445
uint32_t src_len,
@@ -1502,7 +1450,7 @@ uint32_t WriteOneByteString(const char* src,
15021450
}
15031451

15041452
if (encoding == UTF8) {
1505-
return convert_latin1_to_utf8_s(src, src_len, dst, dst_len);
1453+
return simdutf::convert_latin1_to_utf8_safe(src, src_len, dst, dst_len);
15061454
} else if (encoding == LATIN1 || encoding == ASCII) {
15071455
const auto size = std::min(src_len, dst_len);
15081456
memcpy(dst, src, size);

0 commit comments

Comments
 (0)