[minor] Fix nits

This commit is contained in:
Luigi Pinca
2021-05-03 19:10:02 +02:00
parent 0af6f93c7d
commit 79cde2f6c8
2 changed files with 14 additions and 14 deletions
+12 -12
View File
@@ -9,12 +9,12 @@
* @return {Boolean} `true` if `buf` contains only correct UTF-8, else `false` * @return {Boolean} `true` if `buf` contains only correct UTF-8, else `false`
* @public * @public
*/ */
const isValidUTF8 = (buf) => { function isValidUTF8(buf) {
var len = buf.length; const len = buf.length;
var i = 0; let i = 0;
while (i < len) { while (i < len) {
if (buf[i] < 0x80) { // 0xxxxxxx if ((buf[i] & 0x80) === 0x00) { // 0xxxxxxx
i++; i++;
} else if ((buf[i] & 0xe0) === 0xc0) { // 110xxxxx 10xxxxxx } else if ((buf[i] & 0xe0) === 0xc0) { // 110xxxxx 10xxxxxx
if ( if (
@@ -23,21 +23,21 @@ const isValidUTF8 = (buf) => {
(buf[i] & 0xfe) === 0xc0 // overlong (buf[i] & 0xfe) === 0xc0 // overlong
) { ) {
return false; return false;
} else {
i += 2;
} }
i += 2;
} else if ((buf[i] & 0xf0) === 0xe0) { // 1110xxxx 10xxxxxx 10xxxxxx } else if ((buf[i] & 0xf0) === 0xe0) { // 1110xxxx 10xxxxxx 10xxxxxx
if ( if (
i + 2 >= len || i + 2 >= len ||
(buf[i + 1] & 0xc0) !== 0x80 || (buf[i + 1] & 0xc0) !== 0x80 ||
(buf[i + 2] & 0xc0) !== 0x80 || (buf[i + 2] & 0xc0) !== 0x80 ||
buf[i] === 0xe0 && (buf[i + 1] & 0xe0) === 0x80 || // overlong buf[i] === 0xe0 && (buf[i + 1] & 0xe0) === 0x80 || // overlong
buf[i] === 0xed && (buf[i + 1] & 0xe0) === 0xa0 // surrogate (U+D800 - U+DFFF) buf[i] === 0xed && (buf[i + 1] & 0xe0) === 0xa0 // surrogate (U+D800 - U+DFFF)
) { ) {
return false; return false;
} else {
i += 3;
} }
i += 3;
} else if ((buf[i] & 0xf8) === 0xf0) { // 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx } else if ((buf[i] & 0xf8) === 0xf0) { // 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
if ( if (
i + 3 >= len || i + 3 >= len ||
@@ -48,15 +48,15 @@ const isValidUTF8 = (buf) => {
buf[i] === 0xf4 && buf[i + 1] > 0x8f || buf[i] > 0xf4 // > U+10FFFF buf[i] === 0xf4 && buf[i + 1] > 0x8f || buf[i] > 0xf4 // > U+10FFFF
) { ) {
return false; return false;
} else {
i += 4;
} }
i += 4;
} else { } else {
return false; return false;
} }
} }
return true; return true;
}; }
module.exports = isValidUTF8; module.exports = isValidUTF8;
+2 -2
View File
@@ -36,13 +36,13 @@ napi_value IsValidUTF8(napi_env env, napi_callback_info info) {
uint64_t chunk; uint64_t chunk;
memcpy(&chunk, buf + i, 8); memcpy(&chunk, buf + i, 8);
if ((chunk & 0x8080808080808080) == 0) { if ((chunk & 0x8080808080808080) == 0x00) {
i = j; i = j;
continue; continue;
} }
} }
while ((buf[i] & 0x80) == 0) { // 0xxxxxxx while ((buf[i] & 0x80) == 0x00) { // 0xxxxxxx
if (++i == len) { if (++i == len) {
goto exit; goto exit;
} }